fix(formatter): fix to prevent formatter from breaking malformed attribute tags

closes #317
This commit is contained in:
Christopher Pickering 2022-08-01 07:56:29 -05:00
parent 69ef16afa8
commit 80263b29bd
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
2 changed files with 14 additions and 6 deletions

View file

@ -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|-)+
| {{.*?}}
| {%.*?%}
"""

View file

@ -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"""<a href = "http://test.test:3000/testtesttesttesttesttesttesttesttesttest">Test</a>\n""",
)
assert output.exit_code == 0
def test_ignored_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(