diff --git a/premailer/premailer.py b/premailer/premailer.py
index 66907ca..b144519 100644
--- a/premailer/premailer.py
+++ b/premailer/premailer.py
@@ -345,8 +345,11 @@ def transform(self, html=None, pretty_print=True, **kwargs):
# " }}">
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,
)
@@ -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([^"]*?)"',
+ lambda match: '="' +
+ match.groups()[0] +
+ '{{' + unescape(unquote(match.groups()[1])) + '}}' +
+ match.groups()[2] + '"',
out,
)
return out
diff --git a/premailer/tests/test_premailer.py b/premailer/tests/test_premailer.py
index c1e803e..877a665 100644
--- a/premailer/tests/test_premailer.py
+++ b/premailer/tests/test_premailer.py
@@ -3049,6 +3049,7 @@ def test_preserve_handlebar_syntax(self):
" }}">
+ " }}?subject=x">
"""
@@ -3059,6 +3060,7 @@ def test_preserve_handlebar_syntax(self):