Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Unreleased
- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`1793`
- Use ``flit_core`` instead of ``setuptools`` as build backend.
- Fix ``indent`` filter not respecting ``blank=False`` for the first line
when ``first=True`` is set. :issue:`2176`


Version 3.1.6
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def do_indent(
indention + line if line else line for line in lines
)

if first:
if first and (blank or rv.split("\n", 1)[0]):
rv = indention + rv

return rv
Expand Down
17 changes: 16 additions & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _test_indent_multiline_template(env, markup=False):
t = env.from_string("{{ foo|indent(2, false, true) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n '
t = env.from_string("{{ foo|indent(2, true, false) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n'
assert t.render(foo=text) == '\n foo bar\n "baz"\n'
t = env.from_string("{{ foo|indent(2, true, true) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n '

Expand All @@ -179,6 +179,21 @@ def test_indent(self, env):
t = env.from_string('{{ "jinja"|indent(blank=true) }}')
assert t.render() == "jinja"

def test_indent_first_blank(self, env):
# ``blank=False`` (the default) must suppress indentation of the first
# line when it is empty, even when ``first=True``. Regression for
# https://github.com/pallets/jinja/issues/2176
t = env.from_string("{% filter indent(4, first=true) %}{% endfilter %}")
assert t.render() == ""
# A leading blank line is not indented when blank=False.
t = env.from_string('{{ "\nhello"|indent(4, first=true) }}')
assert t.render() == "\n hello"
# blank=True still indents the first line even when it is empty.
t = env.from_string(
"{% filter indent(4, first=true, blank=true) %}{% endfilter %}"
)
assert t.render() == " "

def test_indent_markup_input(self, env):
"""
Tests cases where the filter input is a Markup type
Expand Down
Loading