diff options
| author | Rémy Voet <ryv@odoo.com> | 2020-03-09 14:03:56 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2020-03-12 13:54:32 +0000 |
| commit | 974f512f5f5d3b9f80a8c3fcde290e4f55cf1230 (patch) | |
| tree | 072718b0e00f0759ac0cc98dd2a5fe5ef0a3665a /odoo | |
| parent | b1037eb2d1688607bfa928125361430948285715 (diff) | |
[IMP] tools: add a tool method to find if an html content is empty
In odoo empty html is not always completely empty. This tool method allow
to consider html holding void tags as empty html. This happens notably when
using the editor that may store <p><br><p> even if displayed content is void.
Task ID 2126509
PR #41827
Diffstat (limited to 'odoo')
| -rw-r--r-- | odoo/addons/base/tests/test_mail.py | 24 | ||||
| -rw-r--r-- | odoo/tools/mail.py | 14 |
2 files changed, 33 insertions, 5 deletions
diff --git a/odoo/addons/base/tests/test_mail.py b/odoo/addons/base/tests/test_mail.py index e1b80c52c28..c2e9899fca1 100644 --- a/odoo/addons/base/tests/test_mail.py +++ b/odoo/addons/base/tests/test_mail.py @@ -9,7 +9,8 @@ import threading from odoo.tests.common import BaseCase, SavepointCase, TransactionCase from odoo.tools import ( - html_sanitize, append_content_to_html, plaintext2html, email_split, + is_html_empty, html_sanitize, append_content_to_html, plaintext2html, + email_split, misc, formataddr, ) @@ -103,10 +104,10 @@ class TestSanitizer(BaseCase): not_emails = [ '<blockquote cite="mid:CAEJSRZvWvud8c6Qp=wfNG6O1+wK3i_jb33qVrF7XyrgPNjnyUA@mail.gmail.com" type="cite">cat</blockquote>', '<img alt="@github-login" class="avatar" src="/web/image/pi" height="36" width="36">'] - for email in not_emails: - sanitized = html_sanitize(email) - left_part = email.split('>')[0] # take only left part, as the sanitizer could add data information on node - self.assertNotIn(misc.html_escape(email), sanitized, 'html_sanitize stripped emails of original html') + for not_email in not_emails: + sanitized = html_sanitize(not_email) + left_part = not_email.split('>')[0] # take only left part, as the sanitizer could add data information on node + self.assertNotIn(misc.html_escape(not_email), sanitized, 'html_sanitize stripped emails of original html') self.assertIn(left_part, sanitized) def test_style_parsing(self): @@ -318,6 +319,19 @@ class TestHtmlTools(BaseCase): for html, content, plaintext_flag, preserve_flag, container_tag, expected in test_samples: self.assertEqual(append_content_to_html(html, content, plaintext_flag, preserve_flag, container_tag), expected, 'append_content_to_html is broken') + def test_is_html_empty(self): + void_strings_samples = ['', False, ' '] + for content in void_strings_samples: + self.assertTrue(is_html_empty(content)) + + void_html_samples = ['<p><br></p>', '<p><br> </p>'] + for content in void_html_samples: + self.assertTrue(is_html_empty(content), 'Failed with %s' % content) + + valid_html_samples = ['<p><br>1</p>', '<p>1<br > </p>'] + for content in valid_html_samples: + self.assertFalse(is_html_empty(content)) + class TestEmailTools(BaseCase): """ Test some of our generic utility functions for emails """ diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py index 50467df606e..b6640da1240 100644 --- a/odoo/tools/mail.py +++ b/odoo/tools/mail.py @@ -254,6 +254,20 @@ def html_sanitize(src, silent=True, sanitize_tags=True, sanitize_attributes=Fals # HTML/Text management #---------------------------------------------------------- +def is_html_empty(html_content): + """Check if a html content is empty. If there are only formatting tags or + a void content return True. Famous use case if a '<p><br></p>' added by + some web editor. + + :param str html_content: html content, coming from example from an HTML field + :returns: bool, True if no content found or if containing only void formatting tags + """ + if not html_content: + return True + tag_re = re.compile(r'\<\s*\/?(?:p|div|span|br|b|i)\s*\>') + return not bool(re.sub(tag_re, '', html_content).strip()) + + def html_keep_url(text): """ Transform the url into clickable link with <a/> tag """ idx = 0 |
