From 2093aefbe8125de83ed93392fcb9154703dde829 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Fri, 21 Jan 2022 10:41:14 +0000 Subject: [PATCH] [FIX] web_editor: remove check ids in tests The random ids of checklists and stars are preserved in the dom, which is needed for their readonly check features to work. However, it complicates testing. This systematically removes these ids in tests. It also removes the ids when they are not needed anymore (the node is not a checklist anymore for instance). closes odoo/odoo#83137 Signed-off-by: David Monjoie (dmo) --- addons/web_editor/controllers/main.py | 4 +- .../lib/odoo-editor/src/utils/sanitize.js | 15 +- .../lib/odoo-editor/test/spec/list.test.js | 238 +++++++++++++++++- .../static/lib/odoo-editor/test/utils.js | 8 + .../static/src/js/backend/field_html.js | 14 +- 5 files changed, 264 insertions(+), 15 deletions(-) diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index f6b5fdd57f5..e5cc1c481fe 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -118,7 +118,7 @@ class Web_Editor(http.Controller): htmlelem = etree.fromstring("
%s
" % value, etree.HTMLParser()) checked = bool(checked) - li = htmlelem.find(".//li[@id='%s']" % checklistId) + li = htmlelem.find(".//li[@id='checkId-%s']" % checklistId) if li is None: return value @@ -147,7 +147,7 @@ class Web_Editor(http.Controller): value = getattr(record, filename, False) htmlelem = etree.fromstring("
%s
" % value, etree.HTMLParser()) - stars_widget = htmlelem.find(".//span[@id='%s']" % starsId) + stars_widget = htmlelem.find(".//span[@id='checkId-%s']" % starsId) if stars_widget is None: return value diff --git a/addons/web_editor/static/lib/odoo-editor/src/utils/sanitize.js b/addons/web_editor/static/lib/odoo-editor/src/utils/sanitize.js index 2064cad73f0..f4b5db5e9b1 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/utils/sanitize.js +++ b/addons/web_editor/static/lib/odoo-editor/src/utils/sanitize.js @@ -77,7 +77,16 @@ export function areSimilarElements(node, node2) { class Sanitize { constructor(root) { this.root = root; + // Remove unique ids from checklists and stars. These will be renewed + // afterwards. + for (const node of closestBlock(root).querySelectorAll('[id^=checkId-]')) { + node.removeAttribute('id'); + } this.parse(root); + // Ensure unique ids on checklists and stars. + for (const node of closestBlock(root).querySelectorAll('.o_checklist > li, .o_stars')) { + node.setAttribute('id', `checkId-${Math.floor(new Date() * Math.random())}`); + } } parse(node) { @@ -133,12 +142,6 @@ class Sanitize { } } - // Ensure unique ids on checklists and stars. - if (node.nodeName == 'LI' && node.parentElement && node.parentElement.classList.contains('o_checklist') || - node.classList && node.classList.contains('o_stars')) { - node.setAttribute('id', Math.floor(new Date() * Math.random())); - } - // FIXME not parse out of editable zone... this._parse(node.firstChild); this._parse(node.nextSibling); diff --git a/addons/web_editor/static/lib/odoo-editor/test/spec/list.test.js b/addons/web_editor/static/lib/odoo-editor/test/spec/list.test.js index 8c9e1fb024c..1f20676b972 100644 --- a/addons/web_editor/static/lib/odoo-editor/test/spec/list.test.js +++ b/addons/web_editor/static/lib/odoo-editor/test/spec/list.test.js @@ -237,6 +237,7 @@ describe('List', () => { describe('Insert', () => { it('should turn an empty paragraph into a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

[]

', stepFunction: toggleCheckList, // JW cAfter: '', @@ -245,6 +246,7 @@ describe('List', () => { }); it('should turn a paragraph into a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

ab[]cd

', stepFunction: toggleCheckList, // JW cAfter: '', @@ -253,6 +255,7 @@ describe('List', () => { }); it('should turn a heading into a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

ab[]cd

', stepFunction: toggleCheckList, // JW cAfter: '', @@ -261,6 +264,7 @@ describe('List', () => { }); it('should turn a paragraph in a div into a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

ab[]cd

', stepFunction: toggleCheckList, // JW cAfter: '
  • ab[]cd
', @@ -269,6 +273,7 @@ describe('List', () => { }); it('should turn a paragraph with formats into a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

ab cd ef[]gh

', stepFunction: toggleCheckList, @@ -279,6 +284,7 @@ describe('List', () => { }); it('should turn a paragraph between 2 checklist into a checklist item', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: '

d[]ef

', stepFunction: toggleCheckList, @@ -286,8 +292,9 @@ describe('List', () => { '', }); }); - it('should turn a unordered list into a checklist betweet 2 checklist inside a checklist', async () => { + it('should turn a unordered list into a checklist between 2 checklists inside a checklist', async () => { await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: unformat(` `), }); await testEditor(BasicEditor, { + removeCheckIds: true, contentBefore: unformat(`