Skip to content

Commit

Permalink
fix(formatter): fixed extra indend in script tags
Browse files Browse the repository at this point in the history
thanks @eilmiv

closes #733
  • Loading branch information
christopherpickering committed Sep 18, 2023
1 parent 6091ffb commit 9d04d51
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
is_ignored_block_closing,
is_ignored_block_opening,
is_safe_closing_tag,
is_script_style_block_closing,
is_script_style_block_opening,
)
from ..settings import Config
from .attributes import format_attributes
Expand Down Expand Up @@ -61,6 +63,7 @@ def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
indent_level = 0
in_set_tag = False
is_raw_first_line = False
in_script_style_tag = False
is_block_raw = False

slt_html = config.indent_html_tags
Expand All @@ -84,6 +87,9 @@ def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
is_block_raw = True
ignored_level += 1

if is_script_style_block_opening(config, item):
in_script_style_tag = True

if is_safe_closing_tag(config, item):
ignored_level -= 1
ignored_level = max(ignored_level, 0)
Expand Down Expand Up @@ -313,7 +319,11 @@ def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
)

# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
if is_ignored_block_closing(config, item):
if is_ignored_block_closing(config, item) and (
in_script_style_tag is False
or (in_script_style_tag and is_script_style_block_closing(config, item))
):
in_script_style_tag = False
if not is_safe_closing_tag(config, item):
ignored_level -= 1
ignored_level = max(ignored_level, 0)
Expand Down
53 changes: 53 additions & 0 deletions src/djlint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ def is_ignored_block_opening(config: Config, item: str) -> bool:
)


def is_script_style_block_opening(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(
config.script_style_inline,
item,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
)
)

if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.

return bool(
re.search(
re.compile(config.script_style_opening, re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
)


def inside_protected_trans_block(config: Config, html: str, match: re.Match) -> bool:
"""Find ignored group closing.
Expand Down Expand Up @@ -146,6 +174,31 @@ def is_ignored_block_closing(config: Config, item: str) -> bool:
)


def is_script_style_block_closing(config: Config, item: str) -> bool:
"""Find ignored group closing.
A valid ignored group closing tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
re.compile(config.script_style_inline, flags=re.IGNORECASE | re.VERBOSE),
item,
)
)

if inline:
last_index = inline[

Check warning on line 192 in src/djlint/helpers.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/helpers.py#L192

Added line #L192 was not covered by tests
-1
].end() # get the last index. The ignored opening should start after this.

return re.search(
re.compile(config.script_style_closing, flags=re.IGNORECASE | re.VERBOSE),
item[last_index:],
)


def is_safe_closing_tag(config: Config, item: str) -> bool:
"""Find ignored group opening.
Expand Down
12 changes: 11 additions & 1 deletion src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ def __init__(
)

# contents of tags will not be formatted
self.script_style_opening: str = r"""
<style
| <script
"""
self.ignored_block_opening: str = r"""
<style
| {\*
Expand All @@ -486,7 +490,10 @@ def __init__(
| {{!--\s*djlint\:off\s*--}}
| {{-?\s*/\*\s*djlint\:off\s*\*/\s*-?}}
"""

self.script_style_closing: str = r"""
</style
| </script
"""
self.ignored_block_closing: str = r"""
</style
| \*}
Expand Down Expand Up @@ -804,6 +811,9 @@ def __init__(
| {%[ ]*?comment\b(?:(?!%}).)*?%}(?:(?!djlint:(?:off|on)).)*?(?={%[ ]*?endcomment[ ]*?%})
| ^---[\s\S]+?---
"""
self.script_style_inline: str = r"""
<(script|style).*?(?=(\</(?:\1)>))
"""
self.ignored_blocks_inline: str = r"""
<(pre|textarea).*?</(\1)>
| <(script|style).*?(?=(\</(?:\3)>))
Expand Down
21 changes: 21 additions & 0 deletions tests/test_html/test_tag_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
from tests.conftest import printer

test_data = [
pytest.param(
(
"{% block script %}\n"
" <script>\n"
' let arrow = "-->";\n'
" let on_the_move = 1;\n"
" let just_following_the_sign = 2;\n"
" </script>\n"
"{% endblock %}\n"
),
(
"{% block script %}\n"
" <script>\n"
' let arrow = "-->";\n'
" let on_the_move = 1;\n"
" let just_following_the_sign = 2;\n"
" </script>\n"
"{% endblock %}\n"
),
id="github issue 733",
),
pytest.param(
(
"<div>\n"
Expand Down

0 comments on commit 9d04d51

Please sign in to comment.