From 8186d4f3ed8bf22b4b41e1e40f4cd463613a6f7d Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Sun, 19 Jul 2020 18:48:09 -0400 Subject: [PATCH] Don't append extra newline when using |indent filter Fixes #1231 --- CHANGELOG.md | 2 ++ nunjucks/src/filters.js | 4 ++-- tests/filters.js | 8 ++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2956b9b7..658d1924 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------- diff --git a/nunjucks/src/filters.js b/nunjucks/src/filters.js index 8e17f784..1a99c1e1 100644 --- a/nunjucks/src/filters.js +++ b/nunjucks/src/filters.js @@ -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); } diff --git a/tests/filters.js b/tests/filters.js index 6d526d30..2c56bc7c 100644 --- a/tests/filters.js +++ b/tests/filters.js @@ -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 }}', '');