Permalink
Browse files

xc_dom_helper: added helper functions to make IE7-9 happy

  • Loading branch information...
1 parent 7b6e73e commit ebaac8f7d3da32518cd98b2d91be5657e6529885 Tim Evans committed Mar 15, 2011
Showing with 101 additions and 5 deletions.
  1. +76 −5 src/xc_dom_helper.js
  2. +25 −0 test/xc_dom_helper.js
View
@@ -29,6 +29,33 @@ var XC_DOMHelper = {
NOTATION_NODE: 12
},
+ xmlDocument: (function () {
+ var doc = null;
+ try {
+ doc = document.implementation.createDocument('jabber:client', 'xcjs', null);
+ } catch (x) {
+ var msDocs = ["Msxml2.DOMDocument.6.0",
+ "Msxml2.DOMDocument.5.0",
+ "Msxml2.DOMDocument.4.0",
+ "MSXML2.DOMDocument.3.0",
+ "MSXML2.DOMDocument",
+ "MSXML.DOMDocument",
+ "Microsoft.XMLDOM"];
+ for (var i = 0, len = msDocs.length; i < len; i++) {
+ if (doc === null) {
+ try {
+ doc = new ActiveXObject(msDocs[i]);
+ } catch (e) {
+ doc = null;
+ }
+ } else {
+ break;
+ }
+ }
+ }
+ return doc;
+ }()),
+
/**
* Get the first child from a document fragment that is an Element.
*
@@ -99,9 +126,9 @@ var XC_DOMHelper = {
* @returns {String} The document fragment as a string.
*/
serializeToString: function (node) {
- if ("XMLSerializer" in window) {
+ try {
return (new XMLSerializer()).serializeToString(node);
- } else {
+ } catch (x) {
return node.xml;
}
},
@@ -115,10 +142,54 @@ var XC_DOMHelper = {
*/
createElementNS: function (ns, tagName) {
if ("createElementNS" in document) {
- return document.createElementNS(ns, tagName);
+ return this.xmlDocument.createElementNS(ns, tagName);
} else {
- var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- return xmlDoc.createNode(1, tagName, ns);
+ return this.xmlDocument.createNode(1, tagName, ns);
+ }
+ },
+
+ /**
+ * Import the node into the XML Document.
+ *
+ * @param {Element|Node} node The element to import into the document.
+ * @param {Boolean} deep Whether to import all child nodes or not.
+ * @returns {Element} The imported element (suitable for appending as a child).
+ */
+ importNode: function (node, deep) {
+ var i, len, newNode, children;
+ try {
+ node = this.xmlDocument.importNode(node, deep);
+ } catch (x) {
+ switch (node.nodeType) {
+ case XC_DOMHelper.NodeTypes.ELEMENT_NODE:
+ if (node.namespaceURI) {
+ newNode = this.createElementNS(node.namespaceURI, node.nodeName);
+ } else {
+ newNode = this.xmlDocument.createElement(node.nodeName);
+ }
+ if (node.attributes && node.attributes.length > 0) {
+ for (i = 0, len = node.attributes.length; i < len; i++) {
+ if (node.attributes[i].nodeName === 'xmlns') continue;
+ newNode.setAttribute(node.attributes[i].nodeName,
+ node.attributes[i].value);
+ }
+ }
+
+ children = node.childNodes || node.children;
+ if (deep && children && children.length > 0) {
+ for (i = 0, len = children.length; i < len; i++) {
+ newNode.appendChild(this.importNode(children[i], deep));
+ }
+ }
+ break;
+ case XC_DOMHelper.NodeTypes.TEXT_NODE:
+ case XC_DOMHelper.NodeTypes.CDATA_SECTION_NODE:
+ case XC_DOMHelper.NodeTypes.COMMENT_NODE:
+ newNode = this.xmlDocument.createTextNode(node.nodeValue);
+ break;
+ }
+ node = newNode;
}
+ return node;
}
};
View
@@ -113,9 +113,34 @@ XC.Test.XC_DOMHelper = new YAHOO.tool.TestCase({
var foo = XC_DOMHelper.getFirstElementChild(this.demoDoc),
bill = XC_DOMHelper.getElementsByNS(foo, 'fi')[1];
+ bill = XC_DOMHelper.importNode(bill, true);
bill.appendChild(dummy);
Assert.areEqual(dummy, XC_DOMHelper.getFirstElementChild(bill));
+ },
+
+ testImportNode: function () {
+ var Assert = YAHOO.util.Assert;
+ Assert.isFunction(XC_DOMHelper.importNode, 'importNode is not a function');
+
+ Assert.XPathTests(XC_DOMHelper.serializeToString(XC_DOMHelper.importNode(this.demoDoc.firstChild, true)), {
+ bam: {
+ xpath: '/foo/@bam',
+ value: 'baz'
+ },
+ plip: {
+ xpath: '/foo/@plip',
+ value: 'plop'
+ },
+ bar: {
+ xpath: '/foo/bar/fi:value/text()',
+ value: '1'
+ },
+ bar_fi: {
+ xpath: '/foo/fi:bar/fi:value/text()',
+ value: '2'
+ }
+ });
}
});

0 comments on commit ebaac8f

Please sign in to comment.