diff --git a/src/filters.js b/src/filters.js index 1a532295..0465b611 100644 --- a/src/filters.js +++ b/src/filters.js @@ -119,10 +119,12 @@ var filters = { if(str instanceof r.SafeString) { return str; } + str = (str === null || str === undefined) ? '' : str; return r.markSafe(lib.escape(str.toString())); }, safe: function(str) { + str = (str === null || str === undefined) ? '' : str; return r.markSafe(str); }, diff --git a/tests/compiler.js b/tests/compiler.js index 00102fc3..05b92718 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -1309,10 +1309,22 @@ expect(res).to.be('&><\'"'); }); + render('{{ foo }}', { foo: null }, { autoescape: true }, function (err, res) { + expect(res).to.be(''); + }); + + render('{{ foo | safe }}', { foo: null }, { autoescape: true }, function (err, res) { + expect(res).to.be(''); + }); + render('{{ foo }}', { foo: ['
foo
']}, { autoescape: true }, function(err, res) { expect(res).to.be('<p>foo</p>'); }); + render('{{ foo | safe }}', { foo: 'foo
' }, { autoescape: true }, function (err, res) { + expect(res).to.be('foo
'); + }); + render('{{ foo }}', { foo: {toString: function() {return 'foo
'}}}, { autoescape: true }, function(err, res) { expect(res).to.be('<p>foo</p>'); }); diff --git a/tests/filters.js b/tests/filters.js index d349a58a..336a77e1 100644 --- a/tests/filters.js +++ b/tests/filters.js @@ -116,6 +116,10 @@ var res2 = render('{{ foo | escape }}', {foo: {toString: function() { return ''; }}}, { autoescape: false }); expect(res2).to.be('<html>'); + + var res3 = render('{{ foo | escape }}', { foo: null }, { autoescape: false }); + expect(res3).to.be(''); + finish(done); });