Skip to content

Commit 1083dac

Browse files
authoredJun 19, 2016
Merge pull request #13666 from workmanw/is-html-safe
[FEATURE ember-string-ishtmlsafe] Added Ember.String.isHtmlSafe.
2 parents a36564b + 43b2238 commit 1083dac

File tree

9 files changed

+138
-28
lines changed

9 files changed

+138
-28
lines changed
 

‎FEATURES.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,17 @@ for a detailed explanation.
4444

4545
* `ember-runtime-enumerable-includes`
4646

47-
Deprecates `Enumerable#contains` and `Array#contains` in favor of `Enumerable#includes` and `Array#includes`
47+
Deprecates `Enumerable#contains` and `Array#contains` in favor of `Enumerable#includes` and `Array#includes`
4848
to stay in line with ES standards (see [RFC](https://github.com/emberjs/rfcs/blob/master/text/0136-contains-to-includes.md)).
49+
50+
* `ember-string-ishtmlsafe`
51+
52+
Introduces an API to detect if strings are decorated as htmlSafe. Example:
53+
54+
```javascript
55+
var plainString = 'plain string',
56+
safeString = Ember.String.htmlSafe('<div>someValue</div>');
57+
58+
Ember.String.isHtmlSafe(plainString); // false
59+
Ember.String.isHtmlSafe(safeString); // true
60+
```

‎features.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"ember-glimmer": null,
99
"ember-runtime-computed-uniq-by": true,
1010
"ember-improved-instrumentation": null,
11-
"ember-runtime-enumerable-includes": null
11+
"ember-runtime-enumerable-includes": null,
12+
"ember-string-ishtmlsafe": null
1213
}
1314
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import EmberHandlebars from 'ember-htmlbars/compat';
2+
import { isHtmlSafe } from 'ember-htmlbars/utils/string';
3+
import { TestCase } from '../utils/abstract-test-case';
4+
import { moduleFor } from '../utils/test-case';
5+
6+
7+
moduleFor('compat - SafeString', class extends TestCase {
8+
['@test using new results in a deprecation']() {
9+
let result;
10+
11+
expectDeprecation(() => {
12+
result = new EmberHandlebars.SafeString('<b>test</b>');
13+
}, 'Ember.Handlebars.SafeString is deprecated in favor of Ember.String.htmlSafe');
14+
15+
this.assert.equal(result.toHTML(), '<b>test</b>');
16+
17+
// Ensure this functionality is maintained for backwards compat, but also deprecated.
18+
expectDeprecation(() => {
19+
this.assert.ok(result instanceof EmberHandlebars.SafeString);
20+
}, 'Ember.Handlebars.SafeString is deprecated in favor of Ember.String.htmlSafe');
21+
}
22+
23+
['@test isHtmlSafe should detect SafeString']() {
24+
let safeString;
25+
26+
expectDeprecation(() => {
27+
safeString = new EmberHandlebars.SafeString('<b>test</b>');
28+
}, 'Ember.Handlebars.SafeString is deprecated in favor of Ember.String.htmlSafe');
29+
30+
this.assert.ok(isHtmlSafe(safeString));
31+
}
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import SafeString from 'htmlbars-util/safe-string';
2+
import { htmlSafe, isHtmlSafe } from 'ember-htmlbars/utils/string';
3+
import isEnabled from 'ember-metal/features';
4+
import { TestCase } from './abstract-test-case';
5+
import { moduleFor } from './test-case';
6+
7+
moduleFor('SafeString', class extends TestCase {
8+
['@test htmlSafe should return an instance of SafeString']() {
9+
let safeString = htmlSafe('you need to be more <b>bold</b>');
10+
11+
this.assert.ok(safeString instanceof SafeString, 'should be a SafeString');
12+
}
13+
14+
['@test htmlSafe should return an empty string for null']() {
15+
let safeString = htmlSafe(null);
16+
17+
this.assert.equal(safeString instanceof SafeString, true, 'should be a SafeString');
18+
this.assert.equal(safeString.toString(), '', 'should return an empty string');
19+
}
20+
21+
['@test htmlSafe should return an instance of SafeString']() {
22+
let safeString = htmlSafe();
23+
24+
this.assert.equal(safeString instanceof SafeString, true, 'should be a SafeString');
25+
this.assert.equal(safeString.toString(), '', 'should return an empty string');
26+
}
27+
});
28+
29+
if (isEnabled('ember-string-ishtmlsafe')) {
30+
moduleFor('SafeString isHtmlSafe', class extends TestCase {
31+
['@test isHtmlSafe should detect SafeString']() {
32+
let safeString = htmlSafe('<em>Emphasize</em> the important things.');
33+
34+
this.assert.ok(isHtmlSafe(safeString));
35+
}
36+
37+
['@test isHtmlSafe should not detect SafeString on primatives']() {
38+
this.assert.notOk(isHtmlSafe('Hello World'));
39+
this.assert.notOk(isHtmlSafe({}));
40+
this.assert.notOk(isHtmlSafe([]));
41+
this.assert.notOk(isHtmlSafe(10));
42+
this.assert.notOk(isHtmlSafe(null));
43+
}
44+
});
45+
}

‎packages/ember-htmlbars/lib/compat.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import Ember from 'ember-metal/core'; // for Handlebars export
2+
import { deprecate } from 'ember-metal/debug';
23
import {
34
SafeString,
45
escapeExpression
56
} from 'ember-htmlbars/utils/string';
67

7-
const EmberHandlebars = Ember.Handlebars = Ember.Handlebars || {};
8+
let EmberHandlebars = Ember.Handlebars = Ember.Handlebars || {};
9+
Object.defineProperty(EmberHandlebars, 'SafeString', {
10+
get() {
11+
deprecate(
12+
'Ember.Handlebars.SafeString is deprecated in favor of Ember.String.htmlSafe',
13+
false,
14+
{
15+
id: 'ember-htmlbars.ember-handlebars-safestring',
16+
until: '3.0.0',
17+
url: 'http://emberjs.com/deprecations/v2.x#toc_use-ember-string-htmlsafe-over-ember-handlebars-safestring'
18+
}
19+
);
20+
21+
return SafeString;
22+
}
23+
});
824

9-
EmberHandlebars.SafeString = SafeString;
1025
EmberHandlebars.Utils = {
1126
escapeExpression: escapeExpression
1227
};

‎packages/ember-htmlbars/lib/utils/string.js

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { ENV } from 'ember-environment';
77
import EmberStringUtils from 'ember-runtime/system/string';
88
import { SafeString, escapeExpression } from 'htmlbars-util';
9+
import isEnabled from 'ember-metal/features';
910

1011
/**
1112
Mark a string as safe for unescaped output with Ember templates. If you
@@ -38,8 +39,34 @@ if (ENV.EXTEND_PROTOTYPES.String) {
3839
};
3940
}
4041

42+
/**
43+
Detects if a string was decorated using `Ember.String.htmlSafe`.
44+
45+
```javascript
46+
var plainString = 'plain string',
47+
safeString = Ember.String.htmlSafe('<div>someValue</div>');
48+
49+
Ember.String.isHtmlSafe(plainString); // false
50+
Ember.String.isHtmlSafe(safeString); // true
51+
```
52+
53+
@method isHtmlSafe
54+
@for Ember.String
55+
@static
56+
@return {Boolean} `true` if the string was decorated with `htmlSafe`, `false` otherwise.
57+
@public
58+
*/
59+
function isHtmlSafe(str) {
60+
return str && typeof str.toHTML === 'function';
61+
}
62+
63+
if (isEnabled('ember-string-ishtmlsafe')) {
64+
EmberStringUtils.isHtmlSafe = isHtmlSafe;
65+
}
66+
4167
export {
4268
SafeString,
4369
htmlSafe,
70+
isHtmlSafe,
4471
escapeExpression
4572
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../ember-glimmer/tests/compat/safe-string-test.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../ember-glimmer/tests/utils/string-test.js

‎packages/ember-htmlbars/tests/utils/string_test.js

-24
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.