1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2005, 2006, 2007, 2009, 2010, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 
 25 /**
 26  * XmlDocument factory
 27  * 
 28  * @private
 29  */
 30 AjxDebugXmlDocument = function() {
 31 }
 32 
 33 // used to find the Automation server name
 34 getDomDocumentPrefix = function() {
 35 	if (getDomDocumentPrefix.prefix)
 36 		return getDomDocumentPrefix.prefix;
 37 	
 38 	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 39 	var o;
 40 	var len = prefixes.length;
 41 	for (var i = 0; i < len; i++) {
 42 		try {
 43 			// try to create the objects
 44 			o = new ActiveXObject(prefixes[i] + ".DomDocument");
 45 			return getDomDocumentPrefix.prefix = prefixes[i];
 46 		}
 47 		catch (ex) {};
 48 	}
 49 	
 50 	throw new Error("Could not find an installed XML parser");
 51 }
 52 
 53 AjxDebugXmlDocument.prototype.create = 
 54 function () {
 55 	try {
 56 		// DOM2
 57 		if (document.implementation && document.implementation.createDocument) {
 58 			var doc = document.implementation.createDocument("", "", null);
 59 			
 60 			// some versions of Moz do not support the readyState property
 61 			// and the onreadystate event so we patch it!
 62 			if (doc.readyState == null) {
 63 				doc.readyState = 1;
 64 				doc.addEventListener("load", function () {
 65 					doc.readyState = 4;
 66 					if (typeof doc.onreadystatechange == "function")
 67 						doc.onreadystatechange();
 68 				}, false);
 69 			}
 70 			
 71 			return doc;
 72 		}
 73 		if (window.ActiveXObject)
 74 			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
 75 	}
 76 	catch (ex) {}
 77 	throw new Error("Your browser does not support XmlDocument objects");
 78 }
 79 
 80 // Create the loadXML method and xml getter for Mozilla
 81 if (window.DOMParser &&
 82 	window.XMLSerializer &&
 83 	window.Node && Node.prototype && Node.prototype.__defineGetter__)
 84 {
 85 	if (AjxEnv.isSafari) {
 86 		Document.prototype.loadXML = function(s) {
 87 			// parse the string to a new doc
 88 			var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
 89 
 90 			// remove all initial children
 91 			while (this.hasChildNodes()) {
 92 				this.removeChild(this.lastChild);
 93 			}
 94 
 95 			// insert and import nodes
 96 			var len = doc2.childNodes.length;
 97 			for (var i = 0; i < len; i++) {
 98 				this.appendChild(this.importNode(doc2.childNodes[i], true));
 99 			}
100 		};
101 
102 		// This serializes the DOM tree to an XML String
103 		// Usage: var sXml = oNode.xml
104 		Document.prototype.__defineGetter__("xml", function () {
105 			return (new XMLSerializer()).serializeToString(this);
106 		});
107 	}
108 	//
109 	// XMLDocument did not extend Document interface in some versions of Mozilla
110 	// so explicitly define it here.
111 	//
112 	else {
113 		AjxDebugXmlDocument.prototype.loadXML = function(s) {
114 			// parse the string to a new doc
115 			var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
116 		
117 			// remove all initial children
118 			while (this.hasChildNodes())
119 				this.removeChild(this.lastChild);
120 
121 			// insert and import nodes
122 			var len = doc2.childNodes.length;
123 			for (var i = 0; i < len; i++)
124 				this.appendChild(this.importNode(doc2.childNodes[i], true));
125 		};
126 
127 		// This serializes the DOM tree to an XML String
128 		// Usage: var sXml = oNode.xml
129 		AjxDebugXmlDocument.prototype.__defineGetter__("xml", function () {
130 			return (new XMLSerializer()).serializeToString(this);
131 		});
132 	}
133 };
134