Skip to content

Instantly share code, notes, and snippets.

@lakenen
Created March 14, 2013 21:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakenen/5165619 to your computer and use it in GitHub Desktop.
Save lakenen/5165619 to your computer and use it in GitHub Desktop.
document.importNode shim for IE <= 9. Started with http://stackoverflow.com/questions/1811116/ie-support-for-dom-importnode/9883539#9883539, modified to fix issues (http://stackoverflow.com/questions/14593520/ie9-importing-inline-svg-image-elements-broken) with importing image nodes and other nodes with namespaced:attributes
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment