Skip to content

Commit

Permalink
fix: autoescape for non-string values
Browse files Browse the repository at this point in the history
pick from mozilla#836
  • Loading branch information
fengmk2 committed Sep 12, 2016
1 parent 7528f93 commit 6b33c01
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ var filters = {
},

escape: function(str) {
if(typeof str === 'string' ||
str instanceof r.SafeString) {
return lib.escape(str);
if (str == null) str = '';
if(str instanceof r.SafeString) {
return str;
}
return str;
return r.markSafe(lib.escape(str.toString()));
},

safe: function(str) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ function markSafe(val) {
function suppressValue(val, autoescape) {
val = (val !== undefined && val !== null) ? val : '';

if(autoescape && typeof val === 'string') {
val = lib.escape(val);
if(autoescape && !(val instanceof SafeString)) {
val = lib.escape(val.toString());
}

return val;
Expand Down
12 changes: 12 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,5 +420,17 @@
equal('{{ "foo bar baz" | wordcount }}', '3');
finish(done);
});

it('should work with non-string values', function(done) {
equal('{{ foo | escape }}', {foo: null}, '');
equal('{{ foo | escape }}', {foo: ['<html>']}, '&lt;html&gt;');
render('{{ foo }}', { foo: {toString: function() {return '<p>foo</p>'}}}, { autoescape: true }, function(err, res) {
expect(res).to.be('&lt;p&gt;foo&lt;/p&gt;');
});
render('{{ foo }}', { foo: {toString: function() {return '<p>foo</p>'}}}, { autoescape: false }, function(err, res) {
expect(res).to.be('<p>foo</p>');
});
finish(done);
});
});
})();

0 comments on commit 6b33c01

Please sign in to comment.