From a15b318bea0951d1b47758c222c81e69e134a261 Mon Sep 17 00:00:00 2001 From: wayyoungboy <1017761807@qq.com> Date: Sun, 7 Jun 2026 05:54:31 +0800 Subject: [PATCH] Fix indent filter for empty first line --- src/jinja2/filters.py | 3 ++- tests/test_filters.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index c46e20c10..394fb4ab3 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -847,6 +847,7 @@ def do_indent( indention = Markup(indention) newline = Markup(newline) + is_empty = not s s += newline # this quirk is necessary for splitlines method if blank: @@ -860,7 +861,7 @@ def do_indent( indention + line if line else line for line in lines ) - if first: + if first and (not is_empty or blank): rv = indention + rv return rv diff --git a/tests/test_filters.py b/tests/test_filters.py index 4601469a6..dd37df763 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -179,6 +179,10 @@ def test_indent(self, env): t = env.from_string('{{ "jinja"|indent(blank=true) }}') assert t.render() == "jinja" + def test_indent_empty_string_with_first(self, env): + t = env.from_string('{{ ""|indent(first=true) }}') + assert t.render() == "" + def test_indent_markup_input(self, env): """ Tests cases where the filter input is a Markup type