mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-16 21:40:24 +00:00
fix(attributes): fixed poor django attribute parsing
Allow multiple attribute styles inside a quoted value. Split the attribute name into a group to help resolve #427 closes #438
This commit is contained in:
parent
b84ddeb1d0
commit
bd90f08065
3 changed files with 48 additions and 17 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -510,29 +510,54 @@ class Config:
|
|||
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 = (
|
||||
|
|
|
|||
Loading…
Reference in a new issue