-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13845 from chadhietala/rando-tests
[Glimmer2] tests for html-safe and render
- Loading branch information
Showing
17 changed files
with
279 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
// jscs:disable | ||
"'": ''', | ||
// jscs:enable | ||
'`': '`', | ||
'=': '=' | ||
}; | ||
|
||
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'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../ember-glimmer/lib/utils/string.js |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.