Skip to content

Commit

Permalink
Use toHTML vs. instanceof checks for SafeString
Browse files Browse the repository at this point in the history
Allows for us to play nicely in environments such as Node that could have multiple versions of the library loaded. Also allows for implementors to provide their own behavior, provided they know what they are doing.

Fixes #886
  • Loading branch information
kpdecker committed Nov 8, 2014
1 parent c8af90f commit 01a22e6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/handlebars/safe-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function SafeString(string) {
this.string = string;
}

SafeString.prototype.toString = function() {
SafeString.prototype.toString = SafeString.prototype.toHTML = function() {
return "" + this.string;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export var isArray = Array.isArray || function(value) {

export function escapeExpression(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof SafeString) {
return string.toString();
if (string && string.toHTML) {
return string.toHTML();
} else if (string == null) {
return "";
} else if (!string) {
Expand Down
6 changes: 6 additions & 0 deletions spec/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe('utils', function() {
var string = new Handlebars.SafeString('foo<&"\'>');
equals(Handlebars.Utils.escapeExpression(string), 'foo<&"\'>');

var obj = {
toHTML: function() {
return 'foo<&"\'>';
}
};
equals(Handlebars.Utils.escapeExpression(obj), 'foo<&"\'>');
});
it('should handle falsy', function() {
equals(Handlebars.Utils.escapeExpression(''), '');
Expand Down

0 comments on commit 01a22e6

Please sign in to comment.