Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting on "/>" in HTML attributes #1102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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