Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1015: Specific mixture of quotes and escaped quotes (e.g. in a json string in an html attribute) breaks the html #1016

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,7 @@ def fix_handlebars_template_tags(
func = partial(format_attributes, config, item)

tmp = re.sub(
rf"(\s*?)(<(?:{config.indent_html_tags}))\s((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}\/])+?)(\s?/?>)",
func,
tmp,
flags=RE_FLAGS_IX,
config.indent_html_tags_regex, func, tmp, flags=RE_FLAGS_IX
)

# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
Expand Down
33 changes: 31 additions & 2 deletions djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,14 +727,43 @@ def __init__(
"""
)

self.html_tag_regex = r"""
self.template_tags = r"""
{{(?:(?!}}).)*}}|{%(?:(?!%}).)*%}
"""

self.html_tag_attribute_regex = rf"""
((?:\s*?(?:
\"(?:{self.template_tags}|\\\"|[^\"])*\"
|'(?:{self.template_tags}|\\'|[^'])*'
|{self.template_tags}
|[^'\">{{}}/\s]|/(?!>)
))+)?
"""

self.html_tag_regex = rf"""
(</?(?:!(?!--))?) # an opening bracket (< or </ or <!), but not a comment
([^\s>!\[]+\b) # a tag name
((?:\s*?(?:\"[^\"]*\"|'[^']*'|{{(?:(?!}}).)*}}|{%(?:(?!%}).)*%}|[^'\">{}/\s]|/(?!>)))+)? # any attributes
{self.html_tag_attribute_regex} # any attributes
\s*? # potentially some whitespace
(/?>) # a closing bracket (/> or >)
"""

self.indent_html_tags_attribute_regex = rf"""
\s((?:
(?<!\\)\"(?:{self.template_tags}|\\\"|[^\"])*\"
|(?<!\\)'(?:{self.template_tags}|\\'|[^'])*'
|{self.template_tags}
|[^'\">{{}}\/]
)+?)
"""

self.indent_html_tags_regex: str = rf"""
(\s*?)
(<(?:{self.indent_html_tags}))
{self.indent_html_tags_attribute_regex}
(\s*?/?>)
"""

self.attribute_style_pattern: str = (
r"^(.*?)(style=)([\"|'])(([^\"']+?;)+?)\3"
)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_config/test_max_attribute_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@
({"max_attribute_length": 3, "custom_html": "[\\w\\-]+"}),
id="long tag custom_html",
),
pytest.param(
(
'<option data-json=\'{ "icon": "<img class=\\"ss\\">" }\' {% if True %}selected{% endif %}>'
),
(
'<option data-json=\'{ "icon": "<img class=\\"ss\\">" }\'\n'
" {% if True %}selected{% endif %}\n"
" >\n"
),
({"max_attribute_length": 1}),
id="with_html_tag_in_attribute_escaped_and_template_tag",
),
]


Expand Down