diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index 14208770d..be4869b55 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -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. @@ -832,7 +837,7 @@ def do_indent( else: indention = " " * width - newline = "\n" + newline = str(environment.newline_sequence) if isinstance(s, Markup): indention = Markup(indention) diff --git a/tests/test_filters.py b/tests/test_filters.py index d8e9114d0..762a037a2 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -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)