diff --git a/lib/handlebars/safe-string.js b/lib/handlebars/safe-string.js index 2ae49aa89..a6b8ecf20 100644 --- a/lib/handlebars/safe-string.js +++ b/lib/handlebars/safe-string.js @@ -3,7 +3,7 @@ function SafeString(string) { this.string = string; } -SafeString.prototype.toString = function() { +SafeString.prototype.toString = SafeString.prototype.toHTML = function() { return "" + this.string; }; diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js index f38b7afb2..ce850776b 100644 --- a/lib/handlebars/utils.js +++ b/lib/handlebars/utils.js @@ -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) { diff --git a/spec/utils.js b/spec/utils.js index 0216c8de8..4582e2496 100644 --- a/spec/utils.js +++ b/spec/utils.js @@ -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(''), '');