Skip to content

Commit

Permalink
Fix formatting on "/>" in HTML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
novucs committed Jan 10, 2025
1 parent 0925564 commit ed3a368
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ def fix_handlebars_template_tags(
re.search(
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
(?:
<({slt_html})(?:(?:>|\b[^>]+?>)(?:.*?)(?:</(?:\1)>)|\b[^>]*?/>) # <span stuff-or-not>stuff</span> or <img stuff /> >>> match 1
<({slt_html})(?:(?:>|\b[^>]+?>)(?:.*?)(?:</(?:\1)>)|\b(?:[^>"']|"[^"]*"|'[^']*')*?\/>) # <span stuff-or-not>stuff</span> or <img stuff /> >>> match 1
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\2)[ ]+?.*?%}}) # >>> match 2
|{config.ignored_inline_blocks}
)[ \t]*?
(?:
.*? # anything
(?: # followed by another slt
<({slt_html})(?:(?:>|\b[^>]+?>)(?:.*?)(?:</(?:\3)>)|\b[^>]*?/>) # <span stuff-or-not>stuff</span> or <img stuff /> >>> match 3
<({slt_html})(?:(?:>|\b[^>]+?>)(?:.*?)(?:</(?:\3)>)|\b(?:[^>"']|"[^"]*"|'[^']*')*?\/>) # <span stuff-or-not>stuff</span> or <img stuff /> >>> match 3
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\4)[ ]+?.*?%}}) # >>> match 4
|{config.ignored_inline_blocks}
Expand Down
42 changes: 42 additions & 0 deletions tests/test_html/test_hyperscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Test for _hyperscript.
uv run pytest tests/test_html/test_hyperscript.py
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from djlint.reformat import formatter
from tests.conftest import printer

if TYPE_CHECKING:
from djlint.settings import Config

test_data = [
pytest.param(
(
"<div _='on click increment :x then put it into the <output/> in me'>\n"
" Clicks:\n"
" <output>0</output>\n"
"</div>\n"
),
(
"<div _='on click increment :x then put it into the <output/> in me'>\n"
" Clicks:\n"
" <output>0</output>\n"
"</div>\n"
),
id="html_tags_in_attributes",
)
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source: str, expected: str, basic_config: Config) -> None:
output = formatter(basic_config, source)

printer(expected, source, output)
assert expected == output

0 comments on commit ed3a368

Please sign in to comment.