From f7e5fd7aabeb0e2ff89af47073dd7426dbd760eb Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Tue, 7 Mar 2023 21:18:16 +0000 Subject: [PATCH] [FIX] base: use html method from lxml Prior to this change the convert tool did could mangle the value of html fields when importing static data. This is because html does not support 'self-closing' except for HTML5 where it is allowed on void elements (such as img). Browsers will assume that they are opening tags, left unclosed. becomes They will then try to repair them with variying degrees of success. Example where it fails: `` should become `` but it becomes `` i.e. instead of closing the 'self-closing' tag immediately, it puts everything inside a single t node More concretely: ``` ``` becomes ``` ``` which is invalid ------------------------- The fix is simply to tell lxml that we want to print the xml nodes as HTML nodes. This will make sure the output is compliant with the standard and keep the semantic clear for the browser. The issue does not appear before 16.2, as jquery used to fix it for us until an update here 9c41ee5091ac06ac3ca71aeac607195c70061e4a task-3162320 X-original-commit: 8ff2e1018264972107f19755ecda352d78dfa829 Part-of: odoo/odoo#118710 --- .../addons/test_convert/tests/test_convert.py | 22 +++++++++++++++++-- odoo/tools/convert.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/odoo/addons/test_convert/tests/test_convert.py b/odoo/addons/test_convert/tests/test_convert.py index 5a2a34af2a9..549b19acd69 100644 --- a/odoo/addons/test_convert/tests/test_convert.py +++ b/odoo/addons/test_convert/tests/test_convert.py @@ -265,6 +265,24 @@ class TestEvalXML(common.TransactionCase): def test_xml(self): pass - @unittest.skip("not tested") def test_html(self): - pass + self.assertEqual( + self.eval_xml(Field(ET.fromstring( + """ + + + + + + + """), type="html")), + """ + + + + + + + """, + "Evaluating an HTML field should give empty nodes instead of self-closing tags" + ) diff --git a/odoo/tools/convert.py b/odoo/tools/convert.py index 36b70b54b4a..0065cb3d612 100644 --- a/odoo/tools/convert.py +++ b/odoo/tools/convert.py @@ -142,7 +142,7 @@ def _eval_xml(self, node, env): return '\n'\ +_process("".join(etree.tostring(n, encoding='unicode') for n in node)) if t == 'html': - return _process("".join(etree.tostring(n, encoding='unicode') for n in node)) + return _process("".join(etree.tostring(n, method='html', encoding='unicode') for n in node)) data = node.text if node.get('file'):