diff --git a/addons/link_tracker/models/mail_render_mixin.py b/addons/link_tracker/models/mail_render_mixin.py index f7ff2d4548b..dcb0f4ccd18 100644 --- a/addons/link_tracker/models/mail_render_mixin.py +++ b/addons/link_tracker/models/mail_render_mixin.py @@ -38,6 +38,9 @@ class MailRenderMixin(models.AbstractModel): short_schema = base_url + '/r/' for match in set(re.findall(tools.HTML_TAG_URL_REGEX, html)): long_url = match[1] + # Make relative links absolute + if long_url.startswith(('/', '?', '#')): + long_url = base_url + long_url # Don't shorten already-shortened links if long_url.startswith(short_schema): continue @@ -50,7 +53,7 @@ class MailRenderMixin(models.AbstractModel): link = self.env['link.tracker'].search_or_create(create_vals) if link.short_url: # `str` manipulation required to support replacing "&" characters, common in urls - new_href = match[0].replace(long_url, link.short_url) + new_href = match[0].replace(match[1], link.short_url) html = html.replace(markupsafe.Markup(match[0]), markupsafe.Markup(new_href)) return html diff --git a/addons/link_tracker/tests/test_mail_render_mixin.py b/addons/link_tracker/tests/test_mail_render_mixin.py index b008fe75eca..c60198daf23 100644 --- a/addons/link_tracker/tests/test_mail_render_mixin.py +++ b/addons/link_tracker/tests/test_mail_render_mixin.py @@ -30,6 +30,7 @@ class TestMailRenderMixin(common.HttpCase): """, ' test_escaped < > ', 'label', + '', ] self.env["mail.render.mixin"]._shorten_links("".join(test_links), {}) @@ -49,6 +50,7 @@ class TestMailRenderMixin(common.HttpCase): ("url", "=", "https://url_with_params.com?a=b&c=d"), ("label", "=", "label"), ], + [("url", "=", self.base_url + '#')], ] trackers_to_fail = [ [("url", "=", "https://test_542152qsdqsd.com"), ("label", "ilike", "_")] @@ -86,6 +88,7 @@ class TestMailRenderMixin(common.HttpCase): 'And a third: Here\n' 'And a forth: Here\n' 'And a fifth: Here too\n' + 'And a 6th: Here
\n' 'And a last, more complex: There!' .format(base_url=self.base_url) ) @@ -95,6 +98,7 @@ class TestMailRenderMixin(common.HttpCase): 'And a third: Here\n' 'And a forth: Here\n' 'And a fifth: Here too\n' + 'And a 6th: Here
\n' 'And a last, more complex: There!' .format(base_url=self.base_url) )