diff options
author | Nina Vyedin <nina.vyedin@gmail.com> | 2013-03-05 03:04:55 +0400 |
---|---|---|
committer | Nina Vyedin <nina.vyedin@gmail.com> | 2013-03-05 07:56:00 +0400 |
commit | 36d4fbba9c11fa87d2b9dd5b13c703da720a7dc8 (patch) | |
tree | 05380247f5a7fb10fe6d7108cf5adf7d70f020f6 /webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js | |
parent | c5a74be579e24aa43f51767784c547f21b1a8d19 (diff) |
[webdoc] reorganize the sidebar plugin directory and refactor some code so it fits neatly into a plugin
Diffstat (limited to 'webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js')
-rw-r--r-- | webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js b/webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js new file mode 100644 index 00000000..70806c28 --- /dev/null +++ b/webdoc/plugins/sidebar-plugin/dependencies/xtree/xmlextras.js @@ -0,0 +1,147 @@ +//<script> +////////////////// +// Helper Stuff // +////////////////// + +// used to find the Automation server name +function getDomDocumentPrefix() { + if (getDomDocumentPrefix.prefix) + return getDomDocumentPrefix.prefix; + + var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"]; + var o; + for (var i = 0; i < prefixes.length; i++) { + try { + // try to create the objects + o = new ActiveXObject(prefixes[i] + ".DomDocument"); + return getDomDocumentPrefix.prefix = prefixes[i]; + } + catch (ex) {}; + } + + throw new Error("Could not find an installed XML parser"); +} + +function getXmlHttpPrefix() { + if (getXmlHttpPrefix.prefix) + return getXmlHttpPrefix.prefix; + + var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"]; + var o; + for (var i = 0; i < prefixes.length; i++) { + try { + // try to create the objects + o = new ActiveXObject(prefixes[i] + ".XmlHttp"); + return getXmlHttpPrefix.prefix = prefixes[i]; + } + catch (ex) {}; + } + + throw new Error("Could not find an installed XML parser"); +} + +////////////////////////// +// Start the Real stuff // +////////////////////////// + + +// XmlHttp factory +function XmlHttp() {} + +XmlHttp.create = function () { + try { + if (window.XMLHttpRequest) { + var req = new XMLHttpRequest(); + + // some versions of Moz do not support the readyState property + // and the onreadystate event so we patch it! + if (req.readyState == null) { + req.readyState = 1; + req.addEventListener("load", function () { + req.readyState = 4; + if (typeof req.onreadystatechange == "function") + req.onreadystatechange(); + }, false); + } + + return req; + } + if (window.ActiveXObject) { + return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp"); + } + } + catch (ex) {} + // fell through + throw new Error("Your browser does not support XmlHttp objects"); +}; + +// XmlDocument factory +function XmlDocument() {} + +XmlDocument.create = function () { + try { + // DOM2 + if (document.implementation && document.implementation.createDocument) { + var doc = document.implementation.createDocument("", "", null); + + // some versions of Moz do not support the readyState property + // and the onreadystate event so we patch it! + if (doc.readyState == null) { + doc.readyState = 1; + doc.addEventListener("load", function () { + doc.readyState = 4; + if (typeof doc.onreadystatechange == "function") + doc.onreadystatechange(); + }, false); + } + + return doc; + } + if (window.ActiveXObject) + return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument"); + } + catch (ex) {} + throw new Error("Your browser does not support XmlDocument objects"); +}; + +// Create the loadXML method and xml getter for Mozilla +if (window.DOMParser && + window.XMLSerializer && + window.Node && Node.prototype && Node.prototype.__defineGetter__) { + + // XMLDocument did not extend the Document interface in some versions + // of Mozilla. Extend both! + XMLDocument.prototype.loadXML = + Document.prototype.loadXML = function (s) { + + // parse the string to a new doc + var doc2 = (new DOMParser()).parseFromString(s, "text/xml"); + + // remove all initial children + while (this.hasChildNodes()) + this.removeChild(this.lastChild); + + // insert and import nodes + for (var i = 0; i < doc2.childNodes.length; i++) { + this.appendChild(this.importNode(doc2.childNodes[i], true)); + } + }; + + + /* + * xml getter + * + * This serializes the DOM tree to an XML String + * + * Usage: var sXml = oNode.xml + * + */ + // XMLDocument did not extend the Document interface in some versions + // of Mozilla. Extend both! + XMLDocument.prototype.__defineGetter__("xml", function () { + return (new XMLSerializer()).serializeToString(this); + }); + Document.prototype.__defineGetter__("xml", function () { + return (new XMLSerializer()).serializeToString(this); + }); +}
\ No newline at end of file |