forked from neomjs/neo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomApiVnodeCreator.mjs
More file actions
51 lines (44 loc) · 1.97 KB
/
Copy pathDomApiVnodeCreator.mjs
File metadata and controls
51 lines (44 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const DomApiVnodeCreator = {
/**
* Recursively creates a VNode tree suitable for direct DOM API insertion.
* This tree excludes any nodes that are marked as 'moved' within the movedNodes map,
* as their DOM manipulation will be handled by separate moveNode deltas.
*
* @param {Neo.vdom.VNode} vnode The VNode to process.
* @param {Map} [movedNodes] A map of VNodes that are being moved.
* @returns {Object|null} A new VNode tree (or subtree) with moved nodes pruned, or null if the root is a moved node.
*/
create(vnode, movedNodes) {
/*
* A vnode itself can be null (removeDom: true) => opt out.
*
* If the node has a componentId, there is nothing to do (scoped vdom updates), opt out.
*
* If this specific vnode is in the movedNodes map, it means its DOM element
* will be moved by a separate delta. So, we should not include it in this fragment.
*/
if (!vnode || vnode.componentId || (vnode.id && movedNodes?.get(vnode.id))) {
return null // Prune this branch
}
// For text nodes, we can return the original VNode directly, as they have no childNodes array to modify.
if (vnode.vtype === 'text') {
return vnode
}
// For other VNodes (vnode or root), create a shallow clone first.
let clonedVnode = {...vnode, childNodes: []};
// Recursively process children
if (vnode.childNodes.length > 0) {
vnode.childNodes.forEach(child => {
const processedChild = DomApiVnodeCreator.create(child, movedNodes);
// Only add if not pruned
if (processedChild) {
clonedVnode.childNodes.push(processedChild)
}
});
}
return clonedVnode
}
};
const ns = Neo.ns('Neo.vdom.util', true);
ns.DomApiVnodeCreator = DomApiVnodeCreator;
export default DomApiVnodeCreator;