Skip to content

Commit

Permalink
fix(formatter): fixed an edge case wild regex grab
Browse files Browse the repository at this point in the history
When a close template tag was following a quoted attribute on the same line, and was also following
an html tag that had a quoted attribute.. line breaks were not inserted correctly. Thanks
@timoludwig

closes #640
  • Loading branch information
christopherpickering committed May 11, 2023
1 parent 6520f63 commit 0d57e75
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/djlint/formatter/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def add_html_line(out_format: str, match: re.Match) -> str:
# template tag breaks
def should_i_move_template_tag(out_format: str, match: re.Match) -> str:
# ensure template tag is not inside an html tag and also not the first line of the file

if inside_ignored_block(config, html, match):
return match.group(1)

if not re.findall(
r"\<(?:"
+ str(config.indent_html_tags)
+ r")\b(?:\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{\#[^\#]*\#}|[^>{}])*?"
# original
# + r")\b(?:[^>]|{%[^(?:%}]*?%}|{{[^(?:}}]*?}})*?"
+ re.escape(match.group(1)) + "$",
r"\<(?:" + str(config.indent_html_tags)
# added > as not allowed inside a "" or '' to prevent invalid wild html matches
# for issue #640
+ r")\b(?:\"[^\">]*\"|'[^'>]*'|{{[^}]*}}|{%[^%]*%}|{\#[^\#]*\#}|[^>{}])*?"
+ re.escape(match.group(1))
+ "$",
html[: match.end()],
re.MULTILINE | re.VERBOSE,
):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_django/test_quoted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Test django quoted tags.
poetry run pytest tests/test_django/test_filter.py
"""
import pytest

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

test_data = [
pytest.param(
(
"<h1>\n"
' {% if condition1 %}<span class="cls"></span>{% endif %}\n'
' {% if condition2 %}"{{ text }}"{% endif %}\n'
" </h1>\n"
),
(
"<h1>\n"
' {% if condition1 %}<span class="cls"></span>{% endif %}\n'
' {% if condition2 %}"{{ text }}"{% endif %}\n'
"</h1>\n"
),
id="issue #640",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, django_config):
output = formatter(django_config, source)

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

0 comments on commit 0d57e75

Please sign in to comment.