// xpath.js - version 0.7 - Spry Pre-Release 1.6 // // Copyright 2006 Google Inc. // All Rights Reserved var REGEXP_UNICODE=function(){var tests=[' ','\u0120',-1,'!','\u0120',-1,'\u0120','\u0120',0,'\u0121','\u0120',-1,'\u0121','\u0120|\u0121',0,'\u0122','\u0120|\u0121',-1,'\u0120','[\u0120]',0,'\u0121','[\u0120]',-1,'\u0121','[\u0120\u0121]',0,'\u0122','[\u0120\u0121]',-1,'\u0121','[\u0120-\u0121]',0,'\u0122','[\u0120-\u0121]',-1];for(var i=0;i0&&result[0].length>match.length){rule=xpathTokenRules[i];match=result[0];break;}} if(rule&&(rule==TOK_DIV||rule==TOK_MOD||rule==TOK_AND||rule==TOK_OR)&&(!previous||previous.tag==TOK_AT||previous.tag==TOK_DSLASH||previous.tag==TOK_SLASH||previous.tag==TOK_AXIS||previous.tag==TOK_DOLLAR)){rule=TOK_QNAME;} if(rule){expr=expr.substr(match.length);xpathLog('token: '+match+' -- '+rule.label);ahead={tag:rule,match:match,prec:rule.prec?rule.prec:0,expr:makeTokenExpr(match)};}else{xpathLog('DONE');done=true;} while(xpathReduce(stack,ahead)){reduce_count++;xpathLog('stack: '+stackToString(stack));}} xpathLog('stack: '+stackToString(stack));if(stack.length!=1){throw'XPath parse error '+cachekey+':\n'+stackToString(stack);} var result=stack[0].expr;xpathParseCache[cachekey]=result;xpathLog('XPath parse: '+parse_count+' / '+ lexer_count+' / '+reduce_count);return result;} var xpathParseCache={};function xpathCacheLookup(expr){return xpathParseCache[expr];} function xpathReduce(stack,ahead){var cand=null;if(stack.length>0){var top=stack[stack.length-1];var ruleset=xpathRules[top.tag.key];if(ruleset){for(var i=0;iahead.prec||(ahead.tag.left&&cand.prec>=ahead.prec))){for(var i=0;i=0&&s>=0;--p,s-=ds){ds=0;var qmatch=[];if(pattern[p]==Q_MM){p-=1;match.push(qmatch);while(s-ds>=0&&stack[s-ds].tag==pattern[p]){qmatch.push(stack[s-ds]);ds+=1;match.matchlength+=1;}}else if(pattern[p]==Q_01){p-=1;match.push(qmatch);while(s-ds>=0&&ds<2&&stack[s-ds].tag==pattern[p]){qmatch.push(stack[s-ds]);ds+=1;match.matchlength+=1;}}else if(pattern[p]==Q_1M){p-=1;match.push(qmatch);if(stack[s].tag==pattern[p]){while(s-ds>=0&&stack[s-ds].tag==pattern[p]){qmatch.push(stack[s-ds]);ds+=1;match.matchlength+=1;}}else{return[];}}else if(stack[s].tag==pattern[p]){match.push(stack[s]);ds+=1;match.matchlength+=1;}else{return[];} reverseInplace(qmatch);qmatch.expr=mapExpr(qmatch,function(m){return m.expr;});} reverseInplace(match);if(p==-1){return match;}else{return[];}} function xpathTokenPrecedence(tag){return tag.prec||2;} function xpathGrammarPrecedence(frame){var ret=0;if(frame.rule){if(frame.rule.length>=3&&frame.rule[2]>=0){ret=frame.rule[2];}else{for(var i=0;i0;};StringValue.prototype.numberValue=function(){return this.value-0;};StringValue.prototype.nodeSetValue=function(){throw this;};function BooleanValue(value){this.value=value;this.type='boolean';} BooleanValue.prototype.stringValue=function(){return''+this.value;};BooleanValue.prototype.booleanValue=function(){return this.value;};BooleanValue.prototype.numberValue=function(){return this.value?1:0;};BooleanValue.prototype.nodeSetValue=function(){throw this;};function NumberValue(value){this.value=value;this.type='number';} NumberValue.prototype.stringValue=function(){return''+this.value;};NumberValue.prototype.booleanValue=function(){return!!this.value;};NumberValue.prototype.numberValue=function(){return this.value-0;};NumberValue.prototype.nodeSetValue=function(){throw this;};function NodeSetValue(value){this.value=value;this.type='node-set';} NodeSetValue.prototype.stringValue=function(){if(this.value.length==0){return'';}else{return xmlValue(this.value[0]);}};NodeSetValue.prototype.booleanValue=function(){return this.value.length>0;};NodeSetValue.prototype.numberValue=function(){return this.stringValue()-0;};NodeSetValue.prototype.nodeSetValue=function(){return this.value;};function TokenExpr(m){this.value=m;} TokenExpr.prototype.evaluate=function(){return new StringValue(this.value);};function LocationExpr(){this.absolute=false;this.steps=[];} LocationExpr.prototype.appendStep=function(s){this.steps.push(s);};LocationExpr.prototype.prependStep=function(s){var steps0=this.steps;this.steps=[s];for(var i=0;i0){s=this.args[0].evaluate(ctx).stringValue();}else{s=new NodeSetValue([ctx.node]).stringValue();} return new NumberValue(s.length);},'normalize-space':function(ctx){var s;if(this.args.length>0){s=this.args[0].evaluate(ctx).stringValue();}else{s=new NodeSetValue([ctx.node]).stringValue();} s=s.replace(/^\s*/,'').replace(/\s*$/,'').replace(/\s+/g,' ');return new StringValue(s);},'translate':function(ctx){assert(this.args.length==3);var s0=this.args[0].evaluate(ctx).stringValue();var s1=this.args[1].evaluate(ctx).stringValue();var s2=this.args[2].evaluate(ctx).stringValue();for(var i=0;i=1);var c=this.args[0].evaluate(ctx).numberValue();var ret=[];for(var i=0;i':ret=this.compare(ctx,function(x1,x2){return x1>x2;});break;case'>=':ret=this.compare(ctx,function(x1,x2){return x1>=x2;});break;default:alert('BinaryExpr.evaluate: '+this.op.value);} return ret;};BinaryExpr.prototype.compare=function(ctx,cmp){var v1=this.expr1.evaluate(ctx);var v2=this.expr2.evaluate(ctx);var ret;if(v1.type=='node-set'&&v2.type=='node-set'){var n1=v1.nodeSetValue();var n2=v2.nodeSetValue();ret=false;for(var i1=0;i10){return new FilterExpr(expr,predicates);}else{return expr;}} function makeUnaryMinusExpr(minus,expr){return new UnaryMinusExpr(expr);} function makeBinaryExpr(expr1,op,expr2){return new BinaryExpr(expr1,op,expr2);} function makeLiteralExpr(token){var value=token.value.substring(1,token.value.length-1);return new LiteralExpr(value);} function makeNumberExpr(token){return new NumberExpr(token.value);} function makeVariableReference(dollar,name){return new VariableExpr(name.value);} function makeSimpleExpr(expr){if(expr.charAt(0)=='$'){return new VariableExpr(expr.substr(1));}else if(expr.charAt(0)=='@'){var a=new NodeTestName(expr.substr(1));var b=new StepExpr('attribute',a);var c=new LocationExpr();c.appendStep(b);return c;}else if(expr.match(/^[0-9]+$/)){return new NumberExpr(expr);}else{var a=new NodeTestName(expr);var b=new StepExpr('child',a);var c=new LocationExpr();c.appendStep(b);return c;}} function makeSimpleExpr2(expr){var steps=stringSplit(expr,'/');var c=new LocationExpr();for(var i=0;i=",prec:13,re:new RegExp("^>=")};var TOK_GT={label:">",prec:13,re:new RegExp("^>")};var TOK_LE={label:"<=",prec:13,re:new RegExp("^<=")};var TOK_LT={label:"<",prec:13,re:new RegExp("^<")};var TOK_PLUS={label:"+",prec:14,re:new RegExp("^\\+"),left:true};var TOK_MINUS={label:"-",prec:14,re:new RegExp("^\\-"),left:true};var TOK_DIV={label:"div",prec:15,re:new RegExp("^div\\b"),left:true};var TOK_MOD={label:"mod",prec:15,re:new RegExp("^mod\\b"),left:true};var TOK_BRACKO={label:"[",prec:32,re:new RegExp("^\\[")};var TOK_BRACKC={label:"]",re:new RegExp("^\\]")};var TOK_DOLLAR={label:"$",re:new RegExp("^\\$")};var TOK_NCNAME={label:"[ncname]",re:new RegExp('^'+XML_NC_NAME)};var TOK_ASTERISK={label:"*",prec:15,re:new RegExp("^\\*"),left:true};var TOK_LITERALQ={label:"[litq]",prec:20,re:new RegExp("^'[^\\']*'")};var TOK_LITERALQQ={label:"[litqq]",prec:20,re:new RegExp('^"[^\\"]*"')};var TOK_NUMBER={label:"[number]",prec:35,re:new RegExp('^\\d+(\\.\\d*)?')};var TOK_QNAME={label:"[qname]",re:new RegExp('^('+XML_NC_NAME+':)?'+XML_NC_NAME)};var TOK_NODEO={label:"[nodetest-start]",re:new RegExp('^(processing-instruction|comment|text|node)\\(')};var xpathTokenRules=[TOK_DSLASH,TOK_SLASH,TOK_DDOT,TOK_DOT,TOK_AXIS,TOK_COLON,TOK_AXISNAME,TOK_NODEO,TOK_PARENO,TOK_PARENC,TOK_BRACKO,TOK_BRACKC,TOK_AT,TOK_COMMA,TOK_OR,TOK_AND,TOK_NEQ,TOK_EQ,TOK_GE,TOK_GT,TOK_LE,TOK_LT,TOK_PLUS,TOK_MINUS,TOK_ASTERISK,TOK_PIPE,TOK_MOD,TOK_DIV,TOK_LITERALQ,TOK_LITERALQQ,TOK_NUMBER,TOK_QNAME,TOK_NCNAME,TOK_DOLLAR];var XPathLocationPath={label:"LocationPath"};var XPathRelativeLocationPath={label:"RelativeLocationPath"};var XPathAbsoluteLocationPath={label:"AbsoluteLocationPath"};var XPathStep={label:"Step"};var XPathNodeTest={label:"NodeTest"};var XPathPredicate={label:"Predicate"};var XPathLiteral={label:"Literal"};var XPathExpr={label:"Expr"};var XPathPrimaryExpr={label:"PrimaryExpr"};var XPathVariableReference={label:"Variablereference"};var XPathNumber={label:"Number"};var XPathFunctionCall={label:"FunctionCall"};var XPathArgumentRemainder={label:"ArgumentRemainder"};var XPathPathExpr={label:"PathExpr"};var XPathUnionExpr={label:"UnionExpr"};var XPathFilterExpr={label:"FilterExpr"};var XPathDigits={label:"Digits"};var xpathNonTerminals=[XPathLocationPath,XPathRelativeLocationPath,XPathAbsoluteLocationPath,XPathStep,XPathNodeTest,XPathPredicate,XPathLiteral,XPathExpr,XPathPrimaryExpr,XPathVariableReference,XPathNumber,XPathFunctionCall,XPathArgumentRemainder,XPathPathExpr,XPathUnionExpr,XPathFilterExpr,XPathDigits];var Q_01={label:"?"};var Q_MM={label:"*"};var Q_1M={label:"+"};var ASSOC_LEFT=true;var xpathGrammarRules=[[XPathLocationPath,[XPathRelativeLocationPath],18,passExpr],[XPathLocationPath,[XPathAbsoluteLocationPath],18,passExpr],[XPathAbsoluteLocationPath,[TOK_SLASH,XPathRelativeLocationPath],18,makeLocationExpr1],[XPathAbsoluteLocationPath,[TOK_DSLASH,XPathRelativeLocationPath],18,makeLocationExpr2],[XPathAbsoluteLocationPath,[TOK_SLASH],0,makeLocationExpr3],[XPathAbsoluteLocationPath,[TOK_DSLASH],0,makeLocationExpr4],[XPathRelativeLocationPath,[XPathStep],31,makeLocationExpr5],[XPathRelativeLocationPath,[XPathRelativeLocationPath,TOK_SLASH,XPathStep],31,makeLocationExpr6],[XPathRelativeLocationPath,[XPathRelativeLocationPath,TOK_DSLASH,XPathStep],31,makeLocationExpr7],[XPathStep,[TOK_DOT],33,makeStepExpr1],[XPathStep,[TOK_DDOT],33,makeStepExpr2],[XPathStep,[TOK_AXISNAME,TOK_AXIS,XPathNodeTest],33,makeStepExpr3],[XPathStep,[TOK_AT,XPathNodeTest],33,makeStepExpr4],[XPathStep,[XPathNodeTest],33,makeStepExpr5],[XPathStep,[XPathStep,XPathPredicate],33,makeStepExpr6],[XPathNodeTest,[TOK_ASTERISK],33,makeNodeTestExpr1],[XPathNodeTest,[TOK_NCNAME,TOK_COLON,TOK_ASTERISK],33,makeNodeTestExpr2],[XPathNodeTest,[TOK_QNAME],33,makeNodeTestExpr3],[XPathNodeTest,[TOK_NODEO,TOK_PARENC],33,makeNodeTestExpr4],[XPathNodeTest,[TOK_NODEO,XPathLiteral,TOK_PARENC],33,makeNodeTestExpr5],[XPathPredicate,[TOK_BRACKO,XPathExpr,TOK_BRACKC],33,makePredicateExpr],[XPathPrimaryExpr,[XPathVariableReference],33,passExpr],[XPathPrimaryExpr,[TOK_PARENO,XPathExpr,TOK_PARENC],33,makePrimaryExpr],[XPathPrimaryExpr,[XPathLiteral],30,passExpr],[XPathPrimaryExpr,[XPathNumber],30,passExpr],[XPathPrimaryExpr,[XPathFunctionCall],30,passExpr],[XPathFunctionCall,[TOK_QNAME,TOK_PARENO,TOK_PARENC],-1,makeFunctionCallExpr1],[XPathFunctionCall,[TOK_QNAME,TOK_PARENO,XPathExpr,XPathArgumentRemainder,Q_MM,TOK_PARENC],-1,makeFunctionCallExpr2],[XPathArgumentRemainder,[TOK_COMMA,XPathExpr],-1,makeArgumentExpr],[XPathUnionExpr,[XPathPathExpr],20,passExpr],[XPathUnionExpr,[XPathUnionExpr,TOK_PIPE,XPathPathExpr],20,makeUnionExpr],[XPathPathExpr,[XPathLocationPath],20,passExpr],[XPathPathExpr,[XPathFilterExpr],19,passExpr],[XPathPathExpr,[XPathFilterExpr,TOK_SLASH,XPathRelativeLocationPath],20,makePathExpr1],[XPathPathExpr,[XPathFilterExpr,TOK_DSLASH,XPathRelativeLocationPath],20,makePathExpr2],[XPathFilterExpr,[XPathPrimaryExpr,XPathPredicate,Q_MM],20,makeFilterExpr],[XPathExpr,[XPathPrimaryExpr],16,passExpr],[XPathExpr,[XPathUnionExpr],16,passExpr],[XPathExpr,[TOK_MINUS,XPathExpr],-1,makeUnaryMinusExpr],[XPathExpr,[XPathExpr,TOK_OR,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_AND,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_EQ,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_NEQ,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_LT,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_LE,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_GT,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_GE,XPathExpr],-1,makeBinaryExpr],[XPathExpr,[XPathExpr,TOK_PLUS,XPathExpr],-1,makeBinaryExpr,ASSOC_LEFT],[XPathExpr,[XPathExpr,TOK_MINUS,XPathExpr],-1,makeBinaryExpr,ASSOC_LEFT],[XPathExpr,[XPathExpr,TOK_ASTERISK,XPathExpr],-1,makeBinaryExpr,ASSOC_LEFT],[XPathExpr,[XPathExpr,TOK_DIV,XPathExpr],-1,makeBinaryExpr,ASSOC_LEFT],[XPathExpr,[XPathExpr,TOK_MOD,XPathExpr],-1,makeBinaryExpr,ASSOC_LEFT],[XPathLiteral,[TOK_LITERALQ],-1,makeLiteralExpr],[XPathLiteral,[TOK_LITERALQQ],-1,makeLiteralExpr],[XPathNumber,[TOK_NUMBER],-1,makeNumberExpr],[XPathVariableReference,[TOK_DOLLAR,TOK_QNAME],200,makeVariableReference]];var xpathRules=[];function xpathParseInit(){if(xpathRules.length){return;} xpathGrammarRules.sort(function(a,b){var la=a[1].length;var lb=b[1].length;if(lalb){return-1;}else{return 0;}});var k=1;for(var i=0;i=0;--j){if(pattern[j]==Q_1M){push_(xpathRules,pattern[j-1].key,rule);break;}else if(pattern[j]==Q_MM||pattern[j]==Q_01){push_(xpathRules,pattern[j-1].key,rule);--j;}else{push_(xpathRules,pattern[j].key,rule);break;}}} xpathLog('XPath parse INIT: '+xpathRules.length+' rule bins');var sum=0;mapExec(xpathRules,function(i){if(i){sum+=i.length;}});xpathLog('XPath parse INIT: '+(sum/xpathRules.length)+' average bin size');} function xpathCollectDescendants(nodelist,node){for(var n=node.firstChild;n;n=n.nextSibling){nodelist.push(n);arguments.callee(nodelist,n);}} function xpathCollectDescendantsReverse(nodelist,node){for(var n=node.lastChild;n;n=n.previousSibling){nodelist.push(n);arguments.callee(nodelist,n);}} function xpathDomEval(expr,node){var expr1=xpathParse(expr);var ret=expr1.evaluate(new ExprContext(node));return ret;} function xpathSort(input,sort){if(sort.length==0){return;} var sortlist=[];for(var i=0;iv2.key[i].value){return+1*o;}else if(v1.key[i].value