From 80263b29bd1c0646ebf4473a6674061621d730ff Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 1 Aug 2022 07:56:29 -0500 Subject: [PATCH] fix(formatter): fix to prevent formatter from breaking malformed attribute tags closes #317 --- src/djlint/settings.py | 12 ++++++------ tests/test_html/test_attributes.py | 8 ++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/djlint/settings.py b/src/djlint/settings.py index eb29fdc..4f7b399 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -486,22 +486,22 @@ class Config: ) self.attribute_pattern: str = ( r""" - (?:[^\s]+?=(?:\"[^\"]*?""" + (?:[^\s]+?[ ]*?=[ ]*?(?:\"[^\"]*?""" + self.template_if_for_pattern + r"""[^\"]*?\"|\'[^\']*?""" + self.template_if_for_pattern + r"""[^\']*?\')) - | (?:[^\s]+?=(?:\"[^\"]*?{{.*?}}[^\"]*?\"|\'[^\']*?{{.*?}}[^\']*?\')) + | (?:[^\s]+?[ ]*?=[ ]*?(?:\"[^\"]*?{{.*?}}[^\"]*?\"|\'[^\']*?{{.*?}}[^\']*?\')) | """ + self.template_if_for_pattern + r""" - | (?:[^\s]+?=(?:\"(?:[^\"]*?{%[^}]*?%}[^\"]*?)+?\")) - | (?:[^\s]+?=(?:\'(?:[^\']*?{%[^}]*?%}[^\']*?)+?\')) - | (?:[^\s]+?=(?:\".*?\"|\'.*?\')) + | (?:[^\s]+?[ ]*?=[ ]*?(?:\"(?:[^\"]*?{%[^}]*?%}[^\"]*?)+?\")) + | (?:[^\s]+?[ ]*?=[ ]*?(?:\'(?:[^\']*?{%[^}]*?%}[^\']*?)+?\')) + | (?:[^\s]+?[ ]*?=[ ]*?(?:\".*?\"|\'.*?\')) | required | checked | (?:\w|-|\.)+ - | (?:\w|-|\.)+=(?:\w|-)+ + | (?:\w|-|\.)+[ ]*?=[ ]*?(?:\w|-)+ | {{.*?}} | {%.*?%} """ diff --git a/tests/test_html/test_attributes.py b/tests/test_html/test_attributes.py index ce820f8..89ca379 100644 --- a/tests/test_html/test_attributes.py +++ b/tests/test_html/test_attributes.py @@ -83,6 +83,14 @@ def test_long_attributes(runner: CliRunner, tmp_file: TextIO) -> None: ) assert output.exit_code == 0 + # attributes with space around = are not brocken + output = reformat( + tmp_file, + runner, + b"""Test\n""", + ) + assert output.exit_code == 0 + def test_ignored_attributes(runner: CliRunner, tmp_file: TextIO) -> None: output = reformat(