diff --git a/src/djlint/formatter/attributes.py b/src/djlint/formatter/attributes.py index 596ed281..41526703 100644 --- a/src/djlint/formatter/attributes.py +++ b/src/djlint/formatter/attributes.py @@ -258,7 +258,12 @@ def format_attributes(config: Config, html: str, match: re.match) -> str: # format attributes as groups attributes = (spacing).join( - re.findall(config.attribute_pattern, match.group(3).strip(), re.VERBOSE) + [ + x.group() + for x in re.finditer( + config.attribute_pattern, match.group(3).strip(), re.VERBOSE + ) + ] ) close = match.group(4) diff --git a/src/djlint/formatter/indent.py b/src/djlint/formatter/indent.py index 80e16c2e..db2ae88f 100644 --- a/src/djlint/formatter/indent.py +++ b/src/djlint/formatter/indent.py @@ -197,6 +197,7 @@ def indent_html(rawcode: str, config: Config) -> str: # if a normal tag, we can try to expand attributes elif is_block_raw is False: # get leading space, and attributes + func = partial(format_attributes, config, item) tmp = re.sub( diff --git a/src/djlint/settings.py b/src/djlint/settings.py index cf43b633..17660be8 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -510,29 +510,54 @@ def __init__( self.template_if_for_pattern = ( r"(?:{%-?\s?(?:if|for)[^}]*?%}(?:.*?{%\s?end(?:if|for)[^}]*?-?%})+?)" ) + self.attribute_pattern: str = ( - r""" - (?:[^\s]+?[ ]*?=[ ]*?(?:\"[^\"]*?""" - + self.template_if_for_pattern - + r"""[^\"]*?\"|\'[^\']*?""" - + self.template_if_for_pattern - + r"""[^\']*?\')) - | (?:[^\s]+?[ ]*?=[ ]*?(?:\"[^\"]*?{{.*?}}[^\"]*?\"|\'[^\']*?{{.*?}}[^\']*?\')) - | """ - + self.template_if_for_pattern + rf""" + (?: + (?: + (?:\w|-|\.)+ | required | checked + ) # attribute name + (?: [ ]*?=[ ]*? # followed by "=" + (?: + \"[^\"]*? # double quoted attribute + (?: + {self.template_if_for_pattern} # if or for loop + | {{{{.*?}}}} # template stuff + | {{%[^}}]*?%}} + | [^\"] # anything else + )*? + \" # closing quote + | '[^']*? # single quoted attribute + (?: + {self.template_if_for_pattern} # if or for loop + | {{{{.*?}}}} # template stuff + | {{%[^}}]*?%}} + | [^'] # anything else + )*? + \' # closing quote + | (?:\w|-)+ # or a non-quoted value + + ) + )? # attribute value + ) + | {self.template_if_for_pattern} + """ + r""" - | (?:[^\s]+?[ ]*?=[ ]*?(?:\"(?:[^\"]*?{%[^}]*?%}[^\"]*?)+?\")) - | (?:[^\s]+?[ ]*?=[ ]*?(?:\'(?:[^\']*?{%[^}]*?%}[^\']*?)+?\')) - | (?:[^\s]+?[ ]*?=[ ]*?(?:\".*?\"|\'.*?\')) - | required - | checked - | (?:\w|-|\.)+ - | (?:\w|-|\.)+[ ]*?=[ ]*?(?:\w|-)+ | {{.*?}} | {%.*?%} """ ) + # + r"""[^\"]*?\"|\'[^\']*?""" + # + self.template_if_for_pattern + # + r"""[^\']*?\')) + # | (?:[^\s]+?[ ]*?=[ ]*?(?:\"[^\"]*?{{.*?}}[^\"]*?\"|\'[^\']*?{{.*?}}[^\']*?\')) + # | """ + # + self.template_if_for_pattern + # + r""" + # | (?:[^\s]+?[ ]*?=[ ]*?(?:\"(?:[^\"]*?{%[^}]*?%}[^\"]*?)+?\")) + ## | (?:[^\s]+?[ ]*?=[ ]*?(?:\'(?:[^\']*?{%[^}]*?%}[^\']*?)+?\')) + # | (?:[^\s]+?[ ]*?=[ ]*?(?:\".*?\"|\'.*?\')) self.attribute_style_pattern: str = r"^(.*?)(style=)([\"|'])(([^\"']+?;)+?)\3" self.start_template_tags: str = (