closes #98, closes #99, closes #97, added tests

This commit is contained in:
Christopher Pickering 2021-10-11 16:08:32 +03:00
parent d634ce61fa
commit 46492fdd05
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 30 additions and 8 deletions

View file

@ -40,7 +40,7 @@ def expand_html(html: str, config: Config) -> str:
# put attributes on one line
html = re.sub(
re.compile(
fr"(<(?:{config.indent_html_tags}))((?:\s(?:(?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>/])*)+?)(/?>)",
fr"(<(?:{config.indent_html_tags})\b)((?:\s(?:(?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>/])*)+?)(/?>)",
flags=re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
_flatten_attributes,
@ -55,7 +55,7 @@ def expand_html(html: str, config: Config) -> str:
# html tags - break before
html = re.sub(
re.compile(
fr"{break_char}\K(</?(?:{html_tags})(?:\s((?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>])*)?>)",
fr"{break_char}\K(</?(?:{html_tags})\b(\"[^\"]*\"|'[^']*'|[^'\">])*>)",
flags=re.IGNORECASE | re.VERBOSE,
),
add_left,
@ -65,7 +65,7 @@ def expand_html(html: str, config: Config) -> str:
# html tags - break after
html = re.sub(
re.compile(
fr"(</?(?:{html_tags})(?:\s((?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>])*)?>)(?=[^\n])",
fr"(</?(?:{html_tags})\b(\"[^\"]*\"|'[^']*'|[^'\">])*>)(?=[^\n])",
flags=re.IGNORECASE | re.VERBOSE,
),
add_right,
@ -82,20 +82,20 @@ def expand_html(html: str, config: Config) -> str:
if not re.findall(
r"\<(?:"
+ str(config.break_html_tags)
+ r")[ ][^>]*?"
+ str(config.indent_html_tags)
+ r")\b(?:[^>]|{%[^(?:%}]*?%}|{{[^(?:}}]*?}})*?"
+ re.escape(match.group(1))
+ "$",
html[: match.end()],
re.MULTILINE | re.VERBOSE,
):
return out_format % match.group(1)
return match.group(1)
# template tags
# break before
html = re.sub(
re.compile(
break_char

View file

@ -191,4 +191,4 @@
flags: re.I
patterns:
- <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b=(\"\"|'')
- <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b[^=]
- <\w+\b[^(?:{(?:%|{|#))>]*?\b(class|id)\b[^=\""]

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_django.py::test_comment --cov=src/djlint \
pytest tests/test_django.py::test_complex_attributes --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -396,6 +396,13 @@ def test_complex_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
)
assert output["exit_code"] == 1
output = reformat(
tmp_file,
runner,
b"""<div class="bg-level{% if value >= 70 %}1{% elif value >= 60 %}2{% elif value >= 50 %}3{% else %}4{% endif %}></div>""",
)
assert output["exit_code"] == 0
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(

View file

@ -164,6 +164,21 @@ def test_dd_tag(runner: CliRunner, tmp_file: TextIO) -> None:
)
def test_span_tag(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,
b"""<span class="icon has-text-grey is-large "><i class="fas fa-lg fa-star"></i></span>""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text()
== """<span class="icon has-text-grey is-large ">
<i class="fas fa-lg fa-star"></i>
</span>
"""
)
def test_dt_tag(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,