Skip to content

Commit e69f38d

Browse files
committed
refactor: migrate importNode to walkDOM
Replace the recursive `importNode` with a `walkDOM`-based implementation. The `enter` callback shallow-clones each source node, stamps it with the target document, clears `parentNode`, then attaches it to its imported parent (or captures it as `destRoot` for the root). ATTRIBUTE_NODE always forces deep traversal since its value lives in a child text node. Also adds JSDoc documenting purpose, parameters, and return value. Removes the now-obsolete commented-out switch cases. GHSA-2v35-w6hq-6mfw
1 parent 375b372 commit e69f38d

1 file changed

Lines changed: 37 additions & 44 deletions

File tree

lib/dom.js

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,50 +1888,43 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces,requireW
18881888
buf.push('??',node.nodeName);
18891889
}
18901890
}
1891-
function importNode(doc,node,deep){
1892-
var node2;
1893-
switch (node.nodeType) {
1894-
case ELEMENT_NODE:
1895-
node2 = node.cloneNode(false);
1896-
node2.ownerDocument = doc;
1897-
//var attrs = node2.attributes;
1898-
//var len = attrs.length;
1899-
//for(var i=0;i<len;i++){
1900-
//node2.setAttributeNodeNS(importNode(doc,attrs.item(i),deep));
1901-
//}
1902-
case DOCUMENT_FRAGMENT_NODE:
1903-
break;
1904-
case ATTRIBUTE_NODE:
1905-
deep = true;
1906-
break;
1907-
//case ENTITY_REFERENCE_NODE:
1908-
//case PROCESSING_INSTRUCTION_NODE:
1909-
////case TEXT_NODE:
1910-
//case CDATA_SECTION_NODE:
1911-
//case COMMENT_NODE:
1912-
// deep = false;
1913-
// break;
1914-
//case DOCUMENT_NODE:
1915-
//case DOCUMENT_TYPE_NODE:
1916-
//cannot be imported.
1917-
//case ENTITY_NODE:
1918-
//case NOTATION_NODE:
1919-
//can not hit in level3
1920-
//default:throw e;
1921-
}
1922-
if(!node2){
1923-
node2 = node.cloneNode(false);//false
1924-
}
1925-
node2.ownerDocument = doc;
1926-
node2.parentNode = null;
1927-
if(deep){
1928-
var child = node.firstChild;
1929-
while(child){
1930-
node2.appendChild(importNode(doc,child,deep));
1931-
child = child.nextSibling;
1932-
}
1933-
}
1934-
return node2;
1891+
/**
1892+
* Imports a node from a different document into `doc`, creating a new copy.
1893+
* Delegates to {@link walkDOM} for traversal. Each node in the subtree is shallow-cloned,
1894+
* stamped with `doc` as its `ownerDocument`, and detached (`parentNode` set to `null`).
1895+
* Children are imported recursively when `deep` is `true`; for {@link Attr} nodes `deep` is
1896+
* always forced to `true`
1897+
* because an attribute's value lives in a child text node.
1898+
*
1899+
* @param {Document} doc
1900+
* The document that will own the imported node.
1901+
* @param {Node} node
1902+
* The node to import.
1903+
* @param {boolean} deep
1904+
* If `true`, descendants are imported recursively.
1905+
* @returns {Node}
1906+
* The newly imported node, now owned by `doc`.
1907+
*/
1908+
function importNode(doc, node, deep) {
1909+
var destRoot;
1910+
walkDOM(node, null, {
1911+
enter: function (srcNode, destParent) {
1912+
// Shallow-clone the node and stamp it into the target document.
1913+
var destNode = srcNode.cloneNode(false);
1914+
destNode.ownerDocument = doc;
1915+
destNode.parentNode = null;
1916+
// capture as the root of the imported subtree or attach to parent.
1917+
if (destParent === null) {
1918+
destRoot = destNode;
1919+
} else {
1920+
destParent.appendChild(destNode);
1921+
}
1922+
// ATTRIBUTE_NODE must always be imported deeply: its value lives in a child text node.
1923+
var shouldDeep = srcNode.nodeType === ATTRIBUTE_NODE || deep;
1924+
return shouldDeep ? destNode : null;
1925+
},
1926+
});
1927+
return destRoot;
19351928
}
19361929
//
19371930
//var _relationMap = {firstChild:1,lastChild:1,previousSibling:1,nextSibling:1,

0 commit comments

Comments
 (0)