fix(alpine js): fix incorrect indent on alpinejs attribs w/ nested HTML

closes #381
This commit is contained in:
Christopher Pickering 2022-09-14 09:43:31 +02:00
parent 0111619870
commit f0d37a59d1
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 33 additions and 3 deletions

View file

@ -63,7 +63,7 @@ def indent_html(rawcode: str, config: Config) -> str:
# if a one-line, inline tag, just process it, only if line starts w/ it
elif (
re.findall(
rf"(<({slt_html})>)(.*?)(</(\2)>[^<]*?$)",
rf"(^<({slt_html})>)(.*?)(</(\2)>[^<]*?$)",
item,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)

View file

@ -746,18 +746,29 @@ class Config:
"""
)
# either a template tag at the start of a line,
# a html tag at the start of a line,
# or an html tag as the end of a line.
# Nothing in between!
self.tag_unindent: str = (
r"""
^
"""
+ self.template_unindent
+ """
| (?:</
| (?:^</
(?:
"""
+ self.indent_html_tags
+ """
)\\b
)
| (?:</
(?:
"""
+ self.indent_html_tags
+ """
)>$
)
"""
)

View file

@ -27,3 +27,22 @@ def test_alpine_js(runner: CliRunner, tmp_file: TextIO) -> None:
)
assert output.exit_code == 0
def test_alpine_nested_html(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<html lang="en">
<body>
<!-- x-data , x-text , x-html -->
<div x-data="{key:'value',message:'hello <b>world</b> '}">
<p x-text="key"></p>
<p x-html="message"></p>
</div>
</body>
</html>
""",
)
assert output.exit_code == 0

View file

@ -5,7 +5,7 @@ run:
pytest tests/test_html/test_tag_textarea.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_html/test_tag_textarea.py::test_textarea_tag
pytest tests/test_html/test_tag_textarea.py::test_a_tag
"""