Skip to content

Commit

Permalink
Remove escapeExpression from @ember/template
Browse files Browse the repository at this point in the history
Co-Authored-By: @chriskrycho
  • Loading branch information
NullVoxPopuli committed Jan 22, 2024
1 parent 13b4322 commit 21f9465
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/@ember/-internals/glimmer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export {
type FunctionBasedHelper,
type FunctionBasedHelperInstance,
} from './lib/helper';
export { SafeString, escapeExpression, htmlSafe, isHTMLSafe } from './lib/utils/string';
export { SafeString, htmlSafe, isHTMLSafe } from './lib/utils/string';
export { Renderer, _resetRenderers, renderSettled } from './lib/renderer';
export {
getTemplate,
Expand Down
59 changes: 6 additions & 53 deletions packages/@ember/-internals/glimmer/lib/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type { SafeString as GlimmerSafeString } from '@glimmer/runtime';
@public
*/
export class SafeString implements GlimmerSafeString {
private __string: string;
private readonly __string: string;

constructor(string: string) {
this.__string = string;
Expand Down Expand Up @@ -67,57 +67,6 @@ export class SafeString implements GlimmerSafeString {
}
}

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

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

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

export function escapeExpression(string: unknown): string {
let s: string;
if (typeof string !== 'string') {
// don't escape SafeStrings, since they're already safe
if (isHTMLSafe(string)) {
return string.toHTML();
} else if (string === null || string === undefined) {
return '';
} else if (!string) {
return String(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.
s = String(string);
} else {
s = string;
}

if (!possible.test(s)) {
return s;
}

// SAFETY: this is technically a lie, but it's a true lie as long as the
// invariant it depends on is upheld: `escapeChar` will always return a string
// as long as its input is one of the characters in `escape`, and it will only
// be called if it matches one of the characters in the `badChar` regex, which
// is hand-maintained to match the set escaped. (It would be nice if TS could
// "see" into the regex to see how this works, but that'd be quite a lot of
// extra fanciness.)
return s.replace(badChars, escapeChar as (s: string) => string);
}

/**
Use this method to indicate that a string should be rendered as HTML
when the string is used in a template. To say this another way,
Expand Down Expand Up @@ -177,6 +126,10 @@ export function htmlSafe(str: string): SafeString {
*/
export function isHTMLSafe(str: unknown): str is SafeString {
return (
str !== null && typeof str === 'object' && 'toHTML' in str && typeof str.toHTML === 'function'
// SAFETY: cast `as SafeString` only present to make this check "legal"; we
// can further improve this by changing the behavior to do an `in` check
// instead, but that's worth landing as a separate change for bisecting if
// it happens to have an impact on e.g. perf.
str !== null && typeof str === 'object' && typeof (str as SafeString).toHTML === 'function'
);
}
7 changes: 0 additions & 7 deletions packages/ember/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
componentCapabilities,
modifierCapabilities,
setComponentManager,
escapeExpression,
getTemplates,
setTemplates,
template,
Expand Down Expand Up @@ -597,9 +596,6 @@ namespace Ember {

interface EmberHandlebars {
template: typeof template;
Utils: {
escapeExpression: typeof escapeExpression;
};
compile?: typeof compile;
precompile?: typeof precompile;
}
Expand Down Expand Up @@ -665,9 +661,6 @@ applicationRunLoadHooks('Ember.Application', EmberApplication);

let EmberHandlebars: EmberHandlebars = {
template,
Utils: {
escapeExpression,
},
};

let EmberHTMLBars: EmberHTMLBars = {
Expand Down
1 change: 0 additions & 1 deletion packages/ember/tests/reexports_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ let allExports = [
},
test56,
],
['Handlebars.Utils.escapeExpression', '@ember/-internals/glimmer', 'escapeExpression', test56],
['_Input', '@ember/-internals/glimmer', 'Input', test56],
['_RegistryProxyMixin', '@ember/-internals/runtime', 'RegistryProxyMixin', test57],
['_ContainerProxyMixin', '@ember/-internals/runtime', 'ContainerProxyMixin', test57],
Expand Down

0 comments on commit 21f9465

Please sign in to comment.