xbImportNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* is this stuff defined? */
  2. if (!document.ELEMENT_NODE) {
  3. document.ELEMENT_NODE = 1;
  4. document.ATTRIBUTE_NODE = 2;
  5. document.TEXT_NODE = 3;
  6. document.CDATA_SECTION_NODE = 4;
  7. document.ENTITY_REFERENCE_NODE = 5;
  8. document.ENTITY_NODE = 6;
  9. document.PROCESSING_INSTRUCTION_NODE = 7;
  10. document.COMMENT_NODE = 8;
  11. document.DOCUMENT_NODE = 9;
  12. document.DOCUMENT_TYPE_NODE = 10;
  13. document.DOCUMENT_FRAGMENT_NODE = 11;
  14. document.NOTATION_NODE = 12;
  15. }
  16. document._importNode = function(node, allChildren) {
  17. /* find the node type to import */
  18. switch (node.nodeType) {
  19. case document.ELEMENT_NODE:
  20. /* create a new element */
  21. var newNode = document.createElement(node.nodeName);
  22. /* does the node have any attributes to add? */
  23. if (node.attributes && node.attributes.length > 0)
  24. /* add all of the attributes */
  25. for (var i = 0, il = node.attributes.length; i < il;) {
  26. if (node.attributes[i].nodeName == 'class') {
  27. newNode.className = node.getAttribute(node.attributes[i++].nodeName);
  28. } else {
  29. newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
  30. }
  31. }
  32. /* are we going after children too, and does the node have any? */
  33. if (allChildren && node.childNodes && node.childNodes.length > 0)
  34. /* recursively get all of the child nodes */
  35. for (var i = 0, il = node.childNodes.length; i < il;)
  36. newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));
  37. return newNode;
  38. break;
  39. case document.TEXT_NODE:
  40. case document.CDATA_SECTION_NODE:
  41. case document.COMMENT_NODE:
  42. return document.createTextNode(node.nodeValue);
  43. break;
  44. }
  45. };