diff --git a/src/djlint/lint.py b/src/djlint/lint.py index 69709be..e8f8bcf 100644 --- a/src/djlint/lint.py +++ b/src/djlint/lint.py @@ -71,26 +71,38 @@ def lint_file(config: Config, this_file: Path) -> Dict: # rule H025 is a special case where the output must be an even number. if rule["name"] == "H025": + open_tags: List[re.Match] = [] + # for match in re.finditer( + # re.compile( + # pattern, flags=build_flags(rule.get("flags", "re.DOTALL")) + # ), + # html, + # ): + # print(r"<(/?(\w+))\s*" +config.attribute_pattern + r"\s*?>") for match in re.finditer( re.compile( - pattern, flags=build_flags(rule.get("flags", "re.DOTALL")) + r"<(/?(\w+))\s*(" + config.attribute_pattern + r"|\s*)*\s*?>", + re.VERBOSE, ), html, ): - if match.group(2) and not re.search( + # print(match) + # print(match.group(2)) + # print(match.group(1)) + if match.group(1) and not re.search( re.compile( rf"^/?{config.always_self_closing_html_tags}\b", re.I | re.X ), - match.group(2), + match.group(1), ): # close tags should equal open tags - if match.group(2)[0] != "/": + if match.group(1)[0] != "/": open_tags.insert(0, match) else: for i, tag in enumerate(copy.deepcopy(open_tags)): - if tag.group(3) == match.group(2)[1:]: + if tag.group(2) == match.group(1)[1:]: open_tags.pop(i) break else: diff --git a/src/djlint/rules.yaml b/src/djlint/rules.yaml index 1b4b56c..955be73 100644 --- a/src/djlint/rules.yaml +++ b/src/djlint/rules.yaml @@ -184,9 +184,8 @@ - rule: name: H025 message: Tag seems to be an orphan. - flags: re.I|re.DOTALL patterns: - - <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{#[^#]*#}|[^'\">{}])*)(? + - None - rule: name: H026 message: Empty id and class tags can be removed. diff --git a/src/djlint/settings.py b/src/djlint/settings.py index aadad02..cf0cfbd 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -507,7 +507,7 @@ class Config: (?: ( (?:\w|-|\.)+ | required | checked - ) # attribute name + )? # attribute name (?: [ ]*?=[ ]*? # followed by "=" ( \"[^\"]*? # double quoted attribute @@ -538,6 +538,7 @@ class Config: """ + r""" | {{.*?}} + | {\#.*?\#} | {%.*?%}) """ ) diff --git a/tests/test_linter/test_linter.py b/tests/test_linter/test_linter.py index 36a0587..a4b6518 100644 --- a/tests/test_linter/test_linter.py +++ b/tests/test_linter/test_linter.py @@ -670,6 +670,14 @@ def test_H025(runner: CliRunner, tmp_file: TextIO) -> None: result = runner.invoke(djlint, [tmp_file.name]) assert "H025" not in result.output + # issue #447 + write_to_file( + tmp_file.name, + b"""""", + ) + assert "H025" not in result.output + def test_H026(runner: CliRunner, tmp_file: TextIO) -> None: write_to_file(tmp_file.name, b'')