diff --git a/src/filters.js b/src/filters.js index e282adff..649c0f46 100644 --- a/src/filters.js +++ b/src/filters.js @@ -97,7 +97,7 @@ var filters = { }, escape: function(str) { - if (str == null) str = ''; + if(str == null) str = ''; if(str instanceof r.SafeString) { return str; } @@ -105,7 +105,11 @@ var filters = { }, safe: function(str) { - return r.markSafe(str); + if(str == null) str = ''; + if(str instanceof r.SafeString) { + return str; + } + return r.markSafe(str.toString()); }, first: function(arr) { diff --git a/tests/filters.js b/tests/filters.js index dffaf7b9..08e4a9a0 100644 --- a/tests/filters.js +++ b/tests/filters.js @@ -424,12 +424,21 @@ it('should work with non-string values', function(done) { equal('{{ foo | escape }}', {foo: null}, ''); equal('{{ foo | escape }}', {foo: ['']}, '<html>'); + equal('a {{ foo | escape }}', {foo: ['']}, 'a <html>'); render('{{ foo }}', { foo: {toString: function() {return '

foo

'}}}, { autoescape: true }, function(err, res) { expect(res).to.be('<p>foo</p>'); }); render('{{ foo }}', { foo: {toString: function() {return '

foo

'}}}, { autoescape: false }, function(err, res) { expect(res).to.be('

foo

'); }); + + equal('{{ foo | safe }}', {foo: ['']}, ''); + render('{{ foo | safe }}', { foo: {toString: function() {return '

foo

'}}}, { autoescape: true }, function(err, res) { + expect(res).to.be('

foo

'); + }); + render('a {{ foo | safe }}', { foo: {toString: function() {return '

foo

'}}}, { autoescape: true }, function(err, res) { + expect(res).to.be('a

foo

'); + }); finish(done); }); });