Skip to content

Commit

Permalink
Don't append extra newline when using |indent filter
Browse files Browse the repository at this point in the history
Fixes #1231
  • Loading branch information
fdintino committed Jul 19, 2020
1 parent 73a4eb3 commit 8186d4f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Changelog
* Fix bug that prevented errors in included templates from being raised when
rendering templates synchronously. Fixes
[#1272](https://github.com/mozilla/nunjucks/pull/1276).
* The `indent` filter no longer appends an additional newline. Fixes
[#1231](https://github.com/mozilla/nunjucks/issues/1231).

3.2.1 (Mar 17 2020)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions nunjucks/src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function indent(str, width, indentfirst) {
const sp = lib.repeat(' ', width);

const res = lines.map((l, i) => {
return (i === 0 && !indentfirst) ? `${l}\n` : `${sp}${l}\n`;
}).join('');
return (i === 0 && !indentfirst) ? l : `${sp}${l}`;
}).join('\n');

return r.copySafeness(str, res);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@

it('indent', function(done) {
equal('{{ "one\ntwo\nthree" | indent }}',
'one\n two\n three\n');
'one\n two\n three');
equal('{{ "one\ntwo\nthree" | indent(2) }}',
'one\n two\n three\n');
'one\n two\n three');
equal('{{ "one\ntwo\nthree" | indent(2, true) }}',
' one\n two\n three\n');
' one\n two\n three');

equal('{{ str | indent }}', {
str: r.markSafe('one\ntwo\nthree')
}, 'one\n two\n three\n');
}, 'one\n two\n three');

equal('{{ "" | indent }}', '');
equal('{{ undefined | indent }}', '');
Expand Down

0 comments on commit 8186d4f

Please sign in to comment.