From 3a398c45bce20afa1ff4569dc227c8c9f76dacb5 Mon Sep 17 00:00:00 2001 From: tbence94 Date: Wed, 11 Dec 2019 14:42:00 +0100 Subject: [PATCH 1/3] nl2br fix --- src/twig.filters.js | 2 +- test/test.filters.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/twig.filters.js b/src/twig.filters.js index fe7ad6f5..e56a8129 100644 --- a/src/twig.filters.js +++ b/src/twig.filters.js @@ -504,7 +504,7 @@ module.exports = function (Twig) { }, nl2br(value) { - if (value === undefined || value === null) { + if (value === undefined || value === null || value === '') { return; } diff --git a/test/test.filters.js b/test/test.filters.js index f839e6cf..d0b801b5 100644 --- a/test/test.filters.js +++ b/test/test.filters.js @@ -495,6 +495,11 @@ describe('Twig.js Filters ->', function () { testTemplate.render().should.equal(''); }); + it('should not fail when passed str', function () { + const testTemplate = twig({data: '{{ myemptystr|nl2br }}'}); + testTemplate.render({myemptystr: ''}).should.equal(''); + }); + it('should not escape br tags if autoescape is on', function () { twig({ autoescape: true, From c426c95816ddb998577a8523263f46fdc85aa5aa Mon Sep 17 00:00:00 2001 From: tbence94 Date: Wed, 11 Dec 2019 14:52:51 +0100 Subject: [PATCH 2/3] escape fix --- src/twig.filters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/twig.filters.js b/src/twig.filters.js index e56a8129..ba6e4276 100644 --- a/src/twig.filters.js +++ b/src/twig.filters.js @@ -405,7 +405,7 @@ module.exports = function (Twig) { }, escape(value, params) { - if (value === undefined || value === null) { + if (value === undefined || value === null || value === '') { return; } From 895345291304d1b552d4aefb35ce86e906f7b36f Mon Sep 17 00:00:00 2001 From: tbence94 Date: Wed, 11 Dec 2019 14:53:04 +0100 Subject: [PATCH 3/3] tests for empty string scenarios --- test/test.filters.js | 45 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/test/test.filters.js b/test/test.filters.js index d0b801b5..c1c3139c 100644 --- a/test/test.filters.js +++ b/test/test.filters.js @@ -13,6 +13,10 @@ describe('Twig.js Filters ->', function () { const testTemplate = twig({data: '{{ undef|url_encode() }}'}); testTemplate.render().should.equal(''); }); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|url_encode() }}'}); + testTemplate.render().should.equal(''); + }); it('should handle special characters', function () { const data = {foo: ' \\&"\'.,-_?/Ķä€台北[]{}\t\r\n\b\u0080'}; const testTemplate = twig({data: '{{ foo|url_encode() }}'}); @@ -183,6 +187,11 @@ describe('Twig.js Filters ->', function () { const testTemplate = twig({data: '{{ undef|keys }}'}); testTemplate.render().should.equal(''); }); + + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|keys }}'}); + testTemplate.render().should.equal(''); + }); }); describe('merge ->', function () { it('should merge two objects into an object', function () { @@ -216,11 +225,14 @@ describe('Twig.js Filters ->', function () { testTemplate = twig({data: '{{ [1+ 5,2,4,76]|join("-" ~ ".") }}'}); testTemplate.render().should.equal('6-.2-.4-.76'); }); - it('should handle undefined', function () { const testTemplate = twig({data: '{{ undef|join }}'}); testTemplate.render().should.equal(''); }); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|join }}'}); + testTemplate.render().should.equal(''); + }); }); // Other @@ -334,6 +346,11 @@ describe('Twig.js Filters ->', function () { const testTemplate = twig({data: '{{ undef|replace }}'}); testTemplate.render().should.equal(''); }); + + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|replace }}'}); + testTemplate.render().should.equal(''); + }); }); describe('format ->', function () { @@ -347,6 +364,11 @@ describe('Twig.js Filters ->', function () { testTemplate.render().should.equal(''); }); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|format }}'}); + testTemplate.render().should.equal(''); + }); + it('should handle positive leading sign without padding', function () { const template = twig({data: '{{ "I like positive numbers like %+d."|format(123) }}'}); template.render({foo: 'foo'}).should.equal('I like positive numbers like +123.'); @@ -388,6 +410,11 @@ describe('Twig.js Filters ->', function () { const testTemplate = twig({data: '{{ undef|striptags }}'}); testTemplate.render().should.equal(''); }); + + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|striptags }}'}); + testTemplate.render().should.equal(''); + }); }); describe('escape ->', function () { @@ -401,6 +428,11 @@ describe('Twig.js Filters ->', function () { testTemplate.render().should.equal(''); }); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|escape }}'}); + testTemplate.render().should.equal(''); + }); + it('should not escape twice if autoescape is on', function () { twig({ autoescape: true, @@ -495,9 +527,9 @@ describe('Twig.js Filters ->', function () { testTemplate.render().should.equal(''); }); - it('should not fail when passed str', function () { - const testTemplate = twig({data: '{{ myemptystr|nl2br }}'}); - testTemplate.render({myemptystr: ''}).should.equal(''); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|nl2br }}'}); + testTemplate.render().should.equal(''); }); it('should not escape br tags if autoescape is on', function () { @@ -543,6 +575,11 @@ describe('Twig.js Filters ->', function () { testTemplate.render().should.equal(''); }); + it('should handle empty strings', function () { + const testTemplate = twig({data: '{{ ""|trim }}'}); + testTemplate.render().should.equal(''); + }); + it('should not autoescape', function () { const template = twig({data: '{{ test|trim }}'}); template.render({test: '\r\n Test\n '}).should.equal('Test');