Skip to content
Open
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
14 changes: 10 additions & 4 deletions premailer/premailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,11 @@ def transform(self, html=None, pretty_print=True, **kwargs):
# <a href="{{ "<Test>" }}"></a>
if self.preserve_handlebar_syntax:
stripped = re.sub(
r'="{{(.*?)}}"',
lambda match: '="{{' + escape(match.groups()[0]) + '}}"',
r'="([^"]*){{(.*?)}}([^"]*?)"',
lambda match: '="' +
match.groups()[0] +
'{{' + escape(match.groups()[1]) + '}}' +
match.groups()[2] + '"',
stripped,
)

Expand Down Expand Up @@ -563,8 +566,11 @@ def transform(self, html=None, pretty_print=True, **kwargs):
# attributes, with their single-character equivalents.
if self.preserve_handlebar_syntax:
out = re.sub(
r'="%7B%7B(.+?)%7D%7D"',
lambda match: '="{{' + unescape(unquote(match.groups()[0])) + '}}"',
r'="([^"]*)%7B%7B(.+?)%7D%7D([^"]*?)"',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To also support lxml version 5 (#297) , i propose the following change.

Suggested change
r'="([^"]*)%7B%7B(.+?)%7D%7D([^"]*?)"',
r'="([^"]*)(?:%7B%7B|{{)(.+?)(?:%7D%7D|}})([^"]*?)"',

lambda match: '="' +
match.groups()[0] +
'{{' + unescape(unquote(match.groups()[1])) + '}}' +
match.groups()[2] + '"',
out,
)
return out
Expand Down
3 changes: 3 additions & 0 deletions premailer/tests/test_premailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,7 @@ def test_preserve_handlebar_syntax(self):
<html>
<img src="{{ data | default: 'Test & <code>' }}">
<a href="{{ data | default: "Test & <code>" }}"></a>
<a href="mailto:{{ data | default: "Test & <code>" }}?subject=x"></a>
</html>
"""

Expand All @@ -3059,6 +3060,7 @@ def test_preserve_handlebar_syntax(self):
<body>
<img src="{{ data | default: 'Test & <code>' }}">
<a href="{{ data | default: "Test & <code>" }}"></a>
<a href="mailto:{{ data | default: "Test & <code>" }}?subject=x"></a>
</body>
</html>
"""
Expand All @@ -3070,6 +3072,7 @@ def test_preserve_handlebar_syntax(self):
<body>
<img src="%7B%7B%20data%20%7C%20default:%20'Test%20&amp;%20&lt;code&gt;'%20%7D%7D">
<a href="%7B%7B%20data%20%7C%20default:%20" test>" }}"&gt;</a>
<a href="mailto:%7B%7B%20data%20%7C%20default:%20" test>" }}?subject=x"&gt;</a>
</body>
</html>
"""
Expand Down