﻿/*@cc_on@*/

if (window.Node && Node.prototype) {
	//add xml and text property to Gecko
	if (window.XMLSerializer) {
		var xs = new XMLSerializer();
		
		//xml is read-only
		Node.prototype.__defineGetter__('xml', function() {
			return xs.serializeToString(this);
		});
		
		//text is read/write
		Node.prototype.__defineGetter__('text', function() {
			return this.textContent;
		});
		Node.prototype.__defineSetter__('text', function(val) {
			return this.textContent = val;
		});
	}
	
	//add selectSingleNode and selectNode method support to UAs that expose Node interface/prototype
	//	and support DOM XPath, since DOM XPath is really convoluted
	if (document.createExpression && document.createNSResolver && document.evaluate) {
		Node.prototype.selectSingleNode = function(expr) {
			var doc = this.ownerDocument || this;
			//Gecko bug: document.createNSResolver(document) doesn't resolve any namespace prefixes,
			//	so use document.createNSResolver(document.documentElement)
			var nsr = doc.createNSResolver(this.documentElement || this);
			return doc.evaluate(expr, this, nsr, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
		}
		
		Node.prototype.selectNodes = function(expr) {
			var doc = this.ownerDocument || this;
			//Gecko bug: document.createNSResolver(document) doesn't resolve any namespace prefixes,
			//	so use document.createNSResolver(document.documentElement)
			var nsr = doc.createNSResolver(this.documentElement || this);
			var snapshot = doc.evaluate(expr, this, nsr, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
			var len = snapshot.snapshotLength;
			var ret = new Array(len);
			for (var i = 0; i < len; i++)
				ret[i] = snapshot.snapshotItem(i);
			return ret;
		}
	}
}
