[FIX] tools: better support 'almost empty' content from editor

It seems quite easy to have a void content in the currently new editor
that actually holds a lot of undesired information, notably a font
tag. This is annoying when having behavior based on an html field
being empty or not. In this commit we therefore improve definition
of an 'empty' html field.

Task ID-2532529
PR odoo/odoo#71793
This commit is contained in:
Thibault Delavallée
2021-07-06 13:30:47 +00:00
parent 6bc1244c4f
commit 28d6468bc4
2 changed files with 13 additions and 5 deletions
+12 -4
View File
@@ -328,13 +328,21 @@ class TestHtmlTools(BaseCase):
for content in void_strings_samples:
self.assertTrue(is_html_empty(content))
void_html_samples = ['<p><br></p>', '<p><br> </p>', '<p><br /></p >', '<p style="margin: 4px"></p>',
'<div style="margin: 4px"></div>', '<p class="oe_testing"><br></p>']
void_html_samples = [
'<p><br></p>', '<p><br> </p>', '<p><br /></p >',
'<p style="margin: 4px"></p>',
'<div style="margin: 4px"></div>',
'<p class="oe_testing"><br></p>',
'<p><span style="font-weight: bolder;"><font style="color: rgb(255, 0, 0);" class=" "></font></span><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>', '<p style="margin: 4px">Hello World</p>',
'<div style="margin: 4px"><p>Hello World</p></div>']
valid_html_samples = [
'<p><br>1</p>', '<p>1<br > </p>', '<p style="margin: 4px">Hello World</p>',
'<div style="margin: 4px"><p>Hello World</p></div>',
'<p><span style="font-weight: bolder;"><font style="color: rgb(255, 0, 0);" class=" ">W</font></span><br></p>',
]
for content in valid_html_samples:
self.assertFalse(is_html_empty(content))
+1 -1
View File
@@ -280,7 +280,7 @@ def is_html_empty(html_content):
"""
if not html_content:
return True
tag_re = re.compile(r'\<\s*\/?(?:p|div|span|br|b|i)(?:(?=\s+\w*)[^/>]*|\s*)/?\s*\>')
tag_re = re.compile(r'\<\s*\/?(?:p|div|span|br|b|i|font)(?:(?=\s+\w*)[^/>]*|\s*)/?\s*\>')
return not bool(re.sub(tag_re, '', html_content).strip())