-
Notifications
You must be signed in to change notification settings - Fork 11
/
importNode.js
32 lines (32 loc) · 1.34 KB
/
importNode.js
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
function importNode(node, allChildren, doc) {
var a, i, il;
doc = doc || document;
try {
return doc.importNode(node, allChildren);
} catch (e) {
switch (node.nodeType) {
case document.ELEMENT_NODE:
var newNode = doc.createElementNS(node.namespaceURI, node.nodeName);
if (node.attributes && node.attributes.length > 0) {
for (i = 0, il = node.attributes.length; i < il; i++) {
a = node.attributes[i];
try {
newNode.setAttributeNS(a.namespaceURI, a.nodeName, node.getAttribute(a.nodeName));
} catch (err) {
// ignore this error... doesn't seem to make a difference
}
}
}
if (allChildren && node.childNodes && node.childNodes.length > 0) {
for (i = 0, il = node.childNodes.length; i < il; i++) {
newNode.appendChild(importNode(node.childNodes[i], allChildren));
}
}
return newNode;
case document.TEXT_NODE:
case document.CDATA_SECTION_NODE:
case document.COMMENT_NODE:
return doc.createTextNode(node.nodeValue);
}
}
}