From a2da81fe083a9dbc37b8355c25fdc23e3b58d16f Mon Sep 17 00:00:00 2001 From: rajeeshp Date: Fri, 19 May 2023 14:41:36 +0530 Subject: [PATCH] Fixed #34578 -- Made "join" template filter respect autoescape for joiner. --- django/template/defaultfilters.py | 5 +++-- tests/template_tests/filter_tests/test_join.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 03676533b7..5289ef34a0 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -586,8 +586,9 @@ def join(value, arg, autoescape=True): """Join a list with a string, like Python's ``str.join(list)``.""" try: if autoescape: - value = [conditional_escape(v) for v in value] - data = conditional_escape(arg).join(value) + data = conditional_escape(arg).join([conditional_escape(v) for v in value]) + else: + data = arg.join(value) except TypeError: # Fail silently if arg isn't iterable. return value return mark_safe(data) diff --git a/tests/template_tests/filter_tests/test_join.py b/tests/template_tests/filter_tests/test_join.py index 08f68537ee..b92b732972 100644 --- a/tests/template_tests/filter_tests/test_join.py +++ b/tests/template_tests/filter_tests/test_join.py @@ -55,6 +55,22 @@ class JoinTests(SimpleTestCase): ) self.assertEqual(output, "alpha & beta & me") + @setup( + { + "join_autoescape_off": ( + "{% autoescape off %}" + "{{ var_list|join:var_joiner }}" + "{% endautoescape %}" + ), + } + ) + def test_join_autoescape_off(self): + var_list = ["

Hello World!

", "beta & me", ""] + context = {"var_list": var_list, "var_joiner": "
"} + output = self.engine.render_to_string("join_autoescape_off", context) + expected_result = "

Hello World!


beta & me
" + self.assertEqual(output, expected_result) + class FunctionTests(SimpleTestCase): def test_list(self): @@ -69,7 +85,7 @@ class FunctionTests(SimpleTestCase): def test_autoescape_off(self): self.assertEqual( join(["", "", ""], "
", autoescape=False), - "<br><br>", + "

", ) def test_noniterable_arg(self):