PageRenderTime 30ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/selenium-core/scripts/xmlextras.js

http://datanucleus-appengine.googlecode.com/
JavaScript | 153 lines | 86 code | 24 blank | 43 comment | 22 complexity | 84cd7cb0009616db7292fa01ecb8c2e8 MD5 | raw file
  1// This is a third party JavaScript library from
  2// http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
  3// i.e. This has not been written by ThoughtWorks.
  4
  5//<script>
  6//////////////////
  7// Helper Stuff //
  8//////////////////
  9
 10// used to find the Automation server name
 11function getDomDocumentPrefix() {
 12	if (getDomDocumentPrefix.prefix)
 13		return getDomDocumentPrefix.prefix;
 14	
 15	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 16	var o;
 17	for (var i = 0; i < prefixes.length; i++) {
 18		try {
 19			// try to create the objects
 20			o = new ActiveXObject(prefixes[i] + ".DomDocument");
 21			return getDomDocumentPrefix.prefix = prefixes[i];
 22		}
 23		catch (ex) {};
 24	}
 25	
 26	throw new Error("Could not find an installed XML parser");
 27}
 28
 29function getXmlHttpPrefix() {
 30	if (getXmlHttpPrefix.prefix)
 31		return getXmlHttpPrefix.prefix;
 32	
 33	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 34	var o;
 35	for (var i = 0; i < prefixes.length; i++) {
 36		try {
 37			// try to create the objects
 38			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
 39			return getXmlHttpPrefix.prefix = prefixes[i];
 40		}
 41		catch (ex) {};
 42	}
 43	
 44	throw new Error("Could not find an installed XML parser");
 45}
 46
 47//////////////////////////
 48// Start the Real stuff //
 49//////////////////////////
 50
 51
 52// XmlHttp factory
 53function XmlHttp() {}
 54
 55XmlHttp.create = function () {
 56	try {
 57		if (window.XMLHttpRequest) {
 58			var req = new XMLHttpRequest();
 59			
 60			// some versions of Moz do not support the readyState property
 61			// and the onreadystate event so we patch it!
 62			if (req.readyState == null) {
 63				req.readyState = 1;
 64				req.addEventListener("load", function () {
 65					req.readyState = 4;
 66					if (typeof req.onreadystatechange == "function")
 67						req.onreadystatechange();
 68				}, false);
 69			}
 70			
 71			return req;
 72		}
 73		if (window.ActiveXObject) {
 74			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
 75		}
 76	}
 77	catch (ex) {}
 78	// fell through
 79	throw new Error("Your browser does not support XmlHttp objects");
 80};
 81
 82// XmlDocument factory
 83function XmlDocument() {}
 84
 85XmlDocument.create = function () {
 86	try {
 87		// DOM2
 88		if (document.implementation && document.implementation.createDocument) {
 89			var doc = document.implementation.createDocument("", "", null);
 90			
 91			// some versions of Moz do not support the readyState property
 92			// and the onreadystate event so we patch it!
 93			if (doc.readyState == null) {
 94				doc.readyState = 1;
 95				doc.addEventListener("load", function () {
 96					doc.readyState = 4;
 97					if (typeof doc.onreadystatechange == "function")
 98						doc.onreadystatechange();
 99				}, false);
100			}
101			
102			return doc;
103		}
104		if (window.ActiveXObject)
105			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
106	}
107	catch (ex) {}
108	throw new Error("Your browser does not support XmlDocument objects");
109};
110
111// Create the loadXML method and xml getter for Mozilla
112if (window.DOMParser &&
113	window.XMLSerializer &&
114	window.Node && Node.prototype && Node.prototype.__defineGetter__) {
115
116	// XMLDocument did not extend the Document interface in some versions
117	// of Mozilla. Extend both!
118	//XMLDocument.prototype.loadXML = 
119	Document.prototype.loadXML = function (s) {
120		
121		// parse the string to a new doc	
122		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
123		
124		// remove all initial children
125		while (this.hasChildNodes())
126			this.removeChild(this.lastChild);
127			
128		// insert and import nodes
129		for (var i = 0; i < doc2.childNodes.length; i++) {
130			this.appendChild(this.importNode(doc2.childNodes[i], true));
131		}
132	};
133	
134	
135	/*
136	 * xml getter
137	 *
138	 * This serializes the DOM tree to an XML String
139	 *
140	 * Usage: var sXml = oNode.xml
141	 *
142	 */
143	// XMLDocument did not extend the Document interface in some versions
144	// of Mozilla. Extend both!
145	/*
146	XMLDocument.prototype.__defineGetter__("xml", function () {
147		return (new XMLSerializer()).serializeToString(this);
148	});
149	*/
150	Document.prototype.__defineGetter__("xml", function () {
151		return (new XMLSerializer()).serializeToString(this);
152	});
153}