Skip to content

Commit

Permalink
Replaced hardcoded LF with newline_sequence in indent filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeromin committed Oct 3, 2024
1 parent f2777b9 commit 7c1b5c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,13 @@ def do_urlize(
return rv


@pass_environment
def do_indent(
s: str, width: t.Union[int, str] = 4, first: bool = False, blank: bool = False
environment: "Environment",
s: str,
width: t.Union[int, str] = 4,
first: bool = False,
blank: bool = False,
) -> str:
"""Return a copy of the string with each line indented by 4 spaces. The
first line and blank lines are not indented by default.
Expand All @@ -832,7 +837,7 @@ def do_indent(
else:
indention = " " * width

newline = "\n"
newline = environment.newline_sequence

if isinstance(s, Markup):
indention = Markup(indention)
Expand Down
11 changes: 6 additions & 5 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,18 @@ def test_format(self, env):

@staticmethod
def _test_indent_multiline_template(env, markup=False):
text = "\n".join(["", "foo bar", '"baz"', ""])
newline = env.newline_sequence
text = newline.join(["", "foo bar", '"baz"', ""])
if markup:
text = Markup(text)
t = env.from_string("{{ foo|indent(2, false, false) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n'
assert t.render(foo=text) == f'{newline} foo bar{newline} "baz"{newline}'
t = env.from_string("{{ foo|indent(2, false, true) }}")
assert t.render(foo=text) == '\n foo bar\n "baz"\n '
assert t.render(foo=text) == f'{newline} foo bar{newline} "baz"{newline} '
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) == f' {newline} foo bar{newline} "baz"{newline}'
t = env.from_string("{{ foo|indent(2, true, true) }}")
assert t.render(foo=text) == ' \n foo bar\n "baz"\n '
assert t.render(foo=text) == f' {newline} foo bar{newline} "baz"{newline} '

def test_indent(self, env):
self._test_indent_multiline_template(env)
Expand Down

0 comments on commit 7c1b5c4

Please sign in to comment.