diff --git a/src/djlint/formatter/indent.py b/src/djlint/formatter/indent.py index fa0ccf47..80db432a 100644 --- a/src/djlint/formatter/indent.py +++ b/src/djlint/formatter/indent.py @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py index b6962a38..01e7ed96 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -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. @@ -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[ + -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. diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 51888890..118f7fb4 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -470,6 +470,10 @@ def __init__( ) # contents of tags will not be formatted + self.script_style_opening: str = r""" +