From ac4adf05210fba2292db5e6ba20218fb36356b90 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Mon, 13 Nov 2023 15:07:46 +0100 Subject: [PATCH] [FIX] web_editor: allow a node to not have oid in serialize This commit addresses a traceback that occurs when trying to serialize a node that has no `oid` property because it was added to the DOM while the mutation observer was not active (eg, using `observerUnactive`). This is the case with technical nodes added for managing the selection around links ("link zws spans"). When serializing a node, we expect it and its descendants to have an `oid` property so we throw an error if such is not the case. But we now have a legitimate case where nodes have no `oid` property so this removes the error. If the `oid` was in fact really supposed to be defined (eg, the node we tried to serialize was the direct target of a mutation), an error will be thrown soon enough after when trying to access said property. Note that other, more ad-hoc fixes specific to the link zws spans would have been possible (and might still be useful in the future), such as: - skipping said link zws spans when serializing - adding a fake `oid` property to the link zws spans task-3593081 X-original-commit: ba6a7fa4cf5ad422816e49b4d3b941297841ed3f Part-of: odoo/odoo#142049 Co-authored-by: Nicolas Bayet Co-authored-by: Antoine Guenet --- .../src/js/editor/odoo-editor/src/utils/serialize.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/serialize.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/serialize.js index cf3efff53df..40a073237e8 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/serialize.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/serialize.js @@ -6,7 +6,7 @@ export function serializeNode(node, nodesToStripFromChildren = new Set()) { oid: node.oid, }; if (!node.oid) { - throw new Error('node.oid can not be falsy.'); + return; } if (node.nodeType === Node.TEXT_NODE) { result.textValue = node.nodeValue; @@ -22,7 +22,10 @@ export function serializeNode(node, nodesToStripFromChildren = new Set()) { if (!["true", ""].includes(node.dataset.oeTransientContent)) { while (child) { if (!nodesToStripFromChildren.has(child.oid)) { - result.children.push(serializeNode(child, nodesToStripFromChildren)); + const serializedChild = serializeNode(child, nodesToStripFromChildren); + if (serializedChild) { + result.children.push(serializedChild); + } } child = child.nextSibling; }