Skip to content

Commit

Permalink
fix(formatter): fixed poor formatting on long inline blocks
Browse files Browse the repository at this point in the history
closes #432
  • Loading branch information
christopherpickering committed Oct 24, 2022
1 parent a626266 commit 62a8879
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
32 changes: 23 additions & 9 deletions src/djlint/formatter/indent.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/djlint/settings.py
Expand Up @@ -641,7 +641,8 @@ def __init__(

self.ignored_inline_blocks: str = r"""
<!--.*?-->
| <(script|style).*?\</(?:\1)>
| <script.*?\</script>
| <style.*?\</style>
| {\*.*?\*}
| {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#}
| <\?php.*?\?>
Expand Down
16 changes: 16 additions & 0 deletions tests/test_html/test_tag_span.py
Expand Up @@ -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(
Expand Down

0 comments on commit 62a8879

Please sign in to comment.