mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-26 21:33:59 +00:00
simplified regex to improve speed
This commit is contained in:
parent
ad032df34f
commit
2029496854
2 changed files with 9 additions and 38 deletions
|
|
@ -82,28 +82,10 @@ def compress_html(html: str, config: Config) -> str:
|
|||
|
||||
html = _strip_html_whitespace(html, config)
|
||||
|
||||
html = re.sub(
|
||||
r"(<([\w]+)[^>]*>)\s+?(<\/\2>)",
|
||||
r"\1\3",
|
||||
html,
|
||||
flags=re.IGNORECASE | re.MULTILINE,
|
||||
)
|
||||
|
||||
# put empty template tags on one line
|
||||
html = re.sub(
|
||||
re.compile(
|
||||
rf"({{%-?[ ]*?({config.start_template_tags})[^}}]+?-?%}})\s+?(\{{\%-?[ ]end\2[^}}]*?\%\}})",
|
||||
flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE,
|
||||
),
|
||||
r"\1\3",
|
||||
html,
|
||||
)
|
||||
|
||||
# put short single line tags on one line
|
||||
# verbose doesn't seem to work with replace groups.
|
||||
html = re.sub(
|
||||
re.compile(
|
||||
fr"(<({config.single_line_html_tags})>)\s*([^<\n]{{,80}})\s*?(</(\2)>)",
|
||||
fr"(<({config.single_line_html_tags})(?:[^<>])*>)\s*([^<\n]{{,80}})\s*?(</(\2)>)",
|
||||
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
|
||||
),
|
||||
r"\1\3\4",
|
||||
|
|
@ -111,24 +93,7 @@ def compress_html(html: str, config: Config) -> str:
|
|||
re.IGNORECASE | re.MULTILINE | re.DOTALL,
|
||||
)
|
||||
|
||||
html = re.sub(
|
||||
re.compile(
|
||||
fr"(<({config.single_line_html_tags})>)\s*?([^<\n]{{,80}})\s*?(</(\2)>)",
|
||||
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
|
||||
),
|
||||
r"\1\3\4",
|
||||
html,
|
||||
)
|
||||
|
||||
html = re.sub(
|
||||
re.compile(
|
||||
fr"(<({config.single_line_html_tags})[ ][^>\n]{{,80}}>)\s*([^<\n]{{,80}})\s*?(</(\2)>)",
|
||||
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
|
||||
),
|
||||
r"\1\3\4",
|
||||
html,
|
||||
)
|
||||
|
||||
# put short template tags back on one line
|
||||
html = re.sub(
|
||||
re.compile(
|
||||
rf"({{%-?[ ]*?({config.single_line_template_tags})[^\n(?:%}})]{{,30}}%}})\s*([^%\n]{{,50}})\s*?({{%-?[ ]+?end(\2)[ ]*?%}})",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ run::
|
|||
|
||||
for a single test, run::
|
||||
|
||||
pytest tests/test_django.py::test_complex_attributes --cov=src/djlint \
|
||||
pytest tests/test_django.py::test_empty_tags_on_one_line --cov=src/djlint \
|
||||
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
"""
|
||||
|
|
@ -20,6 +20,12 @@ from click.testing import CliRunner
|
|||
from .conftest import reformat
|
||||
|
||||
|
||||
def test_empty_tags_on_one_line(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(tmp_file, runner, b"{% if stuff %}\n{% endif %}")
|
||||
assert output["text"] == """{% if stuff %}{% endif %}\n"""
|
||||
assert output["exit_code"] == 1
|
||||
|
||||
|
||||
def test_dj_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
output = reformat(
|
||||
tmp_file, runner, b"{# comment #}\n{% if this %}<div></div>{% endif %}"
|
||||
|
|
|
|||
Loading…
Reference in a new issue