Merge pull request #826 from terminalmage/issue823
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix escaped characters in indent filter
authored by David Lord · committed by GitHub · parents 34b1ab9, a2f5e2c · Browse files · 3 files·+27 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−9 removed
- tests:+17 added−6 removed
- other:+10 added−3 removed
Provenance
- Commit
3bb2ef8- Tree
2fc57ae- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
34b1ab9- Parent 2
a2f5e2c
3 files changed+27 added−9 removed
modified:jinja2/filters.py+9 added-3 removed
552 unmodified lines553 'The "indentfirst" argument is renamed to "first".'554 ), stacklevel=2)555 first = indentfirst556 557- s += u'\n' # this quirk is necessary for splitlines method557 indention = u' ' * width558+ newline = u'\n'559 560+ if isinstance(s, Markup):561+ indention = Markup(indention)562+ newline = Markup(newline)563+564+ s += newline # this quirk is necessary for splitlines method565+566 if blank:561- rv = (u'\n' + indention).join(s.splitlines())567+ rv = (newline + indention).join(s.splitlines())568 else:569 lines = s.splitlines()570 rv = lines.pop(0)571 572 if lines:567- rv += u'\n' + u'\n'.join(573+ rv += newline + newline.join(574 indention + line if line else line for line in lines575 )576 577 if first:619 unmodified linesmodified:tests/test_filters.py+17 added-6 removed
133 unmodified lines134 tmpl = env.from_string('''{{ "%s|%s"|format("a", "b") }}''')135 out = tmpl.render()136 assert out == 'a|b'137 138- def test_indent(self, env):139- text = '\n'.join(['', 'foo bar', ''])138+ @staticmethod139+ def _test_indent_multiline_template(env, markup=False):140+ text = '\n'.join(['', 'foo bar', '"baz"', ''])141+ if markup:142+ text = Markup(text)143 t = env.from_string('{{ foo|indent(2, false, false) }}')141- assert t.render(foo=text) == '\n foo bar\n'144+ assert t.render(foo=text) == '\n foo bar\n "baz"\n'145 t = env.from_string('{{ foo|indent(2, false, true) }}')143- assert t.render(foo=text) == '\n foo bar\n '146+ assert t.render(foo=text) == '\n foo bar\n "baz"\n '147 t = env.from_string('{{ foo|indent(2, true, false) }}')145- assert t.render(foo=text) == ' \n foo bar\n'148+ assert t.render(foo=text) == ' \n foo bar\n "baz"\n'149 t = env.from_string('{{ foo|indent(2, true, true) }}')147- assert t.render(foo=text) == ' \n foo bar\n '150+ assert t.render(foo=text) == ' \n foo bar\n "baz"\n '151 152+ def test_indent(self, env):153+ self._test_indent_multiline_template(env)154 t = env.from_string('{{ "jinja"|indent }}')155 assert t.render() == 'jinja'156 t = env.from_string('{{ "jinja"|indent(first=true) }}')157 assert t.render() == ' jinja'158 t = env.from_string('{{ "jinja"|indent(blank=true) }}')159 assert t.render() == 'jinja'160 161+ def test_indent_markup_input(self, env):162+ '''163+ Tests cases where the filter input is a Markup type164+ '''165+ self._test_indent_multiline_template(env, markup=True)166+167 def test_indentfirst_deprecated(self, env):168 with pytest.warns(DeprecationWarning):169 env.from_string('{{ "jinja"|indent(indentfirst=true) }}').render()170 493 unmodified linesmodified:.gitignore+1 added
13 unmodified lines14 venv-*/15 .coverage16 .coverage.*17 htmlcov18+.pytest_cache/