From 31161ce1aa7d20cd7aae2f28a7ecbd1f1f812966 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Thu, 22 Sep 2022 09:41:20 +0200 Subject: [PATCH] fix(formatter): fixed issue with blank links added between blocks closes #402 --- src/djlint/formatter/condense.py | 9 ++++-- .../test_blank_lines_after_tag/html_ten.html | 4 +++ .../test_blank_lines_after_tag/test_config.py | 30 ++++++++++++++++++- .../test_config.py | 17 ++++++++++- 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/test_config/test_blank_lines_after_tag/html_ten.html diff --git a/src/djlint/formatter/condense.py b/src/djlint/formatter/condense.py index 17225ca..afe9f77 100644 --- a/src/djlint/formatter/condense.py +++ b/src/djlint/formatter/condense.py @@ -104,7 +104,11 @@ def condense_html(html: str, config: Config) -> str: if inside_ignored_block(config, html, match): return match.group() - return match.group() + "\n" + # check that next line is not blank. + if html[match.end() : match.end() + 1] != "\n": # noqa:E203 + return match.group() + "\n" + + return match.group() func = partial(add_blank_line_after, config, html) @@ -113,7 +117,7 @@ def condense_html(html: str, config: Config) -> str: for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]: html = re.sub( re.compile( - rf"((?:{{%\s*?{tag}\b[^}}]+?%}}\n?)+)(?=[^\n])", + rf"((?:{{%\s*?{tag}\b[^}}]+?%}}\n?)+)", re.IGNORECASE | re.MULTILINE | re.DOTALL, ), func, @@ -132,6 +136,7 @@ def condense_html(html: str, config: Config) -> str: # should we add blank lines before load tags? if config.blank_line_before_tag: for tag in [x.strip() for x in config.blank_line_before_tag.split(",")]: + html = re.sub( re.compile( rf"(? None: assert """1 file would be updated.""" in result.output assert result.exit_code == 1 + +def test_blank_lines_after_tag_two(runner: CliRunner) -> None: result = runner.invoke( djlint, ["tests/test_config/test_blank_lines_after_tag/html_two.html", "--check"], @@ -49,6 +51,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: assert """1 file would be updated.""" in result.output assert result.exit_code == 1 + +def test_blank_lines_after_tag_three(runner: CliRunner) -> None: # check blocks that do not start on a newline - they should be left as is. result = runner.invoke( djlint, @@ -58,6 +62,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: assert """0 files would be updated.""" in result.output assert result.exit_code == 0 + +def test_blank_lines_after_tag_four(runner: CliRunner) -> None: result = runner.invoke( djlint, ["tests/test_config/test_blank_lines_after_tag/html_four.html", "--check"], @@ -74,6 +80,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: in result.output ) + +def test_blank_lines_after_tag_five(runner: CliRunner) -> None: # something perfect should stay perfect :) result = runner.invoke( djlint, @@ -81,6 +89,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_after_tag_six(runner: CliRunner) -> None: # something perfect should stay perfect :) result = runner.invoke( djlint, @@ -88,6 +98,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_after_tag_seven(runner: CliRunner) -> None: # make sure endblock doesn't pick up endblocktrans :) result = runner.invoke( djlint, @@ -95,6 +107,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_after_tag_eight(runner: CliRunner) -> None: # check that multiple blank lines are not added result = runner.invoke( djlint, @@ -104,8 +118,11 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: "--check", ], ) + print(result.output) assert result.exit_code == 0 + +def test_blank_lines_after_tag_nine(runner: CliRunner) -> None: result = runner.invoke( djlint, [ @@ -114,3 +131,14 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: ], ) assert result.exit_code == 0 + + +def test_blank_lines_after_tag_ten(runner: CliRunner) -> None: + result = runner.invoke( + djlint, + [ + "tests/test_config/test_blank_lines_after_tag/html_ten.html", + "--check", + ], + ) + assert result.exit_code == 0 diff --git a/tests/test_config/test_blank_lines_before_tag/test_config.py b/tests/test_config/test_blank_lines_before_tag/test_config.py index c252eea..7ac1758 100644 --- a/tests/test_config/test_blank_lines_before_tag/test_config.py +++ b/tests/test_config/test_blank_lines_before_tag/test_config.py @@ -7,7 +7,7 @@ run:: for a single test, run:: - pytest tests/test_config.py::test_custom_html --cov=src/djlint \ + pytest tests/test_config/test_blank_lines_before_tag/test_config.py::test_blank_lines_before_tag_four --cov=src/djlint \ --cov-branch --cov-report xml:coverage.xml --cov-report term-missing """ @@ -36,12 +36,16 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: assert """1 file would be updated.""" in result.output assert result.exit_code == 1 + +def test_blank_lines_before_tag_two(runner: CliRunner) -> None: result = runner.invoke( djlint, ["tests/test_config/test_blank_lines_before_tag/html_two.html", "--check"], ) assert result.exit_code == 0 + +def test_blank_lines_before_tag_three(runner: CliRunner) -> None: # check blocks that do not start on a newline - they should be left as is. result = runner.invoke( djlint, @@ -51,12 +55,15 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: assert """0 files would be updated.""" in result.output assert result.exit_code == 0 + +def test_blank_lines_before_tag_four(runner: CliRunner) -> None: result = runner.invoke( djlint, ["tests/test_config/test_blank_lines_before_tag/html_four.html", "--check"], ) assert result.exit_code == 1 + print(result.output) assert ( """ {% block this %} -{% load i18n %} @@ -68,6 +75,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: in result.output ) + +def test_blank_lines_before_tag_five(runner: CliRunner) -> None: # something perfect should stay perfect :) result = runner.invoke( djlint, @@ -75,6 +84,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_before_tag_six(runner: CliRunner) -> None: # something perfect should stay perfect :) result = runner.invoke( djlint, @@ -82,6 +93,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_before_tag_seven(runner: CliRunner) -> None: # make sure endblock doesn't pick up endblocktrans :) result = runner.invoke( djlint, @@ -89,6 +102,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + +def test_blank_lines_before_tag_eight(runner: CliRunner) -> None: # check that multiple blank lines are not added result = runner.invoke( djlint,