fixed #160, added test

This commit is contained in:
Christopher Pickering 2022-01-03 09:43:55 -06:00
parent 3c82cbc84b
commit 64a806a3eb
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 25 additions and 28 deletions

View file

@ -4,6 +4,7 @@ from functools import partial
import regex as re
from ..helpers import inside_ignored_block
from ..settings import Config
@ -241,9 +242,13 @@ def format_style(match: re.match) -> str:
return f"{leading_stuff}{tag}{quote}{styles}{quote}"
def format_attributes(config: Config, match: re.match) -> str:
def format_attributes(config: Config, html: str, match: re.match) -> str:
"""Spread long attributes over multiple lines."""
if len(match.group(3).strip()) < config.max_attribute_length:
# check that we are not inside an ingnored block
if (
inside_ignored_block(config, html, match)
or len(match.group(3).strip()) < config.max_attribute_length
):
return match.group()
leading_space = match.group(1)

View file

@ -134,6 +134,7 @@ def indent_html(rawcode: str, config: Config) -> str:
# if a opening raw tag then start ignoring.. only if there is no closing tag
# on the same line
if is_ignored_block_opening(config, item):
is_block_raw = True
is_raw_first_line = False
@ -141,7 +142,7 @@ def indent_html(rawcode: str, config: Config) -> str:
# if a normal tag, we can try to expand attributes
elif is_block_raw is False:
# get leading space, and attributes
func = partial(format_attributes, config)
func = partial(format_attributes, config, item)
tmp = re.sub(
re.compile(

View file

@ -299,8 +299,10 @@ class Config:
| [^\{]{\#
| <pre
| <textarea
| {%[ ]djlint:off[ ]%}
| {\#\s*djlint\:\s*off\s*\#}
| {%[ ]+?comment[ ]+?[^(?:%})]*?%}
| {{!--\s*djlint\:off\s*--}}
| {{-?\s*/\*\s*djlint\:off\s*\*/\s*-?}}
"""
self.ignored_block_closing: str = r"""
@ -312,8 +314,10 @@ class Config:
| \#}
| </pre
| </textarea
| {%[ ]djlint:on[ ]%}
| {\#\s*djlint\:\s*on\s*\#}
| {%[ ]+?endcomment[ ]+?%}
| {{!--\s*djlint\:on\s*--}}
| {{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}}
"""
# ignored block closing tags that
@ -652,11 +656,9 @@ class Config:
| <(script|style).*?(?=(\</(?:\3)>))
# html comment
| <!--\s*djlint\:off\s*-->.*?<!--\s*djlint\:on\s*-->
# django/jinja
| {\#\s*djlint\:off\s*\#}.*?{\#\s*djlint\:on\s*\#}
# django/jinja/nunjucks
| {\#\s*djlint\:\s*off\s*\#}.*?{\#\s*djlint\:\s*on\s*\#}
| {%\s*comment\s*%\}\s*djlint\:off\s*\{%\s*endcomment\s*%\}.*?{%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\}
# nunjucks
| {\#\s*djlint\:off\s*\#}.*?{\#\s*djlint\:on\s*\#}
# handlebars
| {{!--\s*djlint\:off\s*--}}.*?{{!--\s*djlint\:on\s*--}}
# golang

View file

@ -5,7 +5,7 @@ run::
pytest tests/test_html.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_html.py::test_textarea_tag --cov=src/djlint --cov-branch \
pytest tests/test_html.py::test_ignored_block --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
@ -403,26 +403,15 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
assert output.exit_code == 0
assert (
output.text
== """<!-- djlint:off -->
<div><p><span></span></p></div>
<!-- djlint:on -->
{# djlint:off #}
<div><p><span></span></p></div>
{# djlint:on #}
{% comment %} djlint:off {% endcomment %}
<div><p><span></span></p></div>
{% comment %} djlint:on {% endcomment %}
{{ /* djlint:off */ }}
<div><p><span></span></p></div>
{{ /* djlint:on */ }}
{{!-- djlint:off --}}
<div><p><span></span></p></div>
{{!-- djlint:on --}}
"""
output = reformat(
tmp_file,
runner,
b"""{# djlint: off #}<meta name="description" content="{% block meta_content %}Alle vogelkijkhutten van Nederland{% endblock %}">{# djlint:on #}
""",
)
assert output.exit_code == 0
# check script tag
output = reformat(
tmp_file,