mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-16 21:40:24 +00:00
fix(formatter): fixed poor formatting on long inline blocks
closes #432
This commit is contained in:
parent
a6262669ed
commit
62a88796f4
3 changed files with 41 additions and 10 deletions
|
|
@ -37,6 +37,7 @@ def indent_html(rawcode: str, config: Config) -> str:
|
|||
ignored_level = 0
|
||||
|
||||
for item in rawcode_flat_list:
|
||||
|
||||
# if a raw tag first line
|
||||
if not is_block_raw and is_ignored_block_opening(config, item):
|
||||
is_raw_first_line = True
|
||||
|
|
@ -68,19 +69,32 @@ def indent_html(rawcode: str, config: Config) -> str:
|
|||
elif (
|
||||
(
|
||||
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})\b[^>]+?>)(?:.*?)(?:</(?:\2)>[ \t]*?) # <span stuff>stuff</span> >>> match 2
|
||||
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>[ \t]*?) # <img stuff />
|
||||
|(?:<(?:{slt_html})\b[^>]*?/>[ \t]*?) # <img />
|
||||
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(\3)[ ]+?.*?%}}[ \t]*?) # >>> match 3
|
||||
(?:<({slt_html})>)(?:.*?)(?:</(?:\1)>) # <span>stuff</span> >>>> match 1
|
||||
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>) # <span stuff>stuff</span> >>> match 2
|
||||
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|
||||
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|
||||
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\3)[ ]+?.*?%}}) # >>> match 3
|
||||
|{config.ignored_inline_blocks}
|
||||
)
|
||||
+?[^<]*?$ # with no other tags following until end of line
|
||||
)[ \t]*?
|
||||
(?:
|
||||
.*? # 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,
|
||||
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
||||
)
|
||||
)
|
||||
and is_block_raw is False
|
||||
|
|
|
|||
|
|
@ -641,7 +641,8 @@ class Config:
|
|||
|
||||
self.ignored_inline_blocks: str = r"""
|
||||
<!--.*?-->
|
||||
| <(script|style).*?\</(?:\1)>
|
||||
| <script.*?\</script>
|
||||
| <style.*?\</style>
|
||||
| {\*.*?\*}
|
||||
| {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#}
|
||||
| <\?php.*?\?>
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
write_to_file(
|
||||
|
|
|
|||
Loading…
Reference in a new issue