/* XPath.js, an JavaScript implementation of XML Path Language (XPath) Version 1.0 Copyright (C) 2008 Henrik Lindqvist This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */(function(a,b,c){function d(a){return this.e=a,this.i=0,this.js=["with(XPath){return ","}"],this.expression(1,1)||this.error(),new Function("n","nsr",this.js.join(""))}function e(a){if(this==a)return 0;if(this.nodeType==2&&a.nodeType==2)return this.ownerElement&&this.ownerElement==a.ownerElement?32:0;var b=this.ownerElement||this,c=a.ownerElement||a;if(b.sourceIndex>=0&&c.sourceIndex>=0&&b.contains&&c.contains)return(b.contains(c)&&16||c.contains(b)&&8)|(b.sourceIndexg){while(f--!=g)b=b.parentNode;if(b==c)return 10}else if(g>f){while(g--!=f)c=c.parentNode;if(c==b)return 20}while((d=b.parentNode)!=(e=c.parentNode))if(!(b=d)||!(c=e))return 1;while(b=b.nextSibling)if(b==c)return 4;return 2}d.ie=/MSIE/.test(navigator.userAgent),d.prototype={match:function(a,b){var c,d;return!(c=a.exec(this.e.substr(this.i)))||typeof b=="number"&&!(d=c[b])||typeof b=="object"&&!(d=b[c[1]])?!1:(this.m=c,this.i+=c[0].length,d||c)},error:function(a){a=(a||"Syntax error")+" at index "+this.i+": "+this.e.substr(this.i);var b;try{b=new g(51,a)}catch(c){b=new Error(a)}throw b},step:function(a,b,c,e){var f=3;if(this.match(/^(\/\/?|\.\.?|@)\s*/,1))switch(this.m[1]){case"/":c&&this.error();if(!e)return this.step(a,b,1);this.js.splice(a,0,' axis(axes["',"document-root",'"],'),f+=this.nodeTypes.node.call(this,a+f),c=1;break;case"//":c&&this.error(),this.js.splice(a,0,' axis(axes["',"descendant-or-self",'"],'),f+=this.nodeTypes.node.call(this,a+f),c=1;break;case".":!c&&!e&&this.error(),this.js.splice(a,0,' axis(axes["',"self",'"],'),f+=this.nodeTypes.node.call(this,a+f),c=0;break;case"..":!c&&!e&&this.error(),this.js.splice(a,0,' axis(axes["',"parent",'"],'),f+=this.nodeTypes.node.call(this,a+f),c=0;break;case"@":!c&&!e&&this.error(),this.js.splice(a,0,' axis(axes["',"attribute",'"],'),f+=this.nodeTest(a+f,"node")||this.error("Missing nodeTest after @"),c=0}else{if(!c&&!e)return c?this.error():0;if(this.match(/^([a-z]+(?:-[a-z]+)*)\s*::\s*/,d.axes))this.js.splice(a,0,' axis(axes["',this.m[1],'"],'),f+=this.nodeTest(a+f,this.m[1]=="attribute"?"node":"element")||this.error("Missing nodeTest after ::"),c=0;else if(f=this.nodeTest(a,"element"))this.js.splice(a,0,' axis(axes["',"child",'"],'),f+=3,c=0;else return 0}for(var g;g=this.predicate(a+f);f+=g);return e&&this.js.splice(b+f++,0,e),f+=this.step(a,b+f,c),this.js.splice(b+f++,0,")"),f},expression:function(a,b,c){var d,e=this.operand(a);while(d=this.match(/^(or|and|!?=|[<>]=?|[|*+-]|div|mod)\s*/,this.operators)){if(c&&c[0]>=d[0]){this.i-=this.m[0].length;break}this.js.splice(a,0,d[1]),e++,this.js.splice(a+e++,0,d[2]),e+=this.expression(a+e,b,d)||this.error("Missing operand"),this.js.splice(a+e++,0,d[3])}return e},operand:function(a){if(this.match(/^(-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)|"[^"]*"|'[^']*')\s*/,1))return this.js.splice(a,0,this.m[1]),1;var b;if(b=this.match(/^([a-z]+(?:-[a-z]+)*)\s*\(\s*/,this.functions)){var c=1,d;this.js.splice(a,0,b[1]);do d&&this.js.splice(a+c++,0,","),c+=d=this.expression(a+c,a+c);while(d&&this.match(/^,\s*/));return this.match(/^\)\s*/)||this.error("Missing ("),b[0]&&(d&&this.js.splice(a+c++,0,","),this.js.splice(a+c++,0,b[0])),b[2]?this.js.splice(a+c++,0,b[2]):d>1&&this.error("Function has arguments"),c+=this.step(a,a+c),c}if(this.match(/^\(\s*/)){var c=1;return this.js.splice(a,0,"("),c+=this.expression(a+c,a+c),this.match(/^\)\s*/)||this.error("Missing )"),this.js.splice(a+c++,")"),c}return this.step(a,a,0,"[n]")},operators:{"|":[1,"union(",",",")"],or:[1,"bool(",")||bool(",")"],and:[2,"bool(",")&&bool(",")"],"=":[3,"compare(eq,",",",")"],"!=":[3,"compare(ne,",",",")"],"<":[4,"compare(lt,",",",")"],">":[4,"compare(gt,",",",")"],"<=":[4,"compare(le,",",",")"],">=":[4,"compare(ge,",",",")"],"+":[5,"number(",")+number(",")"],"-":[5,"number(",")-number(",")"],"*":[6,"number(",")*number(",")"],div:[6,"number(",")/number(",")"],mod:[6,"number(",")%number(",")"]},functions:{last:[0,"nl.length"],position:[0,"(i+1)"],count:["nl","(",".length||0)"],id:["n","id(",")"],"local-name":["nl","localName(",")"],"namespace-uri":["nl","namespaceURI(",")"],name:["nl","qName(",")"],string:["n","string(",")"],concat:[0,"concat(",")"],"starts-with":[0,"startsWith(",")"],contains:[0,"contains(",")"],"substring-before":[0,"substringBefore(",")"],"substring-after":[0,"substringAfter(",")"],substring:[0,"substring(",")"],"string-length":["n","string(",").length"],"normalize-space":["n","normalizeSpace(",")"],translate:[0,"translate(",")"],"boolean":[0,"bool(",")"],not:[0,"!bool(",")"],"true":[0,"true "],"false":[0,"false "],number:["n","number(",")"],floor:[0,"Math.floor(number(","))"],ceiling:[0,"Math.ceil(number(","))"],round:[0,"Math.round(number(","))"],sum:[0,"sum(",")"]},predicate:function(a){var b=0;if(this.match(/^\[\s*/)){if(b=this.expression(a,a))this.js.splice(a,0,"function(n,i,nl){with(XPath){var r="),b++,this.js.splice(a+b++,0,';return typeof r=="number"?Math.round(r)==i+1:bool(r)}},');this.match(/^\]\s*/)||this.error("Missing ]")}return b},nodeTest:function(a,b){var c;return(c=this.match(/^([a-z]+(?:-[a-z]+)*)\(([^)]*)\)\s*/,this.nodeTypes))?c.call(this,a,this.m[2]):this.match(/^\*\s*/)?this.nodeTypes[b].call(this,a):this.nodeName(a)},nodeType:function(a,b){return this.js.splice(a,0,"function(n){return n.nodeType==",b,"},"),3},nodeTypes:{node:function(a){return this.js.splice(a,0,"null,"),1},element:function(a){return this.nodeType(a,1)},attribute:function(a){return this.nodeType(a,2)},text:function(a){return this.nodeType(a,3)},"processing-instruction":function(a,b){return b?(this.js.splice(a,0,"function(n){return n.nodeType==7&&n.target==",b,"},"),3):this.nodeType(a,7)},comment:function(a){return this.nodeType(a,8)}},nodeName:function(a){return this.match(/^([a-zA-Z_]+(?:-?[a-zA-Z0-9]+)*)(?::([a-zA-Z_]+(?:-?[a-zA-Z0-9]+)*))?\s*/,1)?this.m[2]?(this.js.splice(a,0,'function(n){if(!nsr)throw new XPathException(14);return "',this.m[2],'"==',d.ie?"n.baseName":"n.localName",'&&nsr.lookupNamespaceURI("',this.m[1],'")==n.namespaceURI},'),7):(this.js.splice(a,0,"function(n){return/^",this.m[1],"$/i.test(n.nodeName)},"),3):0}},d.order=function(a,b){var c=a.compareDocumentPosition?a.compareDocumentPosition(b):d.compareDocumentPosition.call(a,b);return c&32?(a=Array.prototype.indexOf.call(a.attributes,a),b=Array.prototype.indexOf.call(b.attributes,b),ab?1:0):c?3-(c&6||3):a==b?0:(a=a.ownerElement)&&(b=b.ownerElement)?d.order(a,b):d.ie?1:0},d.compare=function(a,b,c){if(b instanceof Array&&c instanceof Array){var d=b.map(this.string),e=c.map(this.string);for(b=d.length;--b>=0;)for(c=e.length;--c>=0;)if(!a(d[b],e[c]))return!1;return!0}if(b instanceof Array){for(var f=b.length;--f>=0;)if(!a(this[typeof c](b[f]),c))return!1;return b.length>0}if(c instanceof Array){for(var f=c.length;--f>=0;)if(!a(b,this[typeof b](c[f])))return!1;return c.length>0}return typeof b=="boolean"||typeof c=="boolean"?a(this.bool(b),this.bool(c)):typeof b=="number"||typeof c=="number"?a(this.number(b),this.number(c)):a(this.string(b),this.string(c))},d.eq=function(a,b){return a==b},d.ne=function(a,b){return a!=b},d.lt=function(a,b){return ab},d.le=function(a,b){return a<=b},d.ge=function(a,b){return a>=b},d.id=function(a,b){arguments.length==1&&(b=a);var c=[];for(var d=this.string(a).split(/\s+/),e=d.length;--e>=0;)(a=(b.ownerDocument||b).getElementById(d[e]))&&c.push(a);return c.sort(this.order)},d.localName=new Function("nl","return (nl.length&&nl[0]."+(d.ie?"baseName":"localName")+')||""'),d.namespaceURI=function(a){return a.length&&a[0].namespaceURI||""},d.qName=function(a){return a.length&&a[0].nodeName||""},d.union=function(a,b){if(!a.length)return b;if(!b.length)return a;var c=[],d=a.length-1,e=b.length-1;for(;;){switch(this.order(a[d],b[e])){case-1:c.unshift(b[e--]);break;case 0:e--;case 1:c.unshift(a[d--]);break;default:throw new Error("Invalid order")}if(d<0){++e>0&&c.unshift.apply(c,c.slice.call(b,0,e));break}if(e<0){++d>0&&c.unshift.apply(c,c.slice.call(a,0,d));break}}return c},d.string=d.object=function(a){if(a instanceof Array&&typeof (a=a[0])=="undefined")return"";if(typeof a=="string")return a;switch(a.nodeType){case 1:case 9:case 11:return Array.prototype.map.call(a.childNodes,this.string,this).join("");default:return a.nodeValue||""}return String(a)},d.concat=function(){return Array.prototype.map.call(arguments,this.string,this).join("")},d.startsWith=function(a,b){return this.string(a).substr(0,(b=this.string(b)).length)==b},d.contains=function(a,b){return this.string(a).indexOf(this.string(b))!=-1},d.substringBefore=function(a,b){return a=this.string(a),b=a.indexOf(this.string(b)),b!=-1?a.substr(0,b):""},d.substringAfter=function(a,b){a=this.string(a),b=this.string(b);var c=a.indexOf(b);return c!=-1?a.substr(c+b.length):""},d.substring=function(a,b,c){return a=this.string(a),b=Math.round(this.number(b))-1,arguments.length==2?a.substr(b<0?0:b):a.substr(b<0?0:b,Math.round(this.number(c))-Math.max(0,-b))},d.normalizeSpace=function(a){return this.string(a).replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ")},d.translate=function(a,b,c){a=this.string(a),b=this.string(b),c=this.string(c);var d=[],e=a.length,f=0,g,h;while(--e>=0)((g=b.indexOf(h=a.charAt(f++)))==-1||(h=c.charAt(g)))&&d.push(h);return d.join("")},d.bool=d["boolean"]=function(a){return typeof a=="boolean"?a:a instanceof Array||typeof a=="string"?a.length>0:Boolean(a)},d.number=function(a){return a instanceof Array&&typeof (a=a[0])=="undefined"?0:typeof a=="number"?a:typeof a=="boolean"?a?1:0:Number(this.string(a))},d.sum=function(a){var b=0,c=a.length;while(--c>=0)b+=this.number(a[c]);return b},d.walk=function(a,b){var c,d=a.firstChild;while(d){b.push(d);if(c=d.firstChild)d=c;else for(c=d;!(d=c.nextSibling)&&(c=c.parentNode)&&c!=a;);}return b},d.axes={ancestor:function(a){var b=[];while(a=a.parentNode)b.unshift(a);return b},"ancestor-or-self":function(a){var b=[];do b.unshift(a);while(a=a.parentNode);return b},attribute:new Function("n","var nl = [], a = n.attributes;if(a){attr:for(var x,i=a.length;--i>=0;){if(!(x=a[i]).specified){"+(d.ie?'switch(x.nodeName){case"selected":case"value":if(x.nodeValue)break;default:continue attr;}':"continue;")+"}nl.unshift(x);}}return nl;"),child:function(a){return a.childNodes||[]},descendant:function(a){return this.walk(a,[])},"descendant-or-self":function(a){return this.walk(a,[a])},following:function(a){var b=[],c;while(a)(c=a.nextSibling)?(b.push(a=c),(c=a.firstChild)&&b.push(a=c)):a=a.parentNode;return b},"following-sibling":function(a){var b=[];while(a=a.nextSibling)b.push(a);return b},parent:function(a){return a.parentNode?[a.parentNode]:[]},preceding:function(a){var b=[],c,d=a.parentNode;while(a)if(c=a.previousSibling){for(a=c;c=a.lastChild;a=c);b.unshift(a)}else if(a=a.parentNode)a==d?d=d.parentNode:b.unshift(a);return b},"preceding-sibling":function(a){var b=[];while(a=a.previousSibling)b.unshift(a);return b},self:function(a){return[a]},"document-root":function(a){return[a.ownerDocument||a]}},d.axis=function(a,b){var c,d,e=arguments.length-1,f=arguments[e],g=Array.prototype;for(var h=0,i,j=f.length;--j>=0;){d=a.call(this,f[h++]),b&&d.length&&(d=g.filter.call(d,b,this));for(i=2;i=0;)if(b=/xmlns:(.+)/.exec((c=a.attributes[d]).nodeName))this.ns[b[1]]=c.nodeValue;this.ns[""]=a.getAttribute("targetNamespace")}j.prototype={lookupNamespaceURI:function(a){return this.ns[a||""]}},a.XPathNSResolver=j}if(c||!a.XPathExpression){function k(a,b){this.fn=d.cache[a]||(d.cache[a]=new d(a)),this.nsr=b}k.prototype={evaluate:function(a,b){return new l(this.fn(a,this.nsr),b)}},a.XPathExpression=k}if(c||!a.XPathResult){function l(a,b){if(b==0)switch(typeof a){default:b++;case"boolean":b++;case"string":b++;case"number":b++}this.resultType=b;switch(b){case 1:this.numberValue=d.number(a);return;case 2:this.stringValue=d.string(a);return;case 3:this.booleanValue=d.bool(a);return;case 4:case 5:if(a instanceof Array){this.value=a,this.index=0,this.invalidIteratorState=!1;return}break;case 6:case 7:if(a instanceof Array){this.value=a,this.snapshotLength=a.length;return}break;case 8:case 9:if(a instanceof Array){this.singleNodeValue=a[0];return}}throw new g(52)}var m=l;m.ANY_TYPE=0,m.NUMBER_TYPE=1,m.STRING_TYPE=2,m.BOOLEAN_TYPE=3,m.UNORDERED_NODE_ITERATOR_TYPE=4,m.ORDERED_NODE_ITERATOR_TYPE=5,m.UNORDERED_NODE_SNAPSHOT_TYPE=6,m.ORDERED_NODE_SNAPSHOT_TYPE=7,m.ANY_UNORDERED_NODE_TYPE=8,m.FIRST_ORDERED_NODE_TYPE=9,m.prototype={iterateNext:function(){switch(this.resultType){case 4:case 5:return this.value[this.index++]}throw new g(52)},snapshotItem:function(a){switch(this.resultType){case 6:case 7:return this.value[a]}throw new g(52)}},a.XPathResult=m}if(c||!a.XPathEvaluator){function n(){}var h=n;h.prototype={createExpression:function(a,b){return new k(a,b)},createNSResolver:function(a){return new j(a)},evaluate:function(a,b,c,d){return(new k(a,c)).evaluate(b,d)}},h.install=function(a,b){for(var c in n.prototype)if(b||!a[c])a[c]=n.prototype[c]},a.XPathEvaluator=h,a.Document?h.install(a.Document.prototype,c):h.install(document,c),a.XPath=d}})(window,document,/WebKit/.test(navigator.userAgent)||/Node\.js/.test(navigator.userAgent));