[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 <nby@odoo.com>
Co-authored-by: Antoine Guenet <age@odoo.com>
This commit is contained in:
Nicolas Bayet
2023-11-15 11:26:06 +00:00
co-authored by Antoine Guenet
parent 45825263a4
commit ac4adf0521
@@ -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;
}