Skip to content

Instantly share code, notes, and snippets.

@dchambers
Created March 18, 2015 14:02
Show Gist options
  • Select an option

  • Save dchambers/0abcec9eaf529f993b9d to your computer and use it in GitHub Desktop.

Select an option

Save dchambers/0abcec9eaf529f993b9d to your computer and use it in GitHub Desktop.

Revisions

  1. dchambers created this gist Mar 18, 2015.
    69 changes: 69 additions & 0 deletions importNode-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    'use strict';

    if(!window.DocumentFragment && window.HTMLDocument) {
    window.DocumentFragment = HTMLDocument;
    }

    if(!document.ELEMENT_NODE) {
    document.ELEMENT_NODE = 1;
    document.ATTRIBUTE_NODE = 2;
    document.TEXT_NODE = 3;
    document.CDATA_SECTION_NODE = 4;
    document.ENTITY_REFERENCE_NODE = 5;
    document.ENTITY_NODE = 6;
    document.PROCESSING_INSTRUCTION_NODE = 7;
    document.COMMENT_NODE = 8;
    document.DOCUMENT_NODE = 9;
    document.DOCUMENT_TYPE_NODE = 10;
    document.DOCUMENT_FRAGMENT_NODE = 11;
    document.NOTATION_NODE = 12;
    }

    if(!document.createElementNS) {
    document.createElementNS = function(namespaceURI, qualifiedName) {
    return document.createElement(qualifiedName);
    };
    }

    if(!document.importNode) {
    document.importNode = function(node, deep) {
    var a, i, il;

    switch (node.nodeType) {
    case document.ELEMENT_NODE:
    var newNode = document.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 (deep && node.childNodes && node.childNodes.length > 0) {
    for (i = 0, il = node.childNodes.length; i < il; i++) {
    newNode.appendChild(document.importNode(node.childNodes[i], deep));
    }
    }
    return newNode;

    case document.TEXT_NODE:
    case document.CDATA_SECTION_NODE:
    return document.createTextNode(node.nodeValue);

    case document.COMMENT_NODE:
    return document.createComment(node.nodeValue);

    case document.DOCUMENT_FRAGMENT_NODE:
    docFragment = document.createDocumentFragment();

    for (i = 0, il = node.childNodes.length; i < il; ++i) {
    docFragment.appendChild(document.importNode(node.childNodes[i], deep));
    }
    return docFragment;
    }
    };
    }