From fd52e8c71c7001dcf08ce30948fcf39ad8665b7b Mon Sep 17 00:00:00 2001 From: Oliver Haas Date: Sun, 10 Nov 2024 10:57:24 +0100 Subject: [PATCH 1/5] fix: preline json html thing --- djlint/formatter/indent.py | 3 +- djlint/settings.py | 33 +++++++++++++++++-- .../test_config/test_max_attribute_length.py | 12 +++++++ tests/test_html/test_attributes.py | 6 +--- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/djlint/formatter/indent.py b/djlint/formatter/indent.py index 130c8a847..218cb518e 100644 --- a/djlint/formatter/indent.py +++ b/djlint/formatter/indent.py @@ -260,6 +260,7 @@ def fix_handlebars_template_tags( else: tmp = item + "\n" + # TODO(oliverhaas): This block fails in the end # if a opening raw tag then start ignoring.. only if there is no closing tag # on the same line if is_ignored_block_opening_: @@ -273,7 +274,7 @@ def fix_handlebars_template_tags( func = partial(format_attributes, config, item) tmp = re.sub( - rf"(\s*?)(<(?:{config.indent_html_tags}))\s((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}\/])+?)(\s?/?>)", + config.indent_html_tags_regex, func, tmp, flags=RE_FLAGS_IX, diff --git a/djlint/settings.py b/djlint/settings.py index e81be3605..503f051f2 100644 --- a/djlint/settings.py +++ b/djlint/settings.py @@ -727,14 +727,43 @@ def __init__( """ ) - self.html_tag_regex = r""" + self.template_tags = r""" + {{(?:(?!}}).)*}}|{%(?:(?!%}).)*%} + """ + + self.html_tag_attribute_regex = rf""" + ((?:\s*?(?: + \"(?:{self.template_tags}|\\\"|[^\"])*\" + |'(?:{self.template_tags}|\\'|[^'])*' + |{self.template_tags} + |[^'\">{{}}/\s]|/(?!>) + ))+)? + """ + + self.html_tag_regex = rf""" (!\[]+\b) # a tag name - ((?:\s*?(?:\"[^\"]*\"|'[^']*'|{{(?:(?!}}).)*}}|{%(?:(?!%}).)*%}|[^'\">{}/\s]|/(?!>)))+)? # any attributes + {self.html_tag_attribute_regex} # any attributes \s*? # potentially some whitespace (/?>) # a closing bracket (/> or >) """ + self.indent_html_tags_attribute_regex = rf""" + \s((?: + (?{{}}\/] + )+?) + """ + + self.indent_html_tags_regex: str = rf""" + (\s*?) + (<(?:{self.indent_html_tags})) + {self.indent_html_tags_attribute_regex} + (\s*?/?>) + """ + self.attribute_style_pattern: str = ( r"^(.*?)(style=)([\"|'])(([^\"']+?;)+?)\3" ) diff --git a/tests/test_config/test_max_attribute_length.py b/tests/test_config/test_max_attribute_length.py index 041870c65..d66634991 100644 --- a/tests/test_config/test_max_attribute_length.py +++ b/tests/test_config/test_max_attribute_length.py @@ -103,6 +103,18 @@ ({"max_attribute_length": 3, "custom_html": "[\\w\\-]+"}), id="long tag custom_html", ), + pytest.param( + ( + "