From a2964b4bfe33181054ee9074c2d2fda7eb3bb16a Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 2 Aug 2021 16:32:29 -0400 Subject: [PATCH] Improved preserve_handlebar_syntax regex --- premailer/premailer.py | 14 ++++++++++---- premailer/tests/test_premailer.py | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) 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): " }}"> + " }}?subject=x"> """ @@ -3070,6 +3072,7 @@ def test_preserve_handlebar_syntax(self): " }}"> + " }}?subject=x"> """