Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filters with empty string input #690

Merged
merged 3 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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