Skip to content

Commit

Permalink
[Glimmer2] tests for html-safe and render
Browse files Browse the repository at this point in the history
Maintain our own safestring and escapeexpression for now
  • Loading branch information
chadhietala committed Jul 20, 2016
1 parent 5757ea5 commit a9fe374
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 165 deletions.
4 changes: 3 additions & 1 deletion packages/ember-glimmer/lib/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { default as inputTypeHelper } from './helpers/-input-type';
import { default as queryParams } from './helpers/query-param';
import { default as eachIn } from './helpers/each-in';
import { default as normalizeClassHelper } from './helpers/-normalize-class';
import { default as htmlSafeHelper } from './helpers/-html-safe';
import { OWNER } from 'container/owner';

const builtInComponents = {
Expand Down Expand Up @@ -114,7 +115,8 @@ const builtInHelpers = {
'-class': classHelper,
'-each-in': eachIn,
'-input-type': inputTypeHelper,
'-normalize-class': normalizeClassHelper
'-normalize-class': normalizeClassHelper,
'-html-safe': htmlSafeHelper
};

import { default as ActionModifierManager } from './modifiers/action';
Expand Down
14 changes: 14 additions & 0 deletions packages/ember-glimmer/lib/helpers/-html-safe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { InternalHelperReference } from '../utils/references';
import { SafeString } from '../utils/string';

function htmlSafe({ positional }) {
let path = positional.at(0);
return new SafeString(path.value());
}

export default {
isInternalHelper: true,
toReference(args) {
return new InternalHelperReference(htmlSafe, args);
}
};
103 changes: 103 additions & 0 deletions packages/ember-glimmer/lib/utils/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
@module ember
@submodule ember-glimmer
*/

export class SafeString {
constructor(string) {
this.string = string;
}

toString() {
return `${this.string}`;
}

toHTML() {
return this.toString();
}
}

const escape = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
// jscs:disable
"'": '&#x27;',
// jscs:enable
'`': '&#x60;',
'=': '&#x3D;'
};

const possible = /[&<>"'`=]/;
const badChars = /[&<>"'`=]/g;

function escapeChar(chr) {
return escape[chr];
}

export function escapeExpression(string) {
if (typeof string !== 'string') {
// don't escape SafeStrings, since they're already safe
if (string && string.toHTML) {
return string.toHTML();
} else if (string == null) {
return '';
} else if (!string) {
return string + '';
}

// Force a string conversion as this will be done by the append regardless and
// the regex test will do this transparently behind the scenes, causing issues if
// an object's to string has escaped characters in it.
string = '' + string;
}

if (!possible.test(string)) { return string; }
return string.replace(badChars, escapeChar);
}

/**
Mark a string as safe for unescaped output with Ember templates. If you
return HTML from a helper, use this function to
ensure Ember's rendering layer does not escape the HTML.
```javascript
Ember.String.htmlSafe('<div>someString</div>')
```
@method htmlSafe
@for Ember.String
@static
@return {Handlebars.SafeString} A string that will not be HTML escaped by Handlebars.
@public
*/
export function htmlSafe(str) {
if (str === null || str === undefined) {
str = '';
} else if (typeof str !== 'string') {
str = '' + str;
}
return new SafeString(str);
}

/**
Detects if a string was decorated using `Ember.String.htmlSafe`.
```javascript
var plainString = 'plain string',
safeString = Ember.String.htmlSafe('<div>someValue</div>');
Ember.String.isHTMLSafe(plainString); // false
Ember.String.isHTMLSafe(safeString); // true
```
@method isHTMLSafe
@for Ember.String
@static
@return {Boolean} `true` if the string was decorated with `htmlSafe`, `false` otherwise.
@public
*/
export function isHTMLSafe(str) {
return str && typeof str.toHTML === 'function';
}
45 changes: 0 additions & 45 deletions packages/ember-glimmer/tests/compat/safe-string-test.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ moduleFor('Attribute bindings integration', class extends RenderingTest {
this.assertComponentElement(this.nthChild(0), { tagName: 'div', attrs: { href: 'cat.html' } });
}

// Note: There are no observers in Glimmer
['@htmlbars should teardown observers'](assert) {
let component;
let FooBarComponent = Component.extend({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ moduleFor('Link-to component', class extends ApplicationTest {
});
}

['@htmlbars escaped inline form with (-html-safe) does not escape link title'](assert) {
['@test escaped inline form with (-html-safe) does not escape link title'](assert) {
this.registerTemplate('application', `{{link-to (-html-safe title) 'index'}}`);
this.registerController('application', Controller.extend({
title: '<b>blah</b>'
Expand All @@ -73,7 +73,7 @@ moduleFor('Link-to component', class extends ApplicationTest {
});
}

['@htmlbars unescaped inline form (triple curlies) does not escape link title'](assert) {
['@test unescaped inline form (triple curlies) does not escape link title'](assert) {
this.registerTemplate('application', `{{{link-to title 'index'}}}`);
this.registerController('application', Controller.extend({
title: '<b>blah</b>'
Expand Down
3 changes: 3 additions & 0 deletions packages/ember-glimmer/tests/integration/helpers/get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ moduleFor('Helpers test: {{get}}', class extends RenderingTest {
this.assertText('[] []');
}

// Unspecified behavior in HTMLBars.
['@htmlbars get helper value should be updatable using {{input}} and (mut) - dynamic key'](assert) {
this.registerComponent('-text-field', { ComponentClass: TextField });

Expand Down Expand Up @@ -403,6 +404,7 @@ moduleFor('Helpers test: {{get}}', class extends RenderingTest {
assert.strictEqual(this.$('#get-input').val(), 'banana');
}

// Unspecified behavior in HTMLBars.
['@htmlbars get helper value should be updatable using {{input}} and (mut) - dynamic nested key'](assert) {
this.registerComponent('-text-field', { ComponentClass: TextField });

Expand Down Expand Up @@ -438,6 +440,7 @@ moduleFor('Helpers test: {{get}}', class extends RenderingTest {
assert.strictEqual(this.$('#get-input').val(), 'mcintosh');
}

// Unspecified behavior in HTMLBars.
['@htmlbars get helper value should be updatable using {{input}} and (mut) - static key'](assert) {
this.registerComponent('-text-field', { ComponentClass: TextField });

Expand Down
3 changes: 1 addition & 2 deletions packages/ember-glimmer/tests/utils/string-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import SafeString from 'htmlbars-util/safe-string';
import { htmlSafe, isHTMLSafe } from 'ember-htmlbars/utils/string';
import { SafeString, htmlSafe, isHTMLSafe } from 'ember-glimmer/utils/string';
import isEnabled from 'ember-metal/features';
import { TestCase } from './abstract-test-case';
import { moduleFor } from './test-case';
Expand Down
35 changes: 0 additions & 35 deletions packages/ember-htmlbars/lib/compat.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/ember-htmlbars/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ import queryParamsHelper from 'ember-htmlbars/helpers/query-params';

export { default as template } from './system/template';

// Importing ember-htmlbars/compat updates the
// Ember.Handlebars global if htmlbars is enabled.
import 'ember-htmlbars/compat';

registerHelper('if', ifHelper);
registerHelper('unless', unlessHelper);
registerHelper('with', withHelper);
Expand Down
72 changes: 0 additions & 72 deletions packages/ember-htmlbars/lib/utils/string.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/ember-htmlbars/lib/utils/string.js
1 change: 0 additions & 1 deletion packages/ember-htmlbars/tests/compat/safe-string-test.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/ember-htmlbars/tests/utils/string-test.js

This file was deleted.

Loading

0 comments on commit a9fe374

Please sign in to comment.