diff --git a/src/djlint/formatter/compress_html.py b/src/djlint/formatter/compress_html.py index 420a81b..2f395b7 100644 --- a/src/djlint/formatter/compress_html.py +++ b/src/djlint/formatter/compress_html.py @@ -20,8 +20,13 @@ def _strip_html_whitespace(html: str, config: Config) -> str: rawcode_flat = "" is_block_ignored = False is_group_ignored = False + lstrip = False for item in html.strip().splitlines(): + # we can left strip ignored blocks on the opening tag. + lstrip = not is_group_ignored and bool( + re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE) + ) # start of ignored block. If we are already in an ingored block, keep true. is_group_ignored = is_group_ignored or bool( @@ -52,6 +57,9 @@ def _strip_html_whitespace(html: str, config: Config) -> str: tmp = _clean_line(item) is_block_ignored = True + elif lstrip: + tmp = item.lstrip() + elif is_group_ignored or is_block_ignored: tmp = item @@ -63,6 +71,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str: re.search(config.ignored_group_closing, item, re.IGNORECASE | re.VERBOSE) ): is_group_ignored = False + lstrip = False rawcode_flat = rawcode_flat + tmp + "\n" diff --git a/src/djlint/formatter/indent_html.py b/src/djlint/formatter/indent_html.py index 2994b2f..05c1992 100644 --- a/src/djlint/formatter/indent_html.py +++ b/src/djlint/formatter/indent_html.py @@ -16,6 +16,7 @@ def indent_html(rawcode: str, config: Config) -> str: beautified_code = "" indent_level = 0 + is_raw_first_line = False is_block_raw = False slt_html = config.break_html_tags @@ -27,6 +28,12 @@ def indent_html(rawcode: str, config: Config) -> str: slt_template = config.single_line_template_tags for item in rawcode_flat_list: + # if a raw tag first line + if not is_block_raw and re.search( + config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE + ): + is_raw_first_line = True + # if a raw tag then start ignoring if re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE): is_block_raw = True @@ -97,6 +104,9 @@ 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: + tmp = (indent * indent_level) + item + "\n" + elif is_block_raw is True: tmp = item + "\n" @@ -122,6 +132,7 @@ def indent_html(rawcode: str, config: Config) -> str: re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE) ) or re.search(config.ignored_block_opening, item, re.IGNORECASE | re.VERBOSE): is_block_raw = True + is_raw_first_line = False # if a normal tag, we can try to expand attributes elif ( diff --git a/tests/test_html.py b/tests/test_html.py index 35aa5bb..236b833 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -27,7 +27,7 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None: assert ( Path(tmp_file.name).read_text() == """
-
@@ -64,7 +64,7 @@ def test_html_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None: Path(tmp_file.name).read_text() == """
-