fix(formatter): fixed poor formatting on long inline blocks

closes #432
This commit is contained in:
Christopher Pickering 2022-10-24 15:16:06 +02:00
parent a6262669ed
commit 62a88796f4
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 41 additions and 10 deletions

View file

@ -37,6 +37,7 @@ def indent_html(rawcode: str, config: Config) -> str:
ignored_level = 0 ignored_level = 0
for item in rawcode_flat_list: for item in rawcode_flat_list:
# if a raw tag first line # if a raw tag first line
if not is_block_raw and is_ignored_block_opening(config, item): if not is_block_raw and is_ignored_block_opening(config, item):
is_raw_first_line = True is_raw_first_line = True
@ -68,19 +69,32 @@ def indent_html(rawcode: str, config: Config) -> str:
elif ( elif (
( (
re.findall( re.findall(
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text re.compile(
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
(?: (?:
(?:<({slt_html})>)(?:.*?)(?:</(?:\1)>[ \t]*?) # <span>stuff</span> >>>> match 1 (?:<({slt_html})>)(?:.*?)(?:</(?:\1)>) # <span>stuff</span> >>>> match 1
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>[ \t]*?) # <span stuff>stuff</span> >>> match 2 |(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>) # <span stuff>stuff</span> >>> match 2
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>[ \t]*?) # <img stuff /> |(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:<(?:{slt_html})\b[^>]*?/>[ \t]*?) # <img /> |(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(\3)[ ]+?.*?%}}[ \t]*?) # >>> match 3 |(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\3)[ ]+?.*?%}}) # >>> match 3
|{config.ignored_inline_blocks} |{config.ignored_inline_blocks}
) )[ \t]*?
+?[^<]*?$ # with no other tags following until end of line (?:
.*? # anything
(?: # followed by another slt
(?:<({slt_html})>)(?:.*?)(?:</(?:\4)>) # <span>stuff</span> >>>> match 1
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\5)>) # <span stuff>stuff</span> >>> match 2
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\6)[ ]+?.*?%}}) # >>> match 3
|{config.ignored_inline_blocks}
)[ \t]*?
)*? # optional of course
[^<]*?$ # with no other tags following until end of line
""", """,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
),
item, item,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
) )
) )
and is_block_raw is False and is_block_raw is False

View file

@ -641,7 +641,8 @@ class Config:
self.ignored_inline_blocks: str = r""" self.ignored_inline_blocks: str = r"""
<!--.*?--> <!--.*?-->
| <(script|style).*?\</(?:\1)> | <script.*?\</script>
| <style.*?\</style>
| {\*.*?\*} | {\*.*?\*}
| {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#} | {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#}
| <\?php.*?\?> | <\?php.*?\?>

View file

@ -105,6 +105,22 @@ def test_span_leading_text(runner: CliRunner, tmp_file: TextIO) -> None:
""" """
) )
write_to_file(
tmp_file.name,
b"""<p>
<span class="badge">New</span> You can now use <strong>this feature</strong>
</p>""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text(encoding="utf8")
== """<p>
<span class="badge">New</span> You can now use <strong>this feature</strong>
</p>
"""
)
def test_span_and_template(runner: CliRunner, tmp_file: TextIO) -> None: def test_span_and_template(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file( write_to_file(