fix(formatter): fixed poor indentation on span tags with leading text

closes #398
This commit is contained in:
Christopher Pickering 2022-09-21 09:52:38 +02:00
parent dd2172f01c
commit 8f36fec142
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
2 changed files with 21 additions and 12 deletions

View file

@ -61,10 +61,11 @@ def indent_html(rawcode: str, config: Config) -> str:
tmp = (indent * indent_level) + item + "\n"
# if a one-line, inline tag, just process it, only if line starts w/ it
# or if it is trailing text
elif (
(
re.findall(
rf"""^ # start of a line
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
(?:
(?:<({slt_html})>)(?:.*?)(?:</(?:\1)>[ \t]*?) # <span>stuff</span> >>>> match 1
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>[ \t]*?) # <span stuff>stuff</span> >>> match 2
@ -147,19 +148,9 @@ def indent_html(rawcode: str, config: Config) -> str:
),
item,
)
# # and not ending in a slt like <span><strong></strong>.
# and not re.findall(
# rf"(<({slt_html})>)(.*?)(</(\2)>[^<]*?$)",
# item,
# re.IGNORECASE | re.VERBOSE | re.MULTILINE,
# )
# and not re.findall(
# rf"(<({slt_html})\\b.+?>)(.*?)(</(\2)>[^<]*?$)",
# item,
# re.IGNORECASE | re.VERBOSE | re.MULTILINE,
# )
and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
indent_level = indent_level + 1

View file

@ -86,3 +86,21 @@ def test_nested_string(runner: CliRunner, tmp_file: TextIO) -> None:
</ul>
"""
)
def test_span_leading_text(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,
b"""{% if this %}<p>Text <span>text</span></p>{% endif %}""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text(encoding="utf8")
== """{% if this %}
<p>
Text <span>text</span>
</p>
{% endif %}
"""
)