closes #121, added tests

This commit is contained in:
Christopher Pickering 2021-10-14 14:53:47 +03:00
parent be5652f012
commit 535eb9d659
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
5 changed files with 45 additions and 6 deletions

View file

@ -70,5 +70,5 @@ def condense_html(html: str, config: Config) -> str:
r"\1\n",
html,
)
print(html)
return html

View file

@ -4,7 +4,11 @@ from functools import partial
import regex as re
from ..helpers import is_ignored_block_closing, is_ignored_block_opening
from ..helpers import (
is_ignored_block_closing,
is_ignored_block_opening,
is_safe_closing_tag,
)
from ..settings import Config
from .attributes import format_attributes
@ -107,10 +111,11 @@ def indent_html(rawcode: str, config: Config) -> str:
tmp = (indent * indent_level) + item + "\n"
indent_level = indent_level + 1
elif is_raw_first_line is True:
elif is_raw_first_line is True or is_safe_closing_tag(config, item):
tmp = (indent * indent_level) + item + "\n"
elif is_block_raw is True or item.strip() == "":
tmp = item + "\n"
# otherwise, just leave same level

View file

@ -53,10 +53,35 @@ def is_ignored_block_closing(config: Config, item: str) -> bool:
)
def is_safe_closing_tag(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
re.compile(config.ignored_inline_blocks, flags=re.IGNORECASE | re.VERBOSE),
item,
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return re.search(
re.compile(config.safe_closing_tag, flags=re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
"""Do not add whitespace if the tag is in a non indent block."""
return any(
ignored_match.start() <= match.start() and match.end() <= ignored_match.end()
ignored_match.start(0) <= match.start() and match.end(0) <= ignored_match.end()
for ignored_match in list(
re.finditer(
re.compile(

View file

@ -253,6 +253,13 @@ class Config:
| {%[ ]+?endcomment[ ]+?%}
"""
# ignored block closing tags that
# we can savely indent.
self.safe_closing_tag: str = r"""
</script
| </style
"""
# all html tags possible
self.indent_html_tags: str = r"""
a
@ -560,7 +567,8 @@ class Config:
)
self.ignored_blocks: str = r"""
<(script|style|pre|textarea).*?</(\1)>
<(pre|textarea).*?</(\1)>
| <(script|style).*?(?=(\</(?:\3)>))
# html comment
| <!--\s*djlint\:off\s*-->.*?<!--\s*djlint\:on\s*-->
# django/jinja

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_hr_tag --cov=src/djlint --cov-branch \
pytest tests/test_html.py::test_script_tag --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
@ -54,6 +54,7 @@ def test_script_tag(runner: CliRunner, tmp_file: TextIO) -> None:
b"""<div>\n <script>console.log();\n console.log();\n\n </script>\n</div>""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
assert (
Path(tmp_file.name).read_text()
== """<div>