[FIX] mail: properly make urls absolute in escaped attributes
`_replace_local_links` ensures urls are given as absolute rather relative paths, for link `href`, img `src` and within styles. This failed when the style contained escaped single quotes (eg: `background-image: url("/my_url/path");`). This commit adapts the failing regex appropriately. X-original-commit: 2af980e8cdc72d35729ef2fbaf5067fe7e72bbb8 Part-of: odoo/odoo#80621
This commit is contained in:
@@ -162,7 +162,14 @@ class MailRenderMixin(models.AbstractModel):
|
||||
_sub_relative2absolute.base_url = base_url
|
||||
html = re.sub(r"""(<img(?=\s)[^>]*\ssrc=")(/[^/][^"]+)""", _sub_relative2absolute, html)
|
||||
html = re.sub(r"""(<a(?=\s)[^>]*\shref=")(/[^/][^"]+)""", _sub_relative2absolute, html)
|
||||
html = re.sub(r"""(<[^>]+\bstyle="[^"]+\burl\('?)(/[^/'][^'")]+)""", _sub_relative2absolute, html)
|
||||
html = re.sub(re.compile(
|
||||
r"""( # Group 1: element up to url in style
|
||||
<[^>]+\bstyle=" # Element with a style attribute
|
||||
[^"]+\burl\( # Style attribute contains "url(" style
|
||||
(?:&\#34;|')?) # url style may start with (escaped) quote: capture it
|
||||
( # Group 2: url itself
|
||||
/(?:[^'")]|(?!&\#34;))+ # stop at the first closing quote
|
||||
)""", re.VERBOSE), _sub_relative2absolute, html)
|
||||
|
||||
return wrapper(html)
|
||||
|
||||
|
||||
@@ -221,7 +221,7 @@ class TestMailRender(common.MailCommon):
|
||||
rendered_local_links = [
|
||||
'<div style="background-image:url(%s/web/path?a=a&b=b);"/>' % base_url,
|
||||
'<div style="background-image:url(\'%s/web/path?a=a&b=b\');"/>' % base_url,
|
||||
'<div style="background-image:url("/web/path?a=a&b=b");"/>'
|
||||
'<div style="background-image:url("%s/web/path?a=a&b=b");"/>' % base_url
|
||||
]
|
||||
for source, expected in zip(local_links_template_bits, rendered_local_links):
|
||||
rendered = self.env['mail.render.mixin']._replace_local_links(source)
|
||||
|
||||
Reference in New Issue
Block a user