Skip to content

Commit

Permalink
Merge pull request #690 from tbence94/fix/empty-string-filters
Browse files Browse the repository at this point in the history
Fix filters with empty string input
  • Loading branch information
RobLoach authored Dec 16, 2019
2 parents 70c0fcd + 8953452 commit 216feba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/twig.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ module.exports = function (Twig) {
},

escape(value, params) {
if (value === undefined || value === null) {
if (value === undefined || value === null || value === '') {
return;
}

Expand Down Expand Up @@ -504,7 +504,7 @@ module.exports = function (Twig) {
},

nl2br(value) {
if (value === undefined || value === null) {
if (value === undefined || value === null || value === '') {
return;
}

Expand Down
44 changes: 43 additions & 1 deletion test/test.filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<foo> \\&"\'.,-_?/Ķä€台北[]{}\t\r\n\b\u0080'};
const testTemplate = twig({data: '{{ foo|url_encode() }}'});
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand All @@ -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.');
Expand Down Expand Up @@ -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 () {
Expand All @@ -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,
Expand Down Expand Up @@ -495,6 +527,11 @@ describe('Twig.js Filters ->', function () {
testTemplate.render().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 () {
twig({
autoescape: true,
Expand Down Expand Up @@ -538,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 <a href="">Test</a>\n '}).should.equal('<a href="">Test</a>');
Expand Down

0 comments on commit 216feba

Please sign in to comment.