[FIX] core, mass_mailing: preserve markup status in prepend_html_content

In `prepend_html_content`, if body `html_body` and `html_content` are
`Markup` objects, the processing of `html_content` through `re.sub`
will strip away the `Markup` flag, leading to the later concatenation
of the body and content first escaping the content (to safely convert
it to a `Markup`) before performing the concatenation proper.

As a result, the `preview` of a `mailing.mailing` would be
double-escaped, and the markup hiding it would instead be printed as
part of the body.

Assume that the type of `html_content` is str-compatible, and
immediately rewrap the value after processing it through
`re.sub`. This should do nothing if `html_content` was an actual
`str`, and will re-flag it as a `Markup` if it was one.

Also change the finding of the insertion point to use `re.search`, I
don't understand why this uses `finditer`, it just makes the code more
complicated.

closes odoo/odoo#71090

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Xavier Morel
2021-05-20 10:37:09 +00:00
parent 45b687ea4b
commit a1f45aab0e
2 changed files with 14 additions and 6 deletions
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import lxml.html
from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon
from odoo.tests.common import users
@@ -28,6 +29,16 @@ class TestMailingTest(TestMassMailCommon):
with self.mock_mail_gateway():
mailing_test.send_mail_test()
# not great but matches send_mail_test, maybe that should be a method
# on mailing_test?
record = self.env[mailing.mailing_model_real].search([], limit=1)
first_child = lxml.html.fromstring(self._mails.pop()['body']).xpath('//body/*[1]')[0]
self.assertEqual(first_child.tag, 'div')
self.assertIn('display:none', first_child.get('style'),
"the preview node should be hidden")
self.assertEqual(first_child.text.strip(), "Preview " + record.name,
"the preview node should contain the preview text")
# Test if bad jinja in the subject raises an error
mailing.write({'subject': 'Subject ${object.name_id.id}'})
with self.mock_mail_gateway(), self.assertRaises(Exception):
+3 -6
View File
@@ -433,14 +433,11 @@ def append_content_to_html(html, content, plaintext=True, preserve=False, contai
def prepend_html_content(html_body, html_content):
"""Prepend some HTML content at the beginning of an other HTML content."""
html_content = re.sub(r'(?i)(</?(?:html|body|head|!\s*DOCTYPE)[^>]*>)', '', html_content)
html_content = type(html_content)(re.sub(r'(?i)(</?(?:html|body|head|!\s*DOCTYPE)[^>]*>)', '', html_content))
html_content = html_content.strip()
insert_index = next(re.finditer(r'<body[^>]*>', html_body), None)
if insert_index is None:
insert_index = next(re.finditer(r'<html[^>]*>', html_body), None)
insert_index = insert_index.end() if insert_index else 0
body_match = re.search(r'<body[^>]*>', html_body) or re.search(r'<html[^>]*>', html_body)
insert_index = body_match.end() if body_match else 0
return html_body[:insert_index] + html_content + html_body[insert_index:]