Skip to content

Conversation

IwanBurg
Copy link

@IwanBurg IwanBurg commented Apr 8, 2022

Problem:
preserve_handlebar_syntax=True doesn't preserve handlebars with characters before or after the handlebars.

Examples of html that don't preserve handlebars:
a href="mailto:{{ Test }}"
a href="{{ Test }}?subject=x"

Current regex code:

stripped = re.sub(
                    r'="{{(.*?)}}"',
                    lambda match: '="{{' + escape(match.groups()[0]) + '}}"',
                    stripped,
                )
out = re.sub(
                    r'="%7B%7B(.+?)%7D%7D"',
                    lambda match: '="{{' + unescape(unquote(match.groups()[0])) + '}}"',
                    out,
                )

Proposed regex code:

stripped = re.sub(
                    r'="([^"]*){{(.*?)}}([^"]*?)"',
                    lambda match: '="' +
                                  match.groups()[0] +
                                  '{{' + escape(match.groups()[1]) + '}}' +
                                  match.groups()[2] + '"',
                    stripped,
                )

https://regex101.com/r/tLC41B/2

out = re.sub(
                    r'="([^"]*)%7B%7B(.+?)%7D%7D([^"]*?)"',
                    lambda match: '="' +
                                  match.groups()[0] +
                                  '{{' + unescape(unquote(match.groups()[1])) + '}}' +
                                  match.groups()[2] + '"',
                    out,
                )

https://regex101.com/r/ADvQjO/1

Issue also here:
#271

I also added a testcase to test_premailer.py for:
a href="mailto:{{ data | default: "Test & code" }}?subject=x"

I'm new to this, so I hope this is how it works.

@IwanBurg IwanBurg force-pushed the improved-preserve-handlebar-regex branch from c98f319 to a2964b4 Compare April 11, 2022 11:17
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|}})([^"]*?)"',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants