Skip to content

Commit

Permalink
fix(attributes): fixed poor django attribute parsing
Browse files Browse the repository at this point in the history
Allow multiple attribute styles inside a quoted value. Split the attribute name into a group to help
resolve #427

closes #438
  • Loading branch information
christopherpickering committed Oct 28, 2022
1 parent b84ddeb commit bd90f08
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/djlint/formatter/attributes.py
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/djlint/formatter/indent.py
Expand Up @@ -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(
Expand Down
57 changes: 41 additions & 16 deletions src/djlint/settings.py
Expand Up @@ -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 = (
Expand Down

0 comments on commit bd90f08

Please sign in to comment.