2016-12-17 00:33:47 +01:00
( function ( ) { / *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
2016-11-29 19:28:07 +01:00
*
2016-12-17 00:33:47 +01:00
* Copyright ( c ) 2012 Sergey Ilinsky
2016-11-29 19:28:07 +01:00
* Dual licensed under the MIT and GPL licenses .
*
2016-12-17 00:33:47 +01:00
*
2016-11-29 19:28:07 +01:00
* /
2016-12-17 00:33:47 +01:00
// Javascript objects
2016-11-29 19:28:07 +01:00
var cString = window . String ,
cBoolean = window . Boolean ,
cNumber = window . Number ,
cObject = window . Object ,
cArray = window . Array ,
cRegExp = window . RegExp ,
cDate = window . Date ,
cFunction = window . Function ,
cMath = window . Math ,
2016-12-17 00:33:47 +01:00
// Error Objects
2016-11-29 19:28:07 +01:00
cError = window . Error ,
cSyntaxError = window . SyntaxError ,
cTypeError = window . TypeError ,
2016-12-17 00:33:47 +01:00
// misc
2016-11-29 19:28:07 +01:00
fIsNaN = window . isNaN ,
fIsFinite = window . isFinite ,
nNaN = window . NaN ,
nInfinity = window . Infinity ,
2016-12-17 00:33:47 +01:00
// Functions
fWindow _btoa = window . btoa ,
2016-11-29 19:28:07 +01:00
fWindow _atob = window . atob ,
fWindow _parseInt = window . parseInt ,
fString _trim = ( function ( ) {
return cString . prototype . trim ? function ( sValue ) { return cString ( sValue ) . trim ( ) ; } : function ( sValue ) {
return cString ( sValue ) . replace ( /^\s+|\s+$/g , '' ) ;
} ;
} ) ( ) ,
fArray _indexOf = ( function ( ) {
return cArray . prototype . indexOf ? function ( aValue , vItem ) { return aValue . indexOf ( vItem ) ; } : function ( aValue , vItem ) {
for ( var nIndex = 0 , nLength = aValue . length ; nIndex < nLength ; nIndex ++ )
if ( aValue [ nIndex ] === vItem )
return nIndex ;
return - 1 ;
} ;
} ) ( ) ;
var sNS _XSD = "http://www.w3.org/2001/XMLSchema" ,
sNS _XPF = "http://www.w3.org/2005/xpath-functions" ,
sNS _XNS = "http://www.w3.org/2000/xmlns/" ,
sNS _XML = "http://www.w3.org/XML/1998/namespace" ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cException ( sCode
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) {
this . code = sCode ;
this . message =
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
oException _messages [ sCode ] ;
} ;
cException . prototype = new cError ;
2016-12-17 00:33:47 +01:00
// "http://www.w3.org/2005/xqt-errors"
2016-11-29 19:28:07 +01:00
var oException _messages = { } ;
oException _messages [ "XPDY0002" ] = "Evaluation of an expression relies on some part of the dynamic context that has not been assigned a value." ;
oException _messages [ "XPST0003" ] = "Expression is not a valid instance of the grammar" ;
oException _messages [ "XPTY0004" ] = "Type is not appropriate for the context in which the expression occurs" ;
oException _messages [ "XPST0008" ] = "Expression refers to an element name, attribute name, schema type name, namespace prefix, or variable name that is not defined in the static context" ;
oException _messages [ "XPST0010" ] = "Axis not supported" ;
oException _messages [ "XPST0017" ] = "Expanded QName and number of arguments in a function call do not match the name and arity of a function signature" ;
oException _messages [ "XPTY0018" ] = "The result of the last step in a path expression contains both nodes and atomic values" ;
oException _messages [ "XPTY0019" ] = "The result of a step (other than the last step) in a path expression contains an atomic value." ;
oException _messages [ "XPTY0020" ] = "In an axis step, the context item is not a node." ;
oException _messages [ "XPST0051" ] = "It is a static error if a QName that is used as an AtomicType in a SequenceType is not defined in the in-scope schema types as an atomic type." ;
oException _messages [ "XPST0081" ] = "A QName used in an expression contains a namespace prefix that cannot be expanded into a namespace URI by using the statically known namespaces." ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
oException _messages [ "FORG0001" ] = "Invalid value for cast/constructor." ;
oException _messages [ "FORG0003" ] = "fn:zero-or-one called with a sequence containing more than one item." ;
oException _messages [ "FORG0004" ] = "fn:one-or-more called with a sequence containing no items." ;
oException _messages [ "FORG0005" ] = "fn:exactly-one called with a sequence containing zero or more than one item." ;
oException _messages [ "FORG0006" ] = "Invalid argument type." ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
oException _messages [ "FODC0001" ] = "No context document." ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
oException _messages [ "FORX0001" ] = "Invalid regular expression flags." ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
oException _messages [ "FOCA0002" ] = "Invalid lexical value." ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
oException _messages [ "FOCH0002" ] = "Unsupported collation." ;
oException _messages [ "FONS0004" ] = "No namespace found for prefix." ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cLexer ( sValue ) {
2016-12-17 00:33:47 +01:00
var aMatch = sValue . match ( /\$?(?:(?![0-9-])(?:\w[\w.-]*|\*):)?(?![0-9-])(?:\w[\w.-]*|\*)|\(:|:\)|\/\/|\.\.|::|\d+(?:\.\d*)?(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?|"[^"]*(?:""[^"]*)*"|'[^']*(?:''[^']*)*'|<<|>>|[!<>]=|(?![0-9-])[\w-]+:\*|\s+|./g ) ;
2016-11-29 19:28:07 +01:00
if ( aMatch ) {
var nStack = 0 ;
for ( var nIndex = 0 , nLength = aMatch . length ; nIndex < nLength ; nIndex ++ )
if ( aMatch [ nIndex ] == '(:' )
nStack ++ ;
else
if ( aMatch [ nIndex ] == ':)' && nStack )
nStack -- ;
else
if ( ! nStack && ! /^\s/ . test ( aMatch [ nIndex ] ) )
this [ this . length ++ ] = aMatch [ nIndex ] ;
if ( nStack )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
} ;
cLexer . prototype . index = 0 ;
cLexer . prototype . length = 0 ;
cLexer . prototype . reset = function ( ) {
this . index = 0 ;
} ;
cLexer . prototype . peek = function ( nOffset ) {
return this [ this . index + ( nOffset || 0 ) ] || '' ;
} ;
cLexer . prototype . next = function ( nOffset ) {
return ( this . index += nOffset || 1 ) < this . length ;
} ;
cLexer . prototype . back = function ( nOffset ) {
return ( this . index -= nOffset || 1 ) > 0 ;
} ;
cLexer . prototype . eof = function ( ) {
return this . index >= this . length ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cDOMAdapter ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Custom members
2016-11-29 19:28:07 +01:00
cDOMAdapter . prototype . isNode = function ( oNode ) {
return oNode && ! ! oNode . nodeType ;
} ;
cDOMAdapter . prototype . getProperty = function ( oNode , sName ) {
return oNode [ sName ] ;
} ;
2016-12-17 00:33:47 +01:00
// Standard members
2016-11-29 19:28:07 +01:00
cDOMAdapter . prototype . isSameNode = function ( oNode , oNode2 ) {
return oNode == oNode2 ;
} ;
cDOMAdapter . prototype . compareDocumentPosition = function ( oNode , oNode2 ) {
return oNode . compareDocumentPosition ( oNode2 ) ;
} ;
cDOMAdapter . prototype . lookupNamespaceURI = function ( oNode , sPrefix ) {
return oNode . lookupNamespaceURI ( sPrefix ) ;
} ;
2016-12-17 00:33:47 +01:00
// Document object members
2016-11-29 19:28:07 +01:00
cDOMAdapter . prototype . getElementById = function ( oNode , sId ) {
return oNode . getElementById ( sId ) ;
} ;
2016-12-17 00:33:47 +01:00
// Element/Document object members
2016-11-29 19:28:07 +01:00
cDOMAdapter . prototype . getElementsByTagNameNS = function ( oNode , sNameSpaceURI , sLocalName ) {
return oNode . getElementsByTagNameNS ( sNameSpaceURI , sLocalName ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cDynamicContext ( oStaticContext , vItem , oScope , oDOMAdapter ) {
2016-12-17 00:33:47 +01:00
//
this . staticContext = oStaticContext ;
//
this . item = vItem ;
//
this . scope = oScope || { } ;
2016-11-29 19:28:07 +01:00
this . stack = { } ;
2016-12-17 00:33:47 +01:00
//
this . DOMAdapter = oDOMAdapter || new cDOMAdapter ;
//
var oDate = new cDate ,
2016-11-29 19:28:07 +01:00
nOffset = oDate . getTimezoneOffset ( ) ;
this . dateTime = new cXSDateTime ( oDate . getFullYear ( ) , oDate . getMonth ( ) + 1 , oDate . getDate ( ) , oDate . getHours ( ) , oDate . getMinutes ( ) , oDate . getSeconds ( ) + oDate . getMilliseconds ( ) / 1000 , - nOffset ) ;
this . timezone = new cXSDayTimeDuration ( 0 , cMath . abs ( ~ ~ ( nOffset / 60 ) ) , cMath . abs ( nOffset % 60 ) , 0 , nOffset > 0 ) ;
} ;
cDynamicContext . prototype . item = null ;
cDynamicContext . prototype . position = 0 ;
cDynamicContext . prototype . size = 0 ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cDynamicContext . prototype . scope = null ;
2016-12-17 00:33:47 +01:00
cDynamicContext . prototype . stack = null ; // Variables stack
//
cDynamicContext . prototype . dateTime = null ;
2016-11-29 19:28:07 +01:00
cDynamicContext . prototype . timezone = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cDynamicContext . prototype . staticContext = null ;
2016-12-17 00:33:47 +01:00
// Stack management
2016-11-29 19:28:07 +01:00
cDynamicContext . prototype . pushVariable = function ( sName , vValue ) {
if ( ! this . stack . hasOwnProperty ( sName ) )
this . stack [ sName ] = [ ] ;
this . stack [ sName ] . push ( this . scope [ sName ] ) ;
this . scope [ sName ] = vValue ;
} ;
cDynamicContext . prototype . popVariable = function ( sName ) {
if ( this . stack . hasOwnProperty ( sName ) ) {
this . scope [ sName ] = this . stack [ sName ] . pop ( ) ;
if ( ! this . stack [ sName ] . length ) {
delete this . stack [ sName ] ;
if ( typeof this . scope [ sName ] == "undefined" )
delete this . scope [ sName ] ;
}
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cStaticContext ( ) {
this . dataTypes = { } ;
this . documents = { } ;
this . functions = { } ;
this . collations = { } ;
this . collections = { } ;
} ;
cStaticContext . prototype . baseURI = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . dataTypes = null ;
cStaticContext . prototype . documents = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . functions = null ;
cStaticContext . prototype . defaultFunctionNamespace = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . collations = null ;
cStaticContext . prototype . defaultCollationName = sNS _XPF + "/collation/codepoint" ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . collections = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . namespaceResolver = null ;
cStaticContext . prototype . defaultElementNamespace = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var rStaticContext _uri = /^(?:\{([^\}]+)\})?(.+)$/ ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . setDataType = function ( sUri , fFunction ) {
var aMatch = sUri . match ( rStaticContext _uri ) ;
if ( aMatch )
if ( aMatch [ 1 ] != sNS _XSD )
this . dataTypes [ sUri ] = fFunction ;
} ;
cStaticContext . prototype . getDataType = function ( sUri ) {
var aMatch = sUri . match ( rStaticContext _uri ) ;
if ( aMatch )
2016-12-17 00:33:47 +01:00
return aMatch [ 1 ] == sNS _XSD ? hStaticContext _dataTypes [ aMatch [ 2 ] ] : this . dataTypes [ sUri ] ;
2016-11-29 19:28:07 +01:00
} ;
cStaticContext . prototype . setDocument = function ( sUri , fFunction ) {
this . documents [ sUri ] = fFunction ;
} ;
2016-12-17 00:33:47 +01:00
cStaticContext . prototype . getDocument = function ( sUri ) {
return this . documents [ sUri ] ;
} ;
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . setFunction = function ( sUri , fFunction ) {
var aMatch = sUri . match ( rStaticContext _uri ) ;
if ( aMatch )
if ( aMatch [ 1 ] != sNS _XPF )
this . functions [ sUri ] = fFunction ;
} ;
cStaticContext . prototype . getFunction = function ( sUri ) {
var aMatch = sUri . match ( rStaticContext _uri ) ;
if ( aMatch )
2016-12-17 00:33:47 +01:00
return aMatch [ 1 ] == sNS _XPF ? hStaticContext _functions [ aMatch [ 2 ] ] : this . functions [ sUri ] ;
2016-11-29 19:28:07 +01:00
} ;
cStaticContext . prototype . setCollation = function ( sUri , fFunction ) {
this . collations [ sUri ] = fFunction ;
} ;
cStaticContext . prototype . getCollation = function ( sUri ) {
return this . collations [ sUri ] ;
} ;
cStaticContext . prototype . setCollection = function ( sUri , fFunction ) {
this . collections [ sUri ] = fFunction ;
} ;
2016-12-17 00:33:47 +01:00
cStaticContext . prototype . getCollection = function ( sUri ) {
return this . collections [ sUri ] ;
} ;
2016-11-29 19:28:07 +01:00
cStaticContext . prototype . getURIForPrefix = function ( sPrefix ) {
var oResolver = this . namespaceResolver ,
fResolver = oResolver && oResolver . lookupNamespaceURI ? oResolver . lookupNamespaceURI : oResolver ,
sNameSpaceURI ;
if ( fResolver instanceof cFunction && ( sNameSpaceURI = fResolver . call ( oResolver , sPrefix ) ) )
return sNameSpaceURI ;
if ( sPrefix == 'fn' )
return sNS _XPF ;
if ( sPrefix == 'xs' )
return sNS _XSD ;
if ( sPrefix == "xml" )
return sNS _XML ;
if ( sPrefix == "xmlns" )
return sNS _XNS ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPST0081"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
// Static members
//Converts non-null JavaScript object to XML Schema object
2016-11-29 19:28:07 +01:00
cStaticContext . js2xs = function ( vItem ) {
2016-12-17 00:33:47 +01:00
// Convert types from JavaScript to XPath 2.0
if ( typeof vItem == "boolean" )
2016-11-29 19:28:07 +01:00
vItem = new cXSBoolean ( vItem ) ;
else
if ( typeof vItem == "number" )
vItem = ( fIsNaN ( vItem ) || ! fIsFinite ( vItem ) ) ? new cXSDouble ( vItem ) : fNumericLiteral _parseValue ( cString ( vItem ) ) ;
else
vItem = new cXSString ( cString ( vItem ) ) ;
2016-12-17 00:33:47 +01:00
//
return vItem ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
// Converts non-null XML Schema object to JavaScript object
2016-11-29 19:28:07 +01:00
cStaticContext . xs2js = function ( vItem ) {
if ( vItem instanceof cXSBoolean )
vItem = vItem . valueOf ( ) ;
else
if ( fXSAnyAtomicType _isNumeric ( vItem ) )
vItem = vItem . valueOf ( ) ;
else
vItem = vItem . toString ( ) ;
2016-12-17 00:33:47 +01:00
//
return vItem ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
// System functions with signatures, operators and types
2016-11-29 19:28:07 +01:00
var hStaticContext _functions = { } ,
hStaticContext _signatures = { } ,
hStaticContext _dataTypes = { } ,
hStaticContext _operators = { } ;
function fStaticContext _defineSystemFunction ( sName , aParameters , fFunction ) {
2016-12-17 00:33:47 +01:00
// Register function
hStaticContext _functions [ sName ] = fFunction ;
// Register signature
hStaticContext _signatures [ sName ] = aParameters ;
2016-11-29 19:28:07 +01:00
} ;
function fStaticContext _defineSystemDataType ( sName , fFunction ) {
2016-12-17 00:33:47 +01:00
// Register dataType
hStaticContext _dataTypes [ sName ] = fFunction ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cExpression ( sExpression , oStaticContext ) {
var oLexer = new cLexer ( sExpression ) ,
oExpr = fExpr _parse ( oLexer , oStaticContext ) ;
2016-12-17 00:33:47 +01:00
//
if ( ! oLexer . eof ( ) )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
if ( ! oExpr )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
this . internalExpression = oExpr ;
} ;
cExpression . prototype . internalExpression = null ;
2016-12-17 00:33:47 +01:00
// Public methods
2016-11-29 19:28:07 +01:00
cExpression . prototype . evaluate = function ( oContext ) {
return this . internalExpression . evaluate ( oContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cStringCollator ( ) {
} ;
cStringCollator . prototype . equals = function ( sValue1 , sValue2 ) {
throw "Not implemented" ;
} ;
cStringCollator . prototype . compare = function ( sValue1 , sValue2 ) {
throw "Not implemented" ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSConstants ( ) { } ;
2016-12-17 00:33:47 +01:00
// XML Schema 1.0 Datatypes
2016-11-29 19:28:07 +01:00
cXSConstants . ANYSIMPLETYPE _DT = 1 ;
cXSConstants . STRING _DT = 2 ;
cXSConstants . BOOLEAN _DT = 3 ;
cXSConstants . DECIMAL _DT = 4 ;
cXSConstants . FLOAT _DT = 5 ;
cXSConstants . DOUBLE _DT = 6 ;
cXSConstants . DURATION _DT = 7 ;
cXSConstants . DATETIME _DT = 8 ;
cXSConstants . TIME _DT = 9 ;
cXSConstants . DATE _DT = 10 ;
cXSConstants . GYEARMONTH _DT = 11 ;
cXSConstants . GYEAR _DT = 12 ;
cXSConstants . GMONTHDAY _DT = 13 ;
cXSConstants . GDAY _DT = 14 ;
cXSConstants . GMONTH _DT = 15 ;
cXSConstants . HEXBINARY _DT = 16 ;
cXSConstants . BASE64BINARY _DT = 17 ;
cXSConstants . ANYURI _DT = 18 ;
cXSConstants . QNAME _DT = 19 ;
cXSConstants . NOTATION _DT = 20 ;
cXSConstants . NORMALIZEDSTRING _DT = 21 ;
cXSConstants . TOKEN _DT = 22 ;
cXSConstants . LANGUAGE _DT = 23 ;
cXSConstants . NMTOKEN _DT = 24 ;
cXSConstants . NAME _DT = 25 ;
cXSConstants . NCNAME _DT = 26 ;
cXSConstants . ID _DT = 27 ;
cXSConstants . IDREF _DT = 28 ;
cXSConstants . ENTITY _DT = 29 ;
cXSConstants . INTEGER _DT = 30 ;
cXSConstants . NONPOSITIVEINTEGER _DT = 31 ;
cXSConstants . NEGATIVEINTEGER _DT = 32 ;
cXSConstants . LONG _DT = 33 ;
cXSConstants . INT _DT = 34 ;
cXSConstants . SHORT _DT = 35 ;
cXSConstants . BYTE _DT = 36 ;
cXSConstants . NONNEGATIVEINTEGER _DT = 37 ;
cXSConstants . UNSIGNEDLONG _DT = 38 ;
cXSConstants . UNSIGNEDINT _DT = 39 ;
cXSConstants . UNSIGNEDSHORT _DT = 40 ;
cXSConstants . UNSIGNEDBYTE _DT = 41 ;
cXSConstants . POSITIVEINTEGER _DT = 42 ;
cXSConstants . LISTOFUNION _DT = 43 ;
cXSConstants . LIST _DT = 44 ;
cXSConstants . UNAVAILABLE _DT = 45 ;
2016-12-17 00:33:47 +01:00
// XML Schema 1.1 Datatypes
2016-11-29 19:28:07 +01:00
cXSConstants . DATETIMESTAMP _DT = 46 ;
cXSConstants . DAYMONTHDURATION _DT = 47 ;
cXSConstants . DAYTIMEDURATION _DT = 48 ;
cXSConstants . PRECISIONDECIMAL _DT = 49 ;
cXSConstants . ANYATOMICTYPE _DT = 50 ;
cXSConstants . ANYTYPE _DT = 51 ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
cXSConstants . XT _YEARMONTHDURATION _DT = - 1 ;
cXSConstants . XT _UNTYPEDATOMIC _DT = - 2 ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cExpr ( ) {
this . items = [ ] ;
} ;
cExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fExpr _parse ( oLexer , oStaticContext ) {
var oItem ;
if ( oLexer . eof ( ) || ! ( oItem = fExprSingle _parse ( oLexer , oStaticContext ) ) )
return ;
2016-12-17 00:33:47 +01:00
// Create expression
var oExpr = new cExpr ;
2016-11-29 19:28:07 +01:00
oExpr . items . push ( oItem ) ;
while ( oLexer . peek ( ) == ',' ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oItem = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oExpr . items . push ( oItem ) ;
}
return oExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cExpr . prototype . evaluate = function ( oContext ) {
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = this . items . length ; nIndex < nLength ; nIndex ++ )
oSequence = hStaticContext _operators [ "concatenate" ] . call ( oContext , oSequence , this . items [ nIndex ] . evaluate ( oContext ) ) ;
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cExprSingle ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fExprSingle _parse ( oLexer , oStaticContext ) {
if ( ! oLexer . eof ( ) )
return fIfExpr _parse ( oLexer , oStaticContext )
|| fForExpr _parse ( oLexer , oStaticContext )
|| fQuantifiedExpr _parse ( oLexer , oStaticContext )
|| fOrExpr _parse ( oLexer , oStaticContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cForExpr ( ) {
this . bindings = [ ] ;
this . returnExpr = null ;
} ;
cForExpr . prototype . bindings = null ;
cForExpr . prototype . returnExpr = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fForExpr _parse ( oLexer , oStaticContext ) {
if ( oLexer . peek ( ) == "for" && oLexer . peek ( 1 ) . substr ( 0 , 1 ) == '$' ) {
oLexer . next ( ) ;
var oForExpr = new cForExpr ,
oExpr ;
do {
oForExpr . bindings . push ( fSimpleForBinding _parse ( oLexer , oStaticContext ) ) ;
}
while ( oLexer . peek ( ) == ',' && oLexer . next ( ) ) ;
if ( oLexer . peek ( ) != "return" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oForExpr . returnExpr = oExpr ;
return oForExpr ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
// for $x in X, $y in Y, $z in Z return $x + $y + $z
// for $x in X return for $y in Y return for $z in Z return $x + $y + $z
2016-11-29 19:28:07 +01:00
cForExpr . prototype . evaluate = function ( oContext ) {
var oSequence = [ ] ;
( function ( oSelf , nBinding ) {
var oBinding = oSelf . bindings [ nBinding ++ ] ,
oSequence1 = oBinding . inExpr . evaluate ( oContext ) ,
sUri = ( oBinding . namespaceURI ? '{' + oBinding . namespaceURI + '}' : '' ) + oBinding . localName ;
for ( var nIndex = 0 , nLength = oSequence1 . length ; nIndex < nLength ; nIndex ++ ) {
oContext . pushVariable ( sUri , oSequence1 [ nIndex ] ) ;
if ( nBinding < oSelf . bindings . length )
arguments . callee ( oSelf , nBinding ) ;
else
oSequence = oSequence . concat ( oSelf . returnExpr . evaluate ( oContext ) ) ;
oContext . popVariable ( sUri ) ;
}
} ) ( this , 0 ) ;
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function cSimpleForBinding ( sPrefix , sLocalName , sNameSpaceURI , oInExpr ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
this . inExpr = oInExpr ;
} ;
cSimpleForBinding . prototype . prefix = null ;
cSimpleForBinding . prototype . localName = null ;
cSimpleForBinding . prototype . namespaceURI = null ;
cSimpleForBinding . prototype . inExpr = null ;
function fSimpleForBinding _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . substr ( 1 ) . match ( rNameTest ) ;
if ( ! aMatch )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
if ( aMatch [ 1 ] == '*' || aMatch [ 2 ] == '*' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . peek ( ) != "in" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cSimpleForBinding ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) : null , oExpr ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cIfExpr ( oCondExpr , oThenExpr , oElseExpr ) {
this . condExpr = oCondExpr ;
this . thenExpr = oThenExpr ;
this . elseExpr = oElseExpr ;
} ;
cIfExpr . prototype . condExpr = null ;
cIfExpr . prototype . thenExpr = null ;
cIfExpr . prototype . elseExpr = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fIfExpr _parse ( oLexer , oStaticContext ) {
var oCondExpr ,
oThenExpr ,
oElseExpr ;
if ( oLexer . peek ( ) == "if" && oLexer . peek ( 1 ) == '(' ) {
oLexer . next ( 2 ) ;
2016-12-17 00:33:47 +01:00
//
if ( oLexer . eof ( ) || ! ( oCondExpr = fExpr _parse ( oLexer , oStaticContext ) ) )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
if ( oLexer . peek ( ) != ')' )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . peek ( ) != "then" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oThenExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
if ( oLexer . peek ( ) != "else" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oElseExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
return new cIfExpr ( oCondExpr , oThenExpr , oElseExpr ) ;
2016-11-29 19:28:07 +01:00
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cIfExpr . prototype . evaluate = function ( oContext ) {
return this [ fFunction _sequence _toEBV ( this . condExpr . evaluate ( oContext ) , oContext ) ? "thenExpr" : "elseExpr" ] . evaluate ( oContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cQuantifiedExpr ( sQuantifier ) {
this . quantifier = sQuantifier ;
this . bindings = [ ] ;
this . satisfiesExpr = null ;
} ;
cQuantifiedExpr . prototype . bindings = null ;
cQuantifiedExpr . prototype . quantifier = null ;
cQuantifiedExpr . prototype . satisfiesExpr = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fQuantifiedExpr _parse ( oLexer , oStaticContext ) {
var sQuantifier = oLexer . peek ( ) ;
if ( ( sQuantifier == "some" || sQuantifier == "every" ) && oLexer . peek ( 1 ) . substr ( 0 , 1 ) == '$' ) {
oLexer . next ( ) ;
var oQuantifiedExpr = new cQuantifiedExpr ( sQuantifier ) ,
oExpr ;
do {
oQuantifiedExpr . bindings . push ( fSimpleQuantifiedBinding _parse ( oLexer , oStaticContext ) ) ;
}
while ( oLexer . peek ( ) == ',' && oLexer . next ( ) ) ;
if ( oLexer . peek ( ) != "satisfies" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oQuantifiedExpr . satisfiesExpr = oExpr ;
return oQuantifiedExpr ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cQuantifiedExpr . prototype . evaluate = function ( oContext ) {
2016-12-17 00:33:47 +01:00
// TODO: re-factor
var bEvery = this . quantifier == "every" ,
2016-11-29 19:28:07 +01:00
bResult = bEvery ? true : false ;
( function ( oSelf , nBinding ) {
var oBinding = oSelf . bindings [ nBinding ++ ] ,
oSequence1 = oBinding . inExpr . evaluate ( oContext ) ,
sUri = ( oBinding . namespaceURI ? '{' + oBinding . namespaceURI + '}' : '' ) + oBinding . localName ;
for ( var nIndex = 0 , nLength = oSequence1 . length ; ( nIndex < nLength ) && ( bEvery ? bResult : ! bResult ) ; nIndex ++ ) {
oContext . pushVariable ( sUri , oSequence1 [ nIndex ] ) ;
if ( nBinding < oSelf . bindings . length )
arguments . callee ( oSelf , nBinding ) ;
else
bResult = fFunction _sequence _toEBV ( oSelf . satisfiesExpr . evaluate ( oContext ) , oContext ) ;
oContext . popVariable ( sUri ) ;
}
} ) ( this , 0 ) ;
return [ new cXSBoolean ( bResult ) ] ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function cSimpleQuantifiedBinding ( sPrefix , sLocalName , sNameSpaceURI , oInExpr ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
this . inExpr = oInExpr ;
} ;
cSimpleQuantifiedBinding . prototype . prefix = null ;
cSimpleQuantifiedBinding . prototype . localName = null ;
cSimpleQuantifiedBinding . prototype . namespaceURI = null ;
cSimpleQuantifiedBinding . prototype . inExpr = null ;
function fSimpleQuantifiedBinding _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . substr ( 1 ) . match ( rNameTest ) ;
if ( ! aMatch )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
if ( aMatch [ 1 ] == '*' || aMatch [ 2 ] == '*' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
if ( oLexer . peek ( ) != "in" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cSimpleQuantifiedBinding ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) : null , oExpr ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cComparisonExpr ( oLeft , oRight , sOperator ) {
this . left = oLeft ;
this . right = oRight ;
this . operator = sOperator ;
} ;
cComparisonExpr . prototype . left = null ;
cComparisonExpr . prototype . right = null ;
cComparisonExpr . prototype . operator = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fComparisonExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oRight ;
if ( oLexer . eof ( ) || ! ( oExpr = fRangeExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) in hComparisonExpr _operators ) )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Comparison expression
var sOperator = oLexer . peek ( ) ;
2016-11-29 19:28:07 +01:00
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oRight = fRangeExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cComparisonExpr ( oExpr , oRight , sOperator ) ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cComparisonExpr . prototype . evaluate = function ( oContext ) {
var oResult = hComparisonExpr _operators [ this . operator ] ( this , oContext ) ;
return oResult == null ? [ ] : [ oResult ] ;
} ;
2016-12-17 00:33:47 +01:00
// General comparison
2016-11-29 19:28:07 +01:00
function fComparisonExpr _GeneralComp ( oExpr , oContext ) {
var oLeft = fFunction _sequence _atomize ( oExpr . left . evaluate ( oContext ) , oContext ) ;
if ( ! oLeft . length )
return new cXSBoolean ( false ) ;
var oRight = fFunction _sequence _atomize ( oExpr . right . evaluate ( oContext ) , oContext ) ;
if ( ! oRight . length )
return new cXSBoolean ( false ) ;
var bResult = false ;
for ( var nLeftIndex = 0 , nLeftLength = oLeft . length , bLeft , vLeft ; ( nLeftIndex < nLeftLength ) && ! bResult ; nLeftIndex ++ ) {
for ( var nRightIndex = 0 , nRightLength = oRight . length , bRight , vRight ; ( nRightIndex < nRightLength ) && ! bResult ; nRightIndex ++ ) {
vLeft = oLeft [ nLeftIndex ] ;
vRight = oRight [ nRightIndex ] ;
bLeft = vLeft instanceof cXSUntypedAtomic ;
bRight = vRight instanceof cXSUntypedAtomic ;
if ( bLeft && bRight ) {
2016-12-17 00:33:47 +01:00
// cast xs:untypedAtomic to xs:string
vLeft = cXSString . cast ( vLeft ) ;
2016-11-29 19:28:07 +01:00
vRight = cXSString . cast ( vRight ) ;
}
else {
2016-12-17 00:33:47 +01:00
//
if ( bLeft ) {
// Special: durations
if ( vRight instanceof cXSDayTimeDuration )
2016-11-29 19:28:07 +01:00
vLeft = cXSDayTimeDuration . cast ( vLeft ) ;
else
if ( vRight instanceof cXSYearMonthDuration )
vLeft = cXSYearMonthDuration . cast ( vLeft ) ;
else
2016-12-17 00:33:47 +01:00
//
if ( vRight . primitiveKind )
2016-11-29 19:28:07 +01:00
vLeft = hStaticContext _dataTypes [ vRight . primitiveKind ] . cast ( vLeft ) ;
}
else
if ( bRight ) {
2016-12-17 00:33:47 +01:00
// Special: durations
if ( vLeft instanceof cXSDayTimeDuration )
2016-11-29 19:28:07 +01:00
vRight = cXSDayTimeDuration . cast ( vRight ) ;
else
if ( vLeft instanceof cXSYearMonthDuration )
vRight = cXSYearMonthDuration . cast ( vRight ) ;
else
2016-12-17 00:33:47 +01:00
//
if ( vLeft . primitiveKind )
2016-11-29 19:28:07 +01:00
vRight = hStaticContext _dataTypes [ vLeft . primitiveKind ] . cast ( vRight ) ;
}
2016-12-17 00:33:47 +01:00
// cast xs:anyURI to xs:string
if ( vLeft instanceof cXSAnyURI )
2016-11-29 19:28:07 +01:00
vLeft = cXSString . cast ( vLeft ) ;
if ( vRight instanceof cXSAnyURI )
vRight = cXSString . cast ( vRight ) ;
}
bResult = hComparisonExpr _ValueComp _operators [ hComparisonExpr _GeneralComp _map [ oExpr . operator ] ] ( vLeft , vRight , oContext ) . valueOf ( ) ;
}
}
return new cXSBoolean ( bResult ) ;
} ;
var hComparisonExpr _GeneralComp _map = {
'=' : 'eq' ,
'!=' : 'ne' ,
'>' : 'gt' ,
'<' : 'lt' ,
'>=' : 'ge' ,
'<=' : 'le'
} ;
2016-12-17 00:33:47 +01:00
// Value comparison
2016-11-29 19:28:07 +01:00
function fComparisonExpr _ValueComp ( oExpr , oContext ) {
var oLeft = fFunction _sequence _atomize ( oExpr . left . evaluate ( oContext ) , oContext ) ;
if ( ! oLeft . length )
return null ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oLeft , '?'
2016-11-29 19:28:07 +01:00
) ;
var oRight = fFunction _sequence _atomize ( oExpr . right . evaluate ( oContext ) , oContext ) ;
if ( ! oRight . length )
return null ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-11-29 19:28:07 +01:00
) ;
var vLeft = oLeft [ 0 ] ,
vRight = oRight [ 0 ] ;
2016-12-17 00:33:47 +01:00
// cast xs:untypedAtomic to xs:string
if ( vLeft instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vLeft = cXSString . cast ( vLeft ) ;
if ( vRight instanceof cXSUntypedAtomic )
vRight = cXSString . cast ( vRight ) ;
2016-12-17 00:33:47 +01:00
// cast xs:anyURI to xs:string
if ( vLeft instanceof cXSAnyURI )
2016-11-29 19:28:07 +01:00
vLeft = cXSString . cast ( vLeft ) ;
if ( vRight instanceof cXSAnyURI )
vRight = cXSString . cast ( vRight ) ;
2016-12-17 00:33:47 +01:00
//
return hComparisonExpr _ValueComp _operators [ oExpr . operator ] ( vLeft , vRight , oContext ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var hComparisonExpr _ValueComp _operators = { } ;
hComparisonExpr _ValueComp _operators [ 'eq' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-equal" ;
}
else
if ( oLeft instanceof cXSBoolean ) {
if ( oRight instanceof cXSBoolean )
sOperator = "boolean-equal" ;
}
else
if ( oLeft instanceof cXSString ) {
if ( oRight instanceof cXSString )
return hStaticContext _operators [ "numeric-equal" ] . call ( oContext , hStaticContext _functions [ "compare" ] . call ( oContext , oLeft , oRight ) , new cXSInteger ( 0 ) ) ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "date-equal" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "time-equal" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "dateTime-equal" ;
}
else
if ( oLeft instanceof cXSDuration ) {
if ( oRight instanceof cXSDuration )
sOperator = "duration-equal" ;
}
else
if ( oLeft instanceof cXSGYearMonth ) {
if ( oRight instanceof cXSGYearMonth )
sOperator = "gYearMonth-equal" ;
}
else
if ( oLeft instanceof cXSGYear ) {
if ( oRight instanceof cXSGYear )
sOperator = "gYear-equal" ;
}
else
if ( oLeft instanceof cXSGMonthDay ) {
if ( oRight instanceof cXSGMonthDay )
sOperator = "gMonthDay-equal" ;
}
else
if ( oLeft instanceof cXSGMonth ) {
if ( oRight instanceof cXSGMonth )
sOperator = "gMonth-equal" ;
}
else
if ( oLeft instanceof cXSGDay ) {
if ( oRight instanceof cXSGDay )
sOperator = "gDay-equal" ;
}
2016-12-17 00:33:47 +01:00
// skipped: xs:anyURI (covered by xs:string)
else
2016-11-29 19:28:07 +01:00
if ( oLeft instanceof cXSQName ) {
if ( oRight instanceof cXSQName )
sOperator = "QName-equal" ;
}
else
if ( oLeft instanceof cXSHexBinary ) {
if ( oRight instanceof cXSHexBinary )
sOperator = "hexBinary-equal" ;
}
else
if ( oLeft instanceof cXSBase64Binary ) {
if ( oRight instanceof cXSBase64Binary )
sOperator = "base64Binary-equal" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
// skipped: xs:NOTATION
throw new cException ( "XPTY0004"
) ; // Cannot compare {type1} to {type2}
} ;
2016-11-29 19:28:07 +01:00
hComparisonExpr _ValueComp _operators [ 'ne' ] = function ( oLeft , oRight , oContext ) {
return new cXSBoolean ( ! hComparisonExpr _ValueComp _operators [ 'eq' ] ( oLeft , oRight , oContext ) . valueOf ( ) ) ;
} ;
hComparisonExpr _ValueComp _operators [ 'gt' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-greater-than" ;
}
else
if ( oLeft instanceof cXSBoolean ) {
if ( oRight instanceof cXSBoolean )
sOperator = "boolean-greater-than" ;
}
else
if ( oLeft instanceof cXSString ) {
if ( oRight instanceof cXSString )
return hStaticContext _operators [ "numeric-greater-than" ] . call ( oContext , hStaticContext _functions [ "compare" ] . call ( oContext , oLeft , oRight ) , new cXSInteger ( 0 ) ) ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "date-greater-than" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "time-greater-than" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "dateTime-greater-than" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "yearMonthDuration-greater-than" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "dayTimeDuration-greater-than" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
// skipped: xs:anyURI (covered by xs:string)
throw new cException ( "XPTY0004"
) ; // Cannot compare {type1} to {type2}
} ;
2016-11-29 19:28:07 +01:00
hComparisonExpr _ValueComp _operators [ 'lt' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-less-than" ;
}
else
if ( oLeft instanceof cXSBoolean ) {
if ( oRight instanceof cXSBoolean )
sOperator = "boolean-less-than" ;
}
else
if ( oLeft instanceof cXSString ) {
if ( oRight instanceof cXSString )
return hStaticContext _operators [ "numeric-less-than" ] . call ( oContext , hStaticContext _functions [ "compare" ] . call ( oContext , oLeft , oRight ) , new cXSInteger ( 0 ) ) ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "date-less-than" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "time-less-than" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "dateTime-less-than" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "yearMonthDuration-less-than" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "dayTimeDuration-less-than" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
// skipped: xs:anyURI (covered by xs:string)
throw new cException ( "XPTY0004"
) ; // Cannot compare {type1} to {type2}
} ;
2016-11-29 19:28:07 +01:00
hComparisonExpr _ValueComp _operators [ 'ge' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-less-than" ;
}
else
if ( oLeft instanceof cXSBoolean ) {
if ( oRight instanceof cXSBoolean )
sOperator = "boolean-less-than" ;
}
else
if ( oLeft instanceof cXSString ) {
if ( oRight instanceof cXSString )
return hStaticContext _operators [ "numeric-greater-than" ] . call ( oContext , hStaticContext _functions [ "compare" ] . call ( oContext , oLeft , oRight ) , new cXSInteger ( - 1 ) ) ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "date-less-than" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "time-less-than" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "dateTime-less-than" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "yearMonthDuration-less-than" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "dayTimeDuration-less-than" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return new cXSBoolean ( ! hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) . valueOf ( ) ) ;
2016-12-17 00:33:47 +01:00
// skipped: xs:anyURI (covered by xs:string)
throw new cException ( "XPTY0004"
) ; // Cannot compare {type1} to {type2}
} ;
2016-11-29 19:28:07 +01:00
hComparisonExpr _ValueComp _operators [ 'le' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-greater-than" ;
}
else
if ( oLeft instanceof cXSBoolean ) {
if ( oRight instanceof cXSBoolean )
sOperator = "boolean-greater-than" ;
}
else
if ( oLeft instanceof cXSString ) {
if ( oRight instanceof cXSString )
return hStaticContext _operators [ "numeric-less-than" ] . call ( oContext , hStaticContext _functions [ "compare" ] . call ( oContext , oLeft , oRight ) , new cXSInteger ( 1 ) ) ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "date-greater-than" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "time-greater-than" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "dateTime-greater-than" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "yearMonthDuration-greater-than" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "dayTimeDuration-greater-than" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return new cXSBoolean ( ! hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) . valueOf ( ) ) ;
2016-12-17 00:33:47 +01:00
// skipped: xs:anyURI (covered by xs:string)
throw new cException ( "XPTY0004"
) ; // Cannot compare {type1} to {type2}
} ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// Node comparison
2016-11-29 19:28:07 +01:00
function fComparisonExpr _NodeComp ( oExpr , oContext ) {
var oLeft = oExpr . left . evaluate ( oContext ) ;
if ( ! oLeft . length )
return null ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oLeft , '?'
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Assert item type
fFunctionCall _assertSequenceItemType ( oContext , oLeft , cXTNode
2016-11-29 19:28:07 +01:00
) ;
var oRight = oExpr . right . evaluate ( oContext ) ;
if ( ! oRight . length )
return null ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Assert item type
fFunctionCall _assertSequenceItemType ( oContext , oRight , cXTNode
2016-11-29 19:28:07 +01:00
) ;
return hComparisonExpr _NodeComp _operators [ oExpr . operator ] ( oLeft [ 0 ] , oRight [ 0 ] , oContext ) ;
} ;
var hComparisonExpr _NodeComp _operators = { } ;
hComparisonExpr _NodeComp _operators [ 'is' ] = function ( oLeft , oRight , oContext ) {
return hStaticContext _operators [ "is-same-node" ] . call ( oContext , oLeft , oRight ) ;
} ;
hComparisonExpr _NodeComp _operators [ '>>' ] = function ( oLeft , oRight , oContext ) {
return hStaticContext _operators [ "node-after" ] . call ( oContext , oLeft , oRight ) ;
} ;
hComparisonExpr _NodeComp _operators [ '<<' ] = function ( oLeft , oRight , oContext ) {
return hStaticContext _operators [ "node-before" ] . call ( oContext , oLeft , oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// Operators
2016-11-29 19:28:07 +01:00
var hComparisonExpr _operators = {
2016-12-17 00:33:47 +01:00
// GeneralComp
'=' : fComparisonExpr _GeneralComp ,
2016-11-29 19:28:07 +01:00
'!=' : fComparisonExpr _GeneralComp ,
'<' : fComparisonExpr _GeneralComp ,
'<=' : fComparisonExpr _GeneralComp ,
'>' : fComparisonExpr _GeneralComp ,
'>=' : fComparisonExpr _GeneralComp ,
2016-12-17 00:33:47 +01:00
// ValueComp
'eq' : fComparisonExpr _ValueComp ,
2016-11-29 19:28:07 +01:00
'ne' : fComparisonExpr _ValueComp ,
'lt' : fComparisonExpr _ValueComp ,
'le' : fComparisonExpr _ValueComp ,
'gt' : fComparisonExpr _ValueComp ,
'ge' : fComparisonExpr _ValueComp ,
2016-12-17 00:33:47 +01:00
// NodeComp
'is' : fComparisonExpr _NodeComp ,
2016-11-29 19:28:07 +01:00
'>>' : fComparisonExpr _NodeComp ,
'<<' : fComparisonExpr _NodeComp
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cAdditiveExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cAdditiveExpr . prototype . left = null ;
cAdditiveExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var hAdditiveExpr _operators = { } ;
hAdditiveExpr _operators [ '+' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ,
bReverse = false ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-add" ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "add-yearMonthDuration-to-date" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "add-dayTimeDuration-to-date" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSDate ) {
sOperator = "add-yearMonthDuration-to-date" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSDateTime ) {
sOperator = "add-yearMonthDuration-to-dateTime" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "add-yearMonthDurations" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDate ) {
sOperator = "add-dayTimeDuration-to-date" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSTime ) {
sOperator = "add-dayTimeDuration-to-time" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSDateTime ) {
sOperator = "add-dayTimeDuration-to-dateTime" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "add-dayTimeDurations" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "add-dayTimeDuration-to-time" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "add-yearMonthDuration-to-dateTime" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "add-dayTimeDuration-to-dateTime" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , bReverse ? oRight : oLeft , bReverse ? oLeft : oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
hAdditiveExpr _operators [ '-' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-subtract" ;
}
else
if ( oLeft instanceof cXSDate ) {
if ( oRight instanceof cXSDate )
sOperator = "subtract-dates" ;
else
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "subtract-yearMonthDuration-from-date" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "subtract-dayTimeDuration-from-date" ;
}
else
if ( oLeft instanceof cXSTime ) {
if ( oRight instanceof cXSTime )
sOperator = "subtract-times" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "subtract-dayTimeDuration-from-time" ;
}
else
if ( oLeft instanceof cXSDateTime ) {
if ( oRight instanceof cXSDateTime )
sOperator = "subtract-dateTimes" ;
else
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "subtract-yearMonthDuration-from-dateTime" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "subtract-dayTimeDuration-from-dateTime" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "subtract-yearMonthDurations" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "subtract-dayTimeDurations" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fAdditiveExpr _parse ( oLexer , oStaticContext ) {
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fMultiplicativeExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) in hAdditiveExpr _operators ) )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Additive expression
var oAdditiveExpr = new cAdditiveExpr ( oExpr ) ,
2016-11-29 19:28:07 +01:00
sOperator ;
while ( ( sOperator = oLexer . peek ( ) ) in hAdditiveExpr _operators ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fMultiplicativeExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oAdditiveExpr . items . push ( [ sOperator , oExpr ] ) ;
}
return oAdditiveExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cAdditiveExpr . prototype . evaluate = function ( oContext ) {
var oLeft = fFunction _sequence _atomize ( this . left . evaluate ( oContext ) , oContext ) ;
if ( ! oLeft . length )
return [ ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oLeft , '?'
2016-11-29 19:28:07 +01:00
) ;
var vLeft = oLeft [ 0 ] ;
if ( vLeft instanceof cXSUntypedAtomic )
2016-12-17 00:33:47 +01:00
vLeft = cXSDouble . cast ( vLeft ) ; // cast to xs:double
2016-11-29 19:28:07 +01:00
for ( var nIndex = 0 , nLength = this . items . length , oRight , vRight ; nIndex < nLength ; nIndex ++ ) {
oRight = fFunction _sequence _atomize ( this . items [ nIndex ] [ 1 ] . evaluate ( oContext ) , oContext ) ;
if ( ! oRight . length )
return [ ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-11-29 19:28:07 +01:00
) ;
vRight = oRight [ 0 ] ;
if ( vRight instanceof cXSUntypedAtomic )
2016-12-17 00:33:47 +01:00
vRight = cXSDouble . cast ( vRight ) ; // cast to xs:double
2016-11-29 19:28:07 +01:00
vLeft = hAdditiveExpr _operators [ this . items [ nIndex ] [ 0 ] ] ( vLeft , vRight , oContext ) ;
}
return [ vLeft ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cMultiplicativeExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cMultiplicativeExpr . prototype . left = null ;
cMultiplicativeExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var hMultiplicativeExpr _operators = { } ;
hMultiplicativeExpr _operators [ '*' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ,
bReverse = false ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-multiply" ;
else
if ( oRight instanceof cXSYearMonthDuration ) {
sOperator = "multiply-yearMonthDuration" ;
bReverse = true ;
}
else
if ( oRight instanceof cXSDayTimeDuration ) {
sOperator = "multiply-dayTimeDuration" ;
bReverse = true ;
}
}
else {
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "multiply-yearMonthDuration" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "multiply-dayTimeDuration" ;
}
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , bReverse ? oRight : oLeft , bReverse ? oLeft : oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
hMultiplicativeExpr _operators [ 'div' ] = function ( oLeft , oRight , oContext ) {
var sOperator = '' ;
if ( fXSAnyAtomicType _isNumeric ( oLeft ) ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "numeric-divide" ;
}
else
if ( oLeft instanceof cXSYearMonthDuration ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "divide-yearMonthDuration" ;
else
if ( oRight instanceof cXSYearMonthDuration )
sOperator = "divide-yearMonthDuration-by-yearMonthDuration" ;
}
else
if ( oLeft instanceof cXSDayTimeDuration ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
sOperator = "divide-dayTimeDuration" ;
else
if ( oRight instanceof cXSDayTimeDuration )
sOperator = "divide-dayTimeDuration-by-dayTimeDuration" ;
}
2016-12-17 00:33:47 +01:00
// Call operator function
if ( sOperator )
2016-11-29 19:28:07 +01:00
return hStaticContext _operators [ sOperator ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
hMultiplicativeExpr _operators [ 'idiv' ] = function ( oLeft , oRight , oContext ) {
if ( fXSAnyAtomicType _isNumeric ( oLeft ) && fXSAnyAtomicType _isNumeric ( oRight ) )
return hStaticContext _operators [ "numeric-integer-divide" ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
hMultiplicativeExpr _operators [ 'mod' ] = function ( oLeft , oRight , oContext ) {
if ( fXSAnyAtomicType _isNumeric ( oLeft ) && fXSAnyAtomicType _isNumeric ( oRight ) )
return hStaticContext _operators [ "numeric-mod" ] . call ( oContext , oLeft , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fMultiplicativeExpr _parse ( oLexer , oStaticContext ) {
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fUnionExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) in hMultiplicativeExpr _operators ) )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Additive expression
var oMultiplicativeExpr = new cMultiplicativeExpr ( oExpr ) ,
2016-11-29 19:28:07 +01:00
sOperator ;
while ( ( sOperator = oLexer . peek ( ) ) in hMultiplicativeExpr _operators ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fUnionExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oMultiplicativeExpr . items . push ( [ sOperator , oExpr ] ) ;
}
return oMultiplicativeExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cMultiplicativeExpr . prototype . evaluate = function ( oContext ) {
var oLeft = fFunction _sequence _atomize ( this . left . evaluate ( oContext ) , oContext ) ;
2016-12-17 00:33:47 +01:00
//
if ( ! oLeft . length )
2016-11-29 19:28:07 +01:00
return [ ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oLeft , '?'
2016-11-29 19:28:07 +01:00
) ;
var vLeft = oLeft [ 0 ] ;
if ( vLeft instanceof cXSUntypedAtomic )
2016-12-17 00:33:47 +01:00
vLeft = cXSDouble . cast ( vLeft ) ; // cast to xs:double
2016-11-29 19:28:07 +01:00
for ( var nIndex = 0 , nLength = this . items . length , oRight , vRight ; nIndex < nLength ; nIndex ++ ) {
oRight = fFunction _sequence _atomize ( this . items [ nIndex ] [ 1 ] . evaluate ( oContext ) , oContext ) ;
if ( ! oRight . length )
return [ ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-11-29 19:28:07 +01:00
) ;
vRight = oRight [ 0 ] ;
if ( vRight instanceof cXSUntypedAtomic )
2016-12-17 00:33:47 +01:00
vRight = cXSDouble . cast ( vRight ) ; // cast to xs:double
2016-11-29 19:28:07 +01:00
vLeft = hMultiplicativeExpr _operators [ this . items [ nIndex ] [ 0 ] ] ( vLeft , vRight , oContext ) ;
}
return [ vLeft ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cUnaryExpr ( sOperator , oExpr ) {
this . operator = sOperator ;
this . expression = oExpr ;
} ;
cUnaryExpr . prototype . operator = null ;
cUnaryExpr . prototype . expression = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var hUnaryExpr _operators = { } ;
hUnaryExpr _operators [ '-' ] = function ( oRight , oContext ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
return hStaticContext _operators [ "numeric-unary-minus" ] . call ( oContext , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
2016-11-29 19:28:07 +01:00
hUnaryExpr _operators [ '+' ] = function ( oRight , oContext ) {
if ( fXSAnyAtomicType _isNumeric ( oRight ) )
return hStaticContext _operators [ "numeric-unary-plus" ] . call ( oContext , oRight ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
) ; // Arithmetic operator is not defined for arguments of types ({type1}, {type2})
} ;
// Static members
// UnaryExpr := ("-" | "+")* ValueExpr
2016-11-29 19:28:07 +01:00
function fUnaryExpr _parse ( oLexer , oStaticContext ) {
if ( oLexer . eof ( ) )
return ;
if ( ! ( oLexer . peek ( ) in hUnaryExpr _operators ) )
return fValueExpr _parse ( oLexer , oStaticContext ) ;
2016-12-17 00:33:47 +01:00
// Unary expression
var sOperator = '+' ,
2016-11-29 19:28:07 +01:00
oExpr ;
while ( oLexer . peek ( ) in hUnaryExpr _operators ) {
if ( oLexer . peek ( ) == '-' )
sOperator = sOperator == '-' ? '+' : '-' ;
oLexer . next ( ) ;
}
if ( oLexer . eof ( ) || ! ( oExpr = fValueExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cUnaryExpr ( sOperator , oExpr ) ;
} ;
cUnaryExpr . prototype . evaluate = function ( oContext ) {
var oRight = fFunction _sequence _atomize ( this . expression . evaluate ( oContext ) , oContext ) ;
2016-12-17 00:33:47 +01:00
//
if ( ! oRight . length )
2016-11-29 19:28:07 +01:00
return [ ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-11-29 19:28:07 +01:00
) ;
var vRight = oRight [ 0 ] ;
if ( vRight instanceof cXSUntypedAtomic )
2016-12-17 00:33:47 +01:00
vRight = cXSDouble . cast ( vRight ) ; // cast to xs:double
2016-11-29 19:28:07 +01:00
return [ hUnaryExpr _operators [ this . operator ] ( vRight , oContext ) ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cValueExpr ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fValueExpr _parse ( oLexer , oStaticContext ) {
return fPathExpr _parse ( oLexer , oStaticContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cOrExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cOrExpr . prototype . left = null ;
cOrExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fOrExpr _parse ( oLexer , oStaticContext ) {
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fAndExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( oLexer . peek ( ) != "or" )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Or expression
var oOrExpr = new cOrExpr ( oExpr ) ;
2016-11-29 19:28:07 +01:00
while ( oLexer . peek ( ) == "or" ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fAndExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oOrExpr . items . push ( oExpr ) ;
}
return oOrExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cOrExpr . prototype . evaluate = function ( oContext ) {
var bValue = fFunction _sequence _toEBV ( this . left . evaluate ( oContext ) , oContext ) ;
for ( var nIndex = 0 , nLength = this . items . length ; ( nIndex < nLength ) && ! bValue ; nIndex ++ )
bValue = fFunction _sequence _toEBV ( this . items [ nIndex ] . evaluate ( oContext ) , oContext ) ;
return [ new cXSBoolean ( bValue ) ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cAndExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cAndExpr . prototype . left = null ;
cAndExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fAndExpr _parse ( oLexer , oStaticContext ) {
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fComparisonExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( oLexer . peek ( ) != "and" )
return oExpr ;
2016-12-17 00:33:47 +01:00
// And expression
var oAndExpr = new cAndExpr ( oExpr ) ;
2016-11-29 19:28:07 +01:00
while ( oLexer . peek ( ) == "and" ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fComparisonExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oAndExpr . items . push ( oExpr ) ;
}
return oAndExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cAndExpr . prototype . evaluate = function ( oContext ) {
var bValue = fFunction _sequence _toEBV ( this . left . evaluate ( oContext ) , oContext ) ;
for ( var nIndex = 0 , nLength = this . items . length ; ( nIndex < nLength ) && bValue ; nIndex ++ )
bValue = fFunction _sequence _toEBV ( this . items [ nIndex ] . evaluate ( oContext ) , oContext ) ;
return [ new cXSBoolean ( bValue ) ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cStepExpr ( ) {
} ;
cStepExpr . prototype . predicates = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fStepExpr _parse ( oLexer , oStaticContext ) {
if ( ! oLexer . eof ( ) )
return fFilterExpr _parse ( oLexer , oStaticContext )
|| fAxisStep _parse ( oLexer , oStaticContext ) ;
} ;
function fStepExpr _parsePredicates ( oLexer , oStaticContext , oStep ) {
var oExpr ;
2016-12-17 00:33:47 +01:00
// Parse predicates
while ( oLexer . peek ( ) == '[' ) {
2016-11-29 19:28:07 +01:00
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oStep . predicates . push ( oExpr ) ;
if ( oLexer . peek ( ) != ']' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cStepExpr . prototype . applyPredicates = function ( oSequence , oContext ) {
var vContextItem = oContext . item ,
nContextPosition = oContext . position ,
nContextSize = oContext . size ;
2016-12-17 00:33:47 +01:00
//
for ( var nPredicateIndex = 0 , oSequence1 , nPredicateLength = this . predicates . length ; nPredicateIndex < nPredicateLength ; nPredicateIndex ++ ) {
2016-11-29 19:28:07 +01:00
oSequence1 = oSequence ;
oSequence = [ ] ;
for ( var nIndex = 0 , oSequence2 , nLength = oSequence1 . length ; nIndex < nLength ; nIndex ++ ) {
2016-12-17 00:33:47 +01:00
// Set new context
oContext . item = oSequence1 [ nIndex ] ;
2016-11-29 19:28:07 +01:00
oContext . position = nIndex + 1 ;
oContext . size = nLength ;
2016-12-17 00:33:47 +01:00
//
oSequence2 = this . predicates [ nPredicateIndex ] . evaluate ( oContext ) ;
//
if ( oSequence2 . length == 1 && fXSAnyAtomicType _isNumeric ( oSequence2 [ 0 ] ) ) {
2016-11-29 19:28:07 +01:00
if ( oSequence2 [ 0 ] . valueOf ( ) == nIndex + 1 )
oSequence . push ( oSequence1 [ nIndex ] ) ;
}
else
if ( fFunction _sequence _toEBV ( oSequence2 , oContext ) )
oSequence . push ( oSequence1 [ nIndex ] ) ;
}
}
2016-12-17 00:33:47 +01:00
// Restore context
oContext . item = vContextItem ;
2016-11-29 19:28:07 +01:00
oContext . position = nContextPosition ;
oContext . size = nContextSize ;
2016-12-17 00:33:47 +01:00
//
return oSequence ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cAxisStep ( sAxis , oTest ) {
this . axis = sAxis ;
this . test = oTest ;
this . predicates = [ ] ;
} ;
cAxisStep . prototype = new cStepExpr ;
cAxisStep . prototype . axis = null ;
cAxisStep . prototype . test = null ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
var hAxisStep _axises = { } ;
2016-12-17 00:33:47 +01:00
// Forward axis
2016-11-29 19:28:07 +01:00
hAxisStep _axises [ "attribute" ] = { } ;
hAxisStep _axises [ "child" ] = { } ;
hAxisStep _axises [ "descendant" ] = { } ;
hAxisStep _axises [ "descendant-or-self" ] = { } ;
hAxisStep _axises [ "following" ] = { } ;
hAxisStep _axises [ "following-sibling" ] = { } ;
hAxisStep _axises [ "self" ] = { } ;
2016-12-17 00:33:47 +01:00
// hAxisStep_axises["namespace"] = {}; // deprecated in 2.0
// Reverse axis
2016-11-29 19:28:07 +01:00
hAxisStep _axises [ "ancestor" ] = { } ;
hAxisStep _axises [ "ancestor-or-self" ] = { } ;
hAxisStep _axises [ "parent" ] = { } ;
hAxisStep _axises [ "preceding" ] = { } ;
hAxisStep _axises [ "preceding-sibling" ] = { } ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fAxisStep _parse ( oLexer , oStaticContext ) {
var sAxis = oLexer . peek ( ) ,
oExpr ,
oStep ;
if ( oLexer . peek ( 1 ) == '::' ) {
if ( ! ( sAxis in hAxisStep _axises ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( 2 ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fNodeTest _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
oStep = new cAxisStep ( sAxis , oExpr ) ;
2016-11-29 19:28:07 +01:00
}
else
if ( sAxis == '..' ) {
oLexer . next ( ) ;
oStep = new cAxisStep ( "parent" , new cKindTest ( "node" ) ) ;
}
else
if ( sAxis == '@' ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fNodeTest _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
oStep = new cAxisStep ( "attribute" , oExpr ) ;
2016-11-29 19:28:07 +01:00
}
else {
if ( oLexer . eof ( ) || ! ( oExpr = fNodeTest _parse ( oLexer , oStaticContext ) ) )
return ;
oStep = new cAxisStep ( oExpr instanceof cKindTest && oExpr . name == "attribute" ? "attribute" : "child" , oExpr ) ;
}
2016-12-17 00:33:47 +01:00
//
fStepExpr _parsePredicates ( oLexer , oStaticContext , oStep ) ;
2016-11-29 19:28:07 +01:00
return oStep ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cAxisStep . prototype . evaluate = function ( oContext ) {
var oItem = oContext . item ;
if ( ! oContext . DOMAdapter . isNode ( oItem ) )
throw new cException ( "XPTY0020" ) ;
var oSequence = [ ] ,
fGetProperty = oContext . DOMAdapter . getProperty ,
nType = fGetProperty ( oItem , "nodeType" ) ;
switch ( this . axis ) {
2016-12-17 00:33:47 +01:00
// Forward axis
case "attribute" :
2016-11-29 19:28:07 +01:00
if ( nType == 1 )
for ( var aAttributes = fGetProperty ( oItem , "attributes" ) , nIndex = 0 , nLength = aAttributes . length ; nIndex < nLength ; nIndex ++ )
oSequence . push ( aAttributes [ nIndex ] ) ;
break ;
case "child" :
for ( var oNode = fGetProperty ( oItem , "firstChild" ) ; oNode ; oNode = fGetProperty ( oNode , "nextSibling" ) )
oSequence . push ( oNode ) ;
break ;
case "descendant-or-self" :
oSequence . push ( oItem ) ;
2016-12-17 00:33:47 +01:00
// No break left intentionally
case "descendant" :
2016-11-29 19:28:07 +01:00
fAxisStep _getChildrenForward ( fGetProperty ( oItem , "firstChild" ) , oSequence , fGetProperty ) ;
break ;
case "following" :
2016-12-17 00:33:47 +01:00
// TODO: Attribute node context
for ( var oParent = oItem , oSibling ; oParent ; oParent = fGetProperty ( oParent , "parentNode" ) )
2016-11-29 19:28:07 +01:00
if ( oSibling = fGetProperty ( oParent , "nextSibling" ) )
fAxisStep _getChildrenForward ( oSibling , oSequence , fGetProperty ) ;
break ;
case "following-sibling" :
for ( var oNode = oItem ; oNode = fGetProperty ( oNode , "nextSibling" ) ; )
oSequence . push ( oNode ) ;
break ;
case "self" :
oSequence . push ( oItem ) ;
break ;
2016-12-17 00:33:47 +01:00
// Reverse axis
case "ancestor-or-self" :
2016-11-29 19:28:07 +01:00
oSequence . push ( oItem ) ;
2016-12-17 00:33:47 +01:00
// No break left intentionally
case "ancestor" :
2016-11-29 19:28:07 +01:00
for ( var oNode = nType == 2 ? fGetProperty ( oItem , "ownerElement" ) : oItem ; oNode = fGetProperty ( oNode , "parentNode" ) ; )
oSequence . push ( oNode ) ;
break ;
case "parent" :
var oParent = nType == 2 ? fGetProperty ( oItem , "ownerElement" ) : fGetProperty ( oItem , "parentNode" ) ;
if ( oParent )
oSequence . push ( oParent ) ;
break ;
case "preceding" :
2016-12-17 00:33:47 +01:00
// TODO: Attribute node context
for ( var oParent = oItem , oSibling ; oParent ; oParent = fGetProperty ( oParent , "parentNode" ) )
2016-11-29 19:28:07 +01:00
if ( oSibling = fGetProperty ( oParent , "previousSibling" ) )
fAxisStep _getChildrenBackward ( oSibling , oSequence , fGetProperty ) ;
break ;
case "preceding-sibling" :
for ( var oNode = oItem ; oNode = fGetProperty ( oNode , "previousSibling" ) ; )
oSequence . push ( oNode ) ;
break ;
}
2016-12-17 00:33:47 +01:00
// Apply test
if ( oSequence . length && ! ( this . test instanceof cKindTest && this . test . name == "node" ) ) {
2016-11-29 19:28:07 +01:00
var oSequence1 = oSequence ;
oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length ; nIndex < nLength ; nIndex ++ ) {
if ( this . test . test ( oSequence1 [ nIndex ] , oContext ) )
oSequence . push ( oSequence1 [ nIndex ] ) ;
}
}
2016-12-17 00:33:47 +01:00
// Apply predicates
if ( oSequence . length && this . predicates . length )
2016-11-29 19:28:07 +01:00
oSequence = this . applyPredicates ( oSequence , oContext ) ;
2016-12-17 00:33:47 +01:00
// Reverse results if reverse axis
switch ( this . axis ) {
2016-11-29 19:28:07 +01:00
case "ancestor" :
case "ancestor-or-self" :
case "parent" :
case "preceding" :
case "preceding-sibling" :
oSequence . reverse ( ) ;
}
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fAxisStep _getChildrenForward ( oNode , oSequence , fGetProperty ) {
for ( var oChild ; oNode ; oNode = fGetProperty ( oNode , "nextSibling" ) ) {
oSequence . push ( oNode ) ;
if ( oChild = fGetProperty ( oNode , "firstChild" ) )
fAxisStep _getChildrenForward ( oChild , oSequence , fGetProperty ) ;
}
} ;
function fAxisStep _getChildrenBackward ( oNode , oSequence , fGetProperty ) {
for ( var oChild ; oNode ; oNode = fGetProperty ( oNode , "previousSibling" ) ) {
if ( oChild = fGetProperty ( oNode , "lastChild" ) )
fAxisStep _getChildrenBackward ( oChild , oSequence , fGetProperty ) ;
oSequence . push ( oNode ) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cPathExpr ( ) {
this . items = [ ] ;
} ;
cPathExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fPathExpr _parse ( oLexer , oStaticContext ) {
if ( oLexer . eof ( ) )
return ;
var sSingleSlash = '/' ,
sDoubleSlash = '/' + '/' ;
var oPathExpr = new cPathExpr ( ) ,
sSlash = oLexer . peek ( ) ,
oExpr ;
2016-12-17 00:33:47 +01:00
// Parse first step
if ( sSlash == sDoubleSlash || sSlash == sSingleSlash ) {
2016-11-29 19:28:07 +01:00
oLexer . next ( ) ;
oPathExpr . items . push ( new cFunctionCall ( null , "root" , sNS _XPF ) ) ;
2016-12-17 00:33:47 +01:00
//
if ( sSlash == sDoubleSlash )
2016-11-29 19:28:07 +01:00
oPathExpr . items . push ( new cAxisStep ( "descendant-or-self" , new cKindTest ( "node" ) ) ) ;
}
2016-12-17 00:33:47 +01:00
//
if ( oLexer . eof ( ) || ! ( oExpr = fStepExpr _parse ( oLexer , oStaticContext ) ) ) {
2016-11-29 19:28:07 +01:00
if ( sSlash == sSingleSlash )
2016-12-17 00:33:47 +01:00
return oPathExpr . items [ 0 ] ; // '/' expression
if ( sSlash == sDoubleSlash )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return ;
}
oPathExpr . items . push ( oExpr ) ;
2016-12-17 00:33:47 +01:00
// Parse other steps
while ( ( sSlash = oLexer . peek ( ) ) == sSingleSlash || sSlash == sDoubleSlash ) {
2016-11-29 19:28:07 +01:00
if ( sSlash == sDoubleSlash )
oPathExpr . items . push ( new cAxisStep ( "descendant-or-self" , new cKindTest ( "node" ) ) ) ;
2016-12-17 00:33:47 +01:00
//
oLexer . next ( ) ;
2016-11-29 19:28:07 +01:00
if ( oLexer . eof ( ) || ! ( oExpr = fStepExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
oPathExpr . items . push ( oExpr ) ;
2016-11-29 19:28:07 +01:00
}
if ( oPathExpr . items . length == 1 )
return oPathExpr . items [ 0 ] ;
2016-12-17 00:33:47 +01:00
//
return oPathExpr ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cPathExpr . prototype . evaluate = function ( oContext ) {
var vContextItem = oContext . item ;
2016-12-17 00:33:47 +01:00
//
var oSequence = [ vContextItem ] ;
2016-11-29 19:28:07 +01:00
for ( var nItemIndex = 0 , nItemLength = this . items . length , oSequence1 ; nItemIndex < nItemLength ; nItemIndex ++ ) {
oSequence1 = [ ] ;
for ( var nIndex = 0 , nLength = oSequence . length ; nIndex < nLength ; nIndex ++ ) {
2016-12-17 00:33:47 +01:00
// Set new context item
oContext . item = oSequence [ nIndex ] ;
//
for ( var nRightIndex = 0 , oSequence2 = this . items [ nItemIndex ] . evaluate ( oContext ) , nRightLength = oSequence2 . length ; nRightIndex < nRightLength ; nRightIndex ++ )
2016-11-29 19:28:07 +01:00
if ( ( nItemIndex < nItemLength - 1 ) && ! oContext . DOMAdapter . isNode ( oSequence2 [ nRightIndex ] ) )
throw new cException ( "XPTY0019" ) ;
else
if ( fArray _indexOf ( oSequence1 , oSequence2 [ nRightIndex ] ) == - 1 )
oSequence1 . push ( oSequence2 [ nRightIndex ] ) ;
}
oSequence = oSequence1 ;
} ;
2016-12-17 00:33:47 +01:00
// Restore context item
oContext . item = vContextItem ;
//
return fFunction _sequence _order ( oSequence , oContext ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cNodeTest ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fNodeTest _parse ( oLexer , oStaticContext ) {
if ( ! oLexer . eof ( ) )
return fKindTest _parse ( oLexer , oStaticContext )
|| fNameTest _parse ( oLexer , oStaticContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cKindTest ( sName ) {
this . name = sName ;
this . args = [ ] ;
} ;
cKindTest . prototype = new cNodeTest ;
cKindTest . prototype . name = null ;
cKindTest . prototype . args = null ;
var hKindTest _names = { } ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
hKindTest _names [ "document-node" ] = { } ;
hKindTest _names [ "element" ] = { } ;
hKindTest _names [ "attribute" ] = { } ;
hKindTest _names [ "processing-instruction" ] = { } ;
hKindTest _names [ "comment" ] = { } ;
hKindTest _names [ "text" ] = { } ;
hKindTest _names [ "node" ] = { } ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
hKindTest _names [ "schema-element" ] = { } ;
hKindTest _names [ "schema-attribute" ] = { } ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fKindTest _parse ( oLexer , oStaticContext ) {
2016-12-17 00:33:47 +01:00
var sName = oLexer . peek ( ) ,
oValue ;
2016-11-29 19:28:07 +01:00
if ( oLexer . peek ( 1 ) == '(' ) {
2016-12-17 00:33:47 +01:00
//
if ( ! ( sName in hKindTest _names ) )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
oLexer . next ( 2 ) ;
//
var oTest = new cKindTest ( sName ) ;
2016-11-29 19:28:07 +01:00
if ( oLexer . peek ( ) != ')' ) {
if ( sName == "document-node" ) {
2016-12-17 00:33:47 +01:00
// TODO: parse test further
}
2016-11-29 19:28:07 +01:00
else
if ( sName == "element" ) {
2016-12-17 00:33:47 +01:00
// TODO: parse test further
}
2016-11-29 19:28:07 +01:00
else
if ( sName == "attribute" ) {
2016-12-17 00:33:47 +01:00
// TODO: parse test further
}
2016-11-29 19:28:07 +01:00
else
if ( sName == "processing-instruction" ) {
2016-12-17 00:33:47 +01:00
oValue = fStringLiteral _parse ( oLexer , oStaticContext ) ;
if ( ! oValue ) {
oValue = new cStringLiteral ( new cXSString ( oLexer . peek ( ) ) ) ;
oLexer . next ( ) ;
}
oTest . args . push ( oValue ) ;
}
2016-11-29 19:28:07 +01:00
else
if ( sName == "schema-attribute" ) {
2016-12-17 00:33:47 +01:00
// TODO: parse test further
}
2016-11-29 19:28:07 +01:00
else
if ( sName == "schema-element" ) {
2016-12-17 00:33:47 +01:00
// TODO: parse test further
}
2016-11-29 19:28:07 +01:00
}
else {
if ( sName == "schema-attribute" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
else
if ( sName == "schema-element" )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
if ( oLexer . peek ( ) != ')' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
return oTest ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cKindTest . prototype . test = function ( oNode , oContext ) {
var fGetProperty = oContext . DOMAdapter . getProperty ,
2016-12-17 00:33:47 +01:00
nType = oContext . DOMAdapter . isNode ( oNode ) ? fGetProperty ( oNode , "nodeType" ) : 0 ,
sTarget ;
2016-11-29 19:28:07 +01:00
switch ( this . name ) {
2016-12-17 00:33:47 +01:00
// Node type test
case "node" : return ! ! nType ;
2016-11-29 19:28:07 +01:00
case "attribute" : if ( nType != 2 ) return false ; break ;
case "document-node" : return nType == 9 ;
case "element" : return nType == 1 ;
case "processing-instruction" : if ( nType != 7 ) return false ; break ;
case "comment" : return nType == 8 ;
case "text" : return nType == 3 || nType == 4 ;
2016-12-17 00:33:47 +01:00
// Schema tests
case "schema-attribute" :
2016-11-29 19:28:07 +01:00
throw "KindTest '" + "schema-attribute" + "' not implemented" ;
case "schema-element" :
throw "KindTest '" + "schema-element" + "' not implemented" ;
}
2016-12-17 00:33:47 +01:00
// Additional tests
if ( nType == 2 )
2016-11-29 19:28:07 +01:00
return fGetProperty ( oNode , "prefix" ) != "xmlns" && fGetProperty ( oNode , "localName" ) != "xmlns" ;
2016-12-17 00:33:47 +01:00
if ( nType == 7 ) {
sTarget = fGetProperty ( oNode , "target" ) ;
return this . args . length ? sTarget == this . args [ 0 ] . value : sTarget != "xml" ;
}
2016-11-29 19:28:07 +01:00
return true ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cNameTest ( sPrefix , sLocalName , sNameSpaceURI ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
} ;
cNameTest . prototype = new cNodeTest ;
cNameTest . prototype . prefix = null ;
cNameTest . prototype . localName = null ;
cNameTest . prototype . namespaceURI = null ;
2016-12-17 00:33:47 +01:00
// Static members
var rNameTest = /^(?:(?![0-9-])(\w[\w.-]*|\*)\:)?(?![0-9-])(\w[\w.-]*|\*)$/ ;
2016-11-29 19:28:07 +01:00
function fNameTest _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . match ( rNameTest ) ;
if ( aMatch ) {
if ( aMatch [ 1 ] == '*' && aMatch [ 2 ] == '*' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
return new cNameTest ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? aMatch [ 1 ] == '*' ? '*' : oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) || null : oStaticContext . defaultElementNamespace ) ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cNameTest . prototype . test = function ( oNode , oContext ) {
var fGetProperty = oContext . DOMAdapter . getProperty ,
nType = fGetProperty ( oNode , "nodeType" ) ;
if ( nType == 1 || nType == 2 ) {
if ( this . localName == '*' )
return ( nType == 1 || ( fGetProperty ( oNode , "prefix" ) != "xmlns" && fGetProperty ( oNode , "localName" ) != "xmlns" ) ) && ( ! this . prefix || fGetProperty ( oNode , "namespaceURI" ) == this . namespaceURI ) ;
if ( this . localName == fGetProperty ( oNode , "localName" ) )
return this . namespaceURI == '*' || ( nType == 2 && ! this . prefix && ! fGetProperty ( oNode , "prefix" ) ) || fGetProperty ( oNode , "namespaceURI" ) == this . namespaceURI ;
}
2016-12-17 00:33:47 +01:00
//
return false ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cPrimaryExpr ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fPrimaryExpr _parse ( oLexer , oStaticContext ) {
if ( ! oLexer . eof ( ) )
return fContextItemExpr _parse ( oLexer , oStaticContext )
|| fParenthesizedExpr _parse ( oLexer , oStaticContext )
|| fFunctionCall _parse ( oLexer , oStaticContext )
|| fVarRef _parse ( oLexer , oStaticContext )
|| fLiteral _parse ( oLexer , oStaticContext ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cParenthesizedExpr ( oExpr ) {
this . expression = oExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fParenthesizedExpr _parse ( oLexer , oStaticContext ) {
if ( oLexer . peek ( ) == '(' ) {
oLexer . next ( ) ;
2016-12-17 00:33:47 +01:00
// Check if not empty (allowed)
var oExpr = null ;
2016-11-29 19:28:07 +01:00
if ( oLexer . peek ( ) != ')' )
oExpr = fExpr _parse ( oLexer , oStaticContext ) ;
2016-12-17 00:33:47 +01:00
//
if ( oLexer . peek ( ) != ')' )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
2016-12-17 00:33:47 +01:00
//
return new cParenthesizedExpr ( oExpr ) ;
2016-11-29 19:28:07 +01:00
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cParenthesizedExpr . prototype . evaluate = function ( oContext ) {
return this . expression ? this . expression . evaluate ( oContext ) : [ ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cContextItemExpr ( ) {
} ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fContextItemExpr _parse ( oLexer , oStaticContext ) {
if ( oLexer . peek ( ) == '.' ) {
oLexer . next ( ) ;
return new cContextItemExpr ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cContextItemExpr . prototype . evaluate = function ( oContext ) {
if ( oContext . item == null )
throw new cException ( "XPDY0002"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
return [ oContext . item ] ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cLiteral ( ) {
} ;
cLiteral . prototype . value = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fLiteral _parse ( oLexer , oStaticContext ) {
if ( ! oLexer . eof ( ) )
return fNumericLiteral _parse ( oLexer , oStaticContext )
|| fStringLiteral _parse ( oLexer , oStaticContext ) ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cLiteral . prototype . evaluate = function ( oContext ) {
return [ this . value ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cNumericLiteral ( oValue ) {
this . value = oValue ;
} ;
cNumericLiteral . prototype = new cLiteral ;
2016-12-17 00:33:47 +01:00
// Integer | Decimal | Double
2016-11-29 19:28:07 +01:00
var rNumericLiteral = /^[+\-]?(?:(?:(\d+)(?:\.(\d*))?)|(?:\.(\d+)))(?:[eE]([+-])?(\d+))?$/ ;
function fNumericLiteral _parse ( oLexer , oStaticContext ) {
var sValue = oLexer . peek ( ) ,
vValue = fNumericLiteral _parseValue ( sValue ) ;
if ( vValue ) {
oLexer . next ( ) ;
return new cNumericLiteral ( vValue ) ;
}
} ;
function fNumericLiteral _parseValue ( sValue ) {
var aMatch = sValue . match ( rNumericLiteral ) ;
if ( aMatch ) {
var cType = cXSInteger ;
if ( aMatch [ 5 ] )
cType = cXSDouble ;
else
if ( aMatch [ 2 ] || aMatch [ 3 ] )
cType = cXSDecimal ;
return new cType ( + sValue ) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cStringLiteral ( oValue ) {
this . value = oValue ;
} ;
cStringLiteral . prototype = new cLiteral ;
var rStringLiteral = /^'([^']*(?:''[^']*)*)'|"([^"]*(?:""[^"]*)*)"$/ ;
function fStringLiteral _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . match ( rStringLiteral ) ;
if ( aMatch ) {
oLexer . next ( ) ;
return new cStringLiteral ( new cXSString ( aMatch [ 1 ] ? aMatch [ 1 ] . replace ( "''" , "'" ) : aMatch [ 2 ] ? aMatch [ 2 ] . replace ( '""' , '"' ) : '' ) ) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cFilterExpr ( oPrimary ) {
this . expression = oPrimary ;
this . predicates = [ ] ;
} ;
cFilterExpr . prototype = new cStepExpr ;
cFilterExpr . prototype . expression = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fFilterExpr _parse ( oLexer , oStaticContext ) {
var oExpr ;
if ( oLexer . eof ( ) || ! ( oExpr = fPrimaryExpr _parse ( oLexer , oStaticContext ) ) )
return ;
var oFilterExpr = new cFilterExpr ( oExpr ) ;
2016-12-17 00:33:47 +01:00
// Parse predicates
fStepExpr _parsePredicates ( oLexer , oStaticContext , oFilterExpr ) ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// If no predicates found
if ( oFilterExpr . predicates . length == 0 )
2016-11-29 19:28:07 +01:00
return oFilterExpr . expression ;
return oFilterExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cFilterExpr . prototype . evaluate = function ( oContext ) {
var oSequence = this . expression . evaluate ( oContext ) ;
if ( this . predicates . length && oSequence . length )
oSequence = this . applyPredicates ( oSequence , oContext ) ;
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cVarRef ( sPrefix , sLocalName , sNameSpaceURI ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
} ;
cVarRef . prototype . prefix = null ;
cVarRef . prototype . localName = null ;
cVarRef . prototype . namespaceURI = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fVarRef _parse ( oLexer , oStaticContext ) {
if ( oLexer . peek ( ) . substr ( 0 , 1 ) == '$' ) {
var aMatch = oLexer . peek ( ) . substr ( 1 ) . match ( rNameTest ) ;
if ( aMatch ) {
if ( aMatch [ 1 ] == '*' || aMatch [ 2 ] == '*' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
) ;
2016-11-29 19:28:07 +01:00
var oVarRef = new cVarRef ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) : null ) ;
oLexer . next ( ) ;
return oVarRef ;
}
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cVarRef . prototype . evaluate = function ( oContext ) {
var sUri = ( this . namespaceURI ? '{' + this . namespaceURI + '}' : '' ) + this . localName ;
if ( oContext . scope . hasOwnProperty ( sUri ) )
return [ oContext . scope [ sUri ] ] ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPST0008"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cFunctionCall ( sPrefix , sLocalName , sNameSpaceURI ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
this . args = [ ] ;
} ;
cFunctionCall . prototype . prefix = null ;
cFunctionCall . prototype . localName = null ;
cFunctionCall . prototype . namespaceURI = null ;
cFunctionCall . prototype . args = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fFunctionCall _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . match ( rNameTest ) ;
if ( aMatch && oLexer . peek ( 1 ) == '(' ) {
2016-12-17 00:33:47 +01:00
// Reserved "functions"
if ( ! aMatch [ 1 ] && ( aMatch [ 2 ] in hKindTest _names ) )
2016-11-29 19:28:07 +01:00
return fAxisStep _parse ( oLexer , oStaticContext ) ;
2016-12-17 00:33:47 +01:00
// Other functions
if ( aMatch [ 1 ] == '*' || aMatch [ 2 ] == '*' )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
var oFunctionCallExpr = new cFunctionCall ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) || null : oStaticContext . defaultFunctionNamespace ) ,
oExpr ;
oLexer . next ( 2 ) ;
2016-12-17 00:33:47 +01:00
//
if ( oLexer . peek ( ) != ')' ) {
2016-11-29 19:28:07 +01:00
do {
if ( oLexer . eof ( ) || ! ( oExpr = fExprSingle _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
oFunctionCallExpr . args . push ( oExpr ) ;
2016-11-29 19:28:07 +01:00
}
while ( oLexer . peek ( ) == ',' && oLexer . next ( ) ) ;
2016-12-17 00:33:47 +01:00
//
if ( oLexer . peek ( ) != ')' )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
oLexer . next ( ) ;
return oFunctionCallExpr ;
}
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cFunctionCall . prototype . evaluate = function ( oContext ) {
var aArguments = [ ] ,
aParameters ,
fFunction ;
2016-12-17 00:33:47 +01:00
// Evaluate arguments
for ( var nIndex = 0 , nLength = this . args . length ; nIndex < nLength ; nIndex ++ )
2016-11-29 19:28:07 +01:00
aArguments . push ( this . args [ nIndex ] . evaluate ( oContext ) ) ;
var sUri = ( this . namespaceURI ? '{' + this . namespaceURI + '}' : '' ) + this . localName ;
2016-12-17 00:33:47 +01:00
// Call function
if ( this . namespaceURI == sNS _XPF ) {
2016-11-29 19:28:07 +01:00
if ( fFunction = hStaticContext _functions [ this . localName ] ) {
2016-12-17 00:33:47 +01:00
// Validate/Cast arguments
if ( aParameters = hStaticContext _signatures [ this . localName ] )
2016-11-29 19:28:07 +01:00
fFunctionCall _prepare ( this . localName , aParameters , fFunction , aArguments , oContext ) ;
2016-12-17 00:33:47 +01:00
//
var vResult = fFunction . apply ( oContext , aArguments ) ;
//
return vResult == null ? [ ] : vResult instanceof cArray ? vResult : [ vResult ] ;
2016-11-29 19:28:07 +01:00
}
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
else
if ( this . namespaceURI == sNS _XSD ) {
if ( ( fFunction = hStaticContext _dataTypes [ this . localName ] ) && this . localName != "NOTATION" && this . localName != "anyAtomicType" ) {
2016-12-17 00:33:47 +01:00
//
fFunctionCall _prepare ( this . localName , [ [ cXSAnyAtomicType , '?' ] ] , fFunction , aArguments , oContext ) ;
//
return aArguments [ 0 ] === null ? [ ] : [ fFunction . cast ( aArguments [ 0 ] ) ] ;
2016-11-29 19:28:07 +01:00
}
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
else
if ( fFunction = oContext . staticContext . getFunction ( sUri ) ) {
2016-12-17 00:33:47 +01:00
//
var vResult = fFunction . apply ( oContext , aArguments ) ;
//
return vResult == null ? [ ] : vResult instanceof cArray ? vResult : [ vResult ] ;
2016-11-29 19:28:07 +01:00
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPST0017"
2016-11-29 19:28:07 +01:00
) ;
} ;
var aFunctionCall _numbers = [ "first" , "second" , "third" , "fourth" , "fifth" ] ;
function fFunctionCall _prepare ( sName , aParameters , fFunction , aArguments , oContext ) {
var oArgument ,
nArgumentsLength = aArguments . length ,
oParameter ,
nParametersLength = aParameters . length ,
nParametersRequired = 0 ;
2016-12-17 00:33:47 +01:00
// Determine amount of parameters required
while ( ( nParametersRequired < aParameters . length ) && ! aParameters [ nParametersRequired ] [ 2 ] )
2016-11-29 19:28:07 +01:00
nParametersRequired ++ ;
2016-12-17 00:33:47 +01:00
// Validate arguments length
if ( nArgumentsLength > nParametersLength )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
else
if ( nArgumentsLength < nParametersRequired )
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
for ( var nIndex = 0 ; nIndex < nArgumentsLength ; nIndex ++ ) {
oParameter = aParameters [ nIndex ] ;
oArgument = aArguments [ nIndex ] ;
2016-12-17 00:33:47 +01:00
// Check sequence cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oArgument , oParameter [ 1 ]
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Check sequence items data types consistency
fFunctionCall _assertSequenceItemType ( oContext , oArgument , oParameter [ 0 ]
2016-11-29 19:28:07 +01:00
) ;
if ( oParameter [ 1 ] != '+' && oParameter [ 1 ] != '*' )
aArguments [ nIndex ] = oArgument . length ? oArgument [ 0 ] : null ;
}
} ;
function fFunctionCall _assertSequenceItemType ( oContext , oSequence , cItemType
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) {
2016-12-17 00:33:47 +01:00
//
for ( var nIndex = 0 , nLength = oSequence . length , nNodeType , vItem ; nIndex < nLength ; nIndex ++ ) {
2016-11-29 19:28:07 +01:00
vItem = oSequence [ nIndex ] ;
2016-12-17 00:33:47 +01:00
// Node types
if ( cItemType == cXTNode || cItemType . prototype instanceof cXTNode ) {
// Check if is node
if ( ! oContext . DOMAdapter . isNode ( vItem ) )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Check node type
if ( cItemType != cXTNode ) {
2016-11-29 19:28:07 +01:00
nNodeType = oContext . DOMAdapter . getProperty ( vItem , "nodeType" ) ;
if ( [ null , cXTElement , cXTAttribute , cXTText , cXTText , null , null , cXTProcessingInstruction , cXTComment , cXTDocument , null , null , null ] [ nNodeType ] != cItemType )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
}
else
2016-12-17 00:33:47 +01:00
// Atomic types
if ( cItemType == cXSAnyAtomicType || cItemType . prototype instanceof cXSAnyAtomicType ) {
// Atomize item
vItem = fFunction _sequence _atomize ( [ vItem ] , oContext ) [ 0 ] ;
// Convert type if necessary
if ( cItemType != cXSAnyAtomicType ) {
// Cast item to expected type if it's type is xs:untypedAtomic
if ( vItem instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vItem = cItemType . cast ( vItem ) ;
2016-12-17 00:33:47 +01:00
// Cast item to xs:string if it's type is xs:anyURI
else
if ( cItemType == cXSString /* || cItemType.prototype instanceof cXSString*/ ) {
2016-11-29 19:28:07 +01:00
if ( vItem instanceof cXSAnyURI )
vItem = cXSString . cast ( vItem ) ;
}
else
2016-12-17 00:33:47 +01:00
if ( cItemType == cXSDouble /* || cItemType.prototype instanceof cXSDouble*/ ) {
2016-11-29 19:28:07 +01:00
if ( fXSAnyAtomicType _isNumeric ( vItem ) )
vItem = cItemType . cast ( vItem ) ;
}
}
2016-12-17 00:33:47 +01:00
// Check type
if ( ! ( vItem instanceof cItemType ) )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Write value back to sequence
oSequence [ nIndex ] = vItem ;
2016-11-29 19:28:07 +01:00
}
}
} ;
function fFunctionCall _assertSequenceCardinality ( oContext , oSequence , sCardinality
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) {
var nLength = oSequence . length ;
2016-12-17 00:33:47 +01:00
// Check cardinality
if ( sCardinality == '?' ) { // =0 or 1
if ( nLength > 1 )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
else
2016-12-17 00:33:47 +01:00
if ( sCardinality == '+' ) { // =1+
if ( nLength < 1 )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
else
2016-12-17 00:33:47 +01:00
if ( sCardinality != '*' ) { // =1 ('*' =0+)
if ( nLength != 1 )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cIntersectExceptExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cIntersectExceptExpr . prototype . left = null ;
cIntersectExceptExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fIntersectExceptExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
sOperator ;
if ( oLexer . eof ( ) || ! ( oExpr = fInstanceofExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( ( sOperator = oLexer . peek ( ) ) == "intersect" || sOperator == "except" ) )
return oExpr ;
2016-12-17 00:33:47 +01:00
// IntersectExcept expression
var oIntersectExceptExpr = new cIntersectExceptExpr ( oExpr ) ;
2016-11-29 19:28:07 +01:00
while ( ( sOperator = oLexer . peek ( ) ) == "intersect" || sOperator == "except" ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fInstanceofExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oIntersectExceptExpr . items . push ( [ sOperator , oExpr ] ) ;
}
return oIntersectExceptExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cIntersectExceptExpr . prototype . evaluate = function ( oContext ) {
var oSequence = this . left . evaluate ( oContext ) ;
for ( var nIndex = 0 , nLength = this . items . length , oItem ; nIndex < nLength ; nIndex ++ )
oSequence = hStaticContext _operators [ ( oItem = this . items [ nIndex ] ) [ 0 ] ] . call ( oContext , oSequence , oItem [ 1 ] . evaluate ( oContext ) ) ;
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cRangeExpr ( oLeft , oRight ) {
this . left = oLeft ;
this . right = oRight ;
} ;
cRangeExpr . prototype . left = null ;
cRangeExpr . prototype . right = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fRangeExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oRight ;
if ( oLexer . eof ( ) || ! ( oExpr = fAdditiveExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( oLexer . peek ( ) != "to" )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Range expression
oLexer . next ( ) ;
2016-11-29 19:28:07 +01:00
if ( oLexer . eof ( ) || ! ( oRight = fAdditiveExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cRangeExpr ( oExpr , oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cRangeExpr . prototype . evaluate = function ( oContext ) {
2016-12-17 00:33:47 +01:00
//
var oLeft = this . left . evaluate ( oContext ) ;
2016-11-29 19:28:07 +01:00
if ( ! oLeft . length )
return [ ] ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fFunctionCall _assertSequenceCardinality ( oContext , oLeft , '?'
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
fFunctionCall _assertSequenceItemType ( oContext , oLeft , cXSInteger
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
var oRight = this . right . evaluate ( oContext ) ;
if ( ! oRight . length )
return [ ] ;
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
fFunctionCall _assertSequenceCardinality ( oContext , oRight , '?'
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
fFunctionCall _assertSequenceItemType ( oContext , oRight , cXSInteger
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return hStaticContext _operators [ "to" ] . call ( oContext , oLeft [ 0 ] , oRight [ 0 ] ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cUnionExpr ( oExpr ) {
this . left = oExpr ;
this . items = [ ] ;
} ;
cUnionExpr . prototype . left = null ;
cUnionExpr . prototype . items = null ;
2016-12-17 00:33:47 +01:00
// Static members
2016-11-29 19:28:07 +01:00
function fUnionExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
sOperator ;
if ( oLexer . eof ( ) || ! ( oExpr = fIntersectExceptExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( ( sOperator = oLexer . peek ( ) ) == '|' || sOperator == "union" ) )
return oExpr ;
2016-12-17 00:33:47 +01:00
// Union expression
var oUnionExpr = new cUnionExpr ( oExpr ) ;
2016-11-29 19:28:07 +01:00
while ( ( sOperator = oLexer . peek ( ) ) == '|' || sOperator == "union" ) {
oLexer . next ( ) ;
if ( oLexer . eof ( ) || ! ( oExpr = fIntersectExceptExpr _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oUnionExpr . items . push ( oExpr ) ;
}
return oUnionExpr ;
} ;
2016-12-17 00:33:47 +01:00
// Public members
2016-11-29 19:28:07 +01:00
cUnionExpr . prototype . evaluate = function ( oContext ) {
var oSequence = this . left . evaluate ( oContext ) ;
for ( var nIndex = 0 , nLength = this . items . length ; nIndex < nLength ; nIndex ++ )
oSequence = hStaticContext _operators [ "union" ] . call ( oContext , oSequence , this . items [ nIndex ] . evaluate ( oContext ) ) ;
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cInstanceofExpr ( oExpr , oType ) {
this . expression = oExpr ;
this . type = oType ;
} ;
cInstanceofExpr . prototype . expression = null ;
cInstanceofExpr . prototype . type = null ;
function fInstanceofExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oType ;
if ( oLexer . eof ( ) || ! ( oExpr = fTreatExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) == "instance" && oLexer . peek ( 1 ) == "of" ) )
return oExpr ;
oLexer . next ( 2 ) ;
if ( oLexer . eof ( ) || ! ( oType = fSequenceType _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cInstanceofExpr ( oExpr , oType ) ;
} ;
cInstanceofExpr . prototype . evaluate = function ( oContext ) {
var oSequence1 = this . expression . evaluate ( oContext ) ,
oItemType = this . type . itemType ,
sOccurence = this . type . occurence ;
2016-12-17 00:33:47 +01:00
// Validate empty-sequence()
if ( ! oItemType )
2016-11-29 19:28:07 +01:00
return [ new cXSBoolean ( ! oSequence1 . length ) ] ;
2016-12-17 00:33:47 +01:00
// Validate cardinality
if ( ! oSequence1 . length )
2016-11-29 19:28:07 +01:00
return [ new cXSBoolean ( sOccurence == '?' || sOccurence == '*' ) ] ;
if ( oSequence1 . length != 1 )
if ( ! ( sOccurence == '+' || sOccurence == '*' ) )
return [ new cXSBoolean ( false ) ] ;
2016-12-17 00:33:47 +01:00
// Validate type
if ( ! oItemType . test ) // item()
return [ new cXSBoolean ( true ) ] ;
2016-11-29 19:28:07 +01:00
var bValue = true ;
for ( var nIndex = 0 , nLength = oSequence1 . length ; ( nIndex < nLength ) && bValue ; nIndex ++ )
bValue = oItemType . test . test ( oSequence1 [ nIndex ] , oContext ) ;
2016-12-17 00:33:47 +01:00
//
return [ new cXSBoolean ( bValue ) ] ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cTreatExpr ( oExpr , oType ) {
this . expression = oExpr ;
this . type = oType ;
} ;
cTreatExpr . prototype . expression = null ;
cTreatExpr . prototype . type = null ;
function fTreatExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oType ;
if ( oLexer . eof ( ) || ! ( oExpr = fCastableExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) == "treat" && oLexer . peek ( 1 ) == "as" ) )
return oExpr ;
oLexer . next ( 2 ) ;
if ( oLexer . eof ( ) || ! ( oType = fSequenceType _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cTreatExpr ( oExpr , oType ) ;
} ;
cTreatExpr . prototype . evaluate = function ( oContext ) {
var oSequence1 = this . expression . evaluate ( oContext ) ,
oItemType = this . type . itemType ,
sOccurence = this . type . occurence ;
2016-12-17 00:33:47 +01:00
// Validate empty-sequence()
if ( ! oItemType ) {
2016-11-29 19:28:07 +01:00
if ( oSequence1 . length )
throw new cException ( "XPDY0050"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return oSequence1 ;
}
2016-12-17 00:33:47 +01:00
// Validate cardinality
if ( ! ( sOccurence == '?' || sOccurence == '*' ) )
2016-11-29 19:28:07 +01:00
if ( ! oSequence1 . length )
throw new cException ( "XPDY0050"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
if ( ! ( sOccurence == '+' || sOccurence == '*' ) )
if ( oSequence1 . length != 1 )
throw new cException ( "XPDY0050"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
// Validate type
if ( ! oItemType . test ) // item()
return oSequence1 ;
2016-11-29 19:28:07 +01:00
for ( var nIndex = 0 , nLength = oSequence1 . length ; nIndex < nLength ; nIndex ++ )
if ( ! oItemType . test . test ( oSequence1 [ nIndex ] , oContext ) )
throw new cException ( "XPDY0050"
2016-12-17 00:33:47 +01:00
) ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
//
return oSequence1 ;
} ;
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cCastableExpr ( oExpr , oType ) {
this . expression = oExpr ;
this . type = oType ;
} ;
cCastableExpr . prototype . expression = null ;
cCastableExpr . prototype . type = null ;
function fCastableExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oType ;
if ( oLexer . eof ( ) || ! ( oExpr = fCastExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) == "castable" && oLexer . peek ( 1 ) == "as" ) )
return oExpr ;
oLexer . next ( 2 ) ;
if ( oLexer . eof ( ) || ! ( oType = fSingleType _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cCastableExpr ( oExpr , oType ) ;
} ;
cCastableExpr . prototype . evaluate = function ( oContext ) {
var oSequence1 = this . expression . evaluate ( oContext ) ,
oItemType = this . type . itemType ,
sOccurence = this . type . occurence ;
if ( oSequence1 . length > 1 )
return [ new cXSBoolean ( false ) ] ;
else
if ( ! oSequence1 . length )
return [ new cXSBoolean ( sOccurence == '?' ) ] ;
2016-12-17 00:33:47 +01:00
// Try casting
try {
2016-11-29 19:28:07 +01:00
oItemType . cast ( fFunction _sequence _atomize ( oSequence1 , oContext ) [ 0 ] ) ;
}
catch ( e ) {
if ( e . code == "XPST0051" )
throw e ;
if ( e . code == "XPST0017" )
throw new cException ( "XPST0080"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
return [ new cXSBoolean ( false ) ] ;
2016-11-29 19:28:07 +01:00
}
return [ new cXSBoolean ( true ) ] ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cCastExpr ( oExpr , oType ) {
this . expression = oExpr ;
this . type = oType ;
} ;
cCastExpr . prototype . expression = null ;
cCastExpr . prototype . type = null ;
function fCastExpr _parse ( oLexer , oStaticContext ) {
var oExpr ,
oType ;
if ( oLexer . eof ( ) || ! ( oExpr = fUnaryExpr _parse ( oLexer , oStaticContext ) ) )
return ;
if ( ! ( oLexer . peek ( ) == "cast" && oLexer . peek ( 1 ) == "as" ) )
return oExpr ;
oLexer . next ( 2 ) ;
if ( oLexer . eof ( ) || ! ( oType = fSingleType _parse ( oLexer , oStaticContext ) ) )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cCastExpr ( oExpr , oType ) ;
} ;
cCastExpr . prototype . evaluate = function ( oContext ) {
var oSequence1 = this . expression . evaluate ( oContext ) ;
2016-12-17 00:33:47 +01:00
// Validate cardinality
fFunctionCall _assertSequenceCardinality ( oContext , oSequence1 , this . type . occurence
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
if ( ! oSequence1 . length )
2016-11-29 19:28:07 +01:00
return [ ] ;
2016-12-17 00:33:47 +01:00
//
return [ this . type . itemType . cast ( fFunction _sequence _atomize ( oSequence1 , oContext ) [ 0 ] , oContext ) ] ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cAtomicType ( sPrefix , sLocalName , sNameSpaceURI ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
} ;
cAtomicType . prototype . prefix = null ;
cAtomicType . prototype . localName = null ;
cAtomicType . prototype . namespaceURI = null ;
function fAtomicType _parse ( oLexer , oStaticContext ) {
var aMatch = oLexer . peek ( ) . match ( rNameTest ) ;
if ( aMatch ) {
if ( aMatch [ 1 ] == '*' || aMatch [ 2 ] == '*' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
return new cAtomicType ( aMatch [ 1 ] || null , aMatch [ 2 ] , aMatch [ 1 ] ? oStaticContext . getURIForPrefix ( aMatch [ 1 ] ) : null ) ;
}
} ;
cAtomicType . prototype . test = function ( vItem , oContext ) {
2016-12-17 00:33:47 +01:00
// Test
var sUri = ( this . namespaceURI ? '{' + this . namespaceURI + '}' : '' ) + this . localName ,
2016-11-29 19:28:07 +01:00
cType = this . namespaceURI == sNS _XSD ? hStaticContext _dataTypes [ this . localName ] : oContext . staticContext . getDataType ( sUri ) ;
if ( cType )
return vItem instanceof cType ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPST0051"
2016-11-29 19:28:07 +01:00
) ;
} ;
cAtomicType . prototype . cast = function ( vItem , oContext ) {
2016-12-17 00:33:47 +01:00
// Cast
var sUri = ( this . namespaceURI ? '{' + this . namespaceURI + '}' : '' ) + this . localName ,
2016-11-29 19:28:07 +01:00
cType = this . namespaceURI == sNS _XSD ? hStaticContext _dataTypes [ this . localName ] : oContext . staticContext . getDataType ( sUri ) ;
if ( cType )
return cType . cast ( vItem ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPST0051"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cItemType ( oTest ) {
this . test = oTest ;
} ;
cItemType . prototype . test = null ;
function fItemType _parse ( oLexer , oStaticContext ) {
if ( oLexer . eof ( ) )
return ;
var oExpr ;
if ( oLexer . peek ( ) == "item" && oLexer . peek ( 1 ) == '(' ) {
oLexer . next ( 2 ) ;
if ( oLexer . peek ( ) != ')' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
return new cItemType ;
}
2016-12-17 00:33:47 +01:00
// Note! Following step should have been before previous as per spec
if ( oExpr = fKindTest _parse ( oLexer , oStaticContext ) )
2016-11-29 19:28:07 +01:00
return new cItemType ( oExpr ) ;
if ( oExpr = fAtomicType _parse ( oLexer , oStaticContext ) )
return new cItemType ( oExpr ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cSequenceType ( oItemType , sOccurence ) {
this . itemType = oItemType || null ;
this . occurence = sOccurence || null ;
} ;
cSequenceType . prototype . itemType = null ;
cSequenceType . prototype . occurence = null ;
function fSequenceType _parse ( oLexer , oStaticContext ) {
if ( oLexer . eof ( ) )
return ;
if ( oLexer . peek ( ) == "empty-sequence" && oLexer . peek ( 1 ) == '(' ) {
oLexer . next ( 2 ) ;
if ( oLexer . peek ( ) != ')' )
throw new cException ( "XPST0003"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oLexer . next ( ) ;
2016-12-17 00:33:47 +01:00
return new cSequenceType ; // empty sequence
}
2016-11-29 19:28:07 +01:00
var oExpr ,
sOccurence ;
if ( ! oLexer . eof ( ) && ( oExpr = fItemType _parse ( oLexer , oStaticContext ) ) ) {
sOccurence = oLexer . peek ( ) ;
if ( sOccurence == '?' || sOccurence == '*' || sOccurence == '+' )
oLexer . next ( ) ;
else
sOccurence = null ;
return new cSequenceType ( oExpr , sOccurence ) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cSingleType ( oItemType , sOccurence ) {
this . itemType = oItemType || null ;
this . occurence = sOccurence || null ;
} ;
cSingleType . prototype . itemType = null ;
cSingleType . prototype . occurence = null ;
function fSingleType _parse ( oLexer , oStaticContext ) {
var oExpr ,
sOccurence ;
if ( ! oLexer . eof ( ) && ( oExpr = fAtomicType _parse ( oLexer , oStaticContext ) ) ) {
sOccurence = oLexer . peek ( ) ;
if ( sOccurence == '?' )
oLexer . next ( ) ;
else
sOccurence = null ;
return new cSingleType ( oExpr , sOccurence ) ;
}
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSAnyType ( ) {
} ;
cXSAnyType . prototype . builtInKind = cXSConstants . ANYTYPE _DT ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSAnySimpleType ( ) {
} ;
cXSAnySimpleType . prototype = new cXSAnyType ;
cXSAnySimpleType . prototype . builtInKind = cXSConstants . ANYSIMPLETYPE _DT ;
cXSAnySimpleType . prototype . primitiveKind = null ;
2016-12-17 00:33:47 +01:00
cXSAnySimpleType . PRIMITIVE _ANYURI = "anyURI" ; //18;
cXSAnySimpleType . PRIMITIVE _BASE64BINARY = "base64Binary" ; // 17;
cXSAnySimpleType . PRIMITIVE _BOOLEAN = "boolean" ; // 3;
cXSAnySimpleType . PRIMITIVE _DATE = "date" ; // 10;
cXSAnySimpleType . PRIMITIVE _DATETIME = "dateTime" ; // 8;
cXSAnySimpleType . PRIMITIVE _DECIMAL = "decimal" ; // 4;
cXSAnySimpleType . PRIMITIVE _DOUBLE = "double" ; // 6;
cXSAnySimpleType . PRIMITIVE _DURATION = "duration" ; // 7;
cXSAnySimpleType . PRIMITIVE _FLOAT = "float" ; // 5;
cXSAnySimpleType . PRIMITIVE _GDAY = "gDay" ; // 14;
cXSAnySimpleType . PRIMITIVE _GMONTH = "gMonth" ; // 15;
cXSAnySimpleType . PRIMITIVE _GMONTHDAY = "gMonthDay" ; // 13;
cXSAnySimpleType . PRIMITIVE _GYEAR = "gYear" ; // 12;
cXSAnySimpleType . PRIMITIVE _GYEARMONTH = "gYearMonth" ; // 11;
cXSAnySimpleType . PRIMITIVE _HEXBINARY = "hexBinary" ; // 16;
cXSAnySimpleType . PRIMITIVE _NOTATION = "NOTATION" ; // 20;
cXSAnySimpleType . PRIMITIVE _QNAME = "QName" ; // 19;
cXSAnySimpleType . PRIMITIVE _STRING = "string" ; // 2;
cXSAnySimpleType . PRIMITIVE _TIME = "time" ; // 9;
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSAnyAtomicType ( ) {
} ;
cXSAnyAtomicType . prototype = new cXSAnySimpleType ;
cXSAnyAtomicType . prototype . builtInKind = cXSConstants . ANYATOMICTYPE _DT ;
cXSAnyAtomicType . cast = function ( vValue ) {
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
) ; // {http://www.w3.org/2001/XMLSchema}anyAtomicType
} ;
2016-11-29 19:28:07 +01:00
function fXSAnyAtomicType _isNumeric ( vItem ) {
return vItem instanceof cXSFloat || vItem instanceof cXSDouble || vItem instanceof cXSDecimal ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "anyAtomicType" , cXSAnyAtomicType ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSAnyURI ( sScheme , sAuthority , sPath , sQuery , sFragment ) {
this . scheme = sScheme ;
this . authority = sAuthority ;
this . path = sPath ;
this . query = sQuery ;
this . fragment = sFragment ;
} ;
cXSAnyURI . prototype = new cXSAnyAtomicType ;
cXSAnyURI . prototype . builtInKind = cXSConstants . ANYURI _DT ;
cXSAnyURI . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _ANYURI ;
cXSAnyURI . prototype . scheme = null ;
cXSAnyURI . prototype . authority = null ;
cXSAnyURI . prototype . path = null ;
cXSAnyURI . prototype . query = null ;
cXSAnyURI . prototype . fragment = null ;
cXSAnyURI . prototype . toString = function ( ) {
return ( this . scheme ? this . scheme + ':' : '' )
+ ( this . authority ? '/' + '/' + this . authority : '' )
+ ( this . path ? this . path : '' )
+ ( this . query ? '?' + this . query : '' )
+ ( this . fragment ? '#' + this . fragment : '' ) ;
} ;
2016-12-17 00:33:47 +01:00
var rXSAnyURI = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/ ; // http://tools.ietf.org/html/rfc3986
cXSAnyURI . cast = function ( vValue ) {
2016-11-29 19:28:07 +01:00
if ( vValue instanceof cXSAnyURI )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch ;
if ( aMatch = fString _trim ( vValue ) . match ( rXSAnyURI ) )
return new cXSAnyURI ( aMatch [ 2 ] , aMatch [ 4 ] , aMatch [ 5 ] , aMatch [ 7 ] , aMatch [ 9 ] ) ;
throw new cException ( "FORG0001" ) ;
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "anyURI" , cXSAnyURI ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSBase64Binary ( sValue ) {
this . value = sValue ;
} ;
cXSBase64Binary . prototype = new cXSAnyAtomicType ;
cXSBase64Binary . prototype . builtInKind = cXSConstants . BASE64BINARY _DT ;
cXSBase64Binary . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _BASE64BINARY ;
cXSBase64Binary . prototype . value = null ;
cXSBase64Binary . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSBase64Binary . prototype . toString = function ( ) {
return this . value ;
} ;
var rXSBase64Binary = /^((([A-Za-z0-9+\/]\s*){4})*(([A-Za-z0-9+\/]\s*){3}[A-Za-z0-9+\/]|([A-Za-z0-9+\/]\s*){2}[AEIMQUYcgkosw048]\s*=|[A-Za-z0-9+\/]\s*[AQgw]\s*=\s*=))?$/ ;
cXSBase64Binary . cast = function ( vValue ) {
if ( vValue instanceof cXSBase64Binary )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSBase64Binary ) ;
if ( aMatch )
return new cXSBase64Binary ( aMatch [ 0 ] ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSHexBinary ) {
var aMatch = vValue . valueOf ( ) . match ( /.{2}/g ) ,
aValue = [ ] ;
for ( var nIndex = 0 , nLength = aMatch . length ; nIndex < nLength ; nIndex ++ )
aValue . push ( cString . fromCharCode ( fWindow _parseInt ( aMatch [ nIndex ] , 16 ) ) ) ;
return new cXSBase64Binary ( fWindow _btoa ( aValue . join ( '' ) ) ) ;
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "base64Binary" , cXSBase64Binary ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSBoolean ( bValue ) {
this . value = bValue ;
} ;
cXSBoolean . prototype = new cXSAnyAtomicType ;
cXSBoolean . prototype . builtInKind = cXSConstants . BOOLEAN _DT ;
cXSBoolean . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _BOOLEAN ;
cXSBoolean . prototype . value = null ;
cXSBoolean . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSBoolean . prototype . toString = function ( ) {
return cString ( this . value ) ;
} ;
var rXSBoolean = /^(0|1|true|false)$/ ;
cXSBoolean . cast = function ( vValue ) {
if ( vValue instanceof cXSBoolean )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch ;
if ( aMatch = fString _trim ( vValue ) . match ( rXSBoolean ) )
return new cXSBoolean ( aMatch [ 1 ] == '1' || aMatch [ 1 ] == "true" ) ;
throw new cException ( "FORG0001" ) ;
}
if ( fXSAnyAtomicType _isNumeric ( vValue ) )
return new cXSBoolean ( vValue != 0 ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "boolean" , cXSBoolean ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDate ( nYear , nMonth , nDay , nTimezone , bNegative ) {
this . year = nYear ;
this . month = nMonth ;
this . day = nDay ;
this . timezone = nTimezone ;
this . negative = bNegative ;
} ;
cXSDate . prototype = new cXSAnyAtomicType ;
cXSDate . prototype . builtInKind = cXSConstants . DATE _DT ;
cXSDate . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _DATE ;
cXSDate . prototype . year = null ;
cXSDate . prototype . month = null ;
cXSDate . prototype . day = null ;
cXSDate . prototype . timezone = null ;
cXSDate . prototype . negative = null ;
cXSDate . prototype . toString = function ( ) {
return fXSDateTime _getDateComponent ( this )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSDate = /^(-?)([1-9]\d\d\d+|0\d\d\d)-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSDate . cast = function ( vValue ) {
if ( vValue instanceof cXSDate )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDate ) ;
if ( aMatch ) {
var nYear = + aMatch [ 2 ] ,
nMonth = + aMatch [ 3 ] ,
nDay = + aMatch [ 4 ] ;
if ( nDay - 1 < fXSDate _getDaysForYearMonth ( nYear , nMonth ) )
return new cXSDate ( nYear ,
nMonth ,
nDay ,
aMatch [ 5 ] ? aMatch [ 5 ] == 'Z' ? 0 : ( aMatch [ 6 ] == '-' ? - 1 : 1 ) * ( aMatch [ 7 ] * 60 + aMatch [ 8 ] * 1 ) : null ,
aMatch [ 1 ] == '-'
) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "FORG0001"
2016-11-29 19:28:07 +01:00
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDateTime )
return new cXSDate ( vValue . year , vValue . month , vValue . day , vValue . timezone , vValue . negative ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
// Utilities
2016-11-29 19:28:07 +01:00
var aXSDate _days = [ 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ] ;
function fXSDate _getDaysForYearMonth ( nYear , nMonth ) {
return nMonth == 2 && ( nYear % 400 == 0 || nYear % 100 != 0 && nYear % 4 == 0 ) ? 29 : aXSDate _days [ nMonth - 1 ] ;
} ;
function fXSDate _normalize ( oValue , bDay ) {
2016-12-17 00:33:47 +01:00
// Adjust day for month/year
if ( ! bDay ) {
2016-11-29 19:28:07 +01:00
var nDay = fXSDate _getDaysForYearMonth ( oValue . year , oValue . month ) ;
if ( oValue . day > nDay ) {
while ( oValue . day > nDay ) {
oValue . month += 1 ;
if ( oValue . month > 12 ) {
oValue . year += 1 ;
if ( oValue . year == 0 )
oValue . year = 1 ;
oValue . month = 1 ;
}
oValue . day -= nDay ;
nDay = fXSDate _getDaysForYearMonth ( oValue . year , oValue . month ) ;
}
}
else
if ( oValue . day < 1 ) {
while ( oValue . day < 1 ) {
oValue . month -= 1 ;
if ( oValue . month < 1 ) {
oValue . year -= 1 ;
if ( oValue . year == 0 )
oValue . year = - 1 ;
oValue . month = 12 ;
}
nDay = fXSDate _getDaysForYearMonth ( oValue . year , oValue . month ) ;
oValue . day += nDay ;
}
}
}
2016-12-17 00:33:47 +01:00
//? else
// Adjust month
if ( oValue . month > 12 ) {
2016-11-29 19:28:07 +01:00
oValue . year += ~ ~ ( oValue . month / 12 ) ;
if ( oValue . year == 0 )
oValue . year = 1 ;
oValue . month = oValue . month % 12 ;
}
else
if ( oValue . month < 1 ) {
oValue . year += ~ ~ ( oValue . month / 12 ) - 1 ;
if ( oValue . year == 0 )
oValue . year = - 1 ;
oValue . month = oValue . month % 12 + 12 ;
}
return oValue ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "date" , cXSDate ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDateTime ( nYear , nMonth , nDay , nHours , nMinutes , nSeconds , nTimezone , bNegative ) {
this . year = nYear ;
this . month = nMonth ;
this . day = nDay ;
this . hours = nHours ;
this . minutes = nMinutes ;
this . seconds = nSeconds ;
this . timezone = nTimezone ;
this . negative = bNegative ;
} ;
cXSDateTime . prototype = new cXSAnyAtomicType ;
cXSDateTime . prototype . builtInKind = cXSConstants . DATETIME _DT ;
cXSDateTime . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _DATETIME ;
cXSDateTime . prototype . year = null ;
cXSDateTime . prototype . month = null ;
cXSDateTime . prototype . day = null ;
cXSDateTime . prototype . hours = null ;
cXSDateTime . prototype . minutes = null ;
cXSDateTime . prototype . seconds = null ;
cXSDateTime . prototype . timezone = null ;
cXSDateTime . prototype . negative = null ;
cXSDateTime . prototype . toString = function ( ) {
return fXSDateTime _getDateComponent ( this )
+ 'T'
+ fXSDateTime _getTimeComponent ( this )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSDateTime = /^(-?)([1-9]\d\d\d+|0\d\d\d)-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(?:\.(\d+))?|(24:00:00)(?:\.(0+))?)(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSDateTime . cast = function ( vValue ) {
if ( vValue instanceof cXSDateTime )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDateTime ) ;
if ( aMatch ) {
var nYear = + aMatch [ 2 ] ,
nMonth = + aMatch [ 3 ] ,
nDay = + aMatch [ 4 ] ,
bValue = ! ! aMatch [ 10 ] ;
if ( nDay - 1 < fXSDate _getDaysForYearMonth ( nYear , nMonth ) )
return fXSDateTime _normalize ( new cXSDateTime ( nYear ,
nMonth ,
nDay ,
bValue ? 24 : + aMatch [ 6 ] ,
bValue ? 0 : + aMatch [ 7 ] ,
cNumber ( ( bValue ? 0 : aMatch [ 8 ] ) + '.' + ( bValue ? 0 : aMatch [ 9 ] || 0 ) ) ,
aMatch [ 12 ] ? aMatch [ 12 ] == 'Z' ? 0 : ( aMatch [ 13 ] == '-' ? - 1 : 1 ) * ( aMatch [ 14 ] * 60 + aMatch [ 15 ] * 1 ) : null ,
aMatch [ 1 ] == '-'
) ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "FORG0001"
2016-11-29 19:28:07 +01:00
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate )
return new cXSDateTime ( vValue . year , vValue . month , vValue . day , 0 , 0 , 0 , vValue . timezone , vValue . negative ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
// Utilities
2016-11-29 19:28:07 +01:00
function fXSDateTime _pad ( vValue , nLength ) {
var sValue = cString ( vValue ) ;
if ( arguments . length < 2 )
nLength = 2 ;
return ( sValue . length < nLength + 1 ? new cArray ( nLength + 1 - sValue . length ) . join ( '0' ) : '' ) + sValue ;
} ;
function fXSDateTime _getTZComponent ( oDateTime ) {
var nTimezone = oDateTime . timezone ;
return nTimezone == null
? ''
: nTimezone
? ( nTimezone > 0 ? '+' : '-' )
+ fXSDateTime _pad ( cMath . abs ( ~ ~ ( nTimezone / 60 ) ) )
+ ':'
+ fXSDateTime _pad ( cMath . abs ( nTimezone % 60 ) )
: 'Z' ;
} ;
function fXSDateTime _getDateComponent ( oDateTime ) {
return ( oDateTime . negative ? '-' : '' )
+ fXSDateTime _pad ( oDateTime . year , 4 )
+ '-' + fXSDateTime _pad ( oDateTime . month )
+ '-' + fXSDateTime _pad ( oDateTime . day ) ;
} ;
function fXSDateTime _getTimeComponent ( oDateTime ) {
var aValue = cString ( oDateTime . seconds ) . split ( '.' ) ;
return fXSDateTime _pad ( oDateTime . hours )
+ ':' + fXSDateTime _pad ( oDateTime . minutes )
+ ':' + fXSDateTime _pad ( aValue [ 0 ] )
+ ( aValue . length > 1 ? '.' + aValue [ 1 ] : '' ) ;
} ;
function fXSDateTime _normalize ( oValue ) {
return fXSDate _normalize ( fXSTime _normalize ( oValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "dateTime" , cXSDateTime ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDecimal ( nValue ) {
this . value = nValue ;
} ;
cXSDecimal . prototype = new cXSAnyAtomicType ;
cXSDecimal . prototype . builtInKind = cXSConstants . DECIMAL _DT ;
cXSDecimal . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _DECIMAL ;
cXSDecimal . prototype . value = null ;
cXSDecimal . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSDecimal . prototype . toString = function ( ) {
return cString ( this . value ) ;
} ;
var rXSDecimal = /^[+\-]?((\d+(\.\d*)?)|(\.\d+))$/ ;
cXSDecimal . cast = function ( vValue ) {
if ( vValue instanceof cXSDecimal )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDecimal ) ;
if ( aMatch )
return new cXSDecimal ( + vValue ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSBoolean )
return new cXSDecimal ( vValue * 1 ) ;
if ( fXSAnyAtomicType _isNumeric ( vValue ) ) {
2016-12-17 00:33:47 +01:00
if ( ! fIsNaN ( vValue ) && fIsFinite ( vValue ) )
return new cXSDecimal ( + vValue ) ;
throw new cException ( "FOCA0002"
) ;
2016-11-29 19:28:07 +01:00
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "decimal" , cXSDecimal ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDouble ( nValue ) {
this . value = nValue ;
} ;
cXSDouble . prototype = new cXSAnyAtomicType ;
cXSDouble . prototype . builtInKind = cXSConstants . DOUBLE _DT ;
cXSDouble . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _DOUBLE ;
cXSDouble . prototype . value = null ;
cXSDouble . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSDouble . prototype . toString = function ( ) {
return cString ( this . value ) ;
} ;
var rXSDouble = /^([+\-]?((\d+(\.\d*)?)|(\.\d+))([eE][+\-]?\d+)?|(-?INF)|NaN)$/ ;
cXSDouble . cast = function ( vValue ) {
if ( vValue instanceof cXSDouble )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDouble ) ;
if ( aMatch )
return new cXSDouble ( aMatch [ 7 ] ? + aMatch [ 7 ] . replace ( "INF" , "Infinity" ) : + vValue ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSBoolean )
return new cXSDouble ( vValue * 1 ) ;
if ( fXSAnyAtomicType _isNumeric ( vValue ) )
return new cXSDouble ( vValue . value ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "double" , cXSDouble ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDuration ( nYear , nMonth , nDay , nHours , nMinutes , nSeconds , bNegative ) {
this . year = nYear ;
this . month = nMonth ;
this . day = nDay ;
this . hours = nHours ;
this . minutes = nMinutes ;
this . seconds = nSeconds ;
this . negative = bNegative ;
} ;
cXSDuration . prototype = new cXSAnyAtomicType ;
cXSDuration . prototype . builtInKind = cXSConstants . DURATION _DT ;
cXSDuration . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _DURATION ;
cXSDuration . prototype . year = null ;
cXSDuration . prototype . month = null ;
cXSDuration . prototype . day = null ;
cXSDuration . prototype . hours = null ;
cXSDuration . prototype . minutes = null ;
cXSDuration . prototype . seconds = null ;
cXSDuration . prototype . negative = null ;
cXSDuration . prototype . toString = function ( ) {
return ( this . negative ? '-' : '' ) + 'P'
+ ( ( fXSDuration _getYearMonthComponent ( this ) + fXSDuration _getDayTimeComponent ( this ) ) || 'T0S' ) ;
} ;
var rXSDuration = /^(-)?P(?:([0-9]+)Y)?(?:([0-9]+)M)?(?:([0-9]+)D)?(?:T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:((?:(?:[0-9]+(?:.[0-9]*)?)|(?:.[0-9]+)))S)?)?$/ ;
cXSDuration . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
if ( vValue instanceof cXSDuration )
return vValue ;
2016-11-29 19:28:07 +01:00
if ( vValue instanceof cXSYearMonthDuration )
return new cXSDuration ( vValue . year , vValue . month , 0 , 0 , 0 , 0 , vValue . negative ) ;
if ( vValue instanceof cXSDayTimeDuration )
return new cXSDuration ( 0 , 0 , vValue . day , vValue . hours , vValue . minutes , vValue . seconds , vValue . negative ) ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDuration ) ;
if ( aMatch )
return fXSDuration _normalize ( new cXSDuration ( + aMatch [ 2 ] || 0 , + aMatch [ 3 ] || 0 , + aMatch [ 4 ] || 0 , + aMatch [ 5 ] || 0 , + aMatch [ 6 ] || 0 , + aMatch [ 7 ] || 0 , aMatch [ 1 ] == '-' ) ) ;
throw new cException ( "FORG0001" ) ;
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
// Utilities
2016-11-29 19:28:07 +01:00
function fXSDuration _getYearMonthComponent ( oDuration ) {
return ( oDuration . year ? oDuration . year + 'Y' : '' )
+ ( oDuration . month ? oDuration . month + 'M' : '' ) ;
} ;
function fXSDuration _getDayTimeComponent ( oDuration ) {
return ( oDuration . day ? oDuration . day + 'D' : '' )
+ ( oDuration . hours || oDuration . minutes || oDuration . seconds
? 'T'
+ ( oDuration . hours ? oDuration . hours + 'H' : '' )
+ ( oDuration . minutes ? oDuration . minutes + 'M' : '' )
+ ( oDuration . seconds ? oDuration . seconds + 'S' : '' )
: '' ) ;
} ;
function fXSDuration _normalize ( oDuration ) {
return fXSYearMonthDuration _normalize ( fXSDayTimeDuration _normalize ( oDuration ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "duration" , cXSDuration ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSFloat ( nValue ) {
this . value = nValue ;
} ;
cXSFloat . prototype = new cXSAnyAtomicType ;
cXSFloat . prototype . builtInKind = cXSConstants . FLOAT _DT ;
cXSFloat . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _FLOAT ;
cXSFloat . prototype . value = null ;
cXSFloat . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSFloat . prototype . toString = function ( ) {
return cString ( this . value ) ;
} ;
var rXSFloat = /^([+\-]?((\d+(\.\d*)?)|(\.\d+))([eE][+\-]?\d+)?|(-?INF)|NaN)$/ ;
cXSFloat . cast = function ( vValue ) {
if ( vValue instanceof cXSFloat )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSFloat ) ;
if ( aMatch )
return new cXSFloat ( aMatch [ 7 ] ? + aMatch [ 7 ] . replace ( "INF" , "Infinity" ) : + vValue ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSBoolean )
return new cXSFloat ( vValue * 1 ) ;
if ( fXSAnyAtomicType _isNumeric ( vValue ) )
return new cXSFloat ( vValue . value ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "float" , cXSFloat ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSGDay ( nDay , nTimezone ) {
this . day = nDay ;
this . timezone = nTimezone ;
} ;
cXSGDay . prototype = new cXSAnyAtomicType ;
cXSGDay . prototype . builtInKind = cXSConstants . GDAY _DT ;
cXSGDay . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _GDAY ;
cXSGDay . prototype . day = null ;
cXSGDay . prototype . timezone = null ;
cXSGDay . prototype . toString = function ( ) {
return '-'
+ '-'
+ '-' + fXSDateTime _pad ( this . day )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSGDay = /^---(0[1-9]|[12]\d|3[01])(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSGDay . cast = function ( vValue ) {
if ( vValue instanceof cXSGDay )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSGDay ) ;
if ( aMatch ) {
var nDay = + aMatch [ 1 ] ;
return new cXSGDay ( nDay ,
aMatch [ 2 ] ? aMatch [ 2 ] == 'Z' ? 0 : ( aMatch [ 3 ] == '-' ? - 1 : 1 ) * ( aMatch [ 4 ] * 60 + aMatch [ 5 ] * 1 ) : null
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate || vValue instanceof cXSDateTime )
return new cXSGDay ( vValue . day , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "gDay" , cXSGDay ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSGMonth ( nMonth , nTimezone ) {
this . month = nMonth ;
this . timezone = nTimezone ;
} ;
cXSGMonth . prototype = new cXSAnyAtomicType ;
cXSGMonth . prototype . builtInKind = cXSConstants . GMONTH _DT ;
cXSGMonth . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _GMONTH ;
cXSGMonth . prototype . month = null ;
cXSGMonth . prototype . timezone = null ;
cXSGMonth . prototype . toString = function ( ) {
return '-'
+ '-' + fXSDateTime _pad ( this . month )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSGMonth = /^--(0[1-9]|1[0-2])(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSGMonth . cast = function ( vValue ) {
if ( vValue instanceof cXSGMonth )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSGMonth ) ;
if ( aMatch ) {
var nMonth = + aMatch [ 1 ] ;
return new cXSGMonth ( nMonth ,
aMatch [ 2 ] ? aMatch [ 2 ] == 'Z' ? 0 : ( aMatch [ 3 ] == '-' ? - 1 : 1 ) * ( aMatch [ 4 ] * 60 + aMatch [ 5 ] * 1 ) : null
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate || vValue instanceof cXSDateTime )
return new cXSGMonth ( vValue . month , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "gMonth" , cXSGMonth ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSGMonthDay ( nMonth , nDay , nTimezone ) {
this . month = nMonth ;
this . day = nDay ;
this . timezone = nTimezone ;
} ;
cXSGMonthDay . prototype = new cXSAnyAtomicType ;
cXSGMonthDay . prototype . builtInKind = cXSConstants . GMONTHDAY _DT ;
cXSGMonthDay . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _GMONTHDAY ;
cXSGMonthDay . prototype . month = null ;
cXSGMonthDay . prototype . day = null ;
cXSGMonthDay . prototype . timezone = null ;
cXSGMonthDay . prototype . toString = function ( ) {
return '-'
+ '-' + fXSDateTime _pad ( this . month )
+ '-' + fXSDateTime _pad ( this . day )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSGMonthDay = /^--(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSGMonthDay . cast = function ( vValue ) {
if ( vValue instanceof cXSGMonthDay )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSGMonthDay ) ;
if ( aMatch ) {
var nMonth = + aMatch [ 1 ] ,
nDay = + aMatch [ 2 ] ;
if ( nDay - 1 < fXSDate _getDaysForYearMonth ( 1976 , nMonth ) )
return new cXSGMonthDay ( nMonth ,
nDay ,
aMatch [ 3 ] ? aMatch [ 3 ] == 'Z' ? 0 : ( aMatch [ 4 ] == '-' ? - 1 : 1 ) * ( aMatch [ 5 ] * 60 + aMatch [ 6 ] * 1 ) : null
) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "FORG0001"
2016-11-29 19:28:07 +01:00
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate || vValue instanceof cXSDateTime )
return new cXSGMonthDay ( vValue . month , vValue . day , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "gMonthDay" , cXSGMonthDay ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSGYear ( nYear , nTimezone ) {
this . year = nYear ;
this . timezone = nTimezone ;
} ;
cXSGYear . prototype = new cXSAnyAtomicType ;
cXSGYear . prototype . builtInKind = cXSConstants . GYEAR _DT ;
cXSGYear . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _GYEAR ;
cXSGYear . prototype . year = null ;
cXSGYear . prototype . timezone = null ;
cXSGYear . prototype . toString = function ( ) {
return fXSDateTime _pad ( this . year )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSGYear = /^-?([1-9]\d\d\d+|0\d\d\d)(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSGYear . cast = function ( vValue ) {
if ( vValue instanceof cXSGYear )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSGYear ) ;
if ( aMatch ) {
var nYear = + aMatch [ 1 ] ;
return new cXSGYear ( nYear ,
aMatch [ 2 ] ? aMatch [ 2 ] == 'Z' ? 0 : ( aMatch [ 3 ] == '-' ? - 1 : 1 ) * ( aMatch [ 4 ] * 60 + aMatch [ 5 ] * 1 ) : null
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate || vValue instanceof cXSDateTime )
return new cXSGYear ( vValue . year , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "gYear" , cXSGYear ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSGYearMonth ( nYear , nMonth , nTimezone ) {
this . year = nYear ;
this . month = nMonth ;
this . timezone = nTimezone ;
} ;
cXSGYearMonth . prototype = new cXSAnyAtomicType ;
cXSGYearMonth . prototype . builtInKind = cXSConstants . GYEARMONTH _DT ;
cXSGYearMonth . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _GYEARMONTH ;
cXSGYearMonth . prototype . year = null ;
cXSGYearMonth . prototype . month = null ;
cXSGYearMonth . prototype . timezone = null ;
cXSGYearMonth . prototype . toString = function ( ) {
return fXSDateTime _pad ( this . year )
+ '-' + fXSDateTime _pad ( this . month )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSGYearMonth = /^-?([1-9]\d\d\d+|0\d\d\d)-(0[1-9]|1[0-2])(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSGYearMonth . cast = function ( vValue ) {
if ( vValue instanceof cXSGYearMonth )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSGYearMonth ) ;
if ( aMatch ) {
var nYear = + aMatch [ 1 ] ,
nMonth = + aMatch [ 2 ] ;
return new cXSGYearMonth ( nYear ,
nMonth ,
aMatch [ 3 ] ? aMatch [ 3 ] == 'Z' ? 0 : ( aMatch [ 4 ] == '-' ? - 1 : 1 ) * ( aMatch [ 5 ] * 60 + aMatch [ 6 ] * 1 ) : null
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDate || vValue instanceof cXSDateTime )
return new cXSGYearMonth ( vValue . year , vValue . month , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "gYearMonth" , cXSGYearMonth ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSHexBinary ( sValue ) {
this . value = sValue ;
} ;
cXSHexBinary . prototype = new cXSAnyAtomicType ;
cXSHexBinary . prototype . builtInKind = cXSConstants . HEXBINARY _DT ;
cXSHexBinary . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _HEXBINARY ;
cXSHexBinary . prototype . value = null ;
cXSHexBinary . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSHexBinary . prototype . toString = function ( ) {
return this . value ;
} ;
var rXSHexBinary = /^([0-9a-fA-F]{2})*$/ ;
cXSHexBinary . cast = function ( vValue ) {
if ( vValue instanceof cXSHexBinary )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSHexBinary ) ;
if ( aMatch )
return new cXSHexBinary ( aMatch [ 0 ] . toUpperCase ( ) ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSBase64Binary ) {
var sValue = fWindow _atob ( vValue . valueOf ( ) ) ,
aValue = [ ] ;
for ( var nIndex = 0 , nLength = sValue . length , sLetter ; nIndex < nLength ; nIndex ++ ) {
sLetter = sValue . charCodeAt ( nIndex ) . toString ( 16 ) ;
aValue . push ( new cArray ( 3 - sLetter . length ) . join ( '0' ) + sLetter ) ;
}
return new cXSHexBinary ( aValue . join ( '' ) ) ;
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "hexBinary" , cXSHexBinary ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNOTATION ( ) {
} ;
cXSNOTATION . prototype = new cXSAnyAtomicType ;
cXSNOTATION . prototype . builtInKind = cXSConstants . NOTATION _DT ;
cXSNOTATION . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _NOTATION ;
cXSNOTATION . cast = function ( vValue ) {
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
) ; // {http://www.w3.org/2001/XMLSchema}NOTATION
} ;
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "NOTATION" , cXSNOTATION ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSQName ( sPrefix , sLocalName , sNameSpaceURI ) {
this . prefix = sPrefix ;
this . localName = sLocalName ;
this . namespaceURI = sNameSpaceURI ;
} ;
cXSQName . prototype = new cXSAnyAtomicType ;
cXSQName . prototype . builtInKind = cXSConstants . QNAME _DT ;
cXSQName . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _QNAME ;
cXSQName . prototype . prefix = null ;
cXSQName . prototype . localName = null ;
cXSQName . prototype . namespaceURI = null ;
cXSQName . prototype . toString = function ( ) {
return ( this . prefix ? this . prefix + ':' : '' ) + this . localName ;
} ;
2016-12-17 00:33:47 +01:00
var rXSQName = /^(?:(?![0-9-])(\w[\w.-]*)\:)?(?![0-9-])(\w[\w.-]*)$/ ;
2016-11-29 19:28:07 +01:00
cXSQName . cast = function ( vValue ) {
if ( vValue instanceof cXSQName )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSQName ) ;
if ( aMatch )
return new cXSQName ( aMatch [ 1 ] || null , aMatch [ 2 ] , null ) ;
throw new cException ( "FORG0001" ) ;
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "QName" , cXSQName ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSString ( sValue ) {
this . value = sValue ;
} ;
cXSString . prototype = new cXSAnyAtomicType ;
cXSString . prototype . value = null ;
cXSString . prototype . builtInKind = cXSConstants . STRING _DT ;
cXSString . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _STRING ;
cXSString . prototype . valueOf = function ( ) {
return this . value ;
} ;
cXSString . prototype . toString = function ( ) {
return this . value ;
} ;
cXSString . cast = function ( vValue ) {
return new cXSString ( cString ( vValue ) ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "string" , cXSString ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSTime ( nHours , nMinutes , nSeconds , nTimezone ) {
this . hours = nHours ;
this . minutes = nMinutes ;
this . seconds = nSeconds ;
this . timezone = nTimezone ;
} ;
cXSTime . prototype = new cXSAnyAtomicType ;
cXSTime . prototype . builtInKind = cXSConstants . TIME _DT ;
cXSTime . prototype . primitiveKind = cXSAnySimpleType . PRIMITIVE _TIME ;
cXSTime . prototype . hours = null ;
cXSTime . prototype . minutes = null ;
cXSTime . prototype . seconds = null ;
cXSTime . prototype . timezone = null ;
cXSTime . prototype . toString = function ( ) {
return fXSDateTime _getTimeComponent ( this )
+ fXSDateTime _getTZComponent ( this ) ;
} ;
var rXSTime = /^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(?:\.(\d+))?|(24:00:00)(?:\.(0+))?)(Z|([+\-])(0\d|1[0-4]):([0-5]\d))?$/ ;
cXSTime . cast = function ( vValue ) {
if ( vValue instanceof cXSTime )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSTime ) ;
if ( aMatch ) {
var bValue = ! ! aMatch [ 6 ] ;
return new cXSTime ( bValue ? 0 : + aMatch [ 2 ] ,
bValue ? 0 : + aMatch [ 3 ] ,
cNumber ( ( bValue ? 0 : aMatch [ 4 ] ) + '.' + ( bValue ? 0 : aMatch [ 5 ] || 0 ) ) ,
aMatch [ 8 ] ? aMatch [ 8 ] == 'Z' ? 0 : ( aMatch [ 9 ] == '-' ? - 1 : 1 ) * ( aMatch [ 10 ] * 60 + aMatch [ 11 ] * 1 ) : null
) ;
}
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDateTime )
return new cXSTime ( vValue . hours , vValue . minutes , vValue . seconds , vValue . timezone ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fXSTime _normalize ( oValue ) {
2016-12-17 00:33:47 +01:00
//
if ( oValue . seconds >= 60 || oValue . seconds < 0 ) {
2016-11-29 19:28:07 +01:00
oValue . minutes += ~ ~ ( oValue . seconds / 60 ) - ( oValue . seconds < 0 && oValue . seconds % 60 ? 1 : 0 ) ;
oValue . seconds = oValue . seconds % 60 + ( oValue . seconds < 0 && oValue . seconds % 60 ? 60 : 0 ) ;
}
2016-12-17 00:33:47 +01:00
//
if ( oValue . minutes >= 60 || oValue . minutes < 0 ) {
2016-11-29 19:28:07 +01:00
oValue . hours += ~ ~ ( oValue . minutes / 60 ) - ( oValue . minutes < 0 && oValue . minutes % 60 ? 1 : 0 ) ;
oValue . minutes = oValue . minutes % 60 + ( oValue . minutes < 0 && oValue . minutes % 60 ? 60 : 0 ) ;
}
2016-12-17 00:33:47 +01:00
//
if ( oValue . hours >= 24 || oValue . hours < 0 ) {
2016-11-29 19:28:07 +01:00
if ( oValue instanceof cXSDateTime )
oValue . day += ~ ~ ( oValue . hours / 24 ) - ( oValue . hours < 0 && oValue . hours % 24 ? 1 : 0 ) ;
oValue . hours = oValue . hours % 24 + ( oValue . hours < 0 && oValue . hours % 24 ? 24 : 0 ) ;
}
2016-12-17 00:33:47 +01:00
//
return oValue ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "time" , cXSTime ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSUntypedAtomic ( sValue ) {
this . value = sValue ;
} ;
cXSUntypedAtomic . prototype = new cXSAnyAtomicType ;
cXSUntypedAtomic . prototype . builtInKind = cXSConstants . XT _UNTYPEDATOMIC _DT ;
cXSUntypedAtomic . prototype . toString = function ( ) {
return cString ( this . value ) ;
} ;
cXSUntypedAtomic . cast = function ( vValue ) {
if ( vValue instanceof cXSUntypedAtomic )
return vValue ;
return new cXSUntypedAtomic ( cString ( vValue ) ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "untypedAtomic" , cXSUntypedAtomic ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSYearMonthDuration ( nYear , nMonth , bNegative ) {
cXSDuration . call ( this , nYear , nMonth , 0 , 0 , 0 , 0 , bNegative ) ;
} ;
cXSYearMonthDuration . prototype = new cXSDuration ;
cXSYearMonthDuration . prototype . builtInKind = cXSConstants . XT _YEARMONTHDURATION _DT ;
cXSYearMonthDuration . prototype . toString = function ( ) {
return ( this . negative ? '-' : '' ) + 'P'
+ ( fXSDuration _getYearMonthComponent ( this ) || '0M' ) ;
} ;
var rXSYearMonthDuration = /^(-)?P(?:([0-9]+)Y)?(?:([0-9]+)M)?$/ ;
cXSYearMonthDuration . cast = function ( vValue ) {
if ( vValue instanceof cXSYearMonthDuration )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSYearMonthDuration ) ;
if ( aMatch )
return fXSYearMonthDuration _normalize ( new cXSYearMonthDuration ( + aMatch [ 2 ] || 0 , + aMatch [ 3 ] || 0 , aMatch [ 1 ] == '-' ) ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSDayTimeDuration )
return new cXSYearMonthDuration ( 0 , 0 ) ;
if ( vValue instanceof cXSDuration )
return new cXSYearMonthDuration ( vValue . year , vValue . month , vValue . negative ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fXSYearMonthDuration _normalize ( oDuration ) {
if ( oDuration . month >= 12 ) {
oDuration . year += ~ ~ ( oDuration . month / 12 ) ;
oDuration . month %= 12 ;
}
return oDuration ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "yearMonthDuration" , cXSYearMonthDuration ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSDayTimeDuration ( nDay , nHours , nMinutes , nSeconds , bNegative ) {
cXSDuration . call ( this , 0 , 0 , nDay , nHours , nMinutes , nSeconds , bNegative ) ;
} ;
cXSDayTimeDuration . prototype = new cXSDuration ;
cXSDayTimeDuration . prototype . builtInKind = cXSConstants . DAYTIMEDURATION _DT ;
cXSDayTimeDuration . prototype . toString = function ( ) {
return ( this . negative ? '-' : '' ) + 'P'
+ ( fXSDuration _getDayTimeComponent ( this ) || 'T0S' ) ;
} ;
var rXSDayTimeDuration = /^(-)?P(?:([0-9]+)D)?(?:T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:((?:(?:[0-9]+(?:.[0-9]*)?)|(?:.[0-9]+)))S)?)?$/ ;
cXSDayTimeDuration . cast = function ( vValue ) {
if ( vValue instanceof cXSDayTimeDuration )
return vValue ;
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSDayTimeDuration ) ;
if ( aMatch )
return fXSDayTimeDuration _normalize ( new cXSDayTimeDuration ( + aMatch [ 2 ] || 0 , + aMatch [ 3 ] || 0 , + aMatch [ 4 ] || 0 , + aMatch [ 5 ] || 0 , aMatch [ 1 ] == '-' ) ) ;
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSYearMonthDuration )
return new cXSDayTimeDuration ( 0 , 0 , 0 , 0 ) ;
if ( vValue instanceof cXSDuration )
return new cXSDayTimeDuration ( vValue . day , vValue . hours , vValue . minutes , vValue . seconds , vValue . negative ) ;
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
// Utilities
2016-11-29 19:28:07 +01:00
function fXSDayTimeDuration _normalize ( oDuration ) {
if ( oDuration . seconds >= 60 ) {
oDuration . minutes += ~ ~ ( oDuration . seconds / 60 ) ;
oDuration . seconds %= 60 ;
}
if ( oDuration . minutes >= 60 ) {
oDuration . hours += ~ ~ ( oDuration . minutes / 60 ) ;
oDuration . minutes %= 60 ;
}
if ( oDuration . hours >= 24 ) {
oDuration . day += ~ ~ ( oDuration . hours / 24 ) ;
oDuration . hours %= 24 ;
}
return oDuration ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "dayTimeDuration" , cXSDayTimeDuration ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSInteger ( nValue ) {
this . value = nValue ;
} ;
cXSInteger . prototype = new cXSDecimal ;
cXSInteger . prototype . builtInKind = cXSConstants . INTEGER _DT ;
var rXSInteger = /^[-+]?[0-9]+$/ ;
cXSInteger . cast = function ( vValue ) {
if ( vValue instanceof cXSInteger )
2016-12-17 00:33:47 +01:00
return new cXSInteger ( vValue . value ) ;
2016-11-29 19:28:07 +01:00
if ( vValue instanceof cXSString || vValue instanceof cXSUntypedAtomic ) {
var aMatch = fString _trim ( vValue ) . match ( rXSInteger ) ;
if ( aMatch )
2016-12-17 00:33:47 +01:00
return new cXSInteger ( + vValue ) ;
2016-11-29 19:28:07 +01:00
throw new cException ( "FORG0001" ) ;
}
if ( vValue instanceof cXSBoolean )
return new cXSInteger ( vValue * 1 ) ;
if ( fXSAnyAtomicType _isNumeric ( vValue ) ) {
2016-12-17 00:33:47 +01:00
if ( ! fIsNaN ( vValue ) && fIsFinite ( vValue ) )
return new cXSInteger ( + vValue ) ;
throw new cException ( "FOCA0002"
) ;
2016-11-29 19:28:07 +01:00
}
2016-12-17 00:33:47 +01:00
//
throw new cException ( "XPTY0004"
2016-11-29 19:28:07 +01:00
) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "integer" , cXSInteger ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNonPositiveInteger ( nValue ) {
this . value = nValue ;
} ;
cXSNonPositiveInteger . prototype = new cXSInteger ;
cXSNonPositiveInteger . prototype . builtInKind = cXSConstants . NONPOSITIVEINTEGER _DT ;
cXSNonPositiveInteger . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= 0 )
return new cXSNonPositiveInteger ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "nonPositiveInteger" , cXSNonPositiveInteger ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNegativeInteger ( nValue ) {
this . value = nValue ;
} ;
cXSNegativeInteger . prototype = new cXSNonPositiveInteger ;
cXSNegativeInteger . prototype . builtInKind = cXSConstants . NEGATIVEINTEGER _DT ;
cXSNegativeInteger . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= - 1 )
return new cXSNegativeInteger ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "negativeInteger" , cXSNegativeInteger ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSLong ( nValue ) {
this . value = nValue ;
} ;
cXSLong . prototype = new cXSInteger ;
cXSLong . prototype . builtInKind = cXSConstants . LONG _DT ;
cXSLong . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= 9223372036854775807 && oValue . value >= - 9223372036854775808 )
return new cXSLong ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "long" , cXSLong ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSInt ( nValue ) {
this . value = nValue ;
} ;
cXSInt . prototype = new cXSLong ;
cXSInt . prototype . builtInKind = cXSConstants . INT _DT ;
cXSInt . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= 2147483647 && oValue . value >= - 2147483648 )
return new cXSInt ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "int" , cXSInt ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSShort ( nValue ) {
this . value = nValue ;
} ;
cXSShort . prototype = new cXSInt ;
cXSShort . prototype . builtInKind = cXSConstants . SHORT _DT ;
cXSShort . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= 32767 && oValue . value >= - 32768 )
return new cXSShort ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "short" , cXSShort ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSByte ( nValue ) {
this . value = nValue ;
} ;
cXSByte . prototype = new cXSShort ;
cXSByte . prototype . builtInKind = cXSConstants . BYTE _DT ;
cXSByte . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value <= 127 && oValue . value >= - 128 )
return new cXSByte ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "byte" , cXSByte ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNonNegativeInteger ( nValue ) {
this . value = nValue ;
} ;
cXSNonNegativeInteger . prototype = new cXSInteger ;
cXSNonNegativeInteger . prototype . builtInKind = cXSConstants . NONNEGATIVEINTEGER _DT ;
cXSNonNegativeInteger . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 0 )
return new cXSNonNegativeInteger ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "nonNegativeInteger" , cXSNonNegativeInteger ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSPositiveInteger ( nValue ) {
this . value = nValue ;
} ;
cXSPositiveInteger . prototype = new cXSNonNegativeInteger ;
cXSPositiveInteger . prototype . builtInKind = cXSConstants . POSITIVEINTEGER _DT ;
cXSPositiveInteger . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 1 )
return new cXSPositiveInteger ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "positiveInteger" , cXSPositiveInteger ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSUnsignedLong ( nValue ) {
this . value = nValue ;
} ;
cXSUnsignedLong . prototype = new cXSNonNegativeInteger ;
cXSUnsignedLong . prototype . builtInKind = cXSConstants . UNSIGNEDLONG _DT ;
cXSUnsignedLong . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 1 && oValue . value <= 18446744073709551615 )
return new cXSUnsignedLong ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "unsignedLong" , cXSUnsignedLong ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSUnsignedInt ( nValue ) {
this . value = nValue ;
} ;
cXSUnsignedInt . prototype = new cXSNonNegativeInteger ;
cXSUnsignedInt . prototype . builtInKind = cXSConstants . UNSIGNEDINT _DT ;
cXSUnsignedInt . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 1 && oValue . value <= 4294967295 )
return new cXSUnsignedInt ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "unsignedInt" , cXSUnsignedInt ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSUnsignedShort ( nValue ) {
this . value = nValue ;
} ;
cXSUnsignedShort . prototype = new cXSUnsignedInt ;
cXSUnsignedShort . prototype . builtInKind = cXSConstants . UNSIGNEDSHORT _DT ;
cXSUnsignedShort . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 1 && oValue . value <= 65535 )
return new cXSUnsignedShort ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "unsignedShort" , cXSUnsignedShort ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSUnsignedByte ( nValue ) {
this . value = nValue ;
} ;
cXSUnsignedByte . prototype = new cXSUnsignedShort ;
cXSUnsignedByte . prototype . builtInKind = cXSConstants . UNSIGNEDBYTE _DT ;
cXSUnsignedByte . cast = function ( vValue ) {
2016-12-17 00:33:47 +01:00
var oValue ;
try {
oValue = cXSInteger . cast ( vValue ) ;
}
catch ( oError ) {
throw oError ;
}
// facet validation
if ( oValue . value >= 1 && oValue . value <= 255 )
return new cXSUnsignedByte ( oValue . value ) ;
//
throw new cException ( "FORG0001" ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "unsignedByte" , cXSUnsignedByte ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNormalizedString ( sValue ) {
this . value = sValue ;
} ;
cXSNormalizedString . prototype = new cXSString ;
cXSNormalizedString . prototype . builtInKind = cXSConstants . NORMALIZEDSTRING _DT ;
cXSNormalizedString . cast = function ( vValue ) {
return new cXSNormalizedString ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "normalizedString" , cXSNormalizedString ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSToken ( sValue ) {
this . value = sValue ;
} ;
cXSToken . prototype = new cXSNormalizedString ;
cXSToken . prototype . builtInKind = cXSConstants . TOKEN _DT ;
cXSToken . cast = function ( vValue ) {
return new cXSToken ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "token" , cXSToken ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSName ( sValue ) {
this . value = sValue ;
} ;
cXSName . prototype = new cXSToken ;
cXSName . prototype . builtInKind = cXSConstants . NAME _DT ;
cXSName . cast = function ( vValue ) {
return new cXSName ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "Name" , cXSName ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNCName ( sValue ) {
this . value = sValue ;
} ;
cXSNCName . prototype = new cXSName ;
cXSNCName . prototype . builtInKind = cXSConstants . NCNAME _DT ;
cXSNCName . cast = function ( vValue ) {
return new cXSNCName ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "NCName" , cXSNCName ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSENTITY ( sValue ) {
this . value = sValue ;
} ;
cXSENTITY . prototype = new cXSNCName ;
cXSENTITY . prototype . builtInKind = cXSConstants . ENTITY _DT ;
cXSENTITY . cast = function ( vValue ) {
return new cXSENTITY ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "ENTITY" , cXSENTITY ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSID ( sValue ) {
this . value = sValue ;
} ;
cXSID . prototype = new cXSNCName ;
cXSID . prototype . builtInKind = cXSConstants . ID _DT ;
cXSID . cast = function ( vValue ) {
return new cXSID ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "ID" , cXSID ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
function cXSIDREF ( sValue ) {
this . value = sValue ;
} ;
cXSIDREF . prototype = new cXSNCName ;
cXSIDREF . prototype . builtInKind = cXSConstants . IDREF _DT ;
cXSIDREF . cast = function ( vValue ) {
return new cXSIDREF ( cString ( vValue ) ) ;
} ;
//
fStaticContext _defineSystemDataType ( "IDREF" , cXSIDREF ) ;
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSLanguage ( sValue ) {
this . value = sValue ;
} ;
cXSLanguage . prototype = new cXSToken ;
cXSLanguage . prototype . builtInKind = cXSConstants . LANGUAGE _DT ;
cXSLanguage . cast = function ( vValue ) {
return new cXSLanguage ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "language" , cXSLanguage ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXSNMTOKEN ( sValue ) {
this . value = sValue ;
} ;
cXSNMTOKEN . prototype = new cXSToken ;
cXSNMTOKEN . prototype . builtInKind = cXSConstants . NMTOKEN _DT ;
cXSNMTOKEN . cast = function ( vValue ) {
return new cXSNMTOKEN ( cString ( vValue ) ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemDataType ( "NMTOKEN" , cXSNMTOKEN ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTItem ( ) {
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTNode ( ) {
} ;
cXTNode . prototype = new cXTItem ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTAttribute ( ) {
} ;
cXTAttribute . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTComment ( ) {
} ;
cXTComment . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTDocument ( ) {
} ;
cXTDocument . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTElement ( ) {
} ;
cXTElement . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTProcessingInstruction ( ) {
} ;
cXTProcessingInstruction . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
function cXTText ( ) {
} ;
cXTText . prototype = new cXTNode ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
12.1 Comparisons of base64Binary and hexBinary Values
op : hexBinary - equal
op : base64Binary - equal
* /
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "hexBinary-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) == oRight . valueOf ( ) ) ;
} ;
hStaticContext _operators [ "base64Binary-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) == oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
9.2 Operators on Boolean Values
op : boolean - equal
op : boolean - less - than
op : boolean - greater - than
* /
// 9.2 Operators on Boolean Values
// op:boolean-equal($value1 as xs:boolean, $value2 as xs:boolean) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "boolean-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) == oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:boolean-less-than($arg1 as xs:boolean, $arg2 as xs:boolean) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "boolean-less-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) < oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:boolean-greater-than($arg1 as xs:boolean, $arg2 as xs:boolean) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "boolean-greater-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) > oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
10.4 Comparison Operators on Duration , Date and Time Values
op : yearMonthDuration - less - than
op : yearMonthDuration - greater - than
op : dayTimeDuration - less - than
op : dayTimeDuration - greater - than
op : duration - equal
op : dateTime - equal
op : dateTime - less - than
op : dateTime - greater - than
op : date - equal
op : date - less - than
op : date - greater - than
op : time - equal
op : time - less - than
op : time - greater - than
op : gYearMonth - equal
op : gYear - equal
op : gMonthDay - equal
op : gMonth - equal
op : gDay - equal
10.6 Arithmetic Operators on Durations
op : add - yearMonthDurations
op : subtract - yearMonthDurations
op : multiply - yearMonthDuration
op : divide - yearMonthDuration
op : divide - yearMonthDuration - by - yearMonthDuration
op : add - dayTimeDurations
op : subtract - dayTimeDurations
op : multiply - dayTimeDuration
op : divide - dayTimeDuration
op : divide - dayTimeDuration - by - dayTimeDuration
10.8 Arithmetic Operators on Durations , Dates and Times
op : subtract - dateTimes
op : subtract - dates
op : subtract - times
op : add - yearMonthDuration - to - dateTime
op : add - dayTimeDuration - to - dateTime
op : subtract - yearMonthDuration - from - dateTime
op : subtract - dayTimeDuration - from - dateTime
op : add - yearMonthDuration - to - date
op : add - dayTimeDuration - to - date
op : subtract - yearMonthDuration - from - date
op : subtract - dayTimeDuration - from - date
op : add - dayTimeDuration - to - time
op : subtract - dayTimeDuration - from - time
* /
// 10.4 Comparison Operators on Duration, Date and Time Values
// op:yearMonthDuration-less-than($arg1 as xs:yearMonthDuration, $arg2 as xs:yearMonthDuration) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "yearMonthDuration-less-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( fOperator _yearMonthDuration _toMonths ( oLeft ) < fOperator _yearMonthDuration _toMonths ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:yearMonthDuration-greater-than($arg1 as xs:yearMonthDuration, $arg2 as xs:yearMonthDuration) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "yearMonthDuration-greater-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( fOperator _yearMonthDuration _toMonths ( oLeft ) > fOperator _yearMonthDuration _toMonths ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:dayTimeDuration-less-than($arg1 as dayTimeDuration, $arg2 as dayTimeDuration) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "dayTimeDuration-less-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( fOperator _dayTimeDuration _toSeconds ( oLeft ) < fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:dayTimeDuration-greater-than($arg1 as dayTimeDuration, $arg2 as dayTimeDuration) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "dayTimeDuration-greater-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( fOperator _dayTimeDuration _toSeconds ( oLeft ) > fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:duration-equal($arg1 as xs:duration, $arg2 as xs:duration) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "duration-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . negative == oRight . negative
&& fOperator _yearMonthDuration _toMonths ( oLeft ) == fOperator _yearMonthDuration _toMonths ( oRight )
&& fOperator _dayTimeDuration _toSeconds ( oLeft ) == fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:dateTime-equal($arg1 as xs:dateTime, $arg2 as xs:dateTime) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "dateTime-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes ( oLeft , oRight , 'eq' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:dateTime-less-than($arg1 as xs:dateTime, $arg2 as xs:dateTime) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "dateTime-less-than" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes ( oLeft , oRight , 'lt' ) ;
} ;
2016-12-17 00:33:47 +01:00
//op:dateTime-greater-than($arg1 as xs:dateTime, $arg2 as xs:dateTime) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "dateTime-greater-than" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes ( oLeft , oRight , 'gt' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:date-equal($arg1 as xs:date, $arg2 as xs:date) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "date-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDates ( oLeft , oRight , 'eq' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:date-less-than($arg1 as xs:date, $arg2 as xs:date) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "date-less-than" ] = function ( oLeft , oRight ) {
return fOperator _compareDates ( oLeft , oRight , 'lt' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:date-greater-than($arg1 as xs:date, $arg2 as xs:date) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "date-greater-than" ] = function ( oLeft , oRight ) {
return fOperator _compareDates ( oLeft , oRight , 'gt' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:time-equal($arg1 as xs:time, $arg2 as xs:time) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "time-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareTimes ( oLeft , oRight , 'eq' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:time-less-than($arg1 as xs:time, $arg2 as xs:time) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "time-less-than" ] = function ( oLeft , oRight ) {
return fOperator _compareTimes ( oLeft , oRight , 'lt' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:time-greater-than($arg1 as xs:time, $arg2 as xs:time) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "time-greater-than" ] = function ( oLeft , oRight ) {
return fOperator _compareTimes ( oLeft , oRight , 'gt' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:gYearMonth-equal($arg1 as xs:gYearMonth, $arg2 as xs:gYearMonth) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "gYearMonth-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes (
new cXSDateTime ( oLeft . year , oLeft . month , fXSDate _getDaysForYearMonth ( oLeft . year , oLeft . month ) , 0 , 0 , 0 , oLeft . timezone == null ? this . timezone : oLeft . timezone ) ,
new cXSDateTime ( oRight . year , oRight . month , fXSDate _getDaysForYearMonth ( oRight . year , oRight . month ) , 0 , 0 , 0 , oRight . timezone == null ? this . timezone : oRight . timezone ) ,
'eq'
) ;
} ;
2016-12-17 00:33:47 +01:00
// op:gYear-equal($arg1 as xs:gYear, $arg2 as xs:gYear) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "gYear-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes (
new cXSDateTime ( oLeft . year , 1 , 1 , 0 , 0 , 0 , oLeft . timezone == null ? this . timezone : oLeft . timezone ) ,
new cXSDateTime ( oRight . year , 1 , 1 , 0 , 0 , 0 , oRight . timezone == null ? this . timezone : oRight . timezone ) ,
'eq'
) ;
} ;
2016-12-17 00:33:47 +01:00
// op:gMonthDay-equal($arg1 as xs:gMonthDay, $arg2 as xs:gMonthDay) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "gMonthDay-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes (
new cXSDateTime ( 1972 , oLeft . month , oLeft . day , 0 , 0 , 0 , oLeft . timezone == null ? this . timezone : oLeft . timezone ) ,
new cXSDateTime ( 1972 , oRight . month , oRight . day , 0 , 0 , 0 , oRight . timezone == null ? this . timezone : oRight . timezone ) ,
'eq'
) ;
} ;
2016-12-17 00:33:47 +01:00
// op:gMonth-equal($arg1 as xs:gMonth, $arg2 as xs:gMonth) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "gMonth-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes (
new cXSDateTime ( 1972 , oLeft . month , fXSDate _getDaysForYearMonth ( 1972 , oRight . month ) , 0 , 0 , 0 , oLeft . timezone == null ? this . timezone : oLeft . timezone ) ,
new cXSDateTime ( 1972 , oRight . month , fXSDate _getDaysForYearMonth ( 1972 , oRight . month ) , 0 , 0 , 0 , oRight . timezone == null ? this . timezone : oRight . timezone ) ,
'eq'
) ;
} ;
2016-12-17 00:33:47 +01:00
// op:gDay-equal($arg1 as xs:gDay, $arg2 as xs:gDay) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "gDay-equal" ] = function ( oLeft , oRight ) {
return fOperator _compareDateTimes (
new cXSDateTime ( 1972 , 12 , oLeft . day , 0 , 0 , 0 , oLeft . timezone == null ? this . timezone : oLeft . timezone ) ,
new cXSDateTime ( 1972 , 12 , oRight . day , 0 , 0 , 0 , oRight . timezone == null ? this . timezone : oRight . timezone ) ,
'eq'
) ;
} ;
2016-12-17 00:33:47 +01:00
// 10.6 Arithmetic Operators on Durations
// op:add-yearMonthDurations($arg1 as xs:yearMonthDuration, $arg2 as xs:yearMonthDuration) as xs:yearMonthDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-yearMonthDurations" ] = function ( oLeft , oRight ) {
return fOperator _yearMonthDuration _fromMonths ( fOperator _yearMonthDuration _toMonths ( oLeft ) + fOperator _yearMonthDuration _toMonths ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-yearMonthDurations($arg1 as xs:yearMonthDuration, $arg2 as xs:yearMonthDuration) as xs:yearMonthDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-yearMonthDurations" ] = function ( oLeft , oRight ) {
return fOperator _yearMonthDuration _fromMonths ( fOperator _yearMonthDuration _toMonths ( oLeft ) - fOperator _yearMonthDuration _toMonths ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:multiply-yearMonthDuration($arg1 as xs:yearMonthDuration, $arg2 as xs:double) as xs:yearMonthDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "multiply-yearMonthDuration" ] = function ( oLeft , oRight ) {
return fOperator _yearMonthDuration _fromMonths ( fOperator _yearMonthDuration _toMonths ( oLeft ) * oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:divide-yearMonthDuration($arg1 as xs:yearMonthDuration, $arg2 as xs:double) as xs:yearMonthDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "divide-yearMonthDuration" ] = function ( oLeft , oRight ) {
return fOperator _yearMonthDuration _fromMonths ( fOperator _yearMonthDuration _toMonths ( oLeft ) / oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:divide-yearMonthDuration-by-yearMonthDuration($arg1 as xs:yearMonthDuration, $arg2 as xs:yearMonthDuration) as xs:decimal
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "divide-yearMonthDuration-by-yearMonthDuration" ] = function ( oLeft , oRight ) {
return new cXSDecimal ( fOperator _yearMonthDuration _toMonths ( oLeft ) / fOperator _yearMonthDuration _toMonths ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-dayTimeDurations($arg1 as xs:dayTimeDuration, $arg2 as xs:dayTimeDuration) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-dayTimeDurations" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dayTimeDuration _toSeconds ( oLeft ) + fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-dayTimeDurations($arg1 as xs:dayTimeDuration, $arg2 as xs:dayTimeDuration) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dayTimeDurations" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dayTimeDuration _toSeconds ( oLeft ) - fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:multiply-dayTimeDurations($arg1 as xs:dayTimeDuration, $arg2 as xs:double) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "multiply-dayTimeDuration" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dayTimeDuration _toSeconds ( oLeft ) * oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:divide-dayTimeDurations($arg1 as xs:dayTimeDuration, $arg2 as xs:double) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "divide-dayTimeDuration" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dayTimeDuration _toSeconds ( oLeft ) / oRight ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:divide-dayTimeDuration-by-dayTimeDuration($arg1 as xs:dayTimeDuration, $arg2 as xs:dayTimeDuration) as xs:decimal
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "divide-dayTimeDuration-by-dayTimeDuration" ] = function ( oLeft , oRight ) {
return new cXSDecimal ( fOperator _dayTimeDuration _toSeconds ( oLeft ) / fOperator _dayTimeDuration _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// 10.8 Arithmetic Operators on Durations, Dates and Times
// op:subtract-dateTimes($arg1 as xs:dateTime, $arg2 as xs:dateTime) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dateTimes" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dateTime _toSeconds ( oLeft ) - fOperator _dateTime _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-dates($arg1 as xs:date, $arg2 as xs:date) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dates" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _dateTime _toSeconds ( oLeft ) - fOperator _dateTime _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-times($arg1 as xs:time, $arg2 as xs:time) as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-times" ] = function ( oLeft , oRight ) {
return fOperator _dayTimeDuration _fromSeconds ( fOperator _time _toSeconds ( oLeft ) - fOperator _time _toSeconds ( oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-yearMonthDuration-to-dateTime($arg1 as xs:dateTime, $arg2 as xs:yearMonthDuration) as xs:dateTime
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-yearMonthDuration-to-dateTime" ] = function ( oLeft , oRight ) {
return fOperator _addYearMonthDuration2DateTime ( oLeft , oRight , '+' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-dayTimeDuration-to-dateTime($arg1 as xs:dateTime, $arg2 as xs:dayTimeDuration) as xs:dateTime
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-dayTimeDuration-to-dateTime" ] = function ( oLeft , oRight ) {
return fOperator _addDayTimeDuration2DateTime ( oLeft , oRight , '+' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-yearMonthDuration-from-dateTime($arg1 as xs:dateTime, $arg2 as xs:yearMonthDuration) as xs:dateTime
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-yearMonthDuration-from-dateTime" ] = function ( oLeft , oRight ) {
return fOperator _addYearMonthDuration2DateTime ( oLeft , oRight , '-' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-dayTimeDuration-from-dateTime($arg1 as xs:dateTime, $arg2 as xs:dayTimeDuration) as xs:dateTime
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dayTimeDuration-from-dateTime" ] = function ( oLeft , oRight ) {
return fOperator _addDayTimeDuration2DateTime ( oLeft , oRight , '-' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-yearMonthDuration-to-date($arg1 as xs:date, $arg2 as xs:yearMonthDuration) as xs:date
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-yearMonthDuration-to-date" ] = function ( oLeft , oRight ) {
return fOperator _addYearMonthDuration2DateTime ( oLeft , oRight , '+' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-dayTimeDuration-to-date($arg1 as xs:date, $arg2 as xs:dayTimeDuration) as xs:date
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-dayTimeDuration-to-date" ] = function ( oLeft , oRight ) {
return fOperator _addDayTimeDuration2DateTime ( oLeft , oRight , '+' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-yearMonthDuration-from-date($arg1 as xs:date, $arg2 as xs:yearMonthDuration) as xs:date
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-yearMonthDuration-from-date" ] = function ( oLeft , oRight ) {
return fOperator _addYearMonthDuration2DateTime ( oLeft , oRight , '-' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-dayTimeDuration-from-date($arg1 as xs:date, $arg2 as xs:dayTimeDuration) as xs:date
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dayTimeDuration-from-date" ] = function ( oLeft , oRight ) {
return fOperator _addDayTimeDuration2DateTime ( oLeft , oRight , '-' ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:add-dayTimeDuration-to-time($arg1 as xs:time, $arg2 as xs:dayTimeDuration) as xs:time
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "add-dayTimeDuration-to-time" ] = function ( oLeft , oRight ) {
var oValue = new cXSTime ( oLeft . hours , oLeft . minutes , oLeft . seconds , oLeft . timezone ) ;
oValue . hours += oRight . hours ;
oValue . minutes += oRight . minutes ;
oValue . seconds += oRight . seconds ;
2016-12-17 00:33:47 +01:00
//
return fXSTime _normalize ( oValue ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
// op:subtract-dayTimeDuration-from-time($arg1 as xs:time, $arg2 as xs:dayTimeDuration) as xs:time
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "subtract-dayTimeDuration-from-time" ] = function ( oLeft , oRight ) {
var oValue = new cXSTime ( oLeft . hours , oLeft . minutes , oLeft . seconds , oLeft . timezone ) ;
oValue . hours -= oRight . hours ;
oValue . minutes -= oRight . minutes ;
oValue . seconds -= oRight . seconds ;
2016-12-17 00:33:47 +01:00
//
return fXSTime _normalize ( oValue ) ;
2016-11-29 19:28:07 +01:00
} ;
function fOperator _compareTimes ( oLeft , oRight , sComparator ) {
var nLeft = fOperator _time _toSeconds ( oLeft ) ,
nRight = fOperator _time _toSeconds ( oRight ) ;
return new cXSBoolean ( sComparator == 'lt' ? nLeft < nRight : sComparator == 'gt' ? nLeft > nRight : nLeft == nRight ) ;
} ;
function fOperator _compareDates ( oLeft , oRight , sComparator ) {
return fOperator _compareDateTimes ( cXSDateTime . cast ( oLeft ) , cXSDateTime . cast ( oRight ) , sComparator ) ;
} ;
function fOperator _compareDateTimes ( oLeft , oRight , sComparator ) {
2016-12-17 00:33:47 +01:00
// Adjust object time zone to Z and compare as strings
var oTimezone = new cXSDayTimeDuration ( 0 , 0 , 0 , 0 ) ,
2016-11-29 19:28:07 +01:00
sLeft = fFunction _dateTime _adjustTimezone ( oLeft , oTimezone ) . toString ( ) ,
sRight = fFunction _dateTime _adjustTimezone ( oRight , oTimezone ) . toString ( ) ;
return new cXSBoolean ( sComparator == 'lt' ? sLeft < sRight : sComparator == 'gt' ? sLeft > sRight : sLeft == sRight ) ;
} ;
function fOperator _addYearMonthDuration2DateTime ( oLeft , oRight , sOperator ) {
var oValue ;
if ( oLeft instanceof cXSDate )
oValue = new cXSDate ( oLeft . year , oLeft . month , oLeft . day , oLeft . timezone , oLeft . negative ) ;
else
if ( oLeft instanceof cXSDateTime )
oValue = new cXSDateTime ( oLeft . year , oLeft . month , oLeft . day , oLeft . hours , oLeft . minutes , oLeft . seconds , oLeft . timezone , oLeft . negative ) ;
2016-12-17 00:33:47 +01:00
//
oValue . year = oValue . year + oRight . year * ( sOperator == '-' ? - 1 : 1 ) ;
2016-11-29 19:28:07 +01:00
oValue . month = oValue . month + oRight . month * ( sOperator == '-' ? - 1 : 1 ) ;
2016-12-17 00:33:47 +01:00
//
fXSDate _normalize ( oValue , true ) ;
// Correct day if out of month range
var nDay = fXSDate _getDaysForYearMonth ( oValue . year , oValue . month ) ;
2016-11-29 19:28:07 +01:00
if ( oValue . day > nDay )
oValue . day = nDay ;
2016-12-17 00:33:47 +01:00
//
return oValue ;
2016-11-29 19:28:07 +01:00
} ;
function fOperator _addDayTimeDuration2DateTime ( oLeft , oRight , sOperator ) {
var oValue ;
if ( oLeft instanceof cXSDate ) {
var nValue = ( oRight . hours * 60 + oRight . minutes ) * 60 + oRight . seconds ;
oValue = new cXSDate ( oLeft . year , oLeft . month , oLeft . day , oLeft . timezone , oLeft . negative ) ;
oValue . day = oValue . day + oRight . day * ( sOperator == '-' ? - 1 : 1 ) - 1 * ( nValue && sOperator == '-' ) ;
2016-12-17 00:33:47 +01:00
//
fXSDate _normalize ( oValue ) ;
2016-11-29 19:28:07 +01:00
}
else
if ( oLeft instanceof cXSDateTime ) {
oValue = new cXSDateTime ( oLeft . year , oLeft . month , oLeft . day , oLeft . hours , oLeft . minutes , oLeft . seconds , oLeft . timezone , oLeft . negative ) ;
oValue . seconds = oValue . seconds + oRight . seconds * ( sOperator == '-' ? - 1 : 1 ) ;
oValue . minutes = oValue . minutes + oRight . minutes * ( sOperator == '-' ? - 1 : 1 ) ;
oValue . hours = oValue . hours + oRight . hours * ( sOperator == '-' ? - 1 : 1 ) ;
oValue . day = oValue . day + oRight . day * ( sOperator == '-' ? - 1 : 1 ) ;
2016-12-17 00:33:47 +01:00
//
fXSDateTime _normalize ( oValue ) ;
2016-11-29 19:28:07 +01:00
}
return oValue ;
} ;
2016-12-17 00:33:47 +01:00
// xs:dayTimeDuration to/from seconds
2016-11-29 19:28:07 +01:00
function fOperator _dayTimeDuration _toSeconds ( oDuration ) {
return ( ( ( oDuration . day * 24 + oDuration . hours ) * 60 + oDuration . minutes ) * 60 + oDuration . seconds ) * ( oDuration . negative ? - 1 : 1 ) ;
} ;
function fOperator _dayTimeDuration _fromSeconds ( nValue ) {
var bNegative = ( nValue = cMath . round ( nValue ) ) < 0 ,
nDays = ~ ~ ( ( nValue = cMath . abs ( nValue ) ) / 86400 ) ,
nHours = ~ ~ ( ( nValue -= nDays * 3600 * 24 ) / 3600 ) ,
nMinutes = ~ ~ ( ( nValue -= nHours * 3600 ) / 60 ) ,
nSeconds = nValue -= nMinutes * 60 ;
return new cXSDayTimeDuration ( nDays , nHours , nMinutes , nSeconds , bNegative ) ;
} ;
2016-12-17 00:33:47 +01:00
// xs:yearMonthDuration to/from months
2016-11-29 19:28:07 +01:00
function fOperator _yearMonthDuration _toMonths ( oDuration ) {
return ( oDuration . year * 12 + oDuration . month ) * ( oDuration . negative ? - 1 : 1 ) ;
} ;
function fOperator _yearMonthDuration _fromMonths ( nValue ) {
var nNegative = ( nValue = cMath . round ( nValue ) ) < 0 ,
nYears = ~ ~ ( ( nValue = cMath . abs ( nValue ) ) / 12 ) ,
nMonths = nValue -= nYears * 12 ;
return new cXSYearMonthDuration ( nYears , nMonths , nNegative ) ;
} ;
2016-12-17 00:33:47 +01:00
// xs:time to seconds
2016-11-29 19:28:07 +01:00
function fOperator _time _toSeconds ( oTime ) {
return oTime . seconds + ( oTime . minutes - ( oTime . timezone != null ? oTime . timezone % 60 : 0 ) + ( oTime . hours - ( oTime . timezone != null ? ~ ~ ( oTime . timezone / 60 ) : 0 ) ) * 60 ) * 60 ;
} ;
2016-12-17 00:33:47 +01:00
// This function unlike all other date-related functions rely on interpretor's dateTime handling
2016-11-29 19:28:07 +01:00
function fOperator _dateTime _toSeconds ( oValue ) {
var oDate = new cDate ( ( oValue . negative ? - 1 : 1 ) * oValue . year , oValue . month , oValue . day , 0 , 0 , 0 , 0 ) ;
if ( oValue instanceof cXSDateTime ) {
oDate . setHours ( oValue . hours ) ;
oDate . setMinutes ( oValue . minutes ) ;
oDate . setSeconds ( oValue . seconds ) ;
}
if ( oValue . timezone != null )
oDate . setMinutes ( oDate . getMinutes ( ) - oValue . timezone ) ;
return oDate . getTime ( ) / 1000 ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
14 Functions and Operators on Nodes
op : is - same - node
op : node - before
op : node - after
* /
// 14 Operators on Nodes
// op:is-same-node($parameter1 as node(), $parameter2 as node()) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "is-same-node" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( this . DOMAdapter . isSameNode ( oLeft , oRight ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:node-before($parameter1 as node(), $parameter2 as node()) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "node-before" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( ! ! ( this . DOMAdapter . compareDocumentPosition ( oLeft , oRight ) & 4 ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:node-after($parameter1 as node(), $parameter2 as node()) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "node-after" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( ! ! ( this . DOMAdapter . compareDocumentPosition ( oLeft , oRight ) & 2 ) ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
13.1 Operators on NOTATION
op : NOTATION - equal
* /
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
6.2 Operators on Numeric Values
op : numeric - add
op : numeric - subtract
op : numeric - multiply
op : numeric - divide
op : numeric - integer - divide
op : numeric - mod
op : numeric - unary - plus
op : numeric - unary - minus
6.3 Comparison Operators on Numeric Values
op : numeric - equal
op : numeric - less - than
op : numeric - greater - than
* /
// 6.2 Operators on Numeric Values
2016-11-29 19:28:07 +01:00
function fFunctionCall _numeric _getPower ( oLeft , oRight ) {
if ( fIsNaN ( oLeft ) || ( cMath . abs ( oLeft ) == nInfinity ) || fIsNaN ( oRight ) || ( cMath . abs ( oRight ) == nInfinity ) )
return 0 ;
var aLeft = cString ( oLeft ) . match ( rNumericLiteral ) ,
aRight = cString ( oRight ) . match ( rNumericLiteral ) ,
nPower = cMath . max ( 1 , ( aLeft [ 2 ] || aLeft [ 3 ] || '' ) . length + ( aLeft [ 5 ] || 0 ) * ( aLeft [ 4 ] == '+' ? - 1 : 1 ) , ( aRight [ 2 ] || aRight [ 3 ] || '' ) . length + ( aRight [ 5 ] || 0 ) * ( aRight [ 4 ] == '+' ? - 1 : 1 ) ) ;
return nPower + ( nPower % 2 ? 0 : 1 ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-add($arg1 as numeric, $arg2 as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-add" ] = function ( oLeft , oRight ) {
var nLeft = oLeft . valueOf ( ) ,
nRight = oRight . valueOf ( ) ,
nPower = cMath . pow ( 10 , fFunctionCall _numeric _getPower ( nLeft , nRight ) ) ;
return fOperator _numeric _getResultOfType ( oLeft , oRight , ( ( nLeft * nPower ) + ( nRight * nPower ) ) / nPower ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-subtract($arg1 as numeric, $arg2 as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-subtract" ] = function ( oLeft , oRight ) {
var nLeft = oLeft . valueOf ( ) ,
nRight = oRight . valueOf ( ) ,
nPower = cMath . pow ( 10 , fFunctionCall _numeric _getPower ( nLeft , nRight ) ) ;
return fOperator _numeric _getResultOfType ( oLeft , oRight , ( ( nLeft * nPower ) - ( nRight * nPower ) ) / nPower ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-multiply($arg1 as numeric, $arg2 as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-multiply" ] = function ( oLeft , oRight ) {
var nLeft = oLeft . valueOf ( ) ,
nRight = oRight . valueOf ( ) ,
nPower = cMath . pow ( 10 , fFunctionCall _numeric _getPower ( nLeft , nRight ) ) ;
return fOperator _numeric _getResultOfType ( oLeft , oRight , ( ( nLeft * nPower ) * ( nRight * nPower ) ) / ( nPower * nPower ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-divide($arg1 as numeric, $arg2 as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-divide" ] = function ( oLeft , oRight ) {
var nLeft = oLeft . valueOf ( ) ,
nRight = oRight . valueOf ( ) ,
nPower = cMath . pow ( 10 , fFunctionCall _numeric _getPower ( nLeft , nRight ) ) ;
return fOperator _numeric _getResultOfType ( oLeft , oRight , ( oLeft * nPower ) / ( oRight * nPower ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-integer-divide($arg1 as numeric, $arg2 as numeric) as xs:integer
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-integer-divide" ] = function ( oLeft , oRight ) {
2016-12-17 00:33:47 +01:00
var oValue = oLeft / oRight ;
return new cXSInteger ( cMath . floor ( oValue ) + ( oValue < 0 ) ) ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-mod($arg1 as numeric, $arg2 as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-mod" ] = function ( oLeft , oRight ) {
var nLeft = oLeft . valueOf ( ) ,
nRight = oRight . valueOf ( ) ,
nPower = cMath . pow ( 10 , fFunctionCall _numeric _getPower ( nLeft , nRight ) ) ;
return fOperator _numeric _getResultOfType ( oLeft , oRight , ( ( nLeft * nPower ) % ( nRight * nPower ) ) / nPower ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-unary-plus($arg as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-unary-plus" ] = function ( oRight ) {
return oRight ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-unary-minus($arg as numeric) as numeric
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-unary-minus" ] = function ( oRight ) {
oRight . value *= - 1 ;
return oRight ;
} ;
2016-12-17 00:33:47 +01:00
// 6.3 Comparison Operators on Numeric Values
// op:numeric-equal($arg1 as numeric, $arg2 as numeric) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) == oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-less-than($arg1 as numeric, $arg2 as numeric) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-less-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) < oRight . valueOf ( ) ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:numeric-greater-than($arg1 as numeric, $arg2 as numeric) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "numeric-greater-than" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . valueOf ( ) > oRight . valueOf ( ) ) ;
} ;
function fOperator _numeric _getResultOfType ( oLeft , oRight , nResult ) {
return new ( oLeft instanceof cXSInteger && oRight instanceof cXSInteger && nResult == cMath . round ( nResult ) ? cXSInteger : cXSDecimal ) ( nResult ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
11.2 Functions and Operators Related to QNames
op : QName - equal
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// 11.2 Operators Related to QNames
// op:QName-equal($arg1 as xs:QName, $arg2 as xs:QName) as xs:boolean
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "QName-equal" ] = function ( oLeft , oRight ) {
return new cXSBoolean ( oLeft . localName == oRight . localName && oLeft . namespaceURI == oRight . namespaceURI ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
/ *
15.1 General Functions and Operators on Sequences
op : concatenate
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
15.3 Equals , Union , Intersection and Except
op : union
op : intersect
op : except
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
15.5 Functions and Operators that Generate Sequences
op : to
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// 15.1 General Functions and Operators on Sequences
// op:concatenate($seq1 as item()*, $seq2 as item()*) as item()*
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "concatenate" ] = function ( oSequence1 , oSequence2 ) {
return oSequence1 . concat ( oSequence2 ) ;
} ;
2016-12-17 00:33:47 +01:00
// 15.3 Equals, Union, Intersection and Except
// op:union($parameter1 as node()*, $parameter2 as node()*) as node()*
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "union" ] = function ( oSequence1 , oSequence2 ) {
var oSequence = [ ] ;
2016-12-17 00:33:47 +01:00
// Process first collection
for ( var nIndex = 0 , nLength = oSequence1 . length , oItem ; nIndex < nLength ; nIndex ++ ) {
2016-11-29 19:28:07 +01:00
if ( ! this . DOMAdapter . isNode ( oItem = oSequence1 [ nIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
) ; // Required item type of second operand of 'intersect' is node(); supplied value has item type xs:integer
//
if ( fArray _indexOf ( oSequence , oItem ) == - 1 )
2016-11-29 19:28:07 +01:00
oSequence . push ( oItem ) ;
}
2016-12-17 00:33:47 +01:00
// Process second collection
for ( var nIndex = 0 , nLength = oSequence2 . length , oItem ; nIndex < nLength ; nIndex ++ ) {
2016-11-29 19:28:07 +01:00
if ( ! this . DOMAdapter . isNode ( oItem = oSequence2 [ nIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
) ; // Required item type of second operand of 'intersect' is node(); supplied value has item type xs:integer
//
if ( fArray _indexOf ( oSequence , oItem ) == - 1 )
2016-11-29 19:28:07 +01:00
oSequence . push ( oItem ) ;
}
return fFunction _sequence _order ( oSequence , this ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:intersect($parameter1 as node()*, $parameter2 as node()*) as node()*
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "intersect" ] = function ( oSequence1 , oSequence2 ) {
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length , oItem , bFound ; nIndex < nLength ; nIndex ++ ) {
if ( ! this . DOMAdapter . isNode ( oItem = oSequence1 [ nIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
) ; // Required item type of second operand of 'intersect' is node(); supplied value has item type xs:integer
//
bFound = false ;
2016-11-29 19:28:07 +01:00
for ( var nRightIndex = 0 , nRightLength = oSequence2 . length ; ( nRightIndex < nRightLength ) && ! bFound ; nRightIndex ++ ) {
if ( ! this . DOMAdapter . isNode ( oSequence2 [ nRightIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
bFound = this . DOMAdapter . isSameNode ( oSequence2 [ nRightIndex ] , oItem ) ;
}
2016-12-17 00:33:47 +01:00
//
if ( bFound && fArray _indexOf ( oSequence , oItem ) == - 1 )
2016-11-29 19:28:07 +01:00
oSequence . push ( oItem ) ;
}
return fFunction _sequence _order ( oSequence , this ) ;
} ;
2016-12-17 00:33:47 +01:00
// op:except($parameter1 as node()*, $parameter2 as node()*) as node()*
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "except" ] = function ( oSequence1 , oSequence2 ) {
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length , oItem , bFound ; nIndex < nLength ; nIndex ++ ) {
if ( ! this . DOMAdapter . isNode ( oItem = oSequence1 [ nIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
) ; // Required item type of second operand of 'intersect' is node(); supplied value has item type xs:integer
//
bFound = false ;
2016-11-29 19:28:07 +01:00
for ( var nRightIndex = 0 , nRightLength = oSequence2 . length ; ( nRightIndex < nRightLength ) && ! bFound ; nRightIndex ++ ) {
if ( ! this . DOMAdapter . isNode ( oSequence2 [ nRightIndex ] ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
bFound = this . DOMAdapter . isSameNode ( oSequence2 [ nRightIndex ] , oItem ) ;
}
2016-12-17 00:33:47 +01:00
//
if ( ! bFound && fArray _indexOf ( oSequence , oItem ) == - 1 )
2016-11-29 19:28:07 +01:00
oSequence . push ( oItem ) ;
}
return fFunction _sequence _order ( oSequence , this ) ;
} ;
2016-12-17 00:33:47 +01:00
// 15.5 Functions and Operators that Generate Sequences
// op:to($firstval as xs:integer, $lastval as xs:integer) as xs:integer*
2016-11-29 19:28:07 +01:00
hStaticContext _operators [ "to" ] = function ( oLeft , oRight ) {
var oSequence = [ ] ;
for ( var nIndex = oLeft . valueOf ( ) , nLength = oRight . valueOf ( ) ; nIndex <= nLength ; nIndex ++ )
oSequence . push ( new cXSInteger ( nIndex ) ) ;
2016-12-17 00:33:47 +01:00
//
return oSequence ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
2 Accessors
node - name
nilled
string
data
base - uri
document - uri
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// fn:node-name($arg as node()?) as xs:QName?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "node-name" , [ [ cXTNode , '?' ] ] , function ( oNode ) {
if ( oNode != null ) {
var fGetProperty = this . DOMAdapter . getProperty ;
switch ( fGetProperty ( oNode , "nodeType" ) ) {
2016-12-17 00:33:47 +01:00
case 1 : // ELEMENT_NAME
case 2 : // ATTRIBUTE_NODE
return new cXSQName ( fGetProperty ( oNode , "prefix" ) , fGetProperty ( oNode , "localName" ) , fGetProperty ( oNode , "namespaceURI" ) ) ;
case 5 : // ENTITY_REFERENCE_NODE
throw "Not implemented" ;
case 6 : // ENTITY_NODE
throw "Not implemented" ;
case 7 : // PROCESSING_INSTRUCTION_NODE
return new cXSQName ( null , fGetProperty ( oNode , "target" ) , null ) ;
case 10 : // DOCUMENT_TYPE_NODE
return new cXSQName ( null , fGetProperty ( oNode , "name" ) , null ) ;
2016-11-29 19:28:07 +01:00
}
}
2016-12-17 00:33:47 +01:00
//
return null ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:nilled($arg as node()?) as xs:boolean?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "nilled" , [ [ cXTNode , '?' ] ] , function ( oNode ) {
if ( oNode != null ) {
if ( this . DOMAdapter . getProperty ( oNode , "nodeType" ) == 1 )
2016-12-17 00:33:47 +01:00
return new cXSBoolean ( false ) ; // TODO: Determine if node is nilled
}
//
return null ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:string() as xs:string
// fn:string($arg as item()?) as xs:string
fStaticContext _defineSystemFunction ( "string" , [ [ cXTItem , '?' , true ] ] , function ( /*[*/ oItem /*]*/ ) {
2016-11-29 19:28:07 +01:00
if ( ! arguments . length ) {
if ( ! this . item )
throw new cException ( "XPDY0002" ) ;
oItem = this . item ;
}
return oItem == null ? new cXSString ( '' ) : cXSString . cast ( fFunction _sequence _atomize ( [ oItem ] , this ) [ 0 ] ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:data($arg as item()*) as xs:anyAtomicType*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "data" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return fFunction _sequence _atomize ( oSequence1 , this ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:base-uri() as xs:anyURI?
// fn:base-uri($arg as node()?) as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "base-uri" , [ [ cXTNode , '?' , true ] ] , function ( oNode ) {
if ( ! arguments . length ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
2016-12-17 00:33:47 +01:00
//
return cXSAnyURI . cast ( new cXSString ( this . DOMAdapter . getProperty ( oNode , "baseURI" ) || '' ) ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:document-uri($arg as node()?) as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "document-uri" , [ [ cXTNode , '?' ] ] , function ( oNode ) {
if ( oNode != null ) {
var fGetProperty = this . DOMAdapter . getProperty ;
if ( fGetProperty ( oNode , "nodeType" ) == 9 )
return cXSAnyURI . cast ( new cXSString ( fGetProperty ( oNode , "documentURI" ) || '' ) ) ;
}
2016-12-17 00:33:47 +01:00
//
return null ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
8 Functions on anyURI
resolve - uri
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// fn:resolve-uri($relative as xs:string?) as xs:anyURI?
// fn:resolve-uri($relative as xs:string?, $base as xs:string) as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "resolve-uri" , [ [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( sUri , sBaseUri ) {
if ( arguments . length < 2 ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
sBaseUri = new cXSString ( this . DOMAdapter . getProperty ( this . item , "baseURI" ) || '' ) ;
}
if ( sUri == null )
return null ;
2016-12-17 00:33:47 +01:00
//
if ( sUri . valueOf ( ) == '' || sUri . valueOf ( ) . charAt ( 0 ) == '#' )
2016-11-29 19:28:07 +01:00
return cXSAnyURI . cast ( sBaseUri ) ;
var oUri = cXSAnyURI . cast ( sUri ) ;
if ( oUri . scheme )
return oUri ;
var oBaseUri = cXSAnyURI . cast ( sBaseUri ) ;
oUri . scheme = oBaseUri . scheme ;
if ( ! oUri . authority ) {
2016-12-17 00:33:47 +01:00
// authority
oUri . authority = oBaseUri . authority ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// path
if ( oUri . path . charAt ( 0 ) != '/' ) {
2016-11-29 19:28:07 +01:00
var aUriSegments = oUri . path . split ( '/' ) ,
aBaseUriSegments = oBaseUri . path . split ( '/' ) ;
aBaseUriSegments . pop ( ) ;
var nBaseUriStart = aBaseUriSegments [ 0 ] == '' ? 1 : 0 ;
for ( var nIndex = 0 , nLength = aUriSegments . length ; nIndex < nLength ; nIndex ++ ) {
if ( aUriSegments [ nIndex ] == '..' ) {
if ( aBaseUriSegments . length > nBaseUriStart )
aBaseUriSegments . pop ( ) ;
else {
aBaseUriSegments . push ( aUriSegments [ nIndex ] ) ;
nBaseUriStart ++ ;
}
}
else
if ( aUriSegments [ nIndex ] != '.' )
aBaseUriSegments . push ( aUriSegments [ nIndex ] ) ;
}
if ( aUriSegments [ -- nIndex ] == '..' || aUriSegments [ nIndex ] == '.' )
aBaseUriSegments . push ( '' ) ;
2016-12-17 00:33:47 +01:00
//
oUri . path = aBaseUriSegments . join ( '/' ) ;
2016-11-29 19:28:07 +01:00
}
}
return oUri ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
9.1 Additional Boolean Constructor Functions
true
false
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
9.3 Functions on Boolean Values
not
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// 9.1 Additional Boolean Constructor Functions
// fn:true() as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "true" , [ ] , function ( ) {
return new cXSBoolean ( true ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:false() as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "false" , [ ] , function ( ) {
return new cXSBoolean ( false ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 9.3 Functions on Boolean Values
// fn:not($arg as item()*) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "not" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return new cXSBoolean ( ! fFunction _sequence _toEBV ( oSequence1 , this ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
16 Context Functions
position
last
current - dateTime
current - date
current - time
implicit - timezone
default - collation
static - base - uri
* /
// fn:position() as xs:integer
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "position" , [ ] , function ( ) {
return new cXSInteger ( this . position ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:last() as xs:integer
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "last" , [ ] , function ( ) {
return new cXSInteger ( this . size ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:current-dateTime() as xs:dateTime (2004-05-12T18:17:15.125Z)
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "current-dateTime" , [ ] , function ( ) {
return this . dateTime ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:current-date() as xs:date (2004-05-12+01:00)
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "current-date" , [ ] , function ( ) {
return cXSDate . cast ( this . dateTime ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:current-time() as xs:time (23:17:00.000-05:00)
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "current-time" , [ ] , function ( ) {
return cXSTime . cast ( this . dateTime ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:implicit-timezone() as xs:dayTimeDuration
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "implicit-timezone" , [ ] , function ( ) {
return this . timezone ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:default-collation() as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "default-collation" , [ ] , function ( ) {
return new cXSString ( this . staticContext . defaultCollationName ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:static-base-uri() as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "static-base-uri" , [ ] , function ( ) {
return cXSAnyURI . cast ( new cXSString ( this . staticContext . baseURI || '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
10.5 Component Extraction Functions on Durations , Dates and Times
years - from - duration
months - from - duration
days - from - duration
hours - from - duration
minutes - from - duration
seconds - from - duration
year - from - dateTime
month - from - dateTime
day - from - dateTime
hours - from - dateTime
minutes - from - dateTime
seconds - from - dateTime
timezone - from - dateTime
year - from - date
month - from - date
day - from - date
timezone - from - date
hours - from - time
minutes - from - time
seconds - from - time
timezone - from - time
10.7 Timezone Adjustment Functions on Dates and Time Values
adjust - dateTime - to - timezone
adjust - date - to - timezone
adjust - time - to - timezone
* /
// 10.5 Component Extraction Functions on Durations, Dates and Times
// functions on duration
// fn:years-from-duration($arg as xs:duration?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "years-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "year" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:months-from-duration($arg as xs:duration?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "months-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "month" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:days-from-duration($arg as xs:duration?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "days-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "day" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:hours-from-duration($arg as xs:duration?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "hours-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "hours" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:minutes-from-duration($arg as xs:duration?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "minutes-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "minutes" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:seconds-from-duration($arg as xs:duration?) as xs:decimal?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "seconds-from-duration" , [ [ cXSDuration , '?' ] ] , function ( oDuration ) {
return fFunction _duration _getComponent ( oDuration , "seconds" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// functions on dateTime
// fn:year-from-dateTime($arg as xs:dateTime?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "year-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "year" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:month-from-dateTime($arg as xs:dateTime?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "month-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "month" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:day-from-dateTime($arg as xs:dateTime?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "day-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "day" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:hours-from-dateTime($arg as xs:dateTime?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "hours-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "hours" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:minutes-from-dateTime($arg as xs:dateTime?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "minutes-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "minutes" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:seconds-from-dateTime($arg as xs:dateTime?) as xs:decimal?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "seconds-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "seconds" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:timezone-from-dateTime($arg as xs:dateTime?) as xs:dayTimeDuration?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "timezone-from-dateTime" , [ [ cXSDateTime , '?' ] ] , function ( oDateTime ) {
return fFunction _dateTime _getComponent ( oDateTime , "timezone" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// functions on date
// fn:year-from-date($arg as xs:date?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "year-from-date" , [ [ cXSDate , '?' ] ] , function ( oDate ) {
return fFunction _dateTime _getComponent ( oDate , "year" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:month-from-date($arg as xs:date?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "month-from-date" , [ [ cXSDate , '?' ] ] , function ( oDate ) {
return fFunction _dateTime _getComponent ( oDate , "month" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:day-from-date($arg as xs:date?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "day-from-date" , [ [ cXSDate , '?' ] ] , function ( oDate ) {
return fFunction _dateTime _getComponent ( oDate , "day" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:timezone-from-date($arg as xs:date?) as xs:dayTimeDuration?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "timezone-from-date" , [ [ cXSDate , '?' ] ] , function ( oDate ) {
return fFunction _dateTime _getComponent ( oDate , "timezone" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// functions on time
// fn:hours-from-time($arg as xs:time?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "hours-from-time" , [ [ cXSTime , '?' ] ] , function ( oTime ) {
return fFunction _dateTime _getComponent ( oTime , "hours" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:minutes-from-time($arg as xs:time?) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "minutes-from-time" , [ [ cXSTime , '?' ] ] , function ( oTime ) {
return fFunction _dateTime _getComponent ( oTime , "minutes" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:seconds-from-time($arg as xs:time?) as xs:decimal?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "seconds-from-time" , [ [ cXSTime , '?' ] ] , function ( oTime ) {
return fFunction _dateTime _getComponent ( oTime , "seconds" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:timezone-from-time($arg as xs:time?) as xs:dayTimeDuration?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "timezone-from-time" , [ [ cXSTime , '?' ] ] , function ( oTime ) {
return fFunction _dateTime _getComponent ( oTime , "timezone" ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 10.7 Timezone Adjustment Functions on Dates and Time Values
// fn:adjust-dateTime-to-timezone($arg as xs:dateTime?) as xs:dateTime?
// fn:adjust-dateTime-to-timezone($arg as xs:dateTime?, $timezone as xs:dayTimeDuration?) as xs:dateTime?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "adjust-dateTime-to-timezone" , [ [ cXSDateTime , '?' ] , [ cXSDayTimeDuration , '?' , true ] ] , function ( oDateTime , oDayTimeDuration ) {
return fFunction _dateTime _adjustTimezone ( oDateTime , arguments . length > 1 && oDayTimeDuration != null ? arguments . length > 1 ? oDayTimeDuration : this . timezone : null ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:adjust-date-to-timezone($arg as xs:date?) as xs:date?
// fn:adjust-date-to-timezone($arg as xs:date?, $timezone as xs:dayTimeDuration?) as xs:date?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "adjust-date-to-timezone" , [ [ cXSDate , '?' ] , [ cXSDayTimeDuration , '?' , true ] ] , function ( oDate , oDayTimeDuration ) {
return fFunction _dateTime _adjustTimezone ( oDate , arguments . length > 1 && oDayTimeDuration != null ? arguments . length > 1 ? oDayTimeDuration : this . timezone : null ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:adjust-time-to-timezone($arg as xs:time?) as xs:time?
// fn:adjust-time-to-timezone($arg as xs:time?, $timezone as xs:dayTimeDuration?) as xs:time?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "adjust-time-to-timezone" , [ [ cXSTime , '?' ] , [ cXSDayTimeDuration , '?' , true ] ] , function ( oTime , oDayTimeDuration ) {
return fFunction _dateTime _adjustTimezone ( oTime , arguments . length > 1 && oDayTimeDuration != null ? arguments . length > 1 ? oDayTimeDuration : this . timezone : null ) ;
} ) ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fFunction _duration _getComponent ( oDuration , sName ) {
if ( oDuration == null )
return null ;
var nValue = oDuration [ sName ] * ( oDuration . negative ? - 1 : 1 ) ;
return sName == "seconds" ? new cXSDecimal ( nValue ) : new cXSInteger ( nValue ) ;
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fFunction _dateTime _getComponent ( oDateTime , sName ) {
if ( oDateTime == null )
return null ;
if ( sName == "timezone" ) {
var nTimezone = oDateTime . timezone ;
if ( nTimezone == null )
return null ;
return new cXSDayTimeDuration ( 0 , cMath . abs ( ~ ~ ( nTimezone / 60 ) ) , cMath . abs ( nTimezone % 60 ) , 0 , nTimezone < 0 ) ;
}
else {
var nValue = oDateTime [ sName ] ;
if ( ! ( oDateTime instanceof cXSDate ) ) {
if ( sName == "hours" )
if ( nValue == 24 )
nValue = 0 ;
}
if ( ! ( oDateTime instanceof cXSTime ) )
nValue *= oDateTime . negative ? - 1 : 1 ;
return sName == "seconds" ? new cXSDecimal ( nValue ) : new cXSInteger ( nValue ) ;
}
} ;
2016-12-17 00:33:47 +01:00
//
2016-11-29 19:28:07 +01:00
function fFunction _dateTime _adjustTimezone ( oDateTime , oTimezone ) {
if ( oDateTime == null )
return null ;
2016-12-17 00:33:47 +01:00
// Create a copy
var oValue ;
2016-11-29 19:28:07 +01:00
if ( oDateTime instanceof cXSDate )
oValue = new cXSDate ( oDateTime . year , oDateTime . month , oDateTime . day , oDateTime . timezone , oDateTime . negative ) ;
else
if ( oDateTime instanceof cXSTime )
oValue = new cXSTime ( oDateTime . hours , oDateTime . minutes , oDateTime . seconds , oDateTime . timezone , oDateTime . negative ) ;
else
oValue = new cXSDateTime ( oDateTime . year , oDateTime . month , oDateTime . day , oDateTime . hours , oDateTime . minutes , oDateTime . seconds , oDateTime . timezone , oDateTime . negative ) ;
2016-12-17 00:33:47 +01:00
//
if ( oTimezone == null )
2016-11-29 19:28:07 +01:00
oValue . timezone = null ;
else {
var nTimezone = fOperator _dayTimeDuration _toSeconds ( oTimezone ) / 60 ;
if ( oDateTime . timezone != null ) {
var nDiff = nTimezone - oDateTime . timezone ;
if ( oDateTime instanceof cXSDate ) {
if ( nDiff < 0 )
oValue . day -- ;
}
else {
oValue . minutes += nDiff % 60 ;
oValue . hours += ~ ~ ( nDiff / 60 ) ;
}
2016-12-17 00:33:47 +01:00
//
fXSDateTime _normalize ( oValue ) ;
2016-11-29 19:28:07 +01:00
}
oValue . timezone = nTimezone ;
}
return oValue ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
14 Functions and Operators on Nodes
name
local - name
namespace - uri
number
lang
root
* /
// 14 Functions on Nodes
// fn:name() as xs:string
// fn:name($arg as node()?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "name" , [ [ cXTNode , '?' , true ] ] , function ( oNode ) {
if ( ! arguments . length ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
else
if ( oNode == null )
return new cXSString ( '' ) ;
2016-12-17 00:33:47 +01:00
//
var vValue = hStaticContext _functions [ "node-name" ] . call ( this , oNode ) ;
2016-11-29 19:28:07 +01:00
return new cXSString ( vValue == null ? '' : vValue . toString ( ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:local-name() as xs:string
// fn:local-name($arg as node()?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "local-name" , [ [ cXTNode , '?' , true ] ] , function ( oNode ) {
if ( ! arguments . length ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
else
if ( oNode == null )
return new cXSString ( '' ) ;
2016-12-17 00:33:47 +01:00
//
return new cXSString ( this . DOMAdapter . getProperty ( oNode , "localName" ) || '' ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:namespace-uri() as xs:anyURI
// fn:namespace-uri($arg as node()?) as xs:anyURI
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "namespace-uri" , [ [ cXTNode , '?' , true ] ] , function ( oNode ) {
if ( ! arguments . length ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
else
if ( oNode == null )
return cXSAnyURI . cast ( new cXSString ( '' ) ) ;
2016-12-17 00:33:47 +01:00
//
return cXSAnyURI . cast ( new cXSString ( this . DOMAdapter . getProperty ( oNode , "namespaceURI" ) || '' ) ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:number() as xs:double
// fn:number($arg as xs:anyAtomicType?) as xs:double
fStaticContext _defineSystemFunction ( "number" , [ [ cXSAnyAtomicType , '?' , true ] ] , function ( /*[*/ oItem /*]*/ ) {
2016-11-29 19:28:07 +01:00
if ( ! arguments . length ) {
if ( ! this . item )
throw new cException ( "XPDY0002" ) ;
oItem = fFunction _sequence _atomize ( [ this . item ] , this ) [ 0 ] ;
}
2016-12-17 00:33:47 +01:00
// If input item cannot be cast to xs:decimal, a NaN should be returned
var vValue = new cXSDouble ( nNaN ) ;
2016-11-29 19:28:07 +01:00
if ( oItem != null ) {
try {
vValue = cXSDouble . cast ( oItem ) ;
}
catch ( e ) {
}
}
return vValue ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:lang($testlang as xs:string?) as xs:boolean
// fn:lang($testlang as xs:string?, $node as node()) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "lang" , [ [ cXSString , '?' ] , [ cXTNode , '' , true ] ] , function ( sLang , oNode ) {
if ( arguments . length < 2 ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
var fGetProperty = this . DOMAdapter . getProperty ;
if ( fGetProperty ( oNode , "nodeType" ) == 2 )
oNode = fGetProperty ( oNode , "ownerElement" ) ;
2016-12-17 00:33:47 +01:00
// walk up the tree looking for xml:lang attribute
for ( var aAttributes ; oNode ; oNode = fGetProperty ( oNode , "parentNode" ) )
2016-11-29 19:28:07 +01:00
if ( aAttributes = fGetProperty ( oNode , "attributes" ) )
for ( var nIndex = 0 , nLength = aAttributes . length ; nIndex < nLength ; nIndex ++ )
if ( fGetProperty ( aAttributes [ nIndex ] , "nodeName" ) == "xml:lang" )
return new cXSBoolean ( fGetProperty ( aAttributes [ nIndex ] , "value" ) . replace ( /-.+/ , '' ) . toLowerCase ( ) == sLang . valueOf ( ) . replace ( /-.+/ , '' ) . toLowerCase ( ) ) ;
2016-12-17 00:33:47 +01:00
//
return new cXSBoolean ( false ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:root() as node()
// fn:root($arg as node()?) as node()?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "root" , [ [ cXTNode , '?' , true ] ] , function ( oNode ) {
if ( ! arguments . length ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
else
if ( oNode == null )
return null ;
var fGetProperty = this . DOMAdapter . getProperty ;
2016-12-17 00:33:47 +01:00
// If context node is Attribute
if ( fGetProperty ( oNode , "nodeType" ) == 2 )
2016-11-29 19:28:07 +01:00
oNode = fGetProperty ( oNode , "ownerElement" ) ;
for ( var oParent = oNode ; oParent ; oParent = fGetProperty ( oNode , "parentNode" ) )
oNode = oParent ;
return oNode ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
6.4 Functions on Numeric Values
abs
ceiling
floor
round
round - half - to - even
* /
// 6.4 Functions on Numeric Values
// fn:abs($arg as numeric?) as numeric?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "abs" , [ [ cXSDouble , '?' ] ] , function ( oValue ) {
return new cXSDecimal ( cMath . abs ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:ceiling($arg as numeric?) as numeric?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "ceiling" , [ [ cXSDouble , '?' ] ] , function ( oValue ) {
return new cXSDecimal ( cMath . ceil ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:floor($arg as numeric?) as numeric?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "floor" , [ [ cXSDouble , '?' ] ] , function ( oValue ) {
return new cXSDecimal ( cMath . floor ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:round($arg as numeric?) as numeric?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "round" , [ [ cXSDouble , '?' ] ] , function ( oValue ) {
return new cXSDecimal ( cMath . round ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:round-half-to-even($arg as numeric?) as numeric?
// fn:round-half-to-even($arg as numeric?, $precision as xs:integer) as numeric?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "round-half-to-even" , [ [ cXSDouble , '?' ] , [ cXSInteger , '' , true ] ] , function ( oValue , oPrecision ) {
var nPrecision = arguments . length > 1 ? oPrecision . valueOf ( ) : 0 ;
2016-12-17 00:33:47 +01:00
//
if ( nPrecision < 0 ) {
2016-11-29 19:28:07 +01:00
var oPower = new cXSInteger ( cMath . pow ( 10 , - nPrecision ) ) ,
nRounded = cMath . round ( hStaticContext _operators [ "numeric-divide" ] . call ( this , oValue , oPower ) ) ,
oRounded = new cXSInteger ( nRounded ) ;
nDecimal = cMath . abs ( hStaticContext _operators [ "numeric-subtract" ] . call ( this , oRounded , hStaticContext _operators [ "numeric-divide" ] . call ( this , oValue , oPower ) ) ) ;
return hStaticContext _operators [ "numeric-multiply" ] . call ( this , hStaticContext _operators [ "numeric-add" ] . call ( this , oRounded , new cXSDecimal ( nDecimal == 0.5 && nRounded % 2 ? - 1 : 0 ) ) , oPower ) ;
}
else {
var oPower = new cXSInteger ( cMath . pow ( 10 , nPrecision ) ) ,
nRounded = cMath . round ( hStaticContext _operators [ "numeric-multiply" ] . call ( this , oValue , oPower ) ) ,
oRounded = new cXSInteger ( nRounded ) ;
nDecimal = cMath . abs ( hStaticContext _operators [ "numeric-subtract" ] . call ( this , oRounded , hStaticContext _operators [ "numeric-multiply" ] . call ( this , oValue , oPower ) ) ) ;
return hStaticContext _operators [ "numeric-divide" ] . call ( this , hStaticContext _operators [ "numeric-add" ] . call ( this , oRounded , new cXSDecimal ( nDecimal == 0.5 && nRounded % 2 ? - 1 : 0 ) ) , oPower ) ;
}
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
11.1 Additional Constructor Functions for QNames
resolve - QName
QName
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
11.2 Functions and Operators Related to QNames
prefix - from - QName
local - name - from - QName
namespace - uri - from - QName
namespace - uri - for - prefix
in - scope - prefixes
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// 11.1 Additional Constructor Functions for QNames
// fn:resolve-QName($qname as xs:string?, $element as element()) as xs:QName?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "resolve-QName" , [ [ cXSString , '?' ] , [ cXTElement ] ] , function ( oQName , oElement ) {
if ( oQName == null )
return null ;
var sQName = oQName . valueOf ( ) ,
aMatch = sQName . match ( rXSQName ) ;
if ( ! aMatch )
throw new cException ( "FOCA0002"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
var sPrefix = aMatch [ 1 ] || null ,
sLocalName = aMatch [ 2 ] ,
sNameSpaceURI = this . DOMAdapter . lookupNamespaceURI ( oElement , sPrefix ) ;
2016-12-17 00:33:47 +01:00
//
if ( sPrefix != null && ! sNameSpaceURI )
2016-11-29 19:28:07 +01:00
throw new cException ( "FONS0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cXSQName ( sPrefix , sLocalName , sNameSpaceURI || null ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:QName($paramURI as xs:string?, $paramQName as xs:string) as xs:QName
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "QName" , [ [ cXSString , '?' ] , [ cXSString ] ] , function ( oUri , oQName ) {
var sQName = oQName . valueOf ( ) ,
aMatch = sQName . match ( rXSQName ) ;
if ( ! aMatch )
throw new cException ( "FOCA0002"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cXSQName ( aMatch [ 1 ] || null , aMatch [ 2 ] || null , oUri == null ? '' : oUri . valueOf ( ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 11.2 Functions Related to QNames
// fn:prefix-from-QName($arg as xs:QName?) as xs:NCName?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "prefix-from-QName" , [ [ cXSQName , '?' ] ] , function ( oQName ) {
if ( oQName != null ) {
if ( oQName . prefix )
return new cXSNCName ( oQName . prefix ) ;
}
2016-12-17 00:33:47 +01:00
//
return null ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:local-name-from-QName($arg as xs:QName?) as xs:NCName?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "local-name-from-QName" , [ [ cXSQName , '?' ] ] , function ( oQName ) {
if ( oQName == null )
return null ;
return new cXSNCName ( oQName . localName ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:namespace-uri-from-QName($arg as xs:QName?) as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "namespace-uri-from-QName" , [ [ cXSQName , '?' ] ] , function ( oQName ) {
if ( oQName == null )
return null ;
return cXSAnyURI . cast ( new cXSString ( oQName . namespaceURI || '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:namespace-uri-for-prefix($prefix as xs:string?, $element as element()) as xs:anyURI?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "namespace-uri-for-prefix" , [ [ cXSString , '?' ] , [ cXTElement ] ] , function ( oPrefix , oElement ) {
var sPrefix = oPrefix == null ? '' : oPrefix . valueOf ( ) ,
sNameSpaceURI = this . DOMAdapter . lookupNamespaceURI ( oElement , sPrefix || null ) ;
return sNameSpaceURI == null ? null : cXSAnyURI . cast ( new cXSString ( sNameSpaceURI ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:in-scope-prefixes($element as element()) as xs:string*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "in-scope-prefixes" , [ [ cXTElement ] ] , function ( oElement ) {
throw "Function '" + "in-scope-prefixes" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
15.1 General Functions and Operators on Sequences
boolean
index - of
empty
exists
distinct - values
insert - before
remove
reverse
subsequence
unordered
15.2 Functions That Test the Cardinality of Sequences
zero - or - one
one - or - more
exactly - one
15.3 Equals , Union , Intersection and Except
deep - equal
15.4 Aggregate Functions
count
avg
max
min
sum
15.5 Functions and Operators that Generate Sequences
id
idref
doc
doc - available
collection
element - with - id
* /
// 15.1 General Functions and Operators on Sequences
// fn:boolean($arg as item()*) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "boolean" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return new cXSBoolean ( fFunction _sequence _toEBV ( oSequence1 , this ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:index-of($seqParam as xs:anyAtomicType*, $srchParam as xs:anyAtomicType) as xs:integer*
// fn:index-of($seqParam as xs:anyAtomicType*, $srchParam as xs:anyAtomicType, $collation as xs:string) as xs:integer*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "index-of" , [ [ cXSAnyAtomicType , '*' ] , [ cXSAnyAtomicType ] , [ cXSString , '' , true ] ] , function ( oSequence1 , oSearch , oCollation ) {
if ( ! oSequence1 . length || oSearch == null )
return [ ] ;
2016-12-17 00:33:47 +01:00
// TODO: Implement collation
2016-11-29 19:28:07 +01:00
var vLeft = oSearch ;
2016-12-17 00:33:47 +01:00
// Cast to XSString if Untyped
if ( vLeft instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vLeft = cXSString . cast ( vLeft ) ;
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length , vRight ; nIndex < nLength ; nIndex ++ ) {
vRight = oSequence1 [ nIndex ] ;
2016-12-17 00:33:47 +01:00
// Cast to XSString if Untyped
if ( vRight instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vRight = cXSString . cast ( vRight ) ;
2016-12-17 00:33:47 +01:00
//
if ( vRight . valueOf ( ) === vLeft . valueOf ( ) )
2016-11-29 19:28:07 +01:00
oSequence . push ( new cXSInteger ( nIndex + 1 ) ) ;
}
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:empty($arg as item()*) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "empty" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return new cXSBoolean ( ! oSequence1 . length ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:exists($arg as item()*) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "exists" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return new cXSBoolean ( ! ! oSequence1 . length ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:distinct-values($arg as xs:anyAtomicType*) as xs:anyAtomicType*
// fn:distinct-values($arg as xs:anyAtomicType*, $collation as xs:string) as xs:anyAtomicType*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "distinct-values" , [ [ cXSAnyAtomicType , '*' ] , [ cXSString , '' , true ] ] , function ( oSequence1 , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 1 )
throw "Collation parameter in function '" + "distinct-values" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
if ( ! oSequence1 . length )
return null ;
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length , vLeft ; nIndex < nLength ; nIndex ++ ) {
vLeft = oSequence1 [ nIndex ] ;
2016-12-17 00:33:47 +01:00
// Cast to XSString if Untyped
if ( vLeft instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vLeft = cXSString . cast ( vLeft ) ;
for ( var nRightIndex = 0 , nRightLength = oSequence . length , vRight , bFound = false ; ( nRightIndex < nRightLength ) && ! bFound ; nRightIndex ++ ) {
vRight = oSequence [ nRightIndex ] ;
2016-12-17 00:33:47 +01:00
// Cast to XSString if Untyped
if ( vRight instanceof cXSUntypedAtomic )
2016-11-29 19:28:07 +01:00
vRight = cXSString . cast ( vRight ) ;
2016-12-17 00:33:47 +01:00
//
if ( vRight . valueOf ( ) === vLeft . valueOf ( ) )
2016-11-29 19:28:07 +01:00
bFound = true ;
}
if ( ! bFound )
oSequence . push ( oSequence1 [ nIndex ] ) ;
}
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:insert-before($target as item()*, $position as xs:integer, $inserts as item()*) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "insert-before" , [ [ cXTItem , '*' ] , [ cXSInteger ] , [ cXTItem , '*' ] ] , function ( oSequence1 , oPosition , oSequence3 ) {
if ( ! oSequence1 . length )
return oSequence3 ;
if ( ! oSequence3 . length )
return oSequence1 ;
var nLength = oSequence1 . length ,
nPosition = oPosition . valueOf ( ) ;
if ( nPosition < 1 )
nPosition = 1 ;
else
if ( nPosition > nLength )
nPosition = nLength + 1 ;
var oSequence = [ ] ;
for ( var nIndex = 0 ; nIndex < nLength ; nIndex ++ ) {
if ( nPosition == nIndex + 1 )
oSequence = oSequence . concat ( oSequence3 ) ;
oSequence . push ( oSequence1 [ nIndex ] ) ;
}
if ( nPosition == nIndex + 1 )
oSequence = oSequence . concat ( oSequence3 ) ;
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:remove($target as item()*, $position as xs:integer) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "remove" , [ [ cXTItem , '*' ] , [ cXSInteger ] ] , function ( oSequence1 , oPosition ) {
if ( ! oSequence1 . length )
return [ ] ;
var nLength = oSequence1 . length ,
nPosition = oPosition . valueOf ( ) ;
if ( nPosition < 1 || nPosition > nLength )
return oSequence1 ;
var oSequence = [ ] ;
for ( var nIndex = 0 ; nIndex < nLength ; nIndex ++ )
if ( nPosition != nIndex + 1 )
oSequence . push ( oSequence1 [ nIndex ] ) ;
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:reverse($arg as item()*) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "reverse" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
oSequence1 . reverse ( ) ;
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double) as item()*
// fn:subsequence($sourceSeq as item()*, $startingLoc as xs:double, $length as xs:double) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "subsequence" , [ [ cXTItem , '*' ] , [ cXSDouble , '' ] , [ cXSDouble , '' , true ] ] , function ( oSequence1 , oStart , oLength ) {
var nPosition = cMath . round ( oStart ) ,
nLength = arguments . length > 2 ? cMath . round ( oLength ) : oSequence1 . length - nPosition + 1 ;
2016-12-17 00:33:47 +01:00
// TODO: Handle out-of-range position and length values
return oSequence1 . slice ( nPosition - 1 , nPosition - 1 + nLength ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:unordered($sourceSeq as item()*) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "unordered" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
// 15.2 Functions That Test the Cardinality of Sequences
// fn:zero-or-one($arg as item()*) as item()?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "zero-or-one" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
if ( oSequence1 . length > 1 )
throw new cException ( "FORG0003" ) ;
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:one-or-more($arg as item()*) as item()+
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "one-or-more" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
if ( ! oSequence1 . length )
throw new cException ( "FORG0004" ) ;
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:exactly-one($arg as item()*) as item()
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "exactly-one" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
if ( oSequence1 . length != 1 )
throw new cException ( "FORG0005" ) ;
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
// 15.3 Equals, Union, Intersection and Except
// fn:deep-equal($parameter1 as item()*, $parameter2 as item()*) as xs:boolean
// fn:deep-equal($parameter1 as item()*, $parameter2 as item()*, $collation as string) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "deep-equal" , [ [ cXTItem , '*' ] , [ cXTItem , '*' ] , [ cXSString , '' , true ] ] , function ( oSequence1 , oSequence2 , oCollation ) {
throw "Function '" + "deep-equal" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// 15.4 Aggregate Functions
// fn:count($arg as item()*) as xs:integer
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "count" , [ [ cXTItem , '*' ] ] , function ( oSequence1 ) {
return new cXSInteger ( oSequence1 . length ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:avg($arg as xs:anyAtomicType*) as xs:anyAtomicType?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "avg" , [ [ cXSAnyAtomicType , '*' ] ] , function ( oSequence1 ) {
if ( ! oSequence1 . length )
return null ;
2016-12-17 00:33:47 +01:00
//
try {
2016-11-29 19:28:07 +01:00
var vValue = oSequence1 [ 0 ] ;
if ( vValue instanceof cXSUntypedAtomic )
vValue = cXSDouble . cast ( vValue ) ;
for ( var nIndex = 1 , nLength = oSequence1 . length , vRight ; nIndex < nLength ; nIndex ++ ) {
vRight = oSequence1 [ nIndex ] ;
if ( vRight instanceof cXSUntypedAtomic )
vRight = cXSDouble . cast ( vRight ) ;
vValue = hAdditiveExpr _operators [ '+' ] ( vValue , vRight , this ) ;
}
return hMultiplicativeExpr _operators [ 'div' ] ( vValue , new cXSInteger ( nLength ) , this ) ;
}
catch ( e ) {
2016-12-17 00:33:47 +01:00
// XPTY0004: Arithmetic operator is not defined for provided arguments
throw e . code != "XPTY0004" ? e : new cException ( "FORG0006"
2016-11-29 19:28:07 +01:00
) ;
}
} ) ;
2016-12-17 00:33:47 +01:00
// fn:max($arg as xs:anyAtomicType*) as xs:anyAtomicType?
// fn:max($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "max" , [ [ cXSAnyAtomicType , '*' ] , [ cXSString , '' , true ] ] , function ( oSequence1 , oCollation ) {
if ( ! oSequence1 . length )
return null ;
2016-12-17 00:33:47 +01:00
// TODO: Implement collation
//
try {
2016-11-29 19:28:07 +01:00
var vValue = oSequence1 [ 0 ] ;
2016-12-17 00:33:47 +01:00
if ( vValue instanceof cXSUntypedAtomic )
vValue = cXSDouble . cast ( vValue ) ;
for ( var nIndex = 1 , nLength = oSequence1 . length , vRight ; nIndex < nLength ; nIndex ++ ) {
vRight = oSequence1 [ nIndex ] ;
if ( vRight instanceof cXSUntypedAtomic )
vRight = cXSDouble . cast ( vRight ) ;
if ( hComparisonExpr _ValueComp _operators [ 'ge' ] ( vRight , vValue , this ) . valueOf ( ) )
vValue = vRight ;
}
2016-11-29 19:28:07 +01:00
return vValue ;
}
catch ( e ) {
2016-12-17 00:33:47 +01:00
// XPTY0004: Cannot compare {type1} with {type2}
throw e . code != "XPTY0004" ? e : new cException ( "FORG0006"
2016-11-29 19:28:07 +01:00
) ;
}
} ) ;
2016-12-17 00:33:47 +01:00
// fn:min($arg as xs:anyAtomicType*) as xs:anyAtomicType?
// fn:min($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "min" , [ [ cXSAnyAtomicType , '*' ] , [ cXSString , '' , true ] ] , function ( oSequence1 , oCollation ) {
if ( ! oSequence1 . length )
return null ;
2016-12-17 00:33:47 +01:00
// TODO: Implement collation
//
try {
2016-11-29 19:28:07 +01:00
var vValue = oSequence1 [ 0 ] ;
2016-12-17 00:33:47 +01:00
if ( vValue instanceof cXSUntypedAtomic )
vValue = cXSDouble . cast ( vValue ) ;
for ( var nIndex = 1 , nLength = oSequence1 . length , vRight ; nIndex < nLength ; nIndex ++ ) {
vRight = oSequence1 [ nIndex ] ;
if ( vRight instanceof cXSUntypedAtomic )
vRight = cXSDouble . cast ( vRight ) ;
if ( hComparisonExpr _ValueComp _operators [ 'le' ] ( vRight , vValue , this ) . valueOf ( ) )
vValue = vRight ;
}
2016-11-29 19:28:07 +01:00
return vValue ;
}
catch ( e ) {
2016-12-17 00:33:47 +01:00
// Cannot compare {type1} with {type2}
throw e . code != "XPTY0004" ? e : new cException ( "FORG0006"
2016-11-29 19:28:07 +01:00
) ;
}
} ) ;
2016-12-17 00:33:47 +01:00
// fn:sum($arg as xs:anyAtomicType*) as xs:anyAtomicType
// fn:sum($arg as xs:anyAtomicType*, $zero as xs:anyAtomicType?) as xs:anyAtomicType?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "sum" , [ [ cXSAnyAtomicType , '*' ] , [ cXSAnyAtomicType , '?' , true ] ] , function ( oSequence1 , oZero ) {
if ( ! oSequence1 . length ) {
if ( arguments . length > 1 )
return oZero ;
else
return new cXSDouble ( 0 ) ;
return null ;
}
2016-12-17 00:33:47 +01:00
// TODO: Implement collation
//
try {
2016-11-29 19:28:07 +01:00
var vValue = oSequence1 [ 0 ] ;
if ( vValue instanceof cXSUntypedAtomic )
vValue = cXSDouble . cast ( vValue ) ;
for ( var nIndex = 1 , nLength = oSequence1 . length , vRight ; nIndex < nLength ; nIndex ++ ) {
vRight = oSequence1 [ nIndex ] ;
if ( vRight instanceof cXSUntypedAtomic )
vRight = cXSDouble . cast ( vRight ) ;
vValue = hAdditiveExpr _operators [ '+' ] ( vValue , vRight , this ) ;
}
return vValue ;
}
catch ( e ) {
2016-12-17 00:33:47 +01:00
// XPTY0004: Arithmetic operator is not defined for provided arguments
throw e . code != "XPTY0004" ? e : new cException ( "FORG0006"
2016-11-29 19:28:07 +01:00
) ;
}
} ) ;
2016-12-17 00:33:47 +01:00
// 15.5 Functions and Operators that Generate Sequences
// fn:id($arg as xs:string*) as element()*
// fn:id($arg as xs:string*, $node as node()) as element()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "id" , [ [ cXSString , '*' ] , [ cXTNode , '' , true ] ] , function ( oSequence1 , oNode ) {
if ( arguments . length < 2 ) {
if ( ! this . DOMAdapter . isNode ( this . item ) )
throw new cException ( "XPTY0004"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
oNode = this . item ;
}
2016-12-17 00:33:47 +01:00
// Get root node and check if it is Document
var oDocument = hStaticContext _functions [ "root" ] . call ( this , oNode ) ;
2016-11-29 19:28:07 +01:00
if ( this . DOMAdapter . getProperty ( oDocument , "nodeType" ) != 9 )
throw new cException ( "FODC0001" ) ;
2016-12-17 00:33:47 +01:00
// Search for elements
var oSequence = [ ] ;
2016-11-29 19:28:07 +01:00
for ( var nIndex = 0 ; nIndex < oSequence1 . length ; nIndex ++ )
for ( var nRightIndex = 0 , aValue = fString _trim ( oSequence1 [ nIndex ] ) . split ( /\s+/ ) , nRightLength = aValue . length ; nRightIndex < nRightLength ; nRightIndex ++ )
if ( ( oNode = this . DOMAdapter . getElementById ( oDocument , aValue [ nRightIndex ] ) ) && fArray _indexOf ( oSequence , oNode ) == - 1 )
oSequence . push ( oNode ) ;
2016-12-17 00:33:47 +01:00
//
return fFunction _sequence _order ( oSequence , this ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:idref($arg as xs:string*) as node()*
// fn:idref($arg as xs:string*, $node as node()) as node()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "idref" , [ [ cXSString , '*' ] , [ cXTNode , '' , true ] ] , function ( oSequence1 , oNode ) {
throw "Function '" + "idref" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:doc($uri as xs:string?) as document-node()?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "doc" , [ [ cXSString , '?' , true ] ] , function ( oUri ) {
throw "Function '" + "doc" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:doc-available($uri as xs:string?) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "doc-available" , [ [ cXSString , '?' , true ] ] , function ( oUri ) {
throw "Function '" + "doc-available" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:collection() as node()*
// fn:collection($arg as xs:string?) as node()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "collection" , [ [ cXSString , '?' , true ] ] , function ( oUri ) {
throw "Function '" + "collection" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:element-with-id($arg as xs:string*) as element()*
// fn:element-with-id($arg as xs:string*, $node as node()) as element()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "element-with-id" , [ [ cXSString , '*' ] , [ cXTNode , '' , true ] ] , function ( oSequence1 , oNode ) {
throw "Function '" + "element-with-id" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// EBV calculation
2016-11-29 19:28:07 +01:00
function fFunction _sequence _toEBV ( oSequence1 , oContext ) {
if ( ! oSequence1 . length )
return false ;
var oItem = oSequence1 [ 0 ] ;
if ( oContext . DOMAdapter . isNode ( oItem ) )
return true ;
if ( oSequence1 . length == 1 ) {
if ( oItem instanceof cXSBoolean )
return oItem . value . valueOf ( ) ;
if ( oItem instanceof cXSString )
return ! ! oItem . valueOf ( ) . length ;
if ( fXSAnyAtomicType _isNumeric ( oItem ) )
return ! ( fIsNaN ( oItem . valueOf ( ) ) || oItem . valueOf ( ) == 0 ) ;
throw new cException ( "FORG0006"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
}
throw new cException ( "FORG0006"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
} ;
function fFunction _sequence _atomize ( oSequence1 , oContext ) {
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length , oItem , vItem ; nIndex < nLength ; nIndex ++ ) {
oItem = oSequence1 [ nIndex ] ;
vItem = null ;
2016-12-17 00:33:47 +01:00
// Untyped
if ( oItem == null )
2016-11-29 19:28:07 +01:00
vItem = null ;
2016-12-17 00:33:47 +01:00
// Node type
else
2016-11-29 19:28:07 +01:00
if ( oContext . DOMAdapter . isNode ( oItem ) ) {
var fGetProperty = oContext . DOMAdapter . getProperty ;
switch ( fGetProperty ( oItem , "nodeType" ) ) {
2016-12-17 00:33:47 +01:00
case 1 : // ELEMENT_NODE
vItem = new cXSUntypedAtomic ( fGetProperty ( oItem , "textContent" ) ) ;
2016-11-29 19:28:07 +01:00
break ;
2016-12-17 00:33:47 +01:00
case 2 : // ATTRIBUTE_NODE
vItem = new cXSUntypedAtomic ( fGetProperty ( oItem , "value" ) ) ;
2016-11-29 19:28:07 +01:00
break ;
2016-12-17 00:33:47 +01:00
case 3 : // TEXT_NODE
case 4 : // CDATA_SECTION_NODE
case 8 : // COMMENT_NODE
vItem = new cXSUntypedAtomic ( fGetProperty ( oItem , "data" ) ) ;
2016-11-29 19:28:07 +01:00
break ;
2016-12-17 00:33:47 +01:00
case 7 : // PROCESSING_INSTRUCTION_NODE
vItem = new cXSUntypedAtomic ( fGetProperty ( oItem , "data" ) ) ;
2016-11-29 19:28:07 +01:00
break ;
2016-12-17 00:33:47 +01:00
case 9 : // DOCUMENT_NODE
var oNode = fGetProperty ( oItem , "documentElement" ) ;
2016-11-29 19:28:07 +01:00
vItem = new cXSUntypedAtomic ( oNode ? fGetProperty ( oNode , "textContent" ) : '' ) ;
break ;
}
}
2016-12-17 00:33:47 +01:00
// Base types
else
2016-11-29 19:28:07 +01:00
if ( oItem instanceof cXSAnyAtomicType )
vItem = oItem ;
2016-12-17 00:33:47 +01:00
//
if ( vItem != null )
2016-11-29 19:28:07 +01:00
oSequence . push ( vItem ) ;
}
return oSequence ;
} ;
2016-12-17 00:33:47 +01:00
// Orders items in sequence in document order
2016-11-29 19:28:07 +01:00
function fFunction _sequence _order ( oSequence1 , oContext ) {
return oSequence1 . sort ( function ( oNode , oNode2 ) {
var nPosition = oContext . DOMAdapter . compareDocumentPosition ( oNode , oNode2 ) ;
return nPosition & 2 ? 1 : nPosition & 4 ? - 1 : 0 ;
} ) ;
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
7.2 Functions to Assemble and Disassemble Strings
codepoints - to - string
string - to - codepoints
7.3 Equality and Comparison of Strings
compare
codepoint - equal
7.4 Functions on String Values
concat
string - join
substring
string - length
normalize - space
normalize - unicode
upper - case
lower - case
translate
encode - for - uri
iri - to - uri
escape - html - uri
7.5 Functions Based on Substring Matching
contains
starts - with
ends - with
substring - before
substring - after
7.6 String Functions that Use Pattern Matching
matches
replace
tokenize
* /
// 7.2 Functions to Assemble and Disassemble Strings
// fn:codepoints-to-string($arg as xs:integer*) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "codepoints-to-string" , [ [ cXSInteger , '*' ] ] , function ( oSequence1 ) {
var aValue = [ ] ;
for ( var nIndex = 0 , nLength = oSequence1 . length ; nIndex < nLength ; nIndex ++ )
aValue . push ( cString . fromCharCode ( oSequence1 [ nIndex ] ) ) ;
return new cXSString ( aValue . join ( '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:string-to-codepoints($arg as xs:string?) as xs:integer*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "string-to-codepoints" , [ [ cXSString , '?' ] ] , function ( oValue ) {
if ( oValue == null )
return null ;
var sValue = oValue . valueOf ( ) ;
if ( sValue == '' )
return [ ] ;
var oSequence = [ ] ;
for ( var nIndex = 0 , nLength = sValue . length ; nIndex < nLength ; nIndex ++ )
oSequence . push ( new cXSInteger ( sValue . charCodeAt ( nIndex ) ) ) ;
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
// 7.3 Equality and Comparison of Strings
// fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:integer?
// fn:compare($comparand1 as xs:string?, $comparand2 as xs:string?, $collation as xs:string) as xs:integer?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "compare" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue1 , oValue2 , oCollation ) {
if ( oValue1 == null || oValue2 == null )
return null ;
var sCollation = this . staticContext . defaultCollationName ,
vCollation ;
if ( arguments . length > 2 )
sCollation = oCollation . valueOf ( ) ;
vCollation = sCollation == sNS _XPF + "/collation/codepoint" ? oCodepointStringCollator : this . staticContext . getCollation ( sCollation ) ;
if ( ! vCollation )
throw new cException ( "FOCH0002"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
return new cXSInteger ( vCollation . compare ( oValue1 . valueOf ( ) , oValue2 . valueOf ( ) ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:codepoint-equal($comparand1 as xs:string?, $comparand2 as xs:string?) as xs:boolean?
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "codepoint-equal" , [ [ cXSString , '?' ] , [ cXSString , '?' ] ] , function ( oValue1 , oValue2 ) {
if ( oValue1 == null || oValue2 == null )
return null ;
2016-12-17 00:33:47 +01:00
// TODO: Check if JS uses 'Unicode code point collation' here
2016-11-29 19:28:07 +01:00
return new cXSBoolean ( oValue1 . valueOf ( ) == oValue2 . valueOf ( ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 7.4 Functions on String Values
// fn:concat($arg1 as xs:anyAtomicType?, $arg2 as xs:anyAtomicType?, ...) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "concat" , null , function ( ) {
2016-12-17 00:33:47 +01:00
// check arguments length
if ( arguments . length < 2 )
2016-11-29 19:28:07 +01:00
throw new cException ( "XPST0017"
2016-12-17 00:33:47 +01:00
2016-11-29 19:28:07 +01:00
) ;
var aValue = [ ] ;
for ( var nIndex = 0 , nLength = arguments . length , oSequence ; nIndex < nLength ; nIndex ++ ) {
oSequence = arguments [ nIndex ] ;
2016-12-17 00:33:47 +01:00
// Assert cardinality
fFunctionCall _assertSequenceCardinality ( this , oSequence , '?'
2016-11-29 19:28:07 +01:00
) ;
2016-12-17 00:33:47 +01:00
//
if ( oSequence . length )
2016-11-29 19:28:07 +01:00
aValue [ aValue . length ] = cXSString . cast ( fFunction _sequence _atomize ( oSequence , this ) [ 0 ] ) . valueOf ( ) ;
}
return new cXSString ( aValue . join ( '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "string-join" , [ [ cXSString , '*' ] , [ cXSString ] ] , function ( oSequence1 , oValue ) {
return new cXSString ( oSequence1 . join ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:substring($sourceString as xs:string?, $startingLoc as xs:double) as xs:string
// fn:substring($sourceString as xs:string?, $startingLoc as xs:double, $length as xs:double) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "substring" , [ [ cXSString , '?' ] , [ cXSDouble ] , [ cXSDouble , '' , true ] ] , function ( oValue , oStart , oLength ) {
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
nStart = cMath . round ( oStart ) - 1 ,
nEnd = arguments . length > 2 ? nStart + cMath . round ( oLength ) : sValue . length ;
2016-12-17 00:33:47 +01:00
// TODO: start can be negative
return new cXSString ( nEnd > nStart ? sValue . substring ( nStart , nEnd ) : '' ) ;
2016-11-29 19:28:07 +01:00
} ) ;
2016-12-17 00:33:47 +01:00
// fn:string-length() as xs:integer
// fn:string-length($arg as xs:string?) as xs:integer
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "string-length" , [ [ cXSString , '?' , true ] ] , function ( oValue ) {
if ( ! arguments . length ) {
if ( ! this . item )
throw new cException ( "XPDY0002" ) ;
oValue = cXSString . cast ( fFunction _sequence _atomize ( [ this . item ] , this ) [ 0 ] ) ;
}
return new cXSInteger ( oValue == null ? 0 : oValue . valueOf ( ) . length ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:normalize-space() as xs:string
// fn:normalize-space($arg as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "normalize-space" , [ [ cXSString , '?' , true ] ] , function ( oValue ) {
if ( ! arguments . length ) {
if ( ! this . item )
throw new cException ( "XPDY0002" ) ;
oValue = cXSString . cast ( fFunction _sequence _atomize ( [ this . item ] , this ) [ 0 ] ) ;
}
return new cXSString ( oValue == null ? '' : fString _trim ( oValue ) . replace ( /\s\s+/g , ' ' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:normalize-unicode($arg as xs:string?) as xs:string
// fn:normalize-unicode($arg as xs:string?, $normalizationForm as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "normalize-unicode" , [ [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oNormalization ) {
throw "Function '" + "normalize-unicode" + "' not implemented" ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:upper-case($arg as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "upper-case" , [ [ cXSString , '?' ] ] , function ( oValue ) {
return new cXSString ( oValue == null ? '' : oValue . valueOf ( ) . toUpperCase ( ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:lower-case($arg as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "lower-case" , [ [ cXSString , '?' ] ] , function ( oValue ) {
return new cXSString ( oValue == null ? '' : oValue . valueOf ( ) . toLowerCase ( ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:translate($arg as xs:string?, $mapString as xs:string, $transString as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "translate" , [ [ cXSString , '?' ] , [ cXSString ] , [ cXSString ] ] , function ( oValue , oMap , oTranslate ) {
if ( oValue == null )
return new cXSString ( '' ) ;
var aValue = oValue . valueOf ( ) . split ( '' ) ,
aMap = oMap . valueOf ( ) . split ( '' ) ,
aTranslate = oTranslate . valueOf ( ) . split ( '' ) ,
nTranslateLength = aTranslate . length ,
aReturn = [ ] ;
for ( var nIndex = 0 , nLength = aValue . length , nPosition ; nIndex < nLength ; nIndex ++ )
if ( ( nPosition = aMap . indexOf ( aValue [ nIndex ] ) ) == - 1 )
aReturn [ aReturn . length ] = aValue [ nIndex ] ;
else
if ( nPosition < nTranslateLength )
aReturn [ aReturn . length ] = aTranslate [ nPosition ] ;
return new cXSString ( aReturn . join ( '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:encode-for-uri($uri-part as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "encode-for-uri" , [ [ cXSString , '?' ] ] , function ( oValue ) {
return new cXSString ( oValue == null ? '' : window . encodeURIComponent ( oValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:iri-to-uri($iri as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "iri-to-uri" , [ [ cXSString , '?' ] ] , function ( oValue ) {
return new cXSString ( oValue == null ? '' : window . encodeURI ( window . decodeURI ( oValue ) ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:escape-html-uri($uri as xs:string?) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "escape-html-uri" , [ [ cXSString , '?' ] ] , function ( oValue ) {
if ( oValue == null || oValue . valueOf ( ) == '' )
return new cXSString ( '' ) ;
2016-12-17 00:33:47 +01:00
// Encode
var aValue = oValue . valueOf ( ) . split ( '' ) ;
2016-11-29 19:28:07 +01:00
for ( var nIndex = 0 , nLength = aValue . length , nCode ; nIndex < nLength ; nIndex ++ )
if ( ( nCode = aValue [ nIndex ] . charCodeAt ( 0 ) ) < 32 || nCode > 126 )
aValue [ nIndex ] = window . encodeURIComponent ( aValue [ nIndex ] ) ;
return new cXSString ( aValue . join ( '' ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 7.5 Functions Based on Substring Matching
// fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
// fn:contains($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "contains" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oSearch , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 2 )
throw "Collation parameter in function '" + "contains" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
return new cXSBoolean ( ( oValue == null ? '' : oValue . valueOf ( ) ) . indexOf ( oSearch == null ? '' : oSearch . valueOf ( ) ) >= 0 ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
// fn:starts-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "starts-with" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oSearch , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 2 )
throw "Collation parameter in function '" + "starts-with" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
return new cXSBoolean ( ( oValue == null ? '' : oValue . valueOf ( ) ) . indexOf ( oSearch == null ? '' : oSearch . valueOf ( ) ) == 0 ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean
// fn:ends-with($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "ends-with" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oSearch , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 2 )
throw "Collation parameter in function '" + "ends-with" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
sSearch = oSearch == null ? '' : oSearch . valueOf ( ) ;
return new cXSBoolean ( sValue . indexOf ( sSearch ) == sValue . length - sSearch . length ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?) as xs:string
// fn:substring-before($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "substring-before" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oSearch , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 2 )
throw "Collation parameter in function '" + "substring-before" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
sSearch = oSearch == null ? '' : oSearch . valueOf ( ) ,
nPosition ;
return new cXSString ( ( nPosition = sValue . indexOf ( sSearch ) ) >= 0 ? sValue . substring ( 0 , nPosition ) : '' ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?) as xs:string
// fn:substring-after($arg1 as xs:string?, $arg2 as xs:string?, $collation as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "substring-after" , [ [ cXSString , '?' ] , [ cXSString , '?' ] , [ cXSString , '' , true ] ] , function ( oValue , oSearch , oCollation ) {
2016-12-17 00:33:47 +01:00
if ( arguments . length > 2 )
throw "Collation parameter in function '" + "substring-after" + "' is not implemented" ;
2016-11-29 19:28:07 +01:00
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
sSearch = oSearch == null ? '' : oSearch . valueOf ( ) ,
nPosition ;
return new cXSString ( ( nPosition = sValue . indexOf ( sSearch ) ) >= 0 ? sValue . substring ( nPosition + sSearch . length ) : '' ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// 7.6 String Functions that Use Pattern Matching
2016-11-29 19:28:07 +01:00
function fFunction _string _createRegExp ( sValue , sFlags ) {
var d1 = '\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF' ,
d2 = '\u0370-\u037D\u037F-\u1FFF\u200C-\u200D' ,
d3 = '\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD' ,
c = 'A-Z_a-z\\-.0-9\u00B7' + d1 + '\u0300-\u036F' + d2 + '\u203F-\u2040' + d3 ,
i = 'A-Z_a-z' + d1 + d2 + d3 ;
sValue = sValue
. replace ( /\[\\i-\[:\]\]/g , '[' + i + ']' )
. replace ( /\[\\c-\[:\]\]/g , '[' + c + ']' )
. replace ( /\\i/g , '[:' + i + ']' )
. replace ( /\\I/g , '[^:' + i + ']' )
. replace ( /\\c/g , '[:' + c + ']' )
. replace ( /\\C/g , '[^:' + c + ']' ) ;
2016-12-17 00:33:47 +01:00
// Check if all flags are legal
if ( sFlags && ! sFlags . match ( /^[smix]+$/ ) )
throw new cException ( "FORX0001" ) ; // Invalid character '{%0}' in regular expression flags
2016-11-29 19:28:07 +01:00
var bFlagS = sFlags . indexOf ( 's' ) >= 0 ,
bFlagX = sFlags . indexOf ( 'x' ) >= 0 ;
if ( bFlagS || bFlagX ) {
2016-12-17 00:33:47 +01:00
// Strip 's' and 'x' from flags
sFlags = sFlags . replace ( /[sx]/g , '' ) ;
2016-11-29 19:28:07 +01:00
var aValue = [ ] ,
rValue = /\s/ ;
for ( var nIndex = 0 , nLength = sValue . length , bValue = false , sCharCurr , sCharPrev = '' ; nIndex < nLength ; nIndex ++ ) {
sCharCurr = sValue . charAt ( nIndex ) ;
if ( sCharPrev != '\\' ) {
if ( sCharCurr == '[' )
bValue = true ;
else
if ( sCharCurr == ']' )
bValue = false ;
}
2016-12-17 00:33:47 +01:00
// Replace '\s' for flag 'x' if not in []
if ( bValue || ! ( bFlagX && rValue . test ( sCharCurr ) ) ) {
// Replace '.' for flag 's' if not in []
if ( ! bValue && ( bFlagS && sCharCurr == '.' && sCharPrev != '\\' ) )
2016-11-29 19:28:07 +01:00
aValue [ aValue . length ] = '(?:.|\\s)' ;
else
aValue [ aValue . length ] = sCharCurr ;
}
sCharPrev = sCharCurr ;
}
sValue = aValue . join ( '' ) ;
}
return new cRegExp ( sValue , sFlags + 'g' ) ;
} ;
2016-12-17 00:33:47 +01:00
// fn:matches($input as xs:string?, $pattern as xs:string) as xs:boolean
// fn:matches($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:boolean
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "matches" , [ [ cXSString , '?' ] , [ cXSString ] , [ cXSString , '' , true ] ] , function ( oValue , oPattern , oFlags ) {
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
rRegExp = fFunction _string _createRegExp ( oPattern . valueOf ( ) , arguments . length > 2 ? oFlags . valueOf ( ) : '' ) ;
return new cXSBoolean ( rRegExp . test ( sValue ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string) as xs:string
// fn:replace($input as xs:string?, $pattern as xs:string, $replacement as xs:string, $flags as xs:string) as xs:string
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "replace" , [ [ cXSString , '?' ] , [ cXSString ] , [ cXSString ] , [ cXSString , '' , true ] ] , function ( oValue , oPattern , oReplacement , oFlags ) {
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
rRegExp = fFunction _string _createRegExp ( oPattern . valueOf ( ) , arguments . length > 3 ? oFlags . valueOf ( ) : '' ) ;
return new cXSBoolean ( sValue . replace ( rRegExp , oReplacement . valueOf ( ) ) ) ;
} ) ;
2016-12-17 00:33:47 +01:00
// fn:tokenize($input as xs:string?, $pattern as xs:string) as xs:string*
// fn:tokenize($input as xs:string?, $pattern as xs:string, $flags as xs:string) as xs:string*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "tokenize" , [ [ cXSString , '?' ] , [ cXSString ] , [ cXSString , '' , true ] ] , function ( oValue , oPattern , oFlags ) {
var sValue = oValue == null ? '' : oValue . valueOf ( ) ,
rRegExp = fFunction _string _createRegExp ( oPattern . valueOf ( ) , arguments . length > 2 ? oFlags . valueOf ( ) : '' ) ;
var oSequence = [ ] ;
for ( var nIndex = 0 , aValue = sValue . split ( rRegExp ) , nLength = aValue . length ; nIndex < nLength ; nIndex ++ )
oSequence . push ( new cXSString ( aValue [ nIndex ] ) ) ;
return oSequence ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
/ *
4 The Trace Function
trace
* /
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
// fn:trace($value as item()*, $label as xs:string) as item()*
2016-11-29 19:28:07 +01:00
fStaticContext _defineSystemFunction ( "trace" , [ [ cXTItem , '*' ] , [ cXSString ] ] , function ( oSequence1 , oLabel ) {
var oConsole = window . console ;
if ( oConsole && oConsole . log )
oConsole . log ( oLabel . valueOf ( ) , oSequence1 ) ;
return oSequence1 ;
} ) ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
2016-11-29 19:28:07 +01:00
var oCodepointStringCollator = new cStringCollator ;
oCodepointStringCollator . equals = function ( sValue1 , sValue2 ) {
return sValue1 == sValue2 ;
} ;
oCodepointStringCollator . compare = function ( sValue1 , sValue2 ) {
return sValue1 == sValue2 ? 0 : sValue1 > sValue2 ? 1 : - 1 ;
} ;
function cLXDOMAdapter ( ) {
} ;
cLXDOMAdapter . prototype = new cDOMAdapter ;
var oLXDOMAdapter _staticContext = new cStaticContext ;
cLXDOMAdapter . prototype . getProperty = function ( oNode , sName ) {
if ( sName in oNode )
return oNode [ sName ] ;
if ( sName == "baseURI" ) {
var sBaseURI = '' ,
fResolveUri = oLXDOMAdapter _staticContext . getFunction ( '{' + "http://www.w3.org/2005/xpath-functions" + '}' + "resolve-uri" ) ,
cXSString = oLXDOMAdapter _staticContext . getDataType ( '{' + "http://www.w3.org/2001/XMLSchema" + '}' + "string" ) ;
for ( var oParent = oNode , sUri ; oParent ; oParent = oParent . parentNode )
if ( oParent . nodeType == 1 && ( sUri = oParent . getAttribute ( "xml:base" ) ) )
sBaseURI = fResolveUri ( new cXSString ( sUri ) , new cXSString ( sBaseURI ) ) . toString ( ) ;
return sBaseURI ;
}
else
if ( sName == "textContent" ) {
var aText = [ ] ;
( function ( oNode ) {
for ( var nIndex = 0 , oChild ; oChild = oNode . childNodes [ nIndex ] ; nIndex ++ )
if ( oChild . nodeType == 3 || oChild . nodeType == 4 )
aText . push ( oChild . data ) ;
else
if ( oChild . nodeType == 1 && oChild . firstChild )
arguments . callee ( oChild ) ;
} ) ( oNode ) ;
return aText . join ( '' ) ;
}
} ;
cLXDOMAdapter . prototype . compareDocumentPosition = function ( oNode , oChild ) {
if ( "compareDocumentPosition" in oNode )
return oNode . compareDocumentPosition ( oChild ) ;
if ( oChild == oNode )
return 0 ;
var oAttr1 = null ,
oAttr2 = null ,
aAttributes ,
oAttr , oElement , nIndex , nLength ;
if ( oNode . nodeType == 2 ) {
oAttr1 = oNode ;
oNode = this . getProperty ( oAttr1 , "ownerElement" ) ;
}
if ( oChild . nodeType == 2 ) {
oAttr2 = oChild ;
oChild = this . getProperty ( oAttr2 , "ownerElement" ) ;
}
if ( oAttr1 && oAttr2 && oNode && oNode == oChild ) {
for ( nIndex = 0 , aAttributes = this . getProperty ( oNode , "attributes" ) , nLength = aAttributes . length ; nIndex < nLength ; nIndex ++ ) {
oAttr = aAttributes [ nIndex ] ;
if ( oAttr == oAttr1 )
return 32 | 4 ;
if ( oAttr == oAttr2 )
return 32 | 2 ;
}
}
var aChain1 = [ ] , nLength1 , oNode1 ,
aChain2 = [ ] , nLength2 , oNode2 ;
if ( oAttr1 )
aChain1 . push ( oAttr1 ) ;
for ( oElement = oNode ; oElement ; oElement = oElement . parentNode )
aChain1 . push ( oElement ) ;
if ( oAttr2 )
aChain2 . push ( oAttr2 ) ;
for ( oElement = oChild ; oElement ; oElement = oElement . parentNode )
aChain2 . push ( oElement ) ;
if ( ( ( oNode . ownerDocument || oNode ) != ( oChild . ownerDocument || oChild ) ) || ( aChain1 [ aChain1 . length - 1 ] != aChain2 [ aChain2 . length - 1 ] ) )
return 32 | 1 ;
for ( nIndex = cMath . min ( nLength1 = aChain1 . length , nLength2 = aChain2 . length ) ; nIndex ; -- nIndex )
if ( ( oNode1 = aChain1 [ -- nLength1 ] ) != ( oNode2 = aChain2 [ -- nLength2 ] ) ) {
if ( oNode1 . nodeType == 2 )
return 4 ;
if ( oNode2 . nodeType == 2 )
return 2 ;
if ( ! oNode2 . nextSibling )
return 4 ;
if ( ! oNode1 . nextSibling )
return 2 ;
for ( oElement = oNode2 . previousSibling ; oElement ; oElement = oElement . previousSibling )
if ( oElement == oNode1 )
return 4 ;
return 2 ;
}
return nLength1 < nLength2 ? 4 | 16 : 2 | 8 ;
} ;
cLXDOMAdapter . prototype . lookupNamespaceURI = function ( oNode , sPrefix ) {
if ( "lookupNamespaceURI" in oNode )
return oNode . lookupNamespaceURI ( sPrefix ) ;
for ( ; oNode && oNode . nodeType != 9 ; oNode = oNode . parentNode )
if ( sPrefix == this . getProperty ( oChild , "prefix" ) )
return this . getProperty ( oNode , "namespaceURI" ) ;
else
if ( oNode . nodeType == 1 ) for ( var oAttributes = this . getProperty ( oNode , "attributes" ) , nIndex = 0 , nLength = oAttributes . length , sName = "xmlns" + ':' + sPrefix ; nIndex < nLength ; nIndex ++ )
if ( this . getProperty ( oAttributes [ nIndex ] , "nodeName" ) == sName )
return this . getProperty ( oAttributes [ nIndex ] , "value" ) ;
return null ;
} ;
cLXDOMAdapter . prototype . getElementsByTagNameNS = function ( oNode , sNameSpaceURI , sLocalName ) {
if ( "getElementsByTagNameNS" in oNode )
return oNode . getElementsByTagNameNS ( sNameSpaceURI , sLocalName ) ;
var aElements = [ ] ,
bNameSpaceURI = '*' == sNameSpaceURI ,
bLocalName = '*' == sLocalName ;
( function ( oNode ) {
for ( var nIndex = 0 , oChild ; oChild = oNode . childNodes [ nIndex ] ; nIndex ++ )
if ( oChild . nodeType == 1 ) { if ( ( bLocalName || sLocalName == this . getProperty ( oChild , "localName" ) ) && ( bNameSpaceURI || sNameSpaceURI == this . getProperty ( oChild , "namespaceURI" ) ) )
aElements [ aElements . length ] = oChild ;
if ( oChild . firstChild )
arguments . callee ( oChild ) ;
}
} ) ( oNode ) ;
return aElements ;
} ;
var oMSHTMLDOMAdapter = new cLXDOMAdapter ;
oMSHTMLDOMAdapter . getProperty = function ( oNode , sName ) {
if ( sName == "localName" ) {
if ( oNode . nodeType == 1 )
return oNode . nodeName . toLowerCase ( ) ;
}
if ( sName == "prefix" )
return null ;
if ( sName == "namespaceURI" )
return oNode . nodeType == 1 ? "http://www.w3.org/1999/xhtml" : null ;
if ( sName == "textContent" )
return oNode . innerText ;
if ( sName == "attributes" && oNode . nodeType == 1 ) {
var aAttributes = [ ] ;
for ( var nIndex = 0 , oAttributes = oNode . attributes , nLength = oAttributes . length , oNode2 , oAttribute ; nIndex < nLength ; nIndex ++ ) {
oNode2 = oAttributes [ nIndex ] ;
if ( oNode2 . specified ) {
oAttribute = new cAttr ;
oAttribute . ownerElement = oNode ;
oAttribute . ownerDocument = oNode . ownerDocument ;
oAttribute . specified = true ;
oAttribute . value =
oAttribute . nodeValue = oNode2 . nodeValue ;
oAttribute . name =
oAttribute . nodeName =
oAttribute . localName = oNode2 . nodeName . toLowerCase ( ) ;
aAttributes [ aAttributes . length ] = oAttribute ;
}
}
return aAttributes ;
}
return cLXDOMAdapter . prototype . getProperty . call ( this , oNode , sName ) ;
} ;
var oMSXMLDOMAdapter = new cLXDOMAdapter ;
oMSXMLDOMAdapter . getProperty = function ( oNode , sName ) {
if ( sName == "localName" ) {
if ( oNode . nodeType == 7 )
return null ;
if ( oNode . nodeType == 1 )
return oNode . baseName ;
}
if ( sName == "prefix" || sName == "namespaceURI" )
return oNode [ sName ] || null ;
if ( sName == "textContent" )
return oNode . text ;
if ( sName == "attributes" && oNode . nodeType == 1 ) {
var aAttributes = [ ] ;
for ( var nIndex = 0 , oAttributes = oNode . attributes , nLength = oAttributes . length , oNode2 , oAttribute ; nIndex < nLength ; nIndex ++ ) {
oNode2 = oAttributes [ nIndex ] ;
if ( oNode2 . specified ) {
oAttribute = new cAttr ;
oAttribute . nodeType = 2 ;
oAttribute . ownerElement = oNode ;
oAttribute . ownerDocument = oNode . ownerDocument ;
oAttribute . specified = true ;
oAttribute . value =
oAttribute . nodeValue = oNode2 . nodeValue ;
oAttribute . name =
oAttribute . nodeName = oNode2 . nodeName ;
oAttribute . localName = oNode2 . baseName ;
oAttribute . prefix = oNode2 . prefix || null ;
oAttribute . namespaceURI = oNode2 . namespaceURI || null ;
aAttributes [ aAttributes . length ] = oAttribute ;
}
}
return aAttributes ;
}
return cLXDOMAdapter . prototype . getProperty . call ( this , oNode , sName ) ;
} ;
oMSXMLDOMAdapter . getElementById = function ( oDocument , sId ) {
return oDocument . nodeFromID ( sId ) ;
} ;
2016-12-17 00:33:47 +01:00
function cEvaluator ( ) {
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
} ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . defaultOL2DOMAdapter = new cLXDOMAdapter ;
cEvaluator . prototype . defaultOL2HTMLDOMAdapter = new cLXDOMAdapter ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . defaultHTMLStaticContext = new cStaticContext ;
cEvaluator . prototype . defaultHTMLStaticContext . baseURI = window . document . location . href ;
cEvaluator . prototype . defaultHTMLStaticContext . defaultFunctionNamespace = "http://www.w3.org/2005/xpath-functions" ;
cEvaluator . prototype . defaultHTMLStaticContext . defaultElementNamespace = "http://www.w3.org/1999/xhtml" ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . defaultXMLStaticContext = new cStaticContext ;
cEvaluator . prototype . defaultXMLStaticContext . defaultFunctionNamespace = "http://www.w3.org/2005/xpath-functions" ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . bOldMS = ! ! window . document . namespaces && ! window . document . createElementNS ;
cEvaluator . prototype . bOldW3 = ! cEvaluator . prototype . bOldMS && window . document . documentElement . namespaceURI != "http://www.w3.org/1999/xhtml" ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . defaultDOMAdapter = new cDOMAdapter ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . compile = function ( sExpression , oStaticContext ) {
return new cExpression ( sExpression , oStaticContext ) ;
} ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
cEvaluator . prototype . evaluate = function ( oQuery , sExpression , fNSResolver ) {
if ( ! ( oQuery instanceof window . jQuery ) )
oQuery = new window . jQuery ( oQuery )
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
if ( typeof sExpression == "undefined" || sExpression === null )
sExpression = '' ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
var oNode = oQuery [ 0 ] ;
if ( typeof oNode == "undefined" )
oNode = null ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
var oStaticContext = oNode && ( oNode . nodeType == 9 ? oNode : oNode . ownerDocument ) . createElement ( "div" ) . tagName == "DIV" ? this . defaultHTMLStaticContext : this . defaultXMLStaticContext ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
oStaticContext . namespaceResolver = fNSResolver ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
var oExpression = new cExpression ( cString ( sExpression ) , oStaticContext ) ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
oStaticContext . namespaceResolver = null ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
var aSequence ,
oSequence = new window . jQuery ,
oAdapter = this . defaultOL2DOMAdapter ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
if ( this . bOldMS )
oAdapter = oStaticContext == this . defaultHTMLStaticContext ? oMSHTMLDOMAdapter : oMSXMLDOMAdapter ;
else
if ( this . bOldW3 && oStaticContext == this . defaultHTMLStaticContext )
oAdapter = this . defaultOL2HTMLDOMAdapter ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
aSequence = oExpression . evaluate ( new cDynamicContext ( oStaticContext , oNode , null , oAdapter ) ) ;
for ( var nIndex = 0 , nLength = aSequence . length , oItem ; nIndex < nLength ; nIndex ++ )
oSequence . push ( oAdapter . isNode ( oItem = aSequence [ nIndex ] ) ? oItem : cStaticContext . xs2js ( oItem ) ) ;
2016-11-29 19:28:07 +01:00
2016-12-17 00:33:47 +01:00
return oSequence ;
2016-11-29 19:28:07 +01:00
} ;
2016-12-17 00:33:47 +01:00
/ *
* XPath . js - Pure JavaScript implementation of XPath 2.0 parser and evaluator
*
* Copyright ( c ) 2012 Sergey Ilinsky
* Dual licensed under the MIT and GPL licenses .
*
*
* /
var oXPathEvaluator = new cEvaluator ,
oXPathClasses = oXPathEvaluator . classes = { } ;
//
oXPathClasses . Exception = cException ;
oXPathClasses . Expression = cExpression ;
oXPathClasses . DOMAdapter = cDOMAdapter ;
oXPathClasses . StaticContext = cStaticContext ;
oXPathClasses . DynamicContext = cDynamicContext ;
oXPathClasses . StringCollator = cStringCollator ;
// Publish object
window . xpath = oXPathEvaluator ;
} ) ( )