diff --git a/src/djlint/formatter/compress_html.py b/src/djlint/formatter/compress_html.py index 6027912..27f1bd0 100644 --- a/src/djlint/formatter/compress_html.py +++ b/src/djlint/formatter/compress_html.py @@ -79,6 +79,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str: def compress_html(html: str, config: Config) -> str: """Compress back tags that do not need to be expanded.""" # put empty tags on one line + html = _strip_html_whitespace(html, config) html = re.sub( @@ -91,7 +92,7 @@ def compress_html(html: str, config: Config) -> str: # put empty template tags on one line html = re.sub( re.compile( - rf"({{%-?[ ]*?({config.start_template_tags})[^}}]+?-?%}})\s+?(\{{\%-?[ ]end[^}}]*?\%\}})", + rf"({{%-?[ ]*?({config.start_template_tags})[^}}]+?-?%}})\s+?(\{{\%-?[ ]end\2[^}}]*?\%\}})", flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE, ), r"\1\3", diff --git a/src/djlint/formatter/indent_html.py b/src/djlint/formatter/indent_html.py index 9eb3385..3209a3f 100644 --- a/src/djlint/formatter/indent_html.py +++ b/src/djlint/formatter/indent_html.py @@ -166,7 +166,7 @@ def indent_html(rawcode: str, config: Config) -> str: for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]: beautified_code = re.sub( re.compile( - fr"((?:^{{%\s*?{tag}[^}}]+?%}}\n?)+)", + fr"((?:^\s*?{{%\s*?{tag}[^}}]+?%}}\n?)+)", re.IGNORECASE | re.MULTILINE | re.DOTALL, ), r"\1\n", diff --git a/tests/config_blank_lines_after_tag/html_four.html b/tests/config_blank_lines_after_tag/html_four.html new file mode 100644 index 0000000..f115c2f --- /dev/null +++ b/tests/config_blank_lines_after_tag/html_four.html @@ -0,0 +1,3 @@ +{% block this %} +{% load i18n %} +{% endblock this %} diff --git a/tests/test_config.py b/tests/test_config.py index 448ad4b..3f3b1ef 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -126,6 +126,21 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: assert """0 files would be updated.""" in result.output assert result.exit_code == 0 + result = runner.invoke( + djlint, ["tests/config_blank_lines_after_tag/html_four.html", "--check"] + ) + + assert result.exit_code == 1 + assert ( + """ {% block this %} +-{% load i18n %} ++ {% load i18n %} ++ + {% endblock this %} +""" + in result.output + ) + def test_profile(runner: CliRunner) -> None: result = runner.invoke(djlint, ["tests/config_profile/html.html"]) diff --git a/tests/test_django.py b/tests/test_django.py index f7035db..ce75e0c 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -7,7 +7,7 @@ run:: for a single test, run:: - pytest tests/test_django.py::test_inline_comment --cov=src/djlint \ + pytest tests/test_django.py::test_load_tag --cov=src/djlint \ --cov-branch --cov-report xml:coverage.xml --cov-report term-missing """ @@ -314,3 +314,19 @@ def test_complex_attributes(runner: CliRunner, tmp_file: TextIO) -> None: {% ifchanged comment.stream_id %} comments-msg {% else %} comments-newMsgReply {% endifchanged %}> """ ) + + +def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None: + output = reformat( + tmp_file, + runner, + b"""{% block content %}{% load i18n %}{% endblock %}""", + ) + assert output["exit_code"] == 1 + assert ( + output["text"] + == r"""{% block content %} + {% load i18n %} +{% endblock %} +""" + )