tck-lambdas 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4693 @@
1
+ /*
2
+ * xpath.js
3
+ *
4
+ * An XPath 1.0 library for JavaScript.
5
+ *
6
+ * Cameron McCormack <cam (at) mcc.id.au>
7
+ *
8
+ * This work is licensed under the Creative Commons Attribution-ShareAlike
9
+ * License. To view a copy of this license, visit
10
+ *
11
+ * http://creativecommons.org/licenses/by-sa/2.0/
12
+ *
13
+ * or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,
14
+ * California 94305, USA.
15
+ *
16
+ * Revision 20: April 26, 2011
17
+ * Fixed a typo resulting in FIRST_ORDERED_NODE_TYPE results being wrong,
18
+ * thanks to <shi_a009 (at) hotmail.com>.
19
+ *
20
+ * Revision 19: November 29, 2005
21
+ * Nodesets now store their nodes in a height balanced tree, increasing
22
+ * performance for the common case of selecting nodes in document order,
23
+ * thanks to S閎astien Cramatte <contact (at) zeninteractif.com>.
24
+ * AVL tree code adapted from Raimund Neumann <rnova (at) gmx.net>.
25
+ *
26
+ * Revision 18: October 27, 2005
27
+ * DOM 3 XPath support. Caveats:
28
+ * - namespace prefixes aren't resolved in XPathEvaluator.createExpression,
29
+ * but in XPathExpression.evaluate.
30
+ * - XPathResult.invalidIteratorState is not implemented.
31
+ *
32
+ * Revision 17: October 25, 2005
33
+ * Some core XPath function fixes and a patch to avoid crashing certain
34
+ * versions of MSXML in PathExpr.prototype.getOwnerElement, thanks to
35
+ * S閎astien Cramatte <contact (at) zeninteractif.com>.
36
+ *
37
+ * Revision 16: September 22, 2005
38
+ * Workarounds for some IE 5.5 deficiencies.
39
+ * Fixed problem with prefix node tests on attribute nodes.
40
+ *
41
+ * Revision 15: May 21, 2005
42
+ * Fixed problem with QName node tests on elements with an xmlns="...".
43
+ *
44
+ * Revision 14: May 19, 2005
45
+ * Fixed QName node tests on attribute node regression.
46
+ *
47
+ * Revision 13: May 3, 2005
48
+ * Node tests are case insensitive now if working in an HTML DOM.
49
+ *
50
+ * Revision 12: April 26, 2005
51
+ * Updated licence. Slight code changes to enable use of Dean
52
+ * Edwards' script compression, http://dean.edwards.name/packer/ .
53
+ *
54
+ * Revision 11: April 23, 2005
55
+ * Fixed bug with 'and' and 'or' operators, fix thanks to
56
+ * Sandy McArthur <sandy (at) mcarthur.org>.
57
+ *
58
+ * Revision 10: April 15, 2005
59
+ * Added support for a virtual root node, supposedly helpful for
60
+ * implementing XForms. Fixed problem with QName node tests and
61
+ * the parent axis.
62
+ *
63
+ * Revision 9: March 17, 2005
64
+ * Namespace resolver tweaked so using the document node as the context
65
+ * for namespace lookups is equivalent to using the document element.
66
+ *
67
+ * Revision 8: February 13, 2005
68
+ * Handle implicit declaration of 'xmlns' namespace prefix.
69
+ * Fixed bug when comparing nodesets.
70
+ * Instance data can now be associated with a FunctionResolver, and
71
+ * workaround for MSXML not supporting 'localName' and 'getElementById',
72
+ * thanks to Grant Gongaware.
73
+ * Fix a few problems when the context node is the root node.
74
+ *
75
+ * Revision 7: February 11, 2005
76
+ * Default namespace resolver fix from Grant Gongaware
77
+ * <grant (at) gongaware.com>.
78
+ *
79
+ * Revision 6: February 10, 2005
80
+ * Fixed bug in 'number' function.
81
+ *
82
+ * Revision 5: February 9, 2005
83
+ * Fixed bug where text nodes not getting converted to string values.
84
+ *
85
+ * Revision 4: January 21, 2005
86
+ * Bug in 'name' function, fix thanks to Bill Edney.
87
+ * Fixed incorrect processing of namespace nodes.
88
+ * Fixed NamespaceResolver to resolve 'xml' namespace.
89
+ * Implemented union '|' operator.
90
+ *
91
+ * Revision 3: January 14, 2005
92
+ * Fixed bug with nodeset comparisons, bug lexing < and >.
93
+ *
94
+ * Revision 2: October 26, 2004
95
+ * QName node test namespace handling fixed. Few other bug fixes.
96
+ *
97
+ * Revision 1: August 13, 2004
98
+ * Bug fixes from William J. Edney <bedney (at) technicalpursuit.com>.
99
+ * Added minimal licence.
100
+ *
101
+ * Initial version: June 14, 2004
102
+ */
103
+
104
+ // non-node wrapper
105
+ var xpath = (typeof exports === 'undefined') ? {} : exports;
106
+
107
+ (function(exports) {
108
+ "use strict";
109
+
110
+ // XPathParser ///////////////////////////////////////////////////////////////
111
+
112
+ XPathParser.prototype = new Object();
113
+ XPathParser.prototype.constructor = XPathParser;
114
+ XPathParser.superclass = Object.prototype;
115
+
116
+ function XPathParser() {
117
+ this.init();
118
+ }
119
+
120
+ XPathParser.prototype.init = function() {
121
+ this.reduceActions = [];
122
+
123
+ this.reduceActions[3] = function(rhs) {
124
+ return new OrOperation(rhs[0], rhs[2]);
125
+ };
126
+ this.reduceActions[5] = function(rhs) {
127
+ return new AndOperation(rhs[0], rhs[2]);
128
+ };
129
+ this.reduceActions[7] = function(rhs) {
130
+ return new EqualsOperation(rhs[0], rhs[2]);
131
+ };
132
+ this.reduceActions[8] = function(rhs) {
133
+ return new NotEqualOperation(rhs[0], rhs[2]);
134
+ };
135
+ this.reduceActions[10] = function(rhs) {
136
+ return new LessThanOperation(rhs[0], rhs[2]);
137
+ };
138
+ this.reduceActions[11] = function(rhs) {
139
+ return new GreaterThanOperation(rhs[0], rhs[2]);
140
+ };
141
+ this.reduceActions[12] = function(rhs) {
142
+ return new LessThanOrEqualOperation(rhs[0], rhs[2]);
143
+ };
144
+ this.reduceActions[13] = function(rhs) {
145
+ return new GreaterThanOrEqualOperation(rhs[0], rhs[2]);
146
+ };
147
+ this.reduceActions[15] = function(rhs) {
148
+ return new PlusOperation(rhs[0], rhs[2]);
149
+ };
150
+ this.reduceActions[16] = function(rhs) {
151
+ return new MinusOperation(rhs[0], rhs[2]);
152
+ };
153
+ this.reduceActions[18] = function(rhs) {
154
+ return new MultiplyOperation(rhs[0], rhs[2]);
155
+ };
156
+ this.reduceActions[19] = function(rhs) {
157
+ return new DivOperation(rhs[0], rhs[2]);
158
+ };
159
+ this.reduceActions[20] = function(rhs) {
160
+ return new ModOperation(rhs[0], rhs[2]);
161
+ };
162
+ this.reduceActions[22] = function(rhs) {
163
+ return new UnaryMinusOperation(rhs[1]);
164
+ };
165
+ this.reduceActions[24] = function(rhs) {
166
+ return new BarOperation(rhs[0], rhs[2]);
167
+ };
168
+ this.reduceActions[25] = function(rhs) {
169
+ return new PathExpr(undefined, undefined, rhs[0]);
170
+ };
171
+ this.reduceActions[27] = function(rhs) {
172
+ rhs[0].locationPath = rhs[2];
173
+ return rhs[0];
174
+ };
175
+ this.reduceActions[28] = function(rhs) {
176
+ rhs[0].locationPath = rhs[2];
177
+ rhs[0].locationPath.steps.unshift(new Step(Step.DESCENDANTORSELF, new NodeTest(NodeTest.NODE, undefined), []));
178
+ return rhs[0];
179
+ };
180
+ this.reduceActions[29] = function(rhs) {
181
+ return new PathExpr(rhs[0], [], undefined);
182
+ };
183
+ this.reduceActions[30] = function(rhs) {
184
+ if (Utilities.instance_of(rhs[0], PathExpr)) {
185
+ if (rhs[0].filterPredicates == undefined) {
186
+ rhs[0].filterPredicates = [];
187
+ }
188
+ rhs[0].filterPredicates.push(rhs[1]);
189
+ return rhs[0];
190
+ } else {
191
+ return new PathExpr(rhs[0], [rhs[1]], undefined);
192
+ }
193
+ };
194
+ this.reduceActions[32] = function(rhs) {
195
+ return rhs[1];
196
+ };
197
+ this.reduceActions[33] = function(rhs) {
198
+ return new XString(rhs[0]);
199
+ };
200
+ this.reduceActions[34] = function(rhs) {
201
+ return new XNumber(rhs[0]);
202
+ };
203
+ this.reduceActions[36] = function(rhs) {
204
+ return new FunctionCall(rhs[0], []);
205
+ };
206
+ this.reduceActions[37] = function(rhs) {
207
+ return new FunctionCall(rhs[0], rhs[2]);
208
+ };
209
+ this.reduceActions[38] = function(rhs) {
210
+ return [ rhs[0] ];
211
+ };
212
+ this.reduceActions[39] = function(rhs) {
213
+ rhs[2].unshift(rhs[0]);
214
+ return rhs[2];
215
+ };
216
+ this.reduceActions[43] = function(rhs) {
217
+ return new LocationPath(true, []);
218
+ };
219
+ this.reduceActions[44] = function(rhs) {
220
+ rhs[1].absolute = true;
221
+ return rhs[1];
222
+ };
223
+ this.reduceActions[46] = function(rhs) {
224
+ return new LocationPath(false, [ rhs[0] ]);
225
+ };
226
+ this.reduceActions[47] = function(rhs) {
227
+ rhs[0].steps.push(rhs[2]);
228
+ return rhs[0];
229
+ };
230
+ this.reduceActions[49] = function(rhs) {
231
+ return new Step(rhs[0], rhs[1], []);
232
+ };
233
+ this.reduceActions[50] = function(rhs) {
234
+ return new Step(Step.CHILD, rhs[0], []);
235
+ };
236
+ this.reduceActions[51] = function(rhs) {
237
+ return new Step(rhs[0], rhs[1], rhs[2]);
238
+ };
239
+ this.reduceActions[52] = function(rhs) {
240
+ return new Step(Step.CHILD, rhs[0], rhs[1]);
241
+ };
242
+ this.reduceActions[54] = function(rhs) {
243
+ return [ rhs[0] ];
244
+ };
245
+ this.reduceActions[55] = function(rhs) {
246
+ rhs[1].unshift(rhs[0]);
247
+ return rhs[1];
248
+ };
249
+ this.reduceActions[56] = function(rhs) {
250
+ if (rhs[0] == "ancestor") {
251
+ return Step.ANCESTOR;
252
+ } else if (rhs[0] == "ancestor-or-self") {
253
+ return Step.ANCESTORORSELF;
254
+ } else if (rhs[0] == "attribute") {
255
+ return Step.ATTRIBUTE;
256
+ } else if (rhs[0] == "child") {
257
+ return Step.CHILD;
258
+ } else if (rhs[0] == "descendant") {
259
+ return Step.DESCENDANT;
260
+ } else if (rhs[0] == "descendant-or-self") {
261
+ return Step.DESCENDANTORSELF;
262
+ } else if (rhs[0] == "following") {
263
+ return Step.FOLLOWING;
264
+ } else if (rhs[0] == "following-sibling") {
265
+ return Step.FOLLOWINGSIBLING;
266
+ } else if (rhs[0] == "namespace") {
267
+ return Step.NAMESPACE;
268
+ } else if (rhs[0] == "parent") {
269
+ return Step.PARENT;
270
+ } else if (rhs[0] == "preceding") {
271
+ return Step.PRECEDING;
272
+ } else if (rhs[0] == "preceding-sibling") {
273
+ return Step.PRECEDINGSIBLING;
274
+ } else if (rhs[0] == "self") {
275
+ return Step.SELF;
276
+ }
277
+ return -1;
278
+ };
279
+ this.reduceActions[57] = function(rhs) {
280
+ return Step.ATTRIBUTE;
281
+ };
282
+ this.reduceActions[59] = function(rhs) {
283
+ if (rhs[0] == "comment") {
284
+ return new NodeTest(NodeTest.COMMENT, undefined);
285
+ } else if (rhs[0] == "text") {
286
+ return new NodeTest(NodeTest.TEXT, undefined);
287
+ } else if (rhs[0] == "processing-instruction") {
288
+ return new NodeTest(NodeTest.PI, undefined);
289
+ } else if (rhs[0] == "node") {
290
+ return new NodeTest(NodeTest.NODE, undefined);
291
+ }
292
+ return new NodeTest(-1, undefined);
293
+ };
294
+ this.reduceActions[60] = function(rhs) {
295
+ return new NodeTest(NodeTest.PI, rhs[2]);
296
+ };
297
+ this.reduceActions[61] = function(rhs) {
298
+ return rhs[1];
299
+ };
300
+ this.reduceActions[63] = function(rhs) {
301
+ rhs[1].absolute = true;
302
+ rhs[1].steps.unshift(new Step(Step.DESCENDANTORSELF, new NodeTest(NodeTest.NODE, undefined), []));
303
+ return rhs[1];
304
+ };
305
+ this.reduceActions[64] = function(rhs) {
306
+ rhs[0].steps.push(new Step(Step.DESCENDANTORSELF, new NodeTest(NodeTest.NODE, undefined), []));
307
+ rhs[0].steps.push(rhs[2]);
308
+ return rhs[0];
309
+ };
310
+ this.reduceActions[65] = function(rhs) {
311
+ return new Step(Step.SELF, new NodeTest(NodeTest.NODE, undefined), []);
312
+ };
313
+ this.reduceActions[66] = function(rhs) {
314
+ return new Step(Step.PARENT, new NodeTest(NodeTest.NODE, undefined), []);
315
+ };
316
+ this.reduceActions[67] = function(rhs) {
317
+ return new VariableReference(rhs[1]);
318
+ };
319
+ this.reduceActions[68] = function(rhs) {
320
+ return new NodeTest(NodeTest.NAMETESTANY, undefined);
321
+ };
322
+ this.reduceActions[69] = function(rhs) {
323
+ var prefix = rhs[0].substring(0, rhs[0].indexOf(":"));
324
+ return new NodeTest(NodeTest.NAMETESTPREFIXANY, prefix);
325
+ };
326
+ this.reduceActions[70] = function(rhs) {
327
+ return new NodeTest(NodeTest.NAMETESTQNAME, rhs[0]);
328
+ };
329
+ };
330
+
331
+ XPathParser.actionTable = [
332
+ " s s sssssssss s ss s ss",
333
+ " s ",
334
+ "r rrrrrrrrr rrrrrrr rr r ",
335
+ " rrrrr ",
336
+ " s s sssssssss s ss s ss",
337
+ "rs rrrrrrrr s sssssrrrrrr rrs rs ",
338
+ " s s sssssssss s ss s ss",
339
+ " s ",
340
+ " s ",
341
+ "r rrrrrrrrr rrrrrrr rr rr ",
342
+ "r rrrrrrrrr rrrrrrr rr rr ",
343
+ "r rrrrrrrrr rrrrrrr rr rr ",
344
+ "r rrrrrrrrr rrrrrrr rr rr ",
345
+ "r rrrrrrrrr rrrrrrr rr rr ",
346
+ " s ",
347
+ " s ",
348
+ " s s sssss s s ",
349
+ "r rrrrrrrrr rrrrrrr rr r ",
350
+ "a ",
351
+ "r s rr r ",
352
+ "r sr rr r ",
353
+ "r s rr s rr r ",
354
+ "r rssrr rss rr r ",
355
+ "r rrrrr rrrss rr r ",
356
+ "r rrrrrsss rrrrr rr r ",
357
+ "r rrrrrrrr rrrrr rr r ",
358
+ "r rrrrrrrr rrrrrs rr r ",
359
+ "r rrrrrrrr rrrrrr rr r ",
360
+ "r rrrrrrrr rrrrrr rr r ",
361
+ "r srrrrrrrr rrrrrrs rr sr ",
362
+ "r srrrrrrrr rrrrrrs rr r ",
363
+ "r rrrrrrrrr rrrrrrr rr rr ",
364
+ "r rrrrrrrrr rrrrrrr rr rr ",
365
+ "r rrrrrrrrr rrrrrrr rr rr ",
366
+ "r rrrrrrrr rrrrrr rr r ",
367
+ "r rrrrrrrr rrrrrr rr r ",
368
+ "r rrrrrrrrr rrrrrrr rr r ",
369
+ "r rrrrrrrrr rrrrrrr rr r ",
370
+ " sssss ",
371
+ "r rrrrrrrrr rrrrrrr rr sr ",
372
+ "r rrrrrrrrr rrrrrrr rr r ",
373
+ "r rrrrrrrrr rrrrrrr rr rr ",
374
+ "r rrrrrrrrr rrrrrrr rr rr ",
375
+ " s ",
376
+ "r srrrrrrrr rrrrrrs rr r ",
377
+ "r rrrrrrrr rrrrr rr r ",
378
+ " s ",
379
+ " s ",
380
+ " rrrrr ",
381
+ " s s sssssssss s sss s ss",
382
+ "r srrrrrrrr rrrrrrs rr r ",
383
+ " s s sssssssss s ss s ss",
384
+ " s s sssssssss s ss s ss",
385
+ " s s sssssssss s ss s ss",
386
+ " s s sssssssss s ss s ss",
387
+ " s s sssssssss s ss s ss",
388
+ " s s sssssssss s ss s ss",
389
+ " s s sssssssss s ss s ss",
390
+ " s s sssssssss s ss s ss",
391
+ " s s sssssssss s ss s ss",
392
+ " s s sssssssss s ss s ss",
393
+ " s s sssssssss s ss s ss",
394
+ " s s sssssssss s ss s ss",
395
+ " s s sssssssss s ss s ss",
396
+ " s s sssssssss ss s ss",
397
+ " s s sssssssss s ss s ss",
398
+ " s s sssss s s ",
399
+ " s s sssss s s ",
400
+ "r rrrrrrrrr rrrrrrr rr rr ",
401
+ " s s sssss s s ",
402
+ " s s sssss s s ",
403
+ "r rrrrrrrrr rrrrrrr rr sr ",
404
+ "r rrrrrrrrr rrrrrrr rr sr ",
405
+ "r rrrrrrrrr rrrrrrr rr r ",
406
+ "r rrrrrrrrr rrrrrrr rr rr ",
407
+ " s ",
408
+ "r rrrrrrrrr rrrrrrr rr rr ",
409
+ "r rrrrrrrrr rrrrrrr rr rr ",
410
+ " rr ",
411
+ " s ",
412
+ " rs ",
413
+ "r sr rr r ",
414
+ "r s rr s rr r ",
415
+ "r rssrr rss rr r ",
416
+ "r rssrr rss rr r ",
417
+ "r rrrrr rrrss rr r ",
418
+ "r rrrrr rrrss rr r ",
419
+ "r rrrrr rrrss rr r ",
420
+ "r rrrrr rrrss rr r ",
421
+ "r rrrrrsss rrrrr rr r ",
422
+ "r rrrrrsss rrrrr rr r ",
423
+ "r rrrrrrrr rrrrr rr r ",
424
+ "r rrrrrrrr rrrrr rr r ",
425
+ "r rrrrrrrr rrrrr rr r ",
426
+ "r rrrrrrrr rrrrrr rr r ",
427
+ " r ",
428
+ " s ",
429
+ "r srrrrrrrr rrrrrrs rr r ",
430
+ "r srrrrrrrr rrrrrrs rr r ",
431
+ "r rrrrrrrrr rrrrrrr rr r ",
432
+ "r rrrrrrrrr rrrrrrr rr r ",
433
+ "r rrrrrrrrr rrrrrrr rr r ",
434
+ "r rrrrrrrrr rrrrrrr rr r ",
435
+ "r rrrrrrrrr rrrrrrr rr rr ",
436
+ "r rrrrrrrrr rrrrrrr rr rr ",
437
+ " s s sssssssss s ss s ss",
438
+ "r rrrrrrrrr rrrrrrr rr rr ",
439
+ " r "
440
+ ];
441
+
442
+ XPathParser.actionTableNumber = [
443
+ " 1 0 /.-,+*)(' & %$ # \"!",
444
+ " J ",
445
+ "a aaaaaaaaa aaaaaaa aa a ",
446
+ " YYYYY ",
447
+ " 1 0 /.-,+*)(' & %$ # \"!",
448
+ "K1 KKKKKKKK . +*)('KKKKKK KK# K\" ",
449
+ " 1 0 /.-,+*)(' & %$ # \"!",
450
+ " N ",
451
+ " O ",
452
+ "e eeeeeeeee eeeeeee ee ee ",
453
+ "f fffffffff fffffff ff ff ",
454
+ "d ddddddddd ddddddd dd dd ",
455
+ "B BBBBBBBBB BBBBBBB BB BB ",
456
+ "A AAAAAAAAA AAAAAAA AA AA ",
457
+ " P ",
458
+ " Q ",
459
+ " 1 . +*)(' # \" ",
460
+ "b bbbbbbbbb bbbbbbb bb b ",
461
+ " ",
462
+ "! S !! ! ",
463
+ "\" T\" \"\" \" ",
464
+ "$ V $$ U $$ $ ",
465
+ "& &ZY&& &XW && & ",
466
+ ") ))))) )))\\[ )) ) ",
467
+ ". ....._^] ..... .. . ",
468
+ "1 11111111 11111 11 1 ",
469
+ "5 55555555 55555` 55 5 ",
470
+ "7 77777777 777777 77 7 ",
471
+ "9 99999999 999999 99 9 ",
472
+ ": c:::::::: ::::::b :: a: ",
473
+ "I fIIIIIIII IIIIIIe II I ",
474
+ "= ========= ======= == == ",
475
+ "? ????????? ??????? ?? ?? ",
476
+ "C CCCCCCCCC CCCCCCC CC CC ",
477
+ "J JJJJJJJJ JJJJJJ JJ J ",
478
+ "M MMMMMMMM MMMMMM MM M ",
479
+ "N NNNNNNNNN NNNNNNN NN N ",
480
+ "P PPPPPPPPP PPPPPPP PP P ",
481
+ " +*)(' ",
482
+ "R RRRRRRRRR RRRRRRR RR aR ",
483
+ "U UUUUUUUUU UUUUUUU UU U ",
484
+ "Z ZZZZZZZZZ ZZZZZZZ ZZ ZZ ",
485
+ "c ccccccccc ccccccc cc cc ",
486
+ " j ",
487
+ "L fLLLLLLLL LLLLLLe LL L ",
488
+ "6 66666666 66666 66 6 ",
489
+ " k ",
490
+ " l ",
491
+ " XXXXX ",
492
+ " 1 0 /.-,+*)(' & %$m # \"!",
493
+ "_ f________ ______e __ _ ",
494
+ " 1 0 /.-,+*)(' & %$ # \"!",
495
+ " 1 0 /.-,+*)(' & %$ # \"!",
496
+ " 1 0 /.-,+*)(' & %$ # \"!",
497
+ " 1 0 /.-,+*)(' & %$ # \"!",
498
+ " 1 0 /.-,+*)(' & %$ # \"!",
499
+ " 1 0 /.-,+*)(' & %$ # \"!",
500
+ " 1 0 /.-,+*)(' & %$ # \"!",
501
+ " 1 0 /.-,+*)(' & %$ # \"!",
502
+ " 1 0 /.-,+*)(' & %$ # \"!",
503
+ " 1 0 /.-,+*)(' & %$ # \"!",
504
+ " 1 0 /.-,+*)(' & %$ # \"!",
505
+ " 1 0 /.-,+*)(' & %$ # \"!",
506
+ " 1 0 /.-,+*)(' & %$ # \"!",
507
+ " 1 0 /.-,+*)(' %$ # \"!",
508
+ " 1 0 /.-,+*)(' & %$ # \"!",
509
+ " 1 . +*)(' # \" ",
510
+ " 1 . +*)(' # \" ",
511
+ "> >>>>>>>>> >>>>>>> >> >> ",
512
+ " 1 . +*)(' # \" ",
513
+ " 1 . +*)(' # \" ",
514
+ "Q QQQQQQQQQ QQQQQQQ QQ aQ ",
515
+ "V VVVVVVVVV VVVVVVV VV aV ",
516
+ "T TTTTTTTTT TTTTTTT TT T ",
517
+ "@ @@@@@@@@@ @@@@@@@ @@ @@ ",
518
+ " \x87 ",
519
+ "[ [[[[[[[[[ [[[[[[[ [[ [[ ",
520
+ "D DDDDDDDDD DDDDDDD DD DD ",
521
+ " HH ",
522
+ " \x88 ",
523
+ " F\x89 ",
524
+ "# T# ## # ",
525
+ "% V %% U %% % ",
526
+ "' 'ZY'' 'XW '' ' ",
527
+ "( (ZY(( (XW (( ( ",
528
+ "+ +++++ +++\\[ ++ + ",
529
+ "* ***** ***\\[ ** * ",
530
+ "- ----- ---\\[ -- - ",
531
+ ", ,,,,, ,,,\\[ ,, , ",
532
+ "0 00000_^] 00000 00 0 ",
533
+ "/ /////_^] ///// // / ",
534
+ "2 22222222 22222 22 2 ",
535
+ "3 33333333 33333 33 3 ",
536
+ "4 44444444 44444 44 4 ",
537
+ "8 88888888 888888 88 8 ",
538
+ " ^ ",
539
+ " \x8a ",
540
+ "; f;;;;;;;; ;;;;;;e ;; ; ",
541
+ "< f<<<<<<<< <<<<<<e << < ",
542
+ "O OOOOOOOOO OOOOOOO OO O ",
543
+ "` ````````` ``````` `` ` ",
544
+ "S SSSSSSSSS SSSSSSS SS S ",
545
+ "W WWWWWWWWW WWWWWWW WW W ",
546
+ "\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\ \\\\ \\\\ ",
547
+ "E EEEEEEEEE EEEEEEE EE EE ",
548
+ " 1 0 /.-,+*)(' & %$ # \"!",
549
+ "] ]]]]]]]]] ]]]]]]] ]] ]] ",
550
+ " G "
551
+ ];
552
+
553
+ XPathParser.gotoTable = [
554
+ "3456789:;<=>?@ AB CDEFGH IJ ",
555
+ " ",
556
+ " ",
557
+ " ",
558
+ "L456789:;<=>?@ AB CDEFGH IJ ",
559
+ " M EFGH IJ ",
560
+ " N;<=>?@ AB CDEFGH IJ ",
561
+ " ",
562
+ " ",
563
+ " ",
564
+ " ",
565
+ " ",
566
+ " ",
567
+ " ",
568
+ " ",
569
+ " ",
570
+ " S EFGH IJ ",
571
+ " ",
572
+ " ",
573
+ " ",
574
+ " ",
575
+ " ",
576
+ " ",
577
+ " ",
578
+ " ",
579
+ " ",
580
+ " ",
581
+ " ",
582
+ " ",
583
+ " e ",
584
+ " ",
585
+ " ",
586
+ " ",
587
+ " ",
588
+ " ",
589
+ " ",
590
+ " ",
591
+ " ",
592
+ " h J ",
593
+ " i j ",
594
+ " ",
595
+ " ",
596
+ " ",
597
+ " ",
598
+ " ",
599
+ " ",
600
+ " ",
601
+ " ",
602
+ " ",
603
+ "o456789:;<=>?@ ABpqCDEFGH IJ ",
604
+ " ",
605
+ " r6789:;<=>?@ AB CDEFGH IJ ",
606
+ " s789:;<=>?@ AB CDEFGH IJ ",
607
+ " t89:;<=>?@ AB CDEFGH IJ ",
608
+ " u89:;<=>?@ AB CDEFGH IJ ",
609
+ " v9:;<=>?@ AB CDEFGH IJ ",
610
+ " w9:;<=>?@ AB CDEFGH IJ ",
611
+ " x9:;<=>?@ AB CDEFGH IJ ",
612
+ " y9:;<=>?@ AB CDEFGH IJ ",
613
+ " z:;<=>?@ AB CDEFGH IJ ",
614
+ " {:;<=>?@ AB CDEFGH IJ ",
615
+ " |;<=>?@ AB CDEFGH IJ ",
616
+ " };<=>?@ AB CDEFGH IJ ",
617
+ " ~;<=>?@ AB CDEFGH IJ ",
618
+ " \x7f=>?@ AB CDEFGH IJ ",
619
+ "\x80456789:;<=>?@ AB CDEFGH IJ\x81",
620
+ " \x82 EFGH IJ ",
621
+ " \x83 EFGH IJ ",
622
+ " ",
623
+ " \x84 GH IJ ",
624
+ " \x85 GH IJ ",
625
+ " i \x86 ",
626
+ " i \x87 ",
627
+ " ",
628
+ " ",
629
+ " ",
630
+ " ",
631
+ " ",
632
+ " ",
633
+ " ",
634
+ " ",
635
+ " ",
636
+ " ",
637
+ " ",
638
+ " ",
639
+ " ",
640
+ " ",
641
+ " ",
642
+ " ",
643
+ " ",
644
+ " ",
645
+ " ",
646
+ " ",
647
+ " ",
648
+ " ",
649
+ " ",
650
+ " ",
651
+ " ",
652
+ " ",
653
+ " ",
654
+ " ",
655
+ " ",
656
+ " ",
657
+ " ",
658
+ " ",
659
+ "o456789:;<=>?@ AB\x8cqCDEFGH IJ ",
660
+ " ",
661
+ " "
662
+ ];
663
+
664
+ XPathParser.productions = [
665
+ [1, 1, 2],
666
+ [2, 1, 3],
667
+ [3, 1, 4],
668
+ [3, 3, 3, -9, 4],
669
+ [4, 1, 5],
670
+ [4, 3, 4, -8, 5],
671
+ [5, 1, 6],
672
+ [5, 3, 5, -22, 6],
673
+ [5, 3, 5, -5, 6],
674
+ [6, 1, 7],
675
+ [6, 3, 6, -23, 7],
676
+ [6, 3, 6, -24, 7],
677
+ [6, 3, 6, -6, 7],
678
+ [6, 3, 6, -7, 7],
679
+ [7, 1, 8],
680
+ [7, 3, 7, -25, 8],
681
+ [7, 3, 7, -26, 8],
682
+ [8, 1, 9],
683
+ [8, 3, 8, -12, 9],
684
+ [8, 3, 8, -11, 9],
685
+ [8, 3, 8, -10, 9],
686
+ [9, 1, 10],
687
+ [9, 2, -26, 9],
688
+ [10, 1, 11],
689
+ [10, 3, 10, -27, 11],
690
+ [11, 1, 12],
691
+ [11, 1, 13],
692
+ [11, 3, 13, -28, 14],
693
+ [11, 3, 13, -4, 14],
694
+ [13, 1, 15],
695
+ [13, 2, 13, 16],
696
+ [15, 1, 17],
697
+ [15, 3, -29, 2, -30],
698
+ [15, 1, -15],
699
+ [15, 1, -16],
700
+ [15, 1, 18],
701
+ [18, 3, -13, -29, -30],
702
+ [18, 4, -13, -29, 19, -30],
703
+ [19, 1, 20],
704
+ [19, 3, 20, -31, 19],
705
+ [20, 1, 2],
706
+ [12, 1, 14],
707
+ [12, 1, 21],
708
+ [21, 1, -28],
709
+ [21, 2, -28, 14],
710
+ [21, 1, 22],
711
+ [14, 1, 23],
712
+ [14, 3, 14, -28, 23],
713
+ [14, 1, 24],
714
+ [23, 2, 25, 26],
715
+ [23, 1, 26],
716
+ [23, 3, 25, 26, 27],
717
+ [23, 2, 26, 27],
718
+ [23, 1, 28],
719
+ [27, 1, 16],
720
+ [27, 2, 16, 27],
721
+ [25, 2, -14, -3],
722
+ [25, 1, -32],
723
+ [26, 1, 29],
724
+ [26, 3, -20, -29, -30],
725
+ [26, 4, -21, -29, -15, -30],
726
+ [16, 3, -33, 30, -34],
727
+ [30, 1, 2],
728
+ [22, 2, -4, 14],
729
+ [24, 3, 14, -4, 23],
730
+ [28, 1, -35],
731
+ [28, 1, -2],
732
+ [17, 2, -36, -18],
733
+ [29, 1, -17],
734
+ [29, 1, -19],
735
+ [29, 1, -18]
736
+ ];
737
+
738
+ XPathParser.DOUBLEDOT = 2;
739
+ XPathParser.DOUBLECOLON = 3;
740
+ XPathParser.DOUBLESLASH = 4;
741
+ XPathParser.NOTEQUAL = 5;
742
+ XPathParser.LESSTHANOREQUAL = 6;
743
+ XPathParser.GREATERTHANOREQUAL = 7;
744
+ XPathParser.AND = 8;
745
+ XPathParser.OR = 9;
746
+ XPathParser.MOD = 10;
747
+ XPathParser.DIV = 11;
748
+ XPathParser.MULTIPLYOPERATOR = 12;
749
+ XPathParser.FUNCTIONNAME = 13;
750
+ XPathParser.AXISNAME = 14;
751
+ XPathParser.LITERAL = 15;
752
+ XPathParser.NUMBER = 16;
753
+ XPathParser.ASTERISKNAMETEST = 17;
754
+ XPathParser.QNAME = 18;
755
+ XPathParser.NCNAMECOLONASTERISK = 19;
756
+ XPathParser.NODETYPE = 20;
757
+ XPathParser.PROCESSINGINSTRUCTIONWITHLITERAL = 21;
758
+ XPathParser.EQUALS = 22;
759
+ XPathParser.LESSTHAN = 23;
760
+ XPathParser.GREATERTHAN = 24;
761
+ XPathParser.PLUS = 25;
762
+ XPathParser.MINUS = 26;
763
+ XPathParser.BAR = 27;
764
+ XPathParser.SLASH = 28;
765
+ XPathParser.LEFTPARENTHESIS = 29;
766
+ XPathParser.RIGHTPARENTHESIS = 30;
767
+ XPathParser.COMMA = 31;
768
+ XPathParser.AT = 32;
769
+ XPathParser.LEFTBRACKET = 33;
770
+ XPathParser.RIGHTBRACKET = 34;
771
+ XPathParser.DOT = 35;
772
+ XPathParser.DOLLAR = 36;
773
+
774
+ XPathParser.prototype.tokenize = function(s1) {
775
+ var types = [];
776
+ var values = [];
777
+ var s = s1 + '\0';
778
+
779
+ var pos = 0;
780
+ var c = s.charAt(pos++);
781
+ while (1) {
782
+ while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
783
+ c = s.charAt(pos++);
784
+ }
785
+ if (c == '\0' || pos >= s.length) {
786
+ break;
787
+ }
788
+
789
+ if (c == '(') {
790
+ types.push(XPathParser.LEFTPARENTHESIS);
791
+ values.push(c);
792
+ c = s.charAt(pos++);
793
+ continue;
794
+ }
795
+ if (c == ')') {
796
+ types.push(XPathParser.RIGHTPARENTHESIS);
797
+ values.push(c);
798
+ c = s.charAt(pos++);
799
+ continue;
800
+ }
801
+ if (c == '[') {
802
+ types.push(XPathParser.LEFTBRACKET);
803
+ values.push(c);
804
+ c = s.charAt(pos++);
805
+ continue;
806
+ }
807
+ if (c == ']') {
808
+ types.push(XPathParser.RIGHTBRACKET);
809
+ values.push(c);
810
+ c = s.charAt(pos++);
811
+ continue;
812
+ }
813
+ if (c == '@') {
814
+ types.push(XPathParser.AT);
815
+ values.push(c);
816
+ c = s.charAt(pos++);
817
+ continue;
818
+ }
819
+ if (c == ',') {
820
+ types.push(XPathParser.COMMA);
821
+ values.push(c);
822
+ c = s.charAt(pos++);
823
+ continue;
824
+ }
825
+ if (c == '|') {
826
+ types.push(XPathParser.BAR);
827
+ values.push(c);
828
+ c = s.charAt(pos++);
829
+ continue;
830
+ }
831
+ if (c == '+') {
832
+ types.push(XPathParser.PLUS);
833
+ values.push(c);
834
+ c = s.charAt(pos++);
835
+ continue;
836
+ }
837
+ if (c == '-') {
838
+ types.push(XPathParser.MINUS);
839
+ values.push(c);
840
+ c = s.charAt(pos++);
841
+ continue;
842
+ }
843
+ if (c == '=') {
844
+ types.push(XPathParser.EQUALS);
845
+ values.push(c);
846
+ c = s.charAt(pos++);
847
+ continue;
848
+ }
849
+ if (c == '$') {
850
+ types.push(XPathParser.DOLLAR);
851
+ values.push(c);
852
+ c = s.charAt(pos++);
853
+ continue;
854
+ }
855
+
856
+ if (c == '.') {
857
+ c = s.charAt(pos++);
858
+ if (c == '.') {
859
+ types.push(XPathParser.DOUBLEDOT);
860
+ values.push("..");
861
+ c = s.charAt(pos++);
862
+ continue;
863
+ }
864
+ if (c >= '0' && c <= '9') {
865
+ var number = "." + c;
866
+ c = s.charAt(pos++);
867
+ while (c >= '0' && c <= '9') {
868
+ number += c;
869
+ c = s.charAt(pos++);
870
+ }
871
+ types.push(XPathParser.NUMBER);
872
+ values.push(number);
873
+ continue;
874
+ }
875
+ types.push(XPathParser.DOT);
876
+ values.push('.');
877
+ continue;
878
+ }
879
+
880
+ if (c == '\'' || c == '"') {
881
+ var delimiter = c;
882
+ var literal = "";
883
+ while (pos < s.length && (c = s.charAt(pos)) !== delimiter) {
884
+ literal += c;
885
+ pos += 1;
886
+ }
887
+ if (c !== delimiter) {
888
+ throw XPathException.fromMessage("Unterminated string literal: " + delimiter + literal);
889
+ }
890
+ pos += 1;
891
+ types.push(XPathParser.LITERAL);
892
+ values.push(literal);
893
+ c = s.charAt(pos++);
894
+ continue;
895
+ }
896
+
897
+ if (c >= '0' && c <= '9') {
898
+ var number = c;
899
+ c = s.charAt(pos++);
900
+ while (c >= '0' && c <= '9') {
901
+ number += c;
902
+ c = s.charAt(pos++);
903
+ }
904
+ if (c == '.') {
905
+ if (s.charAt(pos) >= '0' && s.charAt(pos) <= '9') {
906
+ number += c;
907
+ number += s.charAt(pos++);
908
+ c = s.charAt(pos++);
909
+ while (c >= '0' && c <= '9') {
910
+ number += c;
911
+ c = s.charAt(pos++);
912
+ }
913
+ }
914
+ }
915
+ types.push(XPathParser.NUMBER);
916
+ values.push(number);
917
+ continue;
918
+ }
919
+
920
+ if (c == '*') {
921
+ if (types.length > 0) {
922
+ var last = types[types.length - 1];
923
+ if (last != XPathParser.AT
924
+ && last != XPathParser.DOUBLECOLON
925
+ && last != XPathParser.LEFTPARENTHESIS
926
+ && last != XPathParser.LEFTBRACKET
927
+ && last != XPathParser.AND
928
+ && last != XPathParser.OR
929
+ && last != XPathParser.MOD
930
+ && last != XPathParser.DIV
931
+ && last != XPathParser.MULTIPLYOPERATOR
932
+ && last != XPathParser.SLASH
933
+ && last != XPathParser.DOUBLESLASH
934
+ && last != XPathParser.BAR
935
+ && last != XPathParser.PLUS
936
+ && last != XPathParser.MINUS
937
+ && last != XPathParser.EQUALS
938
+ && last != XPathParser.NOTEQUAL
939
+ && last != XPathParser.LESSTHAN
940
+ && last != XPathParser.LESSTHANOREQUAL
941
+ && last != XPathParser.GREATERTHAN
942
+ && last != XPathParser.GREATERTHANOREQUAL) {
943
+ types.push(XPathParser.MULTIPLYOPERATOR);
944
+ values.push(c);
945
+ c = s.charAt(pos++);
946
+ continue;
947
+ }
948
+ }
949
+ types.push(XPathParser.ASTERISKNAMETEST);
950
+ values.push(c);
951
+ c = s.charAt(pos++);
952
+ continue;
953
+ }
954
+
955
+ if (c == ':') {
956
+ if (s.charAt(pos) == ':') {
957
+ types.push(XPathParser.DOUBLECOLON);
958
+ values.push("::");
959
+ pos++;
960
+ c = s.charAt(pos++);
961
+ continue;
962
+ }
963
+ }
964
+
965
+ if (c == '/') {
966
+ c = s.charAt(pos++);
967
+ if (c == '/') {
968
+ types.push(XPathParser.DOUBLESLASH);
969
+ values.push("//");
970
+ c = s.charAt(pos++);
971
+ continue;
972
+ }
973
+ types.push(XPathParser.SLASH);
974
+ values.push('/');
975
+ continue;
976
+ }
977
+
978
+ if (c == '!') {
979
+ if (s.charAt(pos) == '=') {
980
+ types.push(XPathParser.NOTEQUAL);
981
+ values.push("!=");
982
+ pos++;
983
+ c = s.charAt(pos++);
984
+ continue;
985
+ }
986
+ }
987
+
988
+ if (c == '<') {
989
+ if (s.charAt(pos) == '=') {
990
+ types.push(XPathParser.LESSTHANOREQUAL);
991
+ values.push("<=");
992
+ pos++;
993
+ c = s.charAt(pos++);
994
+ continue;
995
+ }
996
+ types.push(XPathParser.LESSTHAN);
997
+ values.push('<');
998
+ c = s.charAt(pos++);
999
+ continue;
1000
+ }
1001
+
1002
+ if (c == '>') {
1003
+ if (s.charAt(pos) == '=') {
1004
+ types.push(XPathParser.GREATERTHANOREQUAL);
1005
+ values.push(">=");
1006
+ pos++;
1007
+ c = s.charAt(pos++);
1008
+ continue;
1009
+ }
1010
+ types.push(XPathParser.GREATERTHAN);
1011
+ values.push('>');
1012
+ c = s.charAt(pos++);
1013
+ continue;
1014
+ }
1015
+
1016
+ if (c == '_' || Utilities.isLetter(c.charCodeAt(0))) {
1017
+ var name = c;
1018
+ c = s.charAt(pos++);
1019
+ while (Utilities.isNCNameChar(c.charCodeAt(0))) {
1020
+ name += c;
1021
+ c = s.charAt(pos++);
1022
+ }
1023
+ if (types.length > 0) {
1024
+ var last = types[types.length - 1];
1025
+ if (last != XPathParser.AT
1026
+ && last != XPathParser.DOUBLECOLON
1027
+ && last != XPathParser.LEFTPARENTHESIS
1028
+ && last != XPathParser.LEFTBRACKET
1029
+ && last != XPathParser.AND
1030
+ && last != XPathParser.OR
1031
+ && last != XPathParser.MOD
1032
+ && last != XPathParser.DIV
1033
+ && last != XPathParser.MULTIPLYOPERATOR
1034
+ && last != XPathParser.SLASH
1035
+ && last != XPathParser.DOUBLESLASH
1036
+ && last != XPathParser.BAR
1037
+ && last != XPathParser.PLUS
1038
+ && last != XPathParser.MINUS
1039
+ && last != XPathParser.EQUALS
1040
+ && last != XPathParser.NOTEQUAL
1041
+ && last != XPathParser.LESSTHAN
1042
+ && last != XPathParser.LESSTHANOREQUAL
1043
+ && last != XPathParser.GREATERTHAN
1044
+ && last != XPathParser.GREATERTHANOREQUAL) {
1045
+ if (name == "and") {
1046
+ types.push(XPathParser.AND);
1047
+ values.push(name);
1048
+ continue;
1049
+ }
1050
+ if (name == "or") {
1051
+ types.push(XPathParser.OR);
1052
+ values.push(name);
1053
+ continue;
1054
+ }
1055
+ if (name == "mod") {
1056
+ types.push(XPathParser.MOD);
1057
+ values.push(name);
1058
+ continue;
1059
+ }
1060
+ if (name == "div") {
1061
+ types.push(XPathParser.DIV);
1062
+ values.push(name);
1063
+ continue;
1064
+ }
1065
+ }
1066
+ }
1067
+ if (c == ':') {
1068
+ if (s.charAt(pos) == '*') {
1069
+ types.push(XPathParser.NCNAMECOLONASTERISK);
1070
+ values.push(name + ":*");
1071
+ pos++;
1072
+ c = s.charAt(pos++);
1073
+ continue;
1074
+ }
1075
+ if (s.charAt(pos) == '_' || Utilities.isLetter(s.charCodeAt(pos))) {
1076
+ name += ':';
1077
+ c = s.charAt(pos++);
1078
+ while (Utilities.isNCNameChar(c.charCodeAt(0))) {
1079
+ name += c;
1080
+ c = s.charAt(pos++);
1081
+ }
1082
+ if (c == '(') {
1083
+ types.push(XPathParser.FUNCTIONNAME);
1084
+ values.push(name);
1085
+ continue;
1086
+ }
1087
+ types.push(XPathParser.QNAME);
1088
+ values.push(name);
1089
+ continue;
1090
+ }
1091
+ if (s.charAt(pos) == ':') {
1092
+ types.push(XPathParser.AXISNAME);
1093
+ values.push(name);
1094
+ continue;
1095
+ }
1096
+ }
1097
+ if (c == '(') {
1098
+ if (name == "comment" || name == "text" || name == "node") {
1099
+ types.push(XPathParser.NODETYPE);
1100
+ values.push(name);
1101
+ continue;
1102
+ }
1103
+ if (name == "processing-instruction") {
1104
+ if (s.charAt(pos) == ')') {
1105
+ types.push(XPathParser.NODETYPE);
1106
+ } else {
1107
+ types.push(XPathParser.PROCESSINGINSTRUCTIONWITHLITERAL);
1108
+ }
1109
+ values.push(name);
1110
+ continue;
1111
+ }
1112
+ types.push(XPathParser.FUNCTIONNAME);
1113
+ values.push(name);
1114
+ continue;
1115
+ }
1116
+ types.push(XPathParser.QNAME);
1117
+ values.push(name);
1118
+ continue;
1119
+ }
1120
+
1121
+ throw new Error("Unexpected character " + c);
1122
+ }
1123
+ types.push(1);
1124
+ values.push("[EOF]");
1125
+ return [types, values];
1126
+ };
1127
+
1128
+ XPathParser.SHIFT = 's';
1129
+ XPathParser.REDUCE = 'r';
1130
+ XPathParser.ACCEPT = 'a';
1131
+
1132
+ XPathParser.prototype.parse = function(s) {
1133
+ var types;
1134
+ var values;
1135
+ var res = this.tokenize(s);
1136
+ if (res == undefined) {
1137
+ return undefined;
1138
+ }
1139
+ types = res[0];
1140
+ values = res[1];
1141
+ var tokenPos = 0;
1142
+ var state = [];
1143
+ var tokenType = [];
1144
+ var tokenValue = [];
1145
+ var s;
1146
+ var a;
1147
+ var t;
1148
+
1149
+ state.push(0);
1150
+ tokenType.push(1);
1151
+ tokenValue.push("_S");
1152
+
1153
+ a = types[tokenPos];
1154
+ t = values[tokenPos++];
1155
+ while (1) {
1156
+ s = state[state.length - 1];
1157
+ switch (XPathParser.actionTable[s].charAt(a - 1)) {
1158
+ case XPathParser.SHIFT:
1159
+ tokenType.push(-a);
1160
+ tokenValue.push(t);
1161
+ state.push(XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32);
1162
+ a = types[tokenPos];
1163
+ t = values[tokenPos++];
1164
+ break;
1165
+ case XPathParser.REDUCE:
1166
+ var num = XPathParser.productions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32][1];
1167
+ var rhs = [];
1168
+ for (var i = 0; i < num; i++) {
1169
+ tokenType.pop();
1170
+ rhs.unshift(tokenValue.pop());
1171
+ state.pop();
1172
+ }
1173
+ var s_ = state[state.length - 1];
1174
+ tokenType.push(XPathParser.productions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32][0]);
1175
+ if (this.reduceActions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32] == undefined) {
1176
+ tokenValue.push(rhs[0]);
1177
+ } else {
1178
+ tokenValue.push(this.reduceActions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32](rhs));
1179
+ }
1180
+ state.push(XPathParser.gotoTable[s_].charCodeAt(XPathParser.productions[XPathParser.actionTableNumber[s].charCodeAt(a - 1) - 32][0] - 2) - 33);
1181
+ break;
1182
+ case XPathParser.ACCEPT:
1183
+ return new XPath(tokenValue.pop());
1184
+ default:
1185
+ throw new Error("XPath parse error");
1186
+ }
1187
+ }
1188
+ };
1189
+
1190
+ // XPath /////////////////////////////////////////////////////////////////////
1191
+
1192
+ XPath.prototype = new Object();
1193
+ XPath.prototype.constructor = XPath;
1194
+ XPath.superclass = Object.prototype;
1195
+
1196
+ function XPath(e) {
1197
+ this.expression = e;
1198
+ }
1199
+
1200
+ XPath.prototype.toString = function() {
1201
+ return this.expression.toString();
1202
+ };
1203
+
1204
+ XPath.prototype.evaluate = function(c) {
1205
+ c.contextNode = c.expressionContextNode;
1206
+ c.contextSize = 1;
1207
+ c.contextPosition = 1;
1208
+ c.caseInsensitive = false;
1209
+ if (c.contextNode != null) {
1210
+ var doc = c.contextNode;
1211
+ if (doc.nodeType != 9 /*Node.DOCUMENT_NODE*/) {
1212
+ doc = doc.ownerDocument;
1213
+ }
1214
+ try {
1215
+ c.caseInsensitive = doc.implementation.hasFeature("HTML", "2.0");
1216
+ } catch (e) {
1217
+ c.caseInsensitive = true;
1218
+ }
1219
+ }
1220
+ return this.expression.evaluate(c);
1221
+ };
1222
+
1223
+ XPath.XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
1224
+ XPath.XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
1225
+
1226
+ // Expression ////////////////////////////////////////////////////////////////
1227
+
1228
+ Expression.prototype = new Object();
1229
+ Expression.prototype.constructor = Expression;
1230
+ Expression.superclass = Object.prototype;
1231
+
1232
+ function Expression() {
1233
+ }
1234
+
1235
+ Expression.prototype.init = function() {
1236
+ };
1237
+
1238
+ Expression.prototype.toString = function() {
1239
+ return "<Expression>";
1240
+ };
1241
+
1242
+ Expression.prototype.evaluate = function(c) {
1243
+ throw new Error("Could not evaluate expression.");
1244
+ };
1245
+
1246
+ // UnaryOperation ////////////////////////////////////////////////////////////
1247
+
1248
+ UnaryOperation.prototype = new Expression();
1249
+ UnaryOperation.prototype.constructor = UnaryOperation;
1250
+ UnaryOperation.superclass = Expression.prototype;
1251
+
1252
+ function UnaryOperation(rhs) {
1253
+ if (arguments.length > 0) {
1254
+ this.init(rhs);
1255
+ }
1256
+ }
1257
+
1258
+ UnaryOperation.prototype.init = function(rhs) {
1259
+ this.rhs = rhs;
1260
+ };
1261
+
1262
+ // UnaryMinusOperation ///////////////////////////////////////////////////////
1263
+
1264
+ UnaryMinusOperation.prototype = new UnaryOperation();
1265
+ UnaryMinusOperation.prototype.constructor = UnaryMinusOperation;
1266
+ UnaryMinusOperation.superclass = UnaryOperation.prototype;
1267
+
1268
+ function UnaryMinusOperation(rhs) {
1269
+ if (arguments.length > 0) {
1270
+ this.init(rhs);
1271
+ }
1272
+ }
1273
+
1274
+ UnaryMinusOperation.prototype.init = function(rhs) {
1275
+ UnaryMinusOperation.superclass.init.call(this, rhs);
1276
+ };
1277
+
1278
+ UnaryMinusOperation.prototype.evaluate = function(c) {
1279
+ return this.rhs.evaluate(c).number().negate();
1280
+ };
1281
+
1282
+ UnaryMinusOperation.prototype.toString = function() {
1283
+ return "-" + this.rhs.toString();
1284
+ };
1285
+
1286
+ // BinaryOperation ///////////////////////////////////////////////////////////
1287
+
1288
+ BinaryOperation.prototype = new Expression();
1289
+ BinaryOperation.prototype.constructor = BinaryOperation;
1290
+ BinaryOperation.superclass = Expression.prototype;
1291
+
1292
+ function BinaryOperation(lhs, rhs) {
1293
+ if (arguments.length > 0) {
1294
+ this.init(lhs, rhs);
1295
+ }
1296
+ }
1297
+
1298
+ BinaryOperation.prototype.init = function(lhs, rhs) {
1299
+ this.lhs = lhs;
1300
+ this.rhs = rhs;
1301
+ };
1302
+
1303
+ // OrOperation ///////////////////////////////////////////////////////////////
1304
+
1305
+ OrOperation.prototype = new BinaryOperation();
1306
+ OrOperation.prototype.constructor = OrOperation;
1307
+ OrOperation.superclass = BinaryOperation.prototype;
1308
+
1309
+ function OrOperation(lhs, rhs) {
1310
+ if (arguments.length > 0) {
1311
+ this.init(lhs, rhs);
1312
+ }
1313
+ }
1314
+
1315
+ OrOperation.prototype.init = function(lhs, rhs) {
1316
+ OrOperation.superclass.init.call(this, lhs, rhs);
1317
+ };
1318
+
1319
+ OrOperation.prototype.toString = function() {
1320
+ return "(" + this.lhs.toString() + " or " + this.rhs.toString() + ")";
1321
+ };
1322
+
1323
+ OrOperation.prototype.evaluate = function(c) {
1324
+ var b = this.lhs.evaluate(c).bool();
1325
+ if (b.booleanValue()) {
1326
+ return b;
1327
+ }
1328
+ return this.rhs.evaluate(c).bool();
1329
+ };
1330
+
1331
+ // AndOperation //////////////////////////////////////////////////////////////
1332
+
1333
+ AndOperation.prototype = new BinaryOperation();
1334
+ AndOperation.prototype.constructor = AndOperation;
1335
+ AndOperation.superclass = BinaryOperation.prototype;
1336
+
1337
+ function AndOperation(lhs, rhs) {
1338
+ if (arguments.length > 0) {
1339
+ this.init(lhs, rhs);
1340
+ }
1341
+ }
1342
+
1343
+ AndOperation.prototype.init = function(lhs, rhs) {
1344
+ AndOperation.superclass.init.call(this, lhs, rhs);
1345
+ };
1346
+
1347
+ AndOperation.prototype.toString = function() {
1348
+ return "(" + this.lhs.toString() + " and " + this.rhs.toString() + ")";
1349
+ };
1350
+
1351
+ AndOperation.prototype.evaluate = function(c) {
1352
+ var b = this.lhs.evaluate(c).bool();
1353
+ if (!b.booleanValue()) {
1354
+ return b;
1355
+ }
1356
+ return this.rhs.evaluate(c).bool();
1357
+ };
1358
+
1359
+ // EqualsOperation ///////////////////////////////////////////////////////////
1360
+
1361
+ EqualsOperation.prototype = new BinaryOperation();
1362
+ EqualsOperation.prototype.constructor = EqualsOperation;
1363
+ EqualsOperation.superclass = BinaryOperation.prototype;
1364
+
1365
+ function EqualsOperation(lhs, rhs) {
1366
+ if (arguments.length > 0) {
1367
+ this.init(lhs, rhs);
1368
+ }
1369
+ }
1370
+
1371
+ EqualsOperation.prototype.init = function(lhs, rhs) {
1372
+ EqualsOperation.superclass.init.call(this, lhs, rhs);
1373
+ };
1374
+
1375
+ EqualsOperation.prototype.toString = function() {
1376
+ return "(" + this.lhs.toString() + " = " + this.rhs.toString() + ")";
1377
+ };
1378
+
1379
+ EqualsOperation.prototype.evaluate = function(c) {
1380
+ return this.lhs.evaluate(c).equals(this.rhs.evaluate(c));
1381
+ };
1382
+
1383
+ // NotEqualOperation /////////////////////////////////////////////////////////
1384
+
1385
+ NotEqualOperation.prototype = new BinaryOperation();
1386
+ NotEqualOperation.prototype.constructor = NotEqualOperation;
1387
+ NotEqualOperation.superclass = BinaryOperation.prototype;
1388
+
1389
+ function NotEqualOperation(lhs, rhs) {
1390
+ if (arguments.length > 0) {
1391
+ this.init(lhs, rhs);
1392
+ }
1393
+ }
1394
+
1395
+ NotEqualOperation.prototype.init = function(lhs, rhs) {
1396
+ NotEqualOperation.superclass.init.call(this, lhs, rhs);
1397
+ };
1398
+
1399
+ NotEqualOperation.prototype.toString = function() {
1400
+ return "(" + this.lhs.toString() + " != " + this.rhs.toString() + ")";
1401
+ };
1402
+
1403
+ NotEqualOperation.prototype.evaluate = function(c) {
1404
+ return this.lhs.evaluate(c).notequal(this.rhs.evaluate(c));
1405
+ };
1406
+
1407
+ // LessThanOperation /////////////////////////////////////////////////////////
1408
+
1409
+ LessThanOperation.prototype = new BinaryOperation();
1410
+ LessThanOperation.prototype.constructor = LessThanOperation;
1411
+ LessThanOperation.superclass = BinaryOperation.prototype;
1412
+
1413
+ function LessThanOperation(lhs, rhs) {
1414
+ if (arguments.length > 0) {
1415
+ this.init(lhs, rhs);
1416
+ }
1417
+ }
1418
+
1419
+ LessThanOperation.prototype.init = function(lhs, rhs) {
1420
+ LessThanOperation.superclass.init.call(this, lhs, rhs);
1421
+ };
1422
+
1423
+ LessThanOperation.prototype.evaluate = function(c) {
1424
+ return this.lhs.evaluate(c).lessthan(this.rhs.evaluate(c));
1425
+ };
1426
+
1427
+ LessThanOperation.prototype.toString = function() {
1428
+ return "(" + this.lhs.toString() + " < " + this.rhs.toString() + ")";
1429
+ };
1430
+
1431
+ // GreaterThanOperation //////////////////////////////////////////////////////
1432
+
1433
+ GreaterThanOperation.prototype = new BinaryOperation();
1434
+ GreaterThanOperation.prototype.constructor = GreaterThanOperation;
1435
+ GreaterThanOperation.superclass = BinaryOperation.prototype;
1436
+
1437
+ function GreaterThanOperation(lhs, rhs) {
1438
+ if (arguments.length > 0) {
1439
+ this.init(lhs, rhs);
1440
+ }
1441
+ }
1442
+
1443
+ GreaterThanOperation.prototype.init = function(lhs, rhs) {
1444
+ GreaterThanOperation.superclass.init.call(this, lhs, rhs);
1445
+ };
1446
+
1447
+ GreaterThanOperation.prototype.evaluate = function(c) {
1448
+ return this.lhs.evaluate(c).greaterthan(this.rhs.evaluate(c));
1449
+ };
1450
+
1451
+ GreaterThanOperation.prototype.toString = function() {
1452
+ return "(" + this.lhs.toString() + " > " + this.rhs.toString() + ")";
1453
+ };
1454
+
1455
+ // LessThanOrEqualOperation //////////////////////////////////////////////////
1456
+
1457
+ LessThanOrEqualOperation.prototype = new BinaryOperation();
1458
+ LessThanOrEqualOperation.prototype.constructor = LessThanOrEqualOperation;
1459
+ LessThanOrEqualOperation.superclass = BinaryOperation.prototype;
1460
+
1461
+ function LessThanOrEqualOperation(lhs, rhs) {
1462
+ if (arguments.length > 0) {
1463
+ this.init(lhs, rhs);
1464
+ }
1465
+ }
1466
+
1467
+ LessThanOrEqualOperation.prototype.init = function(lhs, rhs) {
1468
+ LessThanOrEqualOperation.superclass.init.call(this, lhs, rhs);
1469
+ };
1470
+
1471
+ LessThanOrEqualOperation.prototype.evaluate = function(c) {
1472
+ return this.lhs.evaluate(c).lessthanorequal(this.rhs.evaluate(c));
1473
+ };
1474
+
1475
+ LessThanOrEqualOperation.prototype.toString = function() {
1476
+ return "(" + this.lhs.toString() + " <= " + this.rhs.toString() + ")";
1477
+ };
1478
+
1479
+ // GreaterThanOrEqualOperation ///////////////////////////////////////////////
1480
+
1481
+ GreaterThanOrEqualOperation.prototype = new BinaryOperation();
1482
+ GreaterThanOrEqualOperation.prototype.constructor = GreaterThanOrEqualOperation;
1483
+ GreaterThanOrEqualOperation.superclass = BinaryOperation.prototype;
1484
+
1485
+ function GreaterThanOrEqualOperation(lhs, rhs) {
1486
+ if (arguments.length > 0) {
1487
+ this.init(lhs, rhs);
1488
+ }
1489
+ }
1490
+
1491
+ GreaterThanOrEqualOperation.prototype.init = function(lhs, rhs) {
1492
+ GreaterThanOrEqualOperation.superclass.init.call(this, lhs, rhs);
1493
+ };
1494
+
1495
+ GreaterThanOrEqualOperation.prototype.evaluate = function(c) {
1496
+ return this.lhs.evaluate(c).greaterthanorequal(this.rhs.evaluate(c));
1497
+ };
1498
+
1499
+ GreaterThanOrEqualOperation.prototype.toString = function() {
1500
+ return "(" + this.lhs.toString() + " >= " + this.rhs.toString() + ")";
1501
+ };
1502
+
1503
+ // PlusOperation /////////////////////////////////////////////////////////////
1504
+
1505
+ PlusOperation.prototype = new BinaryOperation();
1506
+ PlusOperation.prototype.constructor = PlusOperation;
1507
+ PlusOperation.superclass = BinaryOperation.prototype;
1508
+
1509
+ function PlusOperation(lhs, rhs) {
1510
+ if (arguments.length > 0) {
1511
+ this.init(lhs, rhs);
1512
+ }
1513
+ }
1514
+
1515
+ PlusOperation.prototype.init = function(lhs, rhs) {
1516
+ PlusOperation.superclass.init.call(this, lhs, rhs);
1517
+ };
1518
+
1519
+ PlusOperation.prototype.evaluate = function(c) {
1520
+ return this.lhs.evaluate(c).number().plus(this.rhs.evaluate(c).number());
1521
+ };
1522
+
1523
+ PlusOperation.prototype.toString = function() {
1524
+ return "(" + this.lhs.toString() + " + " + this.rhs.toString() + ")";
1525
+ };
1526
+
1527
+ // MinusOperation ////////////////////////////////////////////////////////////
1528
+
1529
+ MinusOperation.prototype = new BinaryOperation();
1530
+ MinusOperation.prototype.constructor = MinusOperation;
1531
+ MinusOperation.superclass = BinaryOperation.prototype;
1532
+
1533
+ function MinusOperation(lhs, rhs) {
1534
+ if (arguments.length > 0) {
1535
+ this.init(lhs, rhs);
1536
+ }
1537
+ }
1538
+
1539
+ MinusOperation.prototype.init = function(lhs, rhs) {
1540
+ MinusOperation.superclass.init.call(this, lhs, rhs);
1541
+ };
1542
+
1543
+ MinusOperation.prototype.evaluate = function(c) {
1544
+ return this.lhs.evaluate(c).number().minus(this.rhs.evaluate(c).number());
1545
+ };
1546
+
1547
+ MinusOperation.prototype.toString = function() {
1548
+ return "(" + this.lhs.toString() + " - " + this.rhs.toString() + ")";
1549
+ };
1550
+
1551
+ // MultiplyOperation /////////////////////////////////////////////////////////
1552
+
1553
+ MultiplyOperation.prototype = new BinaryOperation();
1554
+ MultiplyOperation.prototype.constructor = MultiplyOperation;
1555
+ MultiplyOperation.superclass = BinaryOperation.prototype;
1556
+
1557
+ function MultiplyOperation(lhs, rhs) {
1558
+ if (arguments.length > 0) {
1559
+ this.init(lhs, rhs);
1560
+ }
1561
+ }
1562
+
1563
+ MultiplyOperation.prototype.init = function(lhs, rhs) {
1564
+ MultiplyOperation.superclass.init.call(this, lhs, rhs);
1565
+ };
1566
+
1567
+ MultiplyOperation.prototype.evaluate = function(c) {
1568
+ return this.lhs.evaluate(c).number().multiply(this.rhs.evaluate(c).number());
1569
+ };
1570
+
1571
+ MultiplyOperation.prototype.toString = function() {
1572
+ return "(" + this.lhs.toString() + " * " + this.rhs.toString() + ")";
1573
+ };
1574
+
1575
+ // DivOperation //////////////////////////////////////////////////////////////
1576
+
1577
+ DivOperation.prototype = new BinaryOperation();
1578
+ DivOperation.prototype.constructor = DivOperation;
1579
+ DivOperation.superclass = BinaryOperation.prototype;
1580
+
1581
+ function DivOperation(lhs, rhs) {
1582
+ if (arguments.length > 0) {
1583
+ this.init(lhs, rhs);
1584
+ }
1585
+ }
1586
+
1587
+ DivOperation.prototype.init = function(lhs, rhs) {
1588
+ DivOperation.superclass.init.call(this, lhs, rhs);
1589
+ };
1590
+
1591
+ DivOperation.prototype.evaluate = function(c) {
1592
+ return this.lhs.evaluate(c).number().div(this.rhs.evaluate(c).number());
1593
+ };
1594
+
1595
+ DivOperation.prototype.toString = function() {
1596
+ return "(" + this.lhs.toString() + " div " + this.rhs.toString() + ")";
1597
+ };
1598
+
1599
+ // ModOperation //////////////////////////////////////////////////////////////
1600
+
1601
+ ModOperation.prototype = new BinaryOperation();
1602
+ ModOperation.prototype.constructor = ModOperation;
1603
+ ModOperation.superclass = BinaryOperation.prototype;
1604
+
1605
+ function ModOperation(lhs, rhs) {
1606
+ if (arguments.length > 0) {
1607
+ this.init(lhs, rhs);
1608
+ }
1609
+ }
1610
+
1611
+ ModOperation.prototype.init = function(lhs, rhs) {
1612
+ ModOperation.superclass.init.call(this, lhs, rhs);
1613
+ };
1614
+
1615
+ ModOperation.prototype.evaluate = function(c) {
1616
+ return this.lhs.evaluate(c).number().mod(this.rhs.evaluate(c).number());
1617
+ };
1618
+
1619
+ ModOperation.prototype.toString = function() {
1620
+ return "(" + this.lhs.toString() + " mod " + this.rhs.toString() + ")";
1621
+ };
1622
+
1623
+ // BarOperation //////////////////////////////////////////////////////////////
1624
+
1625
+ BarOperation.prototype = new BinaryOperation();
1626
+ BarOperation.prototype.constructor = BarOperation;
1627
+ BarOperation.superclass = BinaryOperation.prototype;
1628
+
1629
+ function BarOperation(lhs, rhs) {
1630
+ if (arguments.length > 0) {
1631
+ this.init(lhs, rhs);
1632
+ }
1633
+ }
1634
+
1635
+ BarOperation.prototype.init = function(lhs, rhs) {
1636
+ BarOperation.superclass.init.call(this, lhs, rhs);
1637
+ };
1638
+
1639
+ BarOperation.prototype.evaluate = function(c) {
1640
+ return this.lhs.evaluate(c).nodeset().union(this.rhs.evaluate(c).nodeset());
1641
+ };
1642
+
1643
+ BarOperation.prototype.toString = function() {
1644
+ return this.lhs.toString() + " | " + this.rhs.toString();
1645
+ };
1646
+
1647
+ // PathExpr //////////////////////////////////////////////////////////////////
1648
+
1649
+ PathExpr.prototype = new Expression();
1650
+ PathExpr.prototype.constructor = PathExpr;
1651
+ PathExpr.superclass = Expression.prototype;
1652
+
1653
+ function PathExpr(filter, filterPreds, locpath) {
1654
+ if (arguments.length > 0) {
1655
+ this.init(filter, filterPreds, locpath);
1656
+ }
1657
+ }
1658
+
1659
+ PathExpr.prototype.init = function(filter, filterPreds, locpath) {
1660
+ PathExpr.superclass.init.call(this);
1661
+ this.filter = filter;
1662
+ this.filterPredicates = filterPreds;
1663
+ this.locationPath = locpath;
1664
+ };
1665
+
1666
+ /**
1667
+ * Returns the topmost node of the tree containing node
1668
+ */
1669
+ function findRoot(node) {
1670
+ while (node && node.parentNode) {
1671
+ node = node.parentNode;
1672
+ }
1673
+
1674
+ return node;
1675
+ }
1676
+
1677
+
1678
+ PathExpr.prototype.evaluate = function(c) {
1679
+ var nodes;
1680
+ var xpc = new XPathContext();
1681
+ xpc.variableResolver = c.variableResolver;
1682
+ xpc.functionResolver = c.functionResolver;
1683
+ xpc.namespaceResolver = c.namespaceResolver;
1684
+ xpc.expressionContextNode = c.expressionContextNode;
1685
+ xpc.virtualRoot = c.virtualRoot;
1686
+ xpc.caseInsensitive = c.caseInsensitive;
1687
+ if (this.filter == null) {
1688
+ nodes = [ c.contextNode ];
1689
+ } else {
1690
+ var ns = this.filter.evaluate(c);
1691
+ if (!Utilities.instance_of(ns, XNodeSet)) {
1692
+ if (this.filterPredicates != null && this.filterPredicates.length > 0 || this.locationPath != null) {
1693
+ throw new Error("Path expression filter must evaluate to a nodset if predicates or location path are used");
1694
+ }
1695
+ return ns;
1696
+ }
1697
+ nodes = ns.toUnsortedArray();
1698
+ if (this.filterPredicates != null) {
1699
+ // apply each of the predicates in turn
1700
+ for (var j = 0; j < this.filterPredicates.length; j++) {
1701
+ var pred = this.filterPredicates[j];
1702
+ var newNodes = [];
1703
+ xpc.contextSize = nodes.length;
1704
+ for (xpc.contextPosition = 1; xpc.contextPosition <= xpc.contextSize; xpc.contextPosition++) {
1705
+ xpc.contextNode = nodes[xpc.contextPosition - 1];
1706
+ if (this.predicateMatches(pred, xpc)) {
1707
+ newNodes.push(xpc.contextNode);
1708
+ }
1709
+ }
1710
+ nodes = newNodes;
1711
+ }
1712
+ }
1713
+ }
1714
+ if (this.locationPath != null) {
1715
+ if (this.locationPath.absolute) {
1716
+ if (nodes[0].nodeType != 9 /*Node.DOCUMENT_NODE*/) {
1717
+ if (xpc.virtualRoot != null) {
1718
+ nodes = [ xpc.virtualRoot ];
1719
+ } else {
1720
+ if (nodes[0].ownerDocument == null) {
1721
+ // IE 5.5 doesn't have ownerDocument?
1722
+ var n = nodes[0];
1723
+ while (n.parentNode != null) {
1724
+ n = n.parentNode;
1725
+ }
1726
+ nodes = [ n ];
1727
+ } else {
1728
+ nodes = [ nodes[0].ownerDocument ];
1729
+ }
1730
+ }
1731
+ } else {
1732
+ nodes = [ nodes[0] ];
1733
+ }
1734
+ }
1735
+ for (var i = 0; i < this.locationPath.steps.length; i++) {
1736
+ var step = this.locationPath.steps[i];
1737
+ var newNodes = [];
1738
+ for (var j = 0; j < nodes.length; j++) {
1739
+ xpc.contextNode = nodes[j];
1740
+ switch (step.axis) {
1741
+ case Step.ANCESTOR:
1742
+ // look at all the ancestor nodes
1743
+ if (xpc.contextNode === xpc.virtualRoot) {
1744
+ break;
1745
+ }
1746
+ var m;
1747
+ if (xpc.contextNode.nodeType == 2 /*Node.ATTRIBUTE_NODE*/) {
1748
+ m = this.getOwnerElement(xpc.contextNode);
1749
+ } else {
1750
+ m = xpc.contextNode.parentNode;
1751
+ }
1752
+ while (m != null) {
1753
+ if (step.nodeTest.matches(m, xpc)) {
1754
+ newNodes.push(m);
1755
+ }
1756
+ if (m === xpc.virtualRoot) {
1757
+ break;
1758
+ }
1759
+ m = m.parentNode;
1760
+ }
1761
+ break;
1762
+
1763
+ case Step.ANCESTORORSELF:
1764
+ // look at all the ancestor nodes and the current node
1765
+ for (var m = xpc.contextNode; m != null; m = m.nodeType == 2 /*Node.ATTRIBUTE_NODE*/ ? this.getOwnerElement(m) : m.parentNode) {
1766
+ if (step.nodeTest.matches(m, xpc)) {
1767
+ newNodes.push(m);
1768
+ }
1769
+ if (m === xpc.virtualRoot) {
1770
+ break;
1771
+ }
1772
+ }
1773
+ break;
1774
+
1775
+ case Step.ATTRIBUTE:
1776
+ // look at the attributes
1777
+ var nnm = xpc.contextNode.attributes;
1778
+ if (nnm != null) {
1779
+ for (var k = 0; k < nnm.length; k++) {
1780
+ var m = nnm.item(k);
1781
+ if (step.nodeTest.matches(m, xpc)) {
1782
+ newNodes.push(m);
1783
+ }
1784
+ }
1785
+ }
1786
+ break;
1787
+
1788
+ case Step.CHILD:
1789
+ // look at all child elements
1790
+ for (var m = xpc.contextNode.firstChild; m != null; m = m.nextSibling) {
1791
+ if (step.nodeTest.matches(m, xpc)) {
1792
+ newNodes.push(m);
1793
+ }
1794
+ }
1795
+ break;
1796
+
1797
+ case Step.DESCENDANT:
1798
+ // look at all descendant nodes
1799
+ var st = [ xpc.contextNode.firstChild ];
1800
+ while (st.length > 0) {
1801
+ for (var m = st.pop(); m != null; ) {
1802
+ if (step.nodeTest.matches(m, xpc)) {
1803
+ newNodes.push(m);
1804
+ }
1805
+ if (m.firstChild != null) {
1806
+ st.push(m.nextSibling);
1807
+ m = m.firstChild;
1808
+ } else {
1809
+ m = m.nextSibling;
1810
+ }
1811
+ }
1812
+ }
1813
+ break;
1814
+
1815
+ case Step.DESCENDANTORSELF:
1816
+ // look at self
1817
+ if (step.nodeTest.matches(xpc.contextNode, xpc)) {
1818
+ newNodes.push(xpc.contextNode);
1819
+ }
1820
+ // look at all descendant nodes
1821
+ var st = [ xpc.contextNode.firstChild ];
1822
+ while (st.length > 0) {
1823
+ for (var m = st.pop(); m != null; ) {
1824
+ if (step.nodeTest.matches(m, xpc)) {
1825
+ newNodes.push(m);
1826
+ }
1827
+ if (m.firstChild != null) {
1828
+ st.push(m.nextSibling);
1829
+ m = m.firstChild;
1830
+ } else {
1831
+ m = m.nextSibling;
1832
+ }
1833
+ }
1834
+ }
1835
+ break;
1836
+
1837
+ case Step.FOLLOWING:
1838
+ if (xpc.contextNode === xpc.virtualRoot) {
1839
+ break;
1840
+ }
1841
+ var st = [];
1842
+ if (xpc.contextNode.firstChild != null) {
1843
+ st.unshift(xpc.contextNode.firstChild);
1844
+ } else {
1845
+ st.unshift(xpc.contextNode.nextSibling);
1846
+ }
1847
+ for (var m = xpc.contextNode.parentNode; m != null && m.nodeType != 9 /*Node.DOCUMENT_NODE*/ && m !== xpc.virtualRoot; m = m.parentNode) {
1848
+ st.unshift(m.nextSibling);
1849
+ }
1850
+ do {
1851
+ for (var m = st.pop(); m != null; ) {
1852
+ if (step.nodeTest.matches(m, xpc)) {
1853
+ newNodes.push(m);
1854
+ }
1855
+ if (m.firstChild != null) {
1856
+ st.push(m.nextSibling);
1857
+ m = m.firstChild;
1858
+ } else {
1859
+ m = m.nextSibling;
1860
+ }
1861
+ }
1862
+ } while (st.length > 0);
1863
+ break;
1864
+
1865
+ case Step.FOLLOWINGSIBLING:
1866
+ if (xpc.contextNode === xpc.virtualRoot) {
1867
+ break;
1868
+ }
1869
+ for (var m = xpc.contextNode.nextSibling; m != null; m = m.nextSibling) {
1870
+ if (step.nodeTest.matches(m, xpc)) {
1871
+ newNodes.push(m);
1872
+ }
1873
+ }
1874
+ break;
1875
+
1876
+ case Step.NAMESPACE:
1877
+ var n = {};
1878
+ if (xpc.contextNode.nodeType == 1 /*Node.ELEMENT_NODE*/) {
1879
+ n["xml"] = XPath.XML_NAMESPACE_URI;
1880
+ n["xmlns"] = XPath.XMLNS_NAMESPACE_URI;
1881
+ for (var m = xpc.contextNode; m != null && m.nodeType == 1 /*Node.ELEMENT_NODE*/; m = m.parentNode) {
1882
+ for (var k = 0; k < m.attributes.length; k++) {
1883
+ var attr = m.attributes.item(k);
1884
+ var nm = String(attr.name);
1885
+ if (nm == "xmlns") {
1886
+ if (n[""] == undefined) {
1887
+ n[""] = attr.value;
1888
+ }
1889
+ } else if (nm.length > 6 && nm.substring(0, 6) == "xmlns:") {
1890
+ var pre = nm.substring(6, nm.length);
1891
+ if (n[pre] == undefined) {
1892
+ n[pre] = attr.value;
1893
+ }
1894
+ }
1895
+ }
1896
+ }
1897
+ for (var pre in n) {
1898
+ var nsn = new XPathNamespace(pre, n[pre], xpc.contextNode);
1899
+ if (step.nodeTest.matches(nsn, xpc)) {
1900
+ newNodes.push(nsn);
1901
+ }
1902
+ }
1903
+ }
1904
+ break;
1905
+
1906
+ case Step.PARENT:
1907
+ m = null;
1908
+ if (xpc.contextNode !== xpc.virtualRoot) {
1909
+ if (xpc.contextNode.nodeType == 2 /*Node.ATTRIBUTE_NODE*/) {
1910
+ m = this.getOwnerElement(xpc.contextNode);
1911
+ } else {
1912
+ m = xpc.contextNode.parentNode;
1913
+ }
1914
+ }
1915
+ if (m != null && step.nodeTest.matches(m, xpc)) {
1916
+ newNodes.push(m);
1917
+ }
1918
+ break;
1919
+
1920
+ case Step.PRECEDING:
1921
+ var st;
1922
+ if (xpc.virtualRoot != null) {
1923
+ st = [ xpc.virtualRoot ];
1924
+ } else {
1925
+ // cannot rely on .ownerDocument because the node may be in a document fragment
1926
+ st = [findRoot(xpc.contextNode)];
1927
+ }
1928
+ outer: while (st.length > 0) {
1929
+ for (var m = st.pop(); m != null; ) {
1930
+ if (m == xpc.contextNode) {
1931
+ break outer;
1932
+ }
1933
+ if (step.nodeTest.matches(m, xpc)) {
1934
+ newNodes.unshift(m);
1935
+ }
1936
+ if (m.firstChild != null) {
1937
+ st.push(m.nextSibling);
1938
+ m = m.firstChild;
1939
+ } else {
1940
+ m = m.nextSibling;
1941
+ }
1942
+ }
1943
+ }
1944
+ break;
1945
+
1946
+ case Step.PRECEDINGSIBLING:
1947
+ if (xpc.contextNode === xpc.virtualRoot) {
1948
+ break;
1949
+ }
1950
+ for (var m = xpc.contextNode.previousSibling; m != null; m = m.previousSibling) {
1951
+ if (step.nodeTest.matches(m, xpc)) {
1952
+ newNodes.push(m);
1953
+ }
1954
+ }
1955
+ break;
1956
+
1957
+ case Step.SELF:
1958
+ if (step.nodeTest.matches(xpc.contextNode, xpc)) {
1959
+ newNodes.push(xpc.contextNode);
1960
+ }
1961
+ break;
1962
+
1963
+ default:
1964
+ }
1965
+ }
1966
+ nodes = newNodes;
1967
+ // apply each of the predicates in turn
1968
+ for (var j = 0; j < step.predicates.length; j++) {
1969
+ var pred = step.predicates[j];
1970
+ var newNodes = [];
1971
+ xpc.contextSize = nodes.length;
1972
+ for (xpc.contextPosition = 1; xpc.contextPosition <= xpc.contextSize; xpc.contextPosition++) {
1973
+ xpc.contextNode = nodes[xpc.contextPosition - 1];
1974
+ if (this.predicateMatches(pred, xpc)) {
1975
+ newNodes.push(xpc.contextNode);
1976
+ } else {
1977
+ }
1978
+ }
1979
+ nodes = newNodes;
1980
+ }
1981
+ }
1982
+ }
1983
+ var ns = new XNodeSet();
1984
+ ns.addArray(nodes);
1985
+ return ns;
1986
+ };
1987
+
1988
+ PathExpr.prototype.predicateMatches = function(pred, c) {
1989
+ var res = pred.evaluate(c);
1990
+ if (Utilities.instance_of(res, XNumber)) {
1991
+ return c.contextPosition == res.numberValue();
1992
+ }
1993
+ return res.booleanValue();
1994
+ };
1995
+
1996
+ PathExpr.prototype.toString = function() {
1997
+ if (this.filter != undefined) {
1998
+ var s = this.filter.toString();
1999
+ if (Utilities.instance_of(this.filter, XString)) {
2000
+ s = "'" + s + "'";
2001
+ }
2002
+ if (this.filterPredicates != undefined) {
2003
+ for (var i = 0; i < this.filterPredicates.length; i++) {
2004
+ s = s + "[" + this.filterPredicates[i].toString() + "]";
2005
+ }
2006
+ }
2007
+ if (this.locationPath != undefined) {
2008
+ if (!this.locationPath.absolute) {
2009
+ s += "/";
2010
+ }
2011
+ s += this.locationPath.toString();
2012
+ }
2013
+ return s;
2014
+ }
2015
+ return this.locationPath.toString();
2016
+ };
2017
+
2018
+ PathExpr.prototype.getOwnerElement = function(n) {
2019
+ // DOM 2 has ownerElement
2020
+ if (n.ownerElement) {
2021
+ return n.ownerElement;
2022
+ }
2023
+ // DOM 1 Internet Explorer can use selectSingleNode (ironically)
2024
+ try {
2025
+ if (n.selectSingleNode) {
2026
+ return n.selectSingleNode("..");
2027
+ }
2028
+ } catch (e) {
2029
+ }
2030
+ // Other DOM 1 implementations must use this egregious search
2031
+ var doc = n.nodeType == 9 /*Node.DOCUMENT_NODE*/
2032
+ ? n
2033
+ : n.ownerDocument;
2034
+ var elts = doc.getElementsByTagName("*");
2035
+ for (var i = 0; i < elts.length; i++) {
2036
+ var elt = elts.item(i);
2037
+ var nnm = elt.attributes;
2038
+ for (var j = 0; j < nnm.length; j++) {
2039
+ var an = nnm.item(j);
2040
+ if (an === n) {
2041
+ return elt;
2042
+ }
2043
+ }
2044
+ }
2045
+ return null;
2046
+ };
2047
+
2048
+ // LocationPath //////////////////////////////////////////////////////////////
2049
+
2050
+ LocationPath.prototype = new Object();
2051
+ LocationPath.prototype.constructor = LocationPath;
2052
+ LocationPath.superclass = Object.prototype;
2053
+
2054
+ function LocationPath(abs, steps) {
2055
+ if (arguments.length > 0) {
2056
+ this.init(abs, steps);
2057
+ }
2058
+ }
2059
+
2060
+ LocationPath.prototype.init = function(abs, steps) {
2061
+ this.absolute = abs;
2062
+ this.steps = steps;
2063
+ };
2064
+
2065
+ LocationPath.prototype.toString = function() {
2066
+ var s;
2067
+ if (this.absolute) {
2068
+ s = "/";
2069
+ } else {
2070
+ s = "";
2071
+ }
2072
+ for (var i = 0; i < this.steps.length; i++) {
2073
+ if (i != 0) {
2074
+ s += "/";
2075
+ }
2076
+ s += this.steps[i].toString();
2077
+ }
2078
+ return s;
2079
+ };
2080
+
2081
+ // Step //////////////////////////////////////////////////////////////////////
2082
+
2083
+ Step.prototype = new Object();
2084
+ Step.prototype.constructor = Step;
2085
+ Step.superclass = Object.prototype;
2086
+
2087
+ function Step(axis, nodetest, preds) {
2088
+ if (arguments.length > 0) {
2089
+ this.init(axis, nodetest, preds);
2090
+ }
2091
+ }
2092
+
2093
+ Step.prototype.init = function(axis, nodetest, preds) {
2094
+ this.axis = axis;
2095
+ this.nodeTest = nodetest;
2096
+ this.predicates = preds;
2097
+ };
2098
+
2099
+ Step.prototype.toString = function() {
2100
+ var s;
2101
+ switch (this.axis) {
2102
+ case Step.ANCESTOR:
2103
+ s = "ancestor";
2104
+ break;
2105
+ case Step.ANCESTORORSELF:
2106
+ s = "ancestor-or-self";
2107
+ break;
2108
+ case Step.ATTRIBUTE:
2109
+ s = "attribute";
2110
+ break;
2111
+ case Step.CHILD:
2112
+ s = "child";
2113
+ break;
2114
+ case Step.DESCENDANT:
2115
+ s = "descendant";
2116
+ break;
2117
+ case Step.DESCENDANTORSELF:
2118
+ s = "descendant-or-self";
2119
+ break;
2120
+ case Step.FOLLOWING:
2121
+ s = "following";
2122
+ break;
2123
+ case Step.FOLLOWINGSIBLING:
2124
+ s = "following-sibling";
2125
+ break;
2126
+ case Step.NAMESPACE:
2127
+ s = "namespace";
2128
+ break;
2129
+ case Step.PARENT:
2130
+ s = "parent";
2131
+ break;
2132
+ case Step.PRECEDING:
2133
+ s = "preceding";
2134
+ break;
2135
+ case Step.PRECEDINGSIBLING:
2136
+ s = "preceding-sibling";
2137
+ break;
2138
+ case Step.SELF:
2139
+ s = "self";
2140
+ break;
2141
+ }
2142
+ s += "::";
2143
+ s += this.nodeTest.toString();
2144
+ for (var i = 0; i < this.predicates.length; i++) {
2145
+ s += "[" + this.predicates[i].toString() + "]";
2146
+ }
2147
+ return s;
2148
+ };
2149
+
2150
+ Step.ANCESTOR = 0;
2151
+ Step.ANCESTORORSELF = 1;
2152
+ Step.ATTRIBUTE = 2;
2153
+ Step.CHILD = 3;
2154
+ Step.DESCENDANT = 4;
2155
+ Step.DESCENDANTORSELF = 5;
2156
+ Step.FOLLOWING = 6;
2157
+ Step.FOLLOWINGSIBLING = 7;
2158
+ Step.NAMESPACE = 8;
2159
+ Step.PARENT = 9;
2160
+ Step.PRECEDING = 10;
2161
+ Step.PRECEDINGSIBLING = 11;
2162
+ Step.SELF = 12;
2163
+
2164
+ // NodeTest //////////////////////////////////////////////////////////////////
2165
+
2166
+ NodeTest.prototype = new Object();
2167
+ NodeTest.prototype.constructor = NodeTest;
2168
+ NodeTest.superclass = Object.prototype;
2169
+
2170
+ function NodeTest(type, value) {
2171
+ if (arguments.length > 0) {
2172
+ this.init(type, value);
2173
+ }
2174
+ }
2175
+
2176
+ NodeTest.prototype.init = function(type, value) {
2177
+ this.type = type;
2178
+ this.value = value;
2179
+ };
2180
+
2181
+ NodeTest.prototype.toString = function() {
2182
+ switch (this.type) {
2183
+ case NodeTest.NAMETESTANY:
2184
+ return "*";
2185
+ case NodeTest.NAMETESTPREFIXANY:
2186
+ return this.value + ":*";
2187
+ case NodeTest.NAMETESTRESOLVEDANY:
2188
+ return "{" + this.value + "}*";
2189
+ case NodeTest.NAMETESTQNAME:
2190
+ return this.value;
2191
+ case NodeTest.NAMETESTRESOLVEDNAME:
2192
+ return "{" + this.namespaceURI + "}" + this.value;
2193
+ case NodeTest.COMMENT:
2194
+ return "comment()";
2195
+ case NodeTest.TEXT:
2196
+ return "text()";
2197
+ case NodeTest.PI:
2198
+ if (this.value != undefined) {
2199
+ return "processing-instruction(\"" + this.value + "\")";
2200
+ }
2201
+ return "processing-instruction()";
2202
+ case NodeTest.NODE:
2203
+ return "node()";
2204
+ }
2205
+ return "<unknown nodetest type>";
2206
+ };
2207
+
2208
+ NodeTest.prototype.matches = function (n, xpc) {
2209
+ var nType = n.nodeType;
2210
+
2211
+ switch (this.type) {
2212
+ case NodeTest.NAMETESTANY:
2213
+ if (nType === 2 /*Node.ATTRIBUTE_NODE*/
2214
+ || nType === 1 /*Node.ELEMENT_NODE*/
2215
+ || nType === XPathNamespace.XPATH_NAMESPACE_NODE) {
2216
+ return true;
2217
+ }
2218
+ return false;
2219
+ case NodeTest.NAMETESTPREFIXANY:
2220
+ if (nType === 2 /*Node.ATTRIBUTE_NODE*/ || nType === 1 /*Node.ELEMENT_NODE*/) {
2221
+ var ns = xpc.namespaceResolver.getNamespace(this.value, xpc.expressionContextNode);
2222
+ if (ns == null) {
2223
+ throw new Error("Cannot resolve QName " + this.value);
2224
+ }
2225
+ return ns === (n.namespaceURI || '');
2226
+ }
2227
+ return false;
2228
+ case NodeTest.NAMETESTQNAME:
2229
+ if (nType === 2 /*Node.ATTRIBUTE_NODE*/
2230
+ || nType === 1 /*Node.ELEMENT_NODE*/
2231
+ || nType === XPathNamespace.XPATH_NAMESPACE_NODE) {
2232
+ var test = Utilities.resolveQName(this.value, xpc.namespaceResolver, xpc.expressionContextNode, false);
2233
+ if (test[0] == null) {
2234
+ throw new Error("Cannot resolve QName " + this.value);
2235
+ }
2236
+
2237
+ test[0] = String(test[0]) || null;
2238
+ test[1] = String(test[1]);
2239
+
2240
+ var node = [
2241
+ String(n.namespaceURI || '') || null,
2242
+ // localName will be null if the node was created with DOM1 createElement()
2243
+ String(n.localName || n.nodeName)
2244
+ ];
2245
+
2246
+ if (xpc.caseInsensitive) {
2247
+ return test[0] === node[0] && test[1].toLowerCase() === node[1].toLowerCase();
2248
+ }
2249
+
2250
+ return test[0] === node[0] && test[1] === node[1];
2251
+ }
2252
+ return false;
2253
+ case NodeTest.COMMENT:
2254
+ return nType === 8 /*Node.COMMENT_NODE*/;
2255
+ case NodeTest.TEXT:
2256
+ return nType === 3 /*Node.TEXT_NODE*/ || nType == 4 /*Node.CDATA_SECTION_NODE*/;
2257
+ case NodeTest.PI:
2258
+ return nType === 7 /*Node.PROCESSING_INSTRUCTION_NODE*/
2259
+ && (this.value == null || n.nodeName == this.value);
2260
+ case NodeTest.NODE:
2261
+ return nType === 9 /*Node.DOCUMENT_NODE*/
2262
+ || nType === 1 /*Node.ELEMENT_NODE*/
2263
+ || nType === 2 /*Node.ATTRIBUTE_NODE*/
2264
+ || nType === 3 /*Node.TEXT_NODE*/
2265
+ || nType === 4 /*Node.CDATA_SECTION_NODE*/
2266
+ || nType === 8 /*Node.COMMENT_NODE*/
2267
+ || nType === 7 /*Node.PROCESSING_INSTRUCTION_NODE*/;
2268
+ }
2269
+ return false;
2270
+ };
2271
+
2272
+ NodeTest.NAMETESTANY = 0;
2273
+ NodeTest.NAMETESTPREFIXANY = 1;
2274
+ NodeTest.NAMETESTQNAME = 2;
2275
+ NodeTest.COMMENT = 3;
2276
+ NodeTest.TEXT = 4;
2277
+ NodeTest.PI = 5;
2278
+ NodeTest.NODE = 6;
2279
+
2280
+ // VariableReference /////////////////////////////////////////////////////////
2281
+
2282
+ VariableReference.prototype = new Expression();
2283
+ VariableReference.prototype.constructor = VariableReference;
2284
+ VariableReference.superclass = Expression.prototype;
2285
+
2286
+ function VariableReference(v) {
2287
+ if (arguments.length > 0) {
2288
+ this.init(v);
2289
+ }
2290
+ }
2291
+
2292
+ VariableReference.prototype.init = function(v) {
2293
+ this.variable = v;
2294
+ };
2295
+
2296
+ VariableReference.prototype.toString = function() {
2297
+ return "$" + this.variable;
2298
+ };
2299
+
2300
+ VariableReference.prototype.evaluate = function(c) {
2301
+ var parts = Utilities.resolveQName(this.variable, c.namespaceResolver, c.contextNode, false);
2302
+
2303
+ if (parts[0] == null) {
2304
+ throw new Error("Cannot resolve QName " + fn);
2305
+ }
2306
+ var result = c.variableResolver.getVariable(parts[1], parts[0]);
2307
+ if (!result) {
2308
+ throw XPathException.fromMessage("Undeclared variable: " + this.toString());
2309
+ }
2310
+ return result;
2311
+ };
2312
+
2313
+ // FunctionCall //////////////////////////////////////////////////////////////
2314
+
2315
+ FunctionCall.prototype = new Expression();
2316
+ FunctionCall.prototype.constructor = FunctionCall;
2317
+ FunctionCall.superclass = Expression.prototype;
2318
+
2319
+ function FunctionCall(fn, args) {
2320
+ if (arguments.length > 0) {
2321
+ this.init(fn, args);
2322
+ }
2323
+ }
2324
+
2325
+ FunctionCall.prototype.init = function(fn, args) {
2326
+ this.functionName = fn;
2327
+ this.arguments = args;
2328
+ };
2329
+
2330
+ FunctionCall.prototype.toString = function() {
2331
+ var s = this.functionName + "(";
2332
+ for (var i = 0; i < this.arguments.length; i++) {
2333
+ if (i > 0) {
2334
+ s += ", ";
2335
+ }
2336
+ s += this.arguments[i].toString();
2337
+ }
2338
+ return s + ")";
2339
+ };
2340
+
2341
+ FunctionCall.prototype.evaluate = function(c) {
2342
+ var f = FunctionResolver.getFunctionFromContext(this.functionName, c);
2343
+
2344
+ if (!f) {
2345
+ throw new Error("Unknown function " + this.functionName);
2346
+ }
2347
+
2348
+ var a = [c].concat(this.arguments);
2349
+ return f.apply(c.functionResolver.thisArg, a);
2350
+ };
2351
+
2352
+ // XString ///////////////////////////////////////////////////////////////////
2353
+
2354
+ XString.prototype = new Expression();
2355
+ XString.prototype.constructor = XString;
2356
+ XString.superclass = Expression.prototype;
2357
+
2358
+ function XString(s) {
2359
+ if (arguments.length > 0) {
2360
+ this.init(s);
2361
+ }
2362
+ }
2363
+
2364
+ XString.prototype.init = function(s) {
2365
+ this.str = String(s);
2366
+ };
2367
+
2368
+ XString.prototype.toString = function() {
2369
+ return this.str;
2370
+ };
2371
+
2372
+ XString.prototype.evaluate = function(c) {
2373
+ return this;
2374
+ };
2375
+
2376
+ XString.prototype.string = function() {
2377
+ return this;
2378
+ };
2379
+
2380
+ XString.prototype.number = function() {
2381
+ return new XNumber(this.str);
2382
+ };
2383
+
2384
+ XString.prototype.bool = function() {
2385
+ return new XBoolean(this.str);
2386
+ };
2387
+
2388
+ XString.prototype.nodeset = function() {
2389
+ throw new Error("Cannot convert string to nodeset");
2390
+ };
2391
+
2392
+ XString.prototype.stringValue = function() {
2393
+ return this.str;
2394
+ };
2395
+
2396
+ XString.prototype.numberValue = function() {
2397
+ return this.number().numberValue();
2398
+ };
2399
+
2400
+ XString.prototype.booleanValue = function() {
2401
+ return this.bool().booleanValue();
2402
+ };
2403
+
2404
+ XString.prototype.equals = function(r) {
2405
+ if (Utilities.instance_of(r, XBoolean)) {
2406
+ return this.bool().equals(r);
2407
+ }
2408
+ if (Utilities.instance_of(r, XNumber)) {
2409
+ return this.number().equals(r);
2410
+ }
2411
+ if (Utilities.instance_of(r, XNodeSet)) {
2412
+ return r.compareWithString(this, Operators.equals);
2413
+ }
2414
+ return new XBoolean(this.str == r.str);
2415
+ };
2416
+
2417
+ XString.prototype.notequal = function(r) {
2418
+ if (Utilities.instance_of(r, XBoolean)) {
2419
+ return this.bool().notequal(r);
2420
+ }
2421
+ if (Utilities.instance_of(r, XNumber)) {
2422
+ return this.number().notequal(r);
2423
+ }
2424
+ if (Utilities.instance_of(r, XNodeSet)) {
2425
+ return r.compareWithString(this, Operators.notequal);
2426
+ }
2427
+ return new XBoolean(this.str != r.str);
2428
+ };
2429
+
2430
+ XString.prototype.lessthan = function(r) {
2431
+ if (Utilities.instance_of(r, XNodeSet)) {
2432
+ return r.compareWithNumber(this.number(), Operators.greaterthanorequal);
2433
+ }
2434
+ return this.number().lessthan(r.number());
2435
+ };
2436
+
2437
+ XString.prototype.greaterthan = function(r) {
2438
+ if (Utilities.instance_of(r, XNodeSet)) {
2439
+ return r.compareWithNumber(this.number(), Operators.lessthanorequal);
2440
+ }
2441
+ return this.number().greaterthan(r.number());
2442
+ };
2443
+
2444
+ XString.prototype.lessthanorequal = function(r) {
2445
+ if (Utilities.instance_of(r, XNodeSet)) {
2446
+ return r.compareWithNumber(this.number(), Operators.greaterthan);
2447
+ }
2448
+ return this.number().lessthanorequal(r.number());
2449
+ };
2450
+
2451
+ XString.prototype.greaterthanorequal = function(r) {
2452
+ if (Utilities.instance_of(r, XNodeSet)) {
2453
+ return r.compareWithNumber(this.number(), Operators.lessthan);
2454
+ }
2455
+ return this.number().greaterthanorequal(r.number());
2456
+ };
2457
+
2458
+ // XNumber ///////////////////////////////////////////////////////////////////
2459
+
2460
+ XNumber.prototype = new Expression();
2461
+ XNumber.prototype.constructor = XNumber;
2462
+ XNumber.superclass = Expression.prototype;
2463
+
2464
+ function XNumber(n) {
2465
+ if (arguments.length > 0) {
2466
+ this.init(n);
2467
+ }
2468
+ }
2469
+
2470
+ XNumber.prototype.init = function(n) {
2471
+ this.num = typeof n === "string" ? this.parse(n) : Number(n);
2472
+ };
2473
+
2474
+ XNumber.prototype.numberFormat = /^\s*-?[0-9]*\.?[0-9]+\s*$/;
2475
+
2476
+ XNumber.prototype.parse = function(s) {
2477
+ // XPath representation of numbers is more restrictive than what Number() or parseFloat() allow
2478
+ return this.numberFormat.test(s) ? parseFloat(s) : Number.NaN;
2479
+ };
2480
+
2481
+ XNumber.prototype.toString = function() {
2482
+ return this.num;
2483
+ };
2484
+
2485
+ XNumber.prototype.evaluate = function(c) {
2486
+ return this;
2487
+ };
2488
+
2489
+ XNumber.prototype.string = function() {
2490
+ return new XString(this.num);
2491
+ };
2492
+
2493
+ XNumber.prototype.number = function() {
2494
+ return this;
2495
+ };
2496
+
2497
+ XNumber.prototype.bool = function() {
2498
+ return new XBoolean(this.num);
2499
+ };
2500
+
2501
+ XNumber.prototype.nodeset = function() {
2502
+ throw new Error("Cannot convert number to nodeset");
2503
+ };
2504
+
2505
+ XNumber.prototype.stringValue = function() {
2506
+ return this.string().stringValue();
2507
+ };
2508
+
2509
+ XNumber.prototype.numberValue = function() {
2510
+ return this.num;
2511
+ };
2512
+
2513
+ XNumber.prototype.booleanValue = function() {
2514
+ return this.bool().booleanValue();
2515
+ };
2516
+
2517
+ XNumber.prototype.negate = function() {
2518
+ return new XNumber(-this.num);
2519
+ };
2520
+
2521
+ XNumber.prototype.equals = function(r) {
2522
+ if (Utilities.instance_of(r, XBoolean)) {
2523
+ return this.bool().equals(r);
2524
+ }
2525
+ if (Utilities.instance_of(r, XString)) {
2526
+ return this.equals(r.number());
2527
+ }
2528
+ if (Utilities.instance_of(r, XNodeSet)) {
2529
+ return r.compareWithNumber(this, Operators.equals);
2530
+ }
2531
+ return new XBoolean(this.num == r.num);
2532
+ };
2533
+
2534
+ XNumber.prototype.notequal = function(r) {
2535
+ if (Utilities.instance_of(r, XBoolean)) {
2536
+ return this.bool().notequal(r);
2537
+ }
2538
+ if (Utilities.instance_of(r, XString)) {
2539
+ return this.notequal(r.number());
2540
+ }
2541
+ if (Utilities.instance_of(r, XNodeSet)) {
2542
+ return r.compareWithNumber(this, Operators.notequal);
2543
+ }
2544
+ return new XBoolean(this.num != r.num);
2545
+ };
2546
+
2547
+ XNumber.prototype.lessthan = function(r) {
2548
+ if (Utilities.instance_of(r, XNodeSet)) {
2549
+ return r.compareWithNumber(this, Operators.greaterthanorequal);
2550
+ }
2551
+ if (Utilities.instance_of(r, XBoolean) || Utilities.instance_of(r, XString)) {
2552
+ return this.lessthan(r.number());
2553
+ }
2554
+ return new XBoolean(this.num < r.num);
2555
+ };
2556
+
2557
+ XNumber.prototype.greaterthan = function(r) {
2558
+ if (Utilities.instance_of(r, XNodeSet)) {
2559
+ return r.compareWithNumber(this, Operators.lessthanorequal);
2560
+ }
2561
+ if (Utilities.instance_of(r, XBoolean) || Utilities.instance_of(r, XString)) {
2562
+ return this.greaterthan(r.number());
2563
+ }
2564
+ return new XBoolean(this.num > r.num);
2565
+ };
2566
+
2567
+ XNumber.prototype.lessthanorequal = function(r) {
2568
+ if (Utilities.instance_of(r, XNodeSet)) {
2569
+ return r.compareWithNumber(this, Operators.greaterthan);
2570
+ }
2571
+ if (Utilities.instance_of(r, XBoolean) || Utilities.instance_of(r, XString)) {
2572
+ return this.lessthanorequal(r.number());
2573
+ }
2574
+ return new XBoolean(this.num <= r.num);
2575
+ };
2576
+
2577
+ XNumber.prototype.greaterthanorequal = function(r) {
2578
+ if (Utilities.instance_of(r, XNodeSet)) {
2579
+ return r.compareWithNumber(this, Operators.lessthan);
2580
+ }
2581
+ if (Utilities.instance_of(r, XBoolean) || Utilities.instance_of(r, XString)) {
2582
+ return this.greaterthanorequal(r.number());
2583
+ }
2584
+ return new XBoolean(this.num >= r.num);
2585
+ };
2586
+
2587
+ XNumber.prototype.plus = function(r) {
2588
+ return new XNumber(this.num + r.num);
2589
+ };
2590
+
2591
+ XNumber.prototype.minus = function(r) {
2592
+ return new XNumber(this.num - r.num);
2593
+ };
2594
+
2595
+ XNumber.prototype.multiply = function(r) {
2596
+ return new XNumber(this.num * r.num);
2597
+ };
2598
+
2599
+ XNumber.prototype.div = function(r) {
2600
+ return new XNumber(this.num / r.num);
2601
+ };
2602
+
2603
+ XNumber.prototype.mod = function(r) {
2604
+ return new XNumber(this.num % r.num);
2605
+ };
2606
+
2607
+ // XBoolean //////////////////////////////////////////////////////////////////
2608
+
2609
+ XBoolean.prototype = new Expression();
2610
+ XBoolean.prototype.constructor = XBoolean;
2611
+ XBoolean.superclass = Expression.prototype;
2612
+
2613
+ function XBoolean(b) {
2614
+ if (arguments.length > 0) {
2615
+ this.init(b);
2616
+ }
2617
+ }
2618
+
2619
+ XBoolean.prototype.init = function(b) {
2620
+ this.b = Boolean(b);
2621
+ };
2622
+
2623
+ XBoolean.prototype.toString = function() {
2624
+ return this.b.toString();
2625
+ };
2626
+
2627
+ XBoolean.prototype.evaluate = function(c) {
2628
+ return this;
2629
+ };
2630
+
2631
+ XBoolean.prototype.string = function() {
2632
+ return new XString(this.b);
2633
+ };
2634
+
2635
+ XBoolean.prototype.number = function() {
2636
+ return new XNumber(this.b);
2637
+ };
2638
+
2639
+ XBoolean.prototype.bool = function() {
2640
+ return this;
2641
+ };
2642
+
2643
+ XBoolean.prototype.nodeset = function() {
2644
+ throw new Error("Cannot convert boolean to nodeset");
2645
+ };
2646
+
2647
+ XBoolean.prototype.stringValue = function() {
2648
+ return this.string().stringValue();
2649
+ };
2650
+
2651
+ XBoolean.prototype.numberValue = function() {
2652
+ return this.num().numberValue();
2653
+ };
2654
+
2655
+ XBoolean.prototype.booleanValue = function() {
2656
+ return this.b;
2657
+ };
2658
+
2659
+ XBoolean.prototype.not = function() {
2660
+ return new XBoolean(!this.b);
2661
+ };
2662
+
2663
+ XBoolean.prototype.equals = function(r) {
2664
+ if (Utilities.instance_of(r, XString) || Utilities.instance_of(r, XNumber)) {
2665
+ return this.equals(r.bool());
2666
+ }
2667
+ if (Utilities.instance_of(r, XNodeSet)) {
2668
+ return r.compareWithBoolean(this, Operators.equals);
2669
+ }
2670
+ return new XBoolean(this.b == r.b);
2671
+ };
2672
+
2673
+ XBoolean.prototype.notequal = function(r) {
2674
+ if (Utilities.instance_of(r, XString) || Utilities.instance_of(r, XNumber)) {
2675
+ return this.notequal(r.bool());
2676
+ }
2677
+ if (Utilities.instance_of(r, XNodeSet)) {
2678
+ return r.compareWithBoolean(this, Operators.notequal);
2679
+ }
2680
+ return new XBoolean(this.b != r.b);
2681
+ };
2682
+
2683
+ XBoolean.prototype.lessthan = function(r) {
2684
+ if (Utilities.instance_of(r, XNodeSet)) {
2685
+ return r.compareWithNumber(this.number(), Operators.greaterthanorequal);
2686
+ }
2687
+ return this.number().lessthan(r.number());
2688
+ };
2689
+
2690
+ XBoolean.prototype.greaterthan = function(r) {
2691
+ if (Utilities.instance_of(r, XNodeSet)) {
2692
+ return r.compareWithNumber(this.number(), Operators.lessthanorequal);
2693
+ }
2694
+ return this.number().greaterthan(r.number());
2695
+ };
2696
+
2697
+ XBoolean.prototype.lessthanorequal = function(r) {
2698
+ if (Utilities.instance_of(r, XNodeSet)) {
2699
+ return r.compareWithNumber(this.number(), Operators.greaterthan);
2700
+ }
2701
+ return this.number().lessthanorequal(r.number());
2702
+ };
2703
+
2704
+ XBoolean.prototype.greaterthanorequal = function(r) {
2705
+ if (Utilities.instance_of(r, XNodeSet)) {
2706
+ return r.compareWithNumber(this.number(), Operators.lessthan);
2707
+ }
2708
+ return this.number().greaterthanorequal(r.number());
2709
+ };
2710
+
2711
+ // AVLTree ///////////////////////////////////////////////////////////////////
2712
+
2713
+ AVLTree.prototype = new Object();
2714
+ AVLTree.prototype.constructor = AVLTree;
2715
+ AVLTree.superclass = Object.prototype;
2716
+
2717
+ function AVLTree(n) {
2718
+ this.init(n);
2719
+ }
2720
+
2721
+ AVLTree.prototype.init = function(n) {
2722
+ this.left = null;
2723
+ this.right = null;
2724
+ this.node = n;
2725
+ this.depth = 1;
2726
+ };
2727
+
2728
+ AVLTree.prototype.balance = function() {
2729
+ var ldepth = this.left == null ? 0 : this.left.depth;
2730
+ var rdepth = this.right == null ? 0 : this.right.depth;
2731
+
2732
+ if (ldepth > rdepth + 1) {
2733
+ // LR or LL rotation
2734
+ var lldepth = this.left.left == null ? 0 : this.left.left.depth;
2735
+ var lrdepth = this.left.right == null ? 0 : this.left.right.depth;
2736
+
2737
+ if (lldepth < lrdepth) {
2738
+ // LR rotation consists of a RR rotation of the left child
2739
+ this.left.rotateRR();
2740
+ // plus a LL rotation of this node, which happens anyway
2741
+ }
2742
+ this.rotateLL();
2743
+ } else if (ldepth + 1 < rdepth) {
2744
+ // RR or RL rorarion
2745
+ var rrdepth = this.right.right == null ? 0 : this.right.right.depth;
2746
+ var rldepth = this.right.left == null ? 0 : this.right.left.depth;
2747
+
2748
+ if (rldepth > rrdepth) {
2749
+ // RR rotation consists of a LL rotation of the right child
2750
+ this.right.rotateLL();
2751
+ // plus a RR rotation of this node, which happens anyway
2752
+ }
2753
+ this.rotateRR();
2754
+ }
2755
+ };
2756
+
2757
+ AVLTree.prototype.rotateLL = function() {
2758
+ // the left side is too long => rotate from the left (_not_ leftwards)
2759
+ var nodeBefore = this.node;
2760
+ var rightBefore = this.right;
2761
+ this.node = this.left.node;
2762
+ this.right = this.left;
2763
+ this.left = this.left.left;
2764
+ this.right.left = this.right.right;
2765
+ this.right.right = rightBefore;
2766
+ this.right.node = nodeBefore;
2767
+ this.right.updateInNewLocation();
2768
+ this.updateInNewLocation();
2769
+ };
2770
+
2771
+ AVLTree.prototype.rotateRR = function() {
2772
+ // the right side is too long => rotate from the right (_not_ rightwards)
2773
+ var nodeBefore = this.node;
2774
+ var leftBefore = this.left;
2775
+ this.node = this.right.node;
2776
+ this.left = this.right;
2777
+ this.right = this.right.right;
2778
+ this.left.right = this.left.left;
2779
+ this.left.left = leftBefore;
2780
+ this.left.node = nodeBefore;
2781
+ this.left.updateInNewLocation();
2782
+ this.updateInNewLocation();
2783
+ };
2784
+
2785
+ AVLTree.prototype.updateInNewLocation = function() {
2786
+ this.getDepthFromChildren();
2787
+ };
2788
+
2789
+ AVLTree.prototype.getDepthFromChildren = function() {
2790
+ this.depth = this.node == null ? 0 : 1;
2791
+ if (this.left != null) {
2792
+ this.depth = this.left.depth + 1;
2793
+ }
2794
+ if (this.right != null && this.depth <= this.right.depth) {
2795
+ this.depth = this.right.depth + 1;
2796
+ }
2797
+ };
2798
+
2799
+ function nodeOrder(n1, n2) {
2800
+ if (n1 === n2) {
2801
+ return 0;
2802
+ }
2803
+
2804
+ if (n1.compareDocumentPosition) {
2805
+ var cpos = n1.compareDocumentPosition(n2);
2806
+
2807
+ if (cpos & 0x01) {
2808
+ // not in the same document; return an arbitrary result (is there a better way to do this)
2809
+ return 1;
2810
+ }
2811
+ if (cpos & 0x0A) {
2812
+ // n2 precedes or contains n1
2813
+ return 1;
2814
+ }
2815
+ if (cpos & 0x14) {
2816
+ // n2 follows or is contained by n1
2817
+ return -1;
2818
+ }
2819
+
2820
+ return 0;
2821
+ }
2822
+
2823
+ var d1 = 0,
2824
+ d2 = 0;
2825
+ for (var m1 = n1; m1 != null; m1 = m1.parentNode || m1.ownerElement) {
2826
+ d1++;
2827
+ }
2828
+ for (var m2 = n2; m2 != null; m2 = m2.parentNode || m2.ownerElement) {
2829
+ d2++;
2830
+ }
2831
+
2832
+ // step up to same depth
2833
+ if (d1 > d2) {
2834
+ while (d1 > d2) {
2835
+ n1 = n1.parentNode || n1.ownerElement;
2836
+ d1--;
2837
+ }
2838
+ if (n1 === n2) {
2839
+ return 1;
2840
+ }
2841
+ } else if (d2 > d1) {
2842
+ while (d2 > d1) {
2843
+ n2 = n2.parentNode || n2.ownerElement;
2844
+ d2--;
2845
+ }
2846
+ if (n1 === n2) {
2847
+ return -1;
2848
+ }
2849
+ }
2850
+
2851
+ var n1Par = n1.parentNode || n1.ownerElement,
2852
+ n2Par = n2.parentNode || n2.ownerElement;
2853
+
2854
+ // find common parent
2855
+ while (n1Par !== n2Par) {
2856
+ n1 = n1Par;
2857
+ n2 = n2Par;
2858
+ n1Par = n1.parentNode || n1.ownerElement;
2859
+ n2Par = n2.parentNode || n2.ownerElement;
2860
+ }
2861
+
2862
+ var n1isAttr = Utilities.isAttribute(n1);
2863
+ var n2isAttr = Utilities.isAttribute(n2);
2864
+
2865
+ if (n1isAttr && !n2isAttr) {
2866
+ return -1;
2867
+ }
2868
+ if (!n1isAttr && n2isAttr) {
2869
+ return 1;
2870
+ }
2871
+
2872
+ if(n1Par) {
2873
+ var cn = n1isAttr ? n1Par.attributes : n1Par.childNodes,
2874
+ len = cn.length;
2875
+ for (var i = 0; i < len; i += 1) {
2876
+ var n = cn[i];
2877
+ if (n === n1) {
2878
+ return -1;
2879
+ }
2880
+ if (n === n2) {
2881
+ return 1;
2882
+ }
2883
+ }
2884
+ }
2885
+
2886
+ throw new Error('Unexpected: could not determine node order');
2887
+ }
2888
+
2889
+ AVLTree.prototype.add = function(n) {
2890
+ if (n === this.node) {
2891
+ return false;
2892
+ }
2893
+
2894
+ var o = nodeOrder(n, this.node);
2895
+
2896
+ var ret = false;
2897
+ if (o == -1) {
2898
+ if (this.left == null) {
2899
+ this.left = new AVLTree(n);
2900
+ ret = true;
2901
+ } else {
2902
+ ret = this.left.add(n);
2903
+ if (ret) {
2904
+ this.balance();
2905
+ }
2906
+ }
2907
+ } else if (o == 1) {
2908
+ if (this.right == null) {
2909
+ this.right = new AVLTree(n);
2910
+ ret = true;
2911
+ } else {
2912
+ ret = this.right.add(n);
2913
+ if (ret) {
2914
+ this.balance();
2915
+ }
2916
+ }
2917
+ }
2918
+
2919
+ if (ret) {
2920
+ this.getDepthFromChildren();
2921
+ }
2922
+ return ret;
2923
+ };
2924
+
2925
+ // XNodeSet //////////////////////////////////////////////////////////////////
2926
+
2927
+ XNodeSet.prototype = new Expression();
2928
+ XNodeSet.prototype.constructor = XNodeSet;
2929
+ XNodeSet.superclass = Expression.prototype;
2930
+
2931
+ function XNodeSet() {
2932
+ this.init();
2933
+ }
2934
+
2935
+ XNodeSet.prototype.init = function() {
2936
+ this.tree = null;
2937
+ this.nodes = [];
2938
+ this.size = 0;
2939
+ };
2940
+
2941
+ XNodeSet.prototype.toString = function() {
2942
+ var p = this.first();
2943
+ if (p == null) {
2944
+ return "";
2945
+ }
2946
+ return this.stringForNode(p);
2947
+ };
2948
+
2949
+ XNodeSet.prototype.evaluate = function(c) {
2950
+ return this;
2951
+ };
2952
+
2953
+ XNodeSet.prototype.string = function() {
2954
+ return new XString(this.toString());
2955
+ };
2956
+
2957
+ XNodeSet.prototype.stringValue = function() {
2958
+ return this.toString();
2959
+ };
2960
+
2961
+ XNodeSet.prototype.number = function() {
2962
+ return new XNumber(this.string());
2963
+ };
2964
+
2965
+ XNodeSet.prototype.numberValue = function() {
2966
+ return Number(this.string());
2967
+ };
2968
+
2969
+ XNodeSet.prototype.bool = function() {
2970
+ return new XBoolean(this.booleanValue());
2971
+ };
2972
+
2973
+ XNodeSet.prototype.booleanValue = function() {
2974
+ return !!this.size;
2975
+ };
2976
+
2977
+ XNodeSet.prototype.nodeset = function() {
2978
+ return this;
2979
+ };
2980
+
2981
+ XNodeSet.prototype.stringForNode = function(n) {
2982
+ if (n.nodeType == 9 /*Node.DOCUMENT_NODE*/ ||
2983
+ n.nodeType == 1 /*Node.ELEMENT_NODE */ ||
2984
+ n.nodeType === 11 /*Node.DOCUMENT_FRAGMENT*/) {
2985
+ return this.stringForContainerNode(n);
2986
+ }
2987
+ if (n.nodeType === 2 /* Node.ATTRIBUTE_NODE */) {
2988
+ return n.value || n.nodeValue;
2989
+ }
2990
+ if (n.isNamespaceNode) {
2991
+ return n.namespace;
2992
+ }
2993
+ return n.nodeValue;
2994
+ };
2995
+
2996
+ XNodeSet.prototype.stringForContainerNode = function(n) {
2997
+ var s = "";
2998
+ for (var n2 = n.firstChild; n2 != null; n2 = n2.nextSibling) {
2999
+ var nt = n2.nodeType;
3000
+ // Element, Text, CDATA, Document, Document Fragment
3001
+ if (nt === 1 || nt === 3 || nt === 4 || nt === 9 || nt === 11) {
3002
+ s += this.stringForNode(n2);
3003
+ }
3004
+ }
3005
+ return s;
3006
+ };
3007
+
3008
+ XNodeSet.prototype.buildTree = function () {
3009
+ if (!this.tree && this.nodes.length) {
3010
+ this.tree = new AVLTree(this.nodes[0]);
3011
+ for (var i = 1; i < this.nodes.length; i += 1) {
3012
+ this.tree.add(this.nodes[i]);
3013
+ }
3014
+ }
3015
+
3016
+ return this.tree;
3017
+ };
3018
+
3019
+ XNodeSet.prototype.first = function() {
3020
+ var p = this.buildTree();
3021
+ if (p == null) {
3022
+ return null;
3023
+ }
3024
+ while (p.left != null) {
3025
+ p = p.left;
3026
+ }
3027
+ return p.node;
3028
+ };
3029
+
3030
+ XNodeSet.prototype.add = function(n) {
3031
+ for (var i = 0; i < this.nodes.length; i += 1) {
3032
+ if (n === this.nodes[i]) {
3033
+ return;
3034
+ }
3035
+ }
3036
+
3037
+ this.tree = null;
3038
+ this.nodes.push(n);
3039
+ this.size += 1;
3040
+ };
3041
+
3042
+ XNodeSet.prototype.addArray = function(ns) {
3043
+ for (var i = 0; i < ns.length; i += 1) {
3044
+ this.add(ns[i]);
3045
+ }
3046
+ };
3047
+
3048
+ /**
3049
+ * Returns an array of the node set's contents in document order
3050
+ */
3051
+ XNodeSet.prototype.toArray = function() {
3052
+ var a = [];
3053
+ this.toArrayRec(this.buildTree(), a);
3054
+ return a;
3055
+ };
3056
+
3057
+ XNodeSet.prototype.toArrayRec = function(t, a) {
3058
+ if (t != null) {
3059
+ this.toArrayRec(t.left, a);
3060
+ a.push(t.node);
3061
+ this.toArrayRec(t.right, a);
3062
+ }
3063
+ };
3064
+
3065
+ /**
3066
+ * Returns an array of the node set's contents in arbitrary order
3067
+ */
3068
+ XNodeSet.prototype.toUnsortedArray = function () {
3069
+ return this.nodes.slice();
3070
+ };
3071
+
3072
+ XNodeSet.prototype.compareWithString = function(r, o) {
3073
+ var a = this.toUnsortedArray();
3074
+ for (var i = 0; i < a.length; i++) {
3075
+ var n = a[i];
3076
+ var l = new XString(this.stringForNode(n));
3077
+ var res = o(l, r);
3078
+ if (res.booleanValue()) {
3079
+ return res;
3080
+ }
3081
+ }
3082
+ return new XBoolean(false);
3083
+ };
3084
+
3085
+ XNodeSet.prototype.compareWithNumber = function(r, o) {
3086
+ var a = this.toUnsortedArray();
3087
+ for (var i = 0; i < a.length; i++) {
3088
+ var n = a[i];
3089
+ var l = new XNumber(this.stringForNode(n));
3090
+ var res = o(l, r);
3091
+ if (res.booleanValue()) {
3092
+ return res;
3093
+ }
3094
+ }
3095
+ return new XBoolean(false);
3096
+ };
3097
+
3098
+ XNodeSet.prototype.compareWithBoolean = function(r, o) {
3099
+ return o(this.bool(), r);
3100
+ };
3101
+
3102
+ XNodeSet.prototype.compareWithNodeSet = function(r, o) {
3103
+ var a = this.toUnsortedArray();
3104
+ for (var i = 0; i < a.length; i++) {
3105
+ var n = a[i];
3106
+ var l = new XString(this.stringForNode(n));
3107
+ var b = r.toUnsortedArray();
3108
+ for (var j = 0; j < b.length; j++) {
3109
+ var n2 = b[j];
3110
+ var r = new XString(this.stringForNode(n2));
3111
+ var res = o(l, r);
3112
+ if (res.booleanValue()) {
3113
+ return res;
3114
+ }
3115
+ }
3116
+ }
3117
+ return new XBoolean(false);
3118
+ };
3119
+
3120
+ XNodeSet.prototype.equals = function(r) {
3121
+ if (Utilities.instance_of(r, XString)) {
3122
+ return this.compareWithString(r, Operators.equals);
3123
+ }
3124
+ if (Utilities.instance_of(r, XNumber)) {
3125
+ return this.compareWithNumber(r, Operators.equals);
3126
+ }
3127
+ if (Utilities.instance_of(r, XBoolean)) {
3128
+ return this.compareWithBoolean(r, Operators.equals);
3129
+ }
3130
+ return this.compareWithNodeSet(r, Operators.equals);
3131
+ };
3132
+
3133
+ XNodeSet.prototype.notequal = function(r) {
3134
+ if (Utilities.instance_of(r, XString)) {
3135
+ return this.compareWithString(r, Operators.notequal);
3136
+ }
3137
+ if (Utilities.instance_of(r, XNumber)) {
3138
+ return this.compareWithNumber(r, Operators.notequal);
3139
+ }
3140
+ if (Utilities.instance_of(r, XBoolean)) {
3141
+ return this.compareWithBoolean(r, Operators.notequal);
3142
+ }
3143
+ return this.compareWithNodeSet(r, Operators.notequal);
3144
+ };
3145
+
3146
+ XNodeSet.prototype.lessthan = function(r) {
3147
+ if (Utilities.instance_of(r, XString)) {
3148
+ return this.compareWithNumber(r.number(), Operators.lessthan);
3149
+ }
3150
+ if (Utilities.instance_of(r, XNumber)) {
3151
+ return this.compareWithNumber(r, Operators.lessthan);
3152
+ }
3153
+ if (Utilities.instance_of(r, XBoolean)) {
3154
+ return this.compareWithBoolean(r, Operators.lessthan);
3155
+ }
3156
+ return this.compareWithNodeSet(r, Operators.lessthan);
3157
+ };
3158
+
3159
+ XNodeSet.prototype.greaterthan = function(r) {
3160
+ if (Utilities.instance_of(r, XString)) {
3161
+ return this.compareWithNumber(r.number(), Operators.greaterthan);
3162
+ }
3163
+ if (Utilities.instance_of(r, XNumber)) {
3164
+ return this.compareWithNumber(r, Operators.greaterthan);
3165
+ }
3166
+ if (Utilities.instance_of(r, XBoolean)) {
3167
+ return this.compareWithBoolean(r, Operators.greaterthan);
3168
+ }
3169
+ return this.compareWithNodeSet(r, Operators.greaterthan);
3170
+ };
3171
+
3172
+ XNodeSet.prototype.lessthanorequal = function(r) {
3173
+ if (Utilities.instance_of(r, XString)) {
3174
+ return this.compareWithNumber(r.number(), Operators.lessthanorequal);
3175
+ }
3176
+ if (Utilities.instance_of(r, XNumber)) {
3177
+ return this.compareWithNumber(r, Operators.lessthanorequal);
3178
+ }
3179
+ if (Utilities.instance_of(r, XBoolean)) {
3180
+ return this.compareWithBoolean(r, Operators.lessthanorequal);
3181
+ }
3182
+ return this.compareWithNodeSet(r, Operators.lessthanorequal);
3183
+ };
3184
+
3185
+ XNodeSet.prototype.greaterthanorequal = function(r) {
3186
+ if (Utilities.instance_of(r, XString)) {
3187
+ return this.compareWithNumber(r.number(), Operators.greaterthanorequal);
3188
+ }
3189
+ if (Utilities.instance_of(r, XNumber)) {
3190
+ return this.compareWithNumber(r, Operators.greaterthanorequal);
3191
+ }
3192
+ if (Utilities.instance_of(r, XBoolean)) {
3193
+ return this.compareWithBoolean(r, Operators.greaterthanorequal);
3194
+ }
3195
+ return this.compareWithNodeSet(r, Operators.greaterthanorequal);
3196
+ };
3197
+
3198
+ XNodeSet.prototype.union = function(r) {
3199
+ var ns = new XNodeSet();
3200
+ ns.addArray(this.toUnsortedArray());
3201
+ ns.addArray(r.toUnsortedArray());
3202
+ return ns;
3203
+ };
3204
+
3205
+ // XPathNamespace ////////////////////////////////////////////////////////////
3206
+
3207
+ XPathNamespace.prototype = new Object();
3208
+ XPathNamespace.prototype.constructor = XPathNamespace;
3209
+ XPathNamespace.superclass = Object.prototype;
3210
+
3211
+ function XPathNamespace(pre, ns, p) {
3212
+ this.isXPathNamespace = true;
3213
+ this.ownerDocument = p.ownerDocument;
3214
+ this.nodeName = "#namespace";
3215
+ this.prefix = pre;
3216
+ this.localName = pre;
3217
+ this.namespaceURI = ns;
3218
+ this.nodeValue = ns;
3219
+ this.ownerElement = p;
3220
+ this.nodeType = XPathNamespace.XPATH_NAMESPACE_NODE;
3221
+ }
3222
+
3223
+ XPathNamespace.prototype.toString = function() {
3224
+ return "{ \"" + this.prefix + "\", \"" + this.namespaceURI + "\" }";
3225
+ };
3226
+
3227
+ // Operators /////////////////////////////////////////////////////////////////
3228
+
3229
+ var Operators = new Object();
3230
+
3231
+ Operators.equals = function(l, r) {
3232
+ return l.equals(r);
3233
+ };
3234
+
3235
+ Operators.notequal = function(l, r) {
3236
+ return l.notequal(r);
3237
+ };
3238
+
3239
+ Operators.lessthan = function(l, r) {
3240
+ return l.lessthan(r);
3241
+ };
3242
+
3243
+ Operators.greaterthan = function(l, r) {
3244
+ return l.greaterthan(r);
3245
+ };
3246
+
3247
+ Operators.lessthanorequal = function(l, r) {
3248
+ return l.lessthanorequal(r);
3249
+ };
3250
+
3251
+ Operators.greaterthanorequal = function(l, r) {
3252
+ return l.greaterthanorequal(r);
3253
+ };
3254
+
3255
+ // XPathContext //////////////////////////////////////////////////////////////
3256
+
3257
+ XPathContext.prototype = new Object();
3258
+ XPathContext.prototype.constructor = XPathContext;
3259
+ XPathContext.superclass = Object.prototype;
3260
+
3261
+ function XPathContext(vr, nr, fr) {
3262
+ this.variableResolver = vr != null ? vr : new VariableResolver();
3263
+ this.namespaceResolver = nr != null ? nr : new NamespaceResolver();
3264
+ this.functionResolver = fr != null ? fr : new FunctionResolver();
3265
+ }
3266
+
3267
+ // VariableResolver //////////////////////////////////////////////////////////
3268
+
3269
+ VariableResolver.prototype = new Object();
3270
+ VariableResolver.prototype.constructor = VariableResolver;
3271
+ VariableResolver.superclass = Object.prototype;
3272
+
3273
+ function VariableResolver() {
3274
+ }
3275
+
3276
+ VariableResolver.prototype.getVariable = function(ln, ns) {
3277
+ return null;
3278
+ };
3279
+
3280
+ // FunctionResolver //////////////////////////////////////////////////////////
3281
+
3282
+ FunctionResolver.prototype = new Object();
3283
+ FunctionResolver.prototype.constructor = FunctionResolver;
3284
+ FunctionResolver.superclass = Object.prototype;
3285
+
3286
+ function FunctionResolver(thisArg) {
3287
+ this.thisArg = thisArg != null ? thisArg : Functions;
3288
+ this.functions = new Object();
3289
+ this.addStandardFunctions();
3290
+ }
3291
+
3292
+ FunctionResolver.prototype.addStandardFunctions = function() {
3293
+ this.functions["{}last"] = Functions.last;
3294
+ this.functions["{}position"] = Functions.position;
3295
+ this.functions["{}count"] = Functions.count;
3296
+ this.functions["{}id"] = Functions.id;
3297
+ this.functions["{}local-name"] = Functions.localName;
3298
+ this.functions["{}namespace-uri"] = Functions.namespaceURI;
3299
+ this.functions["{}name"] = Functions.name;
3300
+ this.functions["{}string"] = Functions.string;
3301
+ this.functions["{}concat"] = Functions.concat;
3302
+ this.functions["{}starts-with"] = Functions.startsWith;
3303
+ this.functions["{}contains"] = Functions.contains;
3304
+ this.functions["{}substring-before"] = Functions.substringBefore;
3305
+ this.functions["{}substring-after"] = Functions.substringAfter;
3306
+ this.functions["{}substring"] = Functions.substring;
3307
+ this.functions["{}string-length"] = Functions.stringLength;
3308
+ this.functions["{}normalize-space"] = Functions.normalizeSpace;
3309
+ this.functions["{}translate"] = Functions.translate;
3310
+ this.functions["{}boolean"] = Functions.boolean_;
3311
+ this.functions["{}not"] = Functions.not;
3312
+ this.functions["{}true"] = Functions.true_;
3313
+ this.functions["{}false"] = Functions.false_;
3314
+ this.functions["{}lang"] = Functions.lang;
3315
+ this.functions["{}number"] = Functions.number;
3316
+ this.functions["{}sum"] = Functions.sum;
3317
+ this.functions["{}floor"] = Functions.floor;
3318
+ this.functions["{}ceiling"] = Functions.ceiling;
3319
+ this.functions["{}round"] = Functions.round;
3320
+ };
3321
+
3322
+ FunctionResolver.prototype.addFunction = function(ns, ln, f) {
3323
+ this.functions["{" + ns + "}" + ln] = f;
3324
+ };
3325
+
3326
+ FunctionResolver.getFunctionFromContext = function(qName, context) {
3327
+ var parts = Utilities.resolveQName(qName, context.namespaceResolver, context.contextNode, false);
3328
+
3329
+ if (parts[0] === null) {
3330
+ throw new Error("Cannot resolve QName " + name);
3331
+ }
3332
+
3333
+ return context.functionResolver.getFunction(parts[1], parts[0]);
3334
+ };
3335
+
3336
+ FunctionResolver.prototype.getFunction = function(localName, namespace) {
3337
+ return this.functions["{" + namespace + "}" + localName];
3338
+ };
3339
+
3340
+ // NamespaceResolver /////////////////////////////////////////////////////////
3341
+
3342
+ NamespaceResolver.prototype = new Object();
3343
+ NamespaceResolver.prototype.constructor = NamespaceResolver;
3344
+ NamespaceResolver.superclass = Object.prototype;
3345
+
3346
+ function NamespaceResolver() {
3347
+ }
3348
+
3349
+ NamespaceResolver.prototype.getNamespace = function(prefix, n) {
3350
+ if (prefix == "xml") {
3351
+ return XPath.XML_NAMESPACE_URI;
3352
+ } else if (prefix == "xmlns") {
3353
+ return XPath.XMLNS_NAMESPACE_URI;
3354
+ }
3355
+ if (n.nodeType == 9 /*Node.DOCUMENT_NODE*/) {
3356
+ n = n.documentElement;
3357
+ } else if (n.nodeType == 2 /*Node.ATTRIBUTE_NODE*/) {
3358
+ n = PathExpr.prototype.getOwnerElement(n);
3359
+ } else if (n.nodeType != 1 /*Node.ELEMENT_NODE*/) {
3360
+ n = n.parentNode;
3361
+ }
3362
+ while (n != null && n.nodeType == 1 /*Node.ELEMENT_NODE*/) {
3363
+ var nnm = n.attributes;
3364
+ for (var i = 0; i < nnm.length; i++) {
3365
+ var a = nnm.item(i);
3366
+ var aname = a.name || a.nodeName;
3367
+ if ((aname === "xmlns" && prefix === "")
3368
+ || aname === "xmlns:" + prefix) {
3369
+ return String(a.value || a.nodeValue);
3370
+ }
3371
+ }
3372
+ n = n.parentNode;
3373
+ }
3374
+ return null;
3375
+ };
3376
+
3377
+ // Functions /////////////////////////////////////////////////////////////////
3378
+
3379
+ var Functions = new Object();
3380
+
3381
+ Functions.last = function() {
3382
+ var c = arguments[0];
3383
+ if (arguments.length != 1) {
3384
+ throw new Error("Function last expects ()");
3385
+ }
3386
+ return new XNumber(c.contextSize);
3387
+ };
3388
+
3389
+ Functions.position = function() {
3390
+ var c = arguments[0];
3391
+ if (arguments.length != 1) {
3392
+ throw new Error("Function position expects ()");
3393
+ }
3394
+ return new XNumber(c.contextPosition);
3395
+ };
3396
+
3397
+ Functions.count = function() {
3398
+ var c = arguments[0];
3399
+ var ns;
3400
+ if (arguments.length != 2 || !Utilities.instance_of(ns = arguments[1].evaluate(c), XNodeSet)) {
3401
+ throw new Error("Function count expects (node-set)");
3402
+ }
3403
+ return new XNumber(ns.size);
3404
+ };
3405
+
3406
+ Functions.id = function() {
3407
+ var c = arguments[0];
3408
+ var id;
3409
+ if (arguments.length != 2) {
3410
+ throw new Error("Function id expects (object)");
3411
+ }
3412
+ id = arguments[1].evaluate(c);
3413
+ if (Utilities.instance_of(id, XNodeSet)) {
3414
+ id = id.toArray().join(" ");
3415
+ } else {
3416
+ id = id.stringValue();
3417
+ }
3418
+ var ids = id.split(/[\x0d\x0a\x09\x20]+/);
3419
+ var count = 0;
3420
+ var ns = new XNodeSet();
3421
+ var doc = c.contextNode.nodeType == 9 /*Node.DOCUMENT_NODE*/
3422
+ ? c.contextNode
3423
+ : c.contextNode.ownerDocument;
3424
+ for (var i = 0; i < ids.length; i++) {
3425
+ var n;
3426
+ if (doc.getElementById) {
3427
+ n = doc.getElementById(ids[i]);
3428
+ } else {
3429
+ n = Utilities.getElementById(doc, ids[i]);
3430
+ }
3431
+ if (n != null) {
3432
+ ns.add(n);
3433
+ count++;
3434
+ }
3435
+ }
3436
+ return ns;
3437
+ };
3438
+
3439
+ Functions.localName = function() {
3440
+ var c = arguments[0];
3441
+ var n;
3442
+ if (arguments.length == 1) {
3443
+ n = c.contextNode;
3444
+ } else if (arguments.length == 2) {
3445
+ n = arguments[1].evaluate(c).first();
3446
+ } else {
3447
+ throw new Error("Function local-name expects (node-set?)");
3448
+ }
3449
+ if (n == null) {
3450
+ return new XString("");
3451
+ }
3452
+
3453
+ return new XString(n.localName || // standard elements and attributes
3454
+ n.baseName || // IE
3455
+ n.target || // processing instructions
3456
+ n.nodeName || // DOM1 elements
3457
+ ""); // fallback
3458
+ };
3459
+
3460
+ Functions.namespaceURI = function() {
3461
+ var c = arguments[0];
3462
+ var n;
3463
+ if (arguments.length == 1) {
3464
+ n = c.contextNode;
3465
+ } else if (arguments.length == 2) {
3466
+ n = arguments[1].evaluate(c).first();
3467
+ } else {
3468
+ throw new Error("Function namespace-uri expects (node-set?)");
3469
+ }
3470
+ if (n == null) {
3471
+ return new XString("");
3472
+ }
3473
+ return new XString(n.namespaceURI);
3474
+ };
3475
+
3476
+ Functions.name = function() {
3477
+ var c = arguments[0];
3478
+ var n;
3479
+ if (arguments.length == 1) {
3480
+ n = c.contextNode;
3481
+ } else if (arguments.length == 2) {
3482
+ n = arguments[1].evaluate(c).first();
3483
+ } else {
3484
+ throw new Error("Function name expects (node-set?)");
3485
+ }
3486
+ if (n == null) {
3487
+ return new XString("");
3488
+ }
3489
+ if (n.nodeType == 1 /*Node.ELEMENT_NODE*/) {
3490
+ return new XString(n.nodeName);
3491
+ } else if (n.nodeType == 2 /*Node.ATTRIBUTE_NODE*/) {
3492
+ return new XString(n.name || n.nodeName);
3493
+ } else if (n.nodeType === 7 /*Node.PROCESSING_INSTRUCTION_NODE*/) {
3494
+ return new XString(n.target || n.nodeName);
3495
+ } else if (n.localName == null) {
3496
+ return new XString("");
3497
+ } else {
3498
+ return new XString(n.localName);
3499
+ }
3500
+ };
3501
+
3502
+ Functions.string = function() {
3503
+ var c = arguments[0];
3504
+ if (arguments.length == 1) {
3505
+ return new XString(XNodeSet.prototype.stringForNode(c.contextNode));
3506
+ } else if (arguments.length == 2) {
3507
+ return arguments[1].evaluate(c).string();
3508
+ }
3509
+ throw new Error("Function string expects (object?)");
3510
+ };
3511
+
3512
+ Functions.concat = function() {
3513
+ var c = arguments[0];
3514
+ if (arguments.length < 3) {
3515
+ throw new Error("Function concat expects (string, string, string*)");
3516
+ }
3517
+ var s = "";
3518
+ for (var i = 1; i < arguments.length; i++) {
3519
+ s += arguments[i].evaluate(c).stringValue();
3520
+ }
3521
+ return new XString(s);
3522
+ };
3523
+
3524
+ Functions.startsWith = function() {
3525
+ var c = arguments[0];
3526
+ if (arguments.length != 3) {
3527
+ throw new Error("Function startsWith expects (string, string)");
3528
+ }
3529
+ var s1 = arguments[1].evaluate(c).stringValue();
3530
+ var s2 = arguments[2].evaluate(c).stringValue();
3531
+ return new XBoolean(s1.substring(0, s2.length) == s2);
3532
+ };
3533
+
3534
+ Functions.contains = function() {
3535
+ var c = arguments[0];
3536
+ if (arguments.length != 3) {
3537
+ throw new Error("Function contains expects (string, string)");
3538
+ }
3539
+ var s1 = arguments[1].evaluate(c).stringValue();
3540
+ var s2 = arguments[2].evaluate(c).stringValue();
3541
+ return new XBoolean(s1.indexOf(s2) !== -1);
3542
+ };
3543
+
3544
+ Functions.substringBefore = function() {
3545
+ var c = arguments[0];
3546
+ if (arguments.length != 3) {
3547
+ throw new Error("Function substring-before expects (string, string)");
3548
+ }
3549
+ var s1 = arguments[1].evaluate(c).stringValue();
3550
+ var s2 = arguments[2].evaluate(c).stringValue();
3551
+ return new XString(s1.substring(0, s1.indexOf(s2)));
3552
+ };
3553
+
3554
+ Functions.substringAfter = function() {
3555
+ var c = arguments[0];
3556
+ if (arguments.length != 3) {
3557
+ throw new Error("Function substring-after expects (string, string)");
3558
+ }
3559
+ var s1 = arguments[1].evaluate(c).stringValue();
3560
+ var s2 = arguments[2].evaluate(c).stringValue();
3561
+ if (s2.length == 0) {
3562
+ return new XString(s1);
3563
+ }
3564
+ var i = s1.indexOf(s2);
3565
+ if (i == -1) {
3566
+ return new XString("");
3567
+ }
3568
+ return new XString(s1.substring(i + s2.length));
3569
+ };
3570
+
3571
+ Functions.substring = function() {
3572
+ var c = arguments[0];
3573
+ if (!(arguments.length == 3 || arguments.length == 4)) {
3574
+ throw new Error("Function substring expects (string, number, number?)");
3575
+ }
3576
+ var s = arguments[1].evaluate(c).stringValue();
3577
+ var n1 = Math.round(arguments[2].evaluate(c).numberValue()) - 1;
3578
+ var n2 = arguments.length == 4 ? n1 + Math.round(arguments[3].evaluate(c).numberValue()) : undefined;
3579
+ return new XString(s.substring(n1, n2));
3580
+ };
3581
+
3582
+ Functions.stringLength = function() {
3583
+ var c = arguments[0];
3584
+ var s;
3585
+ if (arguments.length == 1) {
3586
+ s = XNodeSet.prototype.stringForNode(c.contextNode);
3587
+ } else if (arguments.length == 2) {
3588
+ s = arguments[1].evaluate(c).stringValue();
3589
+ } else {
3590
+ throw new Error("Function string-length expects (string?)");
3591
+ }
3592
+ return new XNumber(s.length);
3593
+ };
3594
+
3595
+ Functions.normalizeSpace = function() {
3596
+ var c = arguments[0];
3597
+ var s;
3598
+ if (arguments.length == 1) {
3599
+ s = XNodeSet.prototype.stringForNode(c.contextNode);
3600
+ } else if (arguments.length == 2) {
3601
+ s = arguments[1].evaluate(c).stringValue();
3602
+ } else {
3603
+ throw new Error("Function normalize-space expects (string?)");
3604
+ }
3605
+ var i = 0;
3606
+ var j = s.length - 1;
3607
+ while (Utilities.isSpace(s.charCodeAt(j))) {
3608
+ j--;
3609
+ }
3610
+ var t = "";
3611
+ while (i <= j && Utilities.isSpace(s.charCodeAt(i))) {
3612
+ i++;
3613
+ }
3614
+ while (i <= j) {
3615
+ if (Utilities.isSpace(s.charCodeAt(i))) {
3616
+ t += " ";
3617
+ while (i <= j && Utilities.isSpace(s.charCodeAt(i))) {
3618
+ i++;
3619
+ }
3620
+ } else {
3621
+ t += s.charAt(i);
3622
+ i++;
3623
+ }
3624
+ }
3625
+ return new XString(t);
3626
+ };
3627
+
3628
+ Functions.translate = function() {
3629
+ var c = arguments[0];
3630
+ if (arguments.length != 4) {
3631
+ throw new Error("Function translate expects (string, string, string)");
3632
+ }
3633
+ var s1 = arguments[1].evaluate(c).stringValue();
3634
+ var s2 = arguments[2].evaluate(c).stringValue();
3635
+ var s3 = arguments[3].evaluate(c).stringValue();
3636
+ var map = [];
3637
+ for (var i = 0; i < s2.length; i++) {
3638
+ var j = s2.charCodeAt(i);
3639
+ if (map[j] == undefined) {
3640
+ var k = i > s3.length ? "" : s3.charAt(i);
3641
+ map[j] = k;
3642
+ }
3643
+ }
3644
+ var t = "";
3645
+ for (var i = 0; i < s1.length; i++) {
3646
+ var c = s1.charCodeAt(i);
3647
+ var r = map[c];
3648
+ if (r == undefined) {
3649
+ t += s1.charAt(i);
3650
+ } else {
3651
+ t += r;
3652
+ }
3653
+ }
3654
+ return new XString(t);
3655
+ };
3656
+
3657
+ Functions.boolean_ = function() {
3658
+ var c = arguments[0];
3659
+ if (arguments.length != 2) {
3660
+ throw new Error("Function boolean expects (object)");
3661
+ }
3662
+ return arguments[1].evaluate(c).bool();
3663
+ };
3664
+
3665
+ Functions.not = function() {
3666
+ var c = arguments[0];
3667
+ if (arguments.length != 2) {
3668
+ throw new Error("Function not expects (object)");
3669
+ }
3670
+ return arguments[1].evaluate(c).bool().not();
3671
+ };
3672
+
3673
+ Functions.true_ = function() {
3674
+ if (arguments.length != 1) {
3675
+ throw new Error("Function true expects ()");
3676
+ }
3677
+ return new XBoolean(true);
3678
+ };
3679
+
3680
+ Functions.false_ = function() {
3681
+ if (arguments.length != 1) {
3682
+ throw new Error("Function false expects ()");
3683
+ }
3684
+ return new XBoolean(false);
3685
+ };
3686
+
3687
+ Functions.lang = function() {
3688
+ var c = arguments[0];
3689
+ if (arguments.length != 2) {
3690
+ throw new Error("Function lang expects (string)");
3691
+ }
3692
+ var lang;
3693
+ for (var n = c.contextNode; n != null && n.nodeType != 9 /*Node.DOCUMENT_NODE*/; n = n.parentNode) {
3694
+ var a = n.getAttributeNS(XPath.XML_NAMESPACE_URI, "lang");
3695
+ if (a != null) {
3696
+ lang = String(a);
3697
+ break;
3698
+ }
3699
+ }
3700
+ if (lang == null) {
3701
+ return new XBoolean(false);
3702
+ }
3703
+ var s = arguments[1].evaluate(c).stringValue();
3704
+ return new XBoolean(lang.substring(0, s.length) == s
3705
+ && (lang.length == s.length || lang.charAt(s.length) == '-'));
3706
+ };
3707
+
3708
+ Functions.number = function() {
3709
+ var c = arguments[0];
3710
+ if (!(arguments.length == 1 || arguments.length == 2)) {
3711
+ throw new Error("Function number expects (object?)");
3712
+ }
3713
+ if (arguments.length == 1) {
3714
+ return new XNumber(XNodeSet.prototype.stringForNode(c.contextNode));
3715
+ }
3716
+ return arguments[1].evaluate(c).number();
3717
+ };
3718
+
3719
+ Functions.sum = function() {
3720
+ var c = arguments[0];
3721
+ var ns;
3722
+ if (arguments.length != 2 || !Utilities.instance_of((ns = arguments[1].evaluate(c)), XNodeSet)) {
3723
+ throw new Error("Function sum expects (node-set)");
3724
+ }
3725
+ ns = ns.toUnsortedArray();
3726
+ var n = 0;
3727
+ for (var i = 0; i < ns.length; i++) {
3728
+ n += new XNumber(XNodeSet.prototype.stringForNode(ns[i])).numberValue();
3729
+ }
3730
+ return new XNumber(n);
3731
+ };
3732
+
3733
+ Functions.floor = function() {
3734
+ var c = arguments[0];
3735
+ if (arguments.length != 2) {
3736
+ throw new Error("Function floor expects (number)");
3737
+ }
3738
+ return new XNumber(Math.floor(arguments[1].evaluate(c).numberValue()));
3739
+ };
3740
+
3741
+ Functions.ceiling = function() {
3742
+ var c = arguments[0];
3743
+ if (arguments.length != 2) {
3744
+ throw new Error("Function ceiling expects (number)");
3745
+ }
3746
+ return new XNumber(Math.ceil(arguments[1].evaluate(c).numberValue()));
3747
+ };
3748
+
3749
+ Functions.round = function() {
3750
+ var c = arguments[0];
3751
+ if (arguments.length != 2) {
3752
+ throw new Error("Function round expects (number)");
3753
+ }
3754
+ return new XNumber(Math.round(arguments[1].evaluate(c).numberValue()));
3755
+ };
3756
+
3757
+ // Utilities /////////////////////////////////////////////////////////////////
3758
+
3759
+ var Utilities = new Object();
3760
+
3761
+ Utilities.isAttribute = function (val) {
3762
+ return val && (val.nodeType === 2 || val.ownerElement);
3763
+ }
3764
+
3765
+ Utilities.splitQName = function(qn) {
3766
+ var i = qn.indexOf(":");
3767
+ if (i == -1) {
3768
+ return [ null, qn ];
3769
+ }
3770
+ return [ qn.substring(0, i), qn.substring(i + 1) ];
3771
+ };
3772
+
3773
+ Utilities.resolveQName = function(qn, nr, n, useDefault) {
3774
+ var parts = Utilities.splitQName(qn);
3775
+ if (parts[0] != null) {
3776
+ parts[0] = nr.getNamespace(parts[0], n);
3777
+ } else {
3778
+ if (useDefault) {
3779
+ parts[0] = nr.getNamespace("", n);
3780
+ if (parts[0] == null) {
3781
+ parts[0] = "";
3782
+ }
3783
+ } else {
3784
+ parts[0] = "";
3785
+ }
3786
+ }
3787
+ return parts;
3788
+ };
3789
+
3790
+ Utilities.isSpace = function(c) {
3791
+ return c == 0x9 || c == 0xd || c == 0xa || c == 0x20;
3792
+ };
3793
+
3794
+ Utilities.isLetter = function(c) {
3795
+ return c >= 0x0041 && c <= 0x005A ||
3796
+ c >= 0x0061 && c <= 0x007A ||
3797
+ c >= 0x00C0 && c <= 0x00D6 ||
3798
+ c >= 0x00D8 && c <= 0x00F6 ||
3799
+ c >= 0x00F8 && c <= 0x00FF ||
3800
+ c >= 0x0100 && c <= 0x0131 ||
3801
+ c >= 0x0134 && c <= 0x013E ||
3802
+ c >= 0x0141 && c <= 0x0148 ||
3803
+ c >= 0x014A && c <= 0x017E ||
3804
+ c >= 0x0180 && c <= 0x01C3 ||
3805
+ c >= 0x01CD && c <= 0x01F0 ||
3806
+ c >= 0x01F4 && c <= 0x01F5 ||
3807
+ c >= 0x01FA && c <= 0x0217 ||
3808
+ c >= 0x0250 && c <= 0x02A8 ||
3809
+ c >= 0x02BB && c <= 0x02C1 ||
3810
+ c == 0x0386 ||
3811
+ c >= 0x0388 && c <= 0x038A ||
3812
+ c == 0x038C ||
3813
+ c >= 0x038E && c <= 0x03A1 ||
3814
+ c >= 0x03A3 && c <= 0x03CE ||
3815
+ c >= 0x03D0 && c <= 0x03D6 ||
3816
+ c == 0x03DA ||
3817
+ c == 0x03DC ||
3818
+ c == 0x03DE ||
3819
+ c == 0x03E0 ||
3820
+ c >= 0x03E2 && c <= 0x03F3 ||
3821
+ c >= 0x0401 && c <= 0x040C ||
3822
+ c >= 0x040E && c <= 0x044F ||
3823
+ c >= 0x0451 && c <= 0x045C ||
3824
+ c >= 0x045E && c <= 0x0481 ||
3825
+ c >= 0x0490 && c <= 0x04C4 ||
3826
+ c >= 0x04C7 && c <= 0x04C8 ||
3827
+ c >= 0x04CB && c <= 0x04CC ||
3828
+ c >= 0x04D0 && c <= 0x04EB ||
3829
+ c >= 0x04EE && c <= 0x04F5 ||
3830
+ c >= 0x04F8 && c <= 0x04F9 ||
3831
+ c >= 0x0531 && c <= 0x0556 ||
3832
+ c == 0x0559 ||
3833
+ c >= 0x0561 && c <= 0x0586 ||
3834
+ c >= 0x05D0 && c <= 0x05EA ||
3835
+ c >= 0x05F0 && c <= 0x05F2 ||
3836
+ c >= 0x0621 && c <= 0x063A ||
3837
+ c >= 0x0641 && c <= 0x064A ||
3838
+ c >= 0x0671 && c <= 0x06B7 ||
3839
+ c >= 0x06BA && c <= 0x06BE ||
3840
+ c >= 0x06C0 && c <= 0x06CE ||
3841
+ c >= 0x06D0 && c <= 0x06D3 ||
3842
+ c == 0x06D5 ||
3843
+ c >= 0x06E5 && c <= 0x06E6 ||
3844
+ c >= 0x0905 && c <= 0x0939 ||
3845
+ c == 0x093D ||
3846
+ c >= 0x0958 && c <= 0x0961 ||
3847
+ c >= 0x0985 && c <= 0x098C ||
3848
+ c >= 0x098F && c <= 0x0990 ||
3849
+ c >= 0x0993 && c <= 0x09A8 ||
3850
+ c >= 0x09AA && c <= 0x09B0 ||
3851
+ c == 0x09B2 ||
3852
+ c >= 0x09B6 && c <= 0x09B9 ||
3853
+ c >= 0x09DC && c <= 0x09DD ||
3854
+ c >= 0x09DF && c <= 0x09E1 ||
3855
+ c >= 0x09F0 && c <= 0x09F1 ||
3856
+ c >= 0x0A05 && c <= 0x0A0A ||
3857
+ c >= 0x0A0F && c <= 0x0A10 ||
3858
+ c >= 0x0A13 && c <= 0x0A28 ||
3859
+ c >= 0x0A2A && c <= 0x0A30 ||
3860
+ c >= 0x0A32 && c <= 0x0A33 ||
3861
+ c >= 0x0A35 && c <= 0x0A36 ||
3862
+ c >= 0x0A38 && c <= 0x0A39 ||
3863
+ c >= 0x0A59 && c <= 0x0A5C ||
3864
+ c == 0x0A5E ||
3865
+ c >= 0x0A72 && c <= 0x0A74 ||
3866
+ c >= 0x0A85 && c <= 0x0A8B ||
3867
+ c == 0x0A8D ||
3868
+ c >= 0x0A8F && c <= 0x0A91 ||
3869
+ c >= 0x0A93 && c <= 0x0AA8 ||
3870
+ c >= 0x0AAA && c <= 0x0AB0 ||
3871
+ c >= 0x0AB2 && c <= 0x0AB3 ||
3872
+ c >= 0x0AB5 && c <= 0x0AB9 ||
3873
+ c == 0x0ABD ||
3874
+ c == 0x0AE0 ||
3875
+ c >= 0x0B05 && c <= 0x0B0C ||
3876
+ c >= 0x0B0F && c <= 0x0B10 ||
3877
+ c >= 0x0B13 && c <= 0x0B28 ||
3878
+ c >= 0x0B2A && c <= 0x0B30 ||
3879
+ c >= 0x0B32 && c <= 0x0B33 ||
3880
+ c >= 0x0B36 && c <= 0x0B39 ||
3881
+ c == 0x0B3D ||
3882
+ c >= 0x0B5C && c <= 0x0B5D ||
3883
+ c >= 0x0B5F && c <= 0x0B61 ||
3884
+ c >= 0x0B85 && c <= 0x0B8A ||
3885
+ c >= 0x0B8E && c <= 0x0B90 ||
3886
+ c >= 0x0B92 && c <= 0x0B95 ||
3887
+ c >= 0x0B99 && c <= 0x0B9A ||
3888
+ c == 0x0B9C ||
3889
+ c >= 0x0B9E && c <= 0x0B9F ||
3890
+ c >= 0x0BA3 && c <= 0x0BA4 ||
3891
+ c >= 0x0BA8 && c <= 0x0BAA ||
3892
+ c >= 0x0BAE && c <= 0x0BB5 ||
3893
+ c >= 0x0BB7 && c <= 0x0BB9 ||
3894
+ c >= 0x0C05 && c <= 0x0C0C ||
3895
+ c >= 0x0C0E && c <= 0x0C10 ||
3896
+ c >= 0x0C12 && c <= 0x0C28 ||
3897
+ c >= 0x0C2A && c <= 0x0C33 ||
3898
+ c >= 0x0C35 && c <= 0x0C39 ||
3899
+ c >= 0x0C60 && c <= 0x0C61 ||
3900
+ c >= 0x0C85 && c <= 0x0C8C ||
3901
+ c >= 0x0C8E && c <= 0x0C90 ||
3902
+ c >= 0x0C92 && c <= 0x0CA8 ||
3903
+ c >= 0x0CAA && c <= 0x0CB3 ||
3904
+ c >= 0x0CB5 && c <= 0x0CB9 ||
3905
+ c == 0x0CDE ||
3906
+ c >= 0x0CE0 && c <= 0x0CE1 ||
3907
+ c >= 0x0D05 && c <= 0x0D0C ||
3908
+ c >= 0x0D0E && c <= 0x0D10 ||
3909
+ c >= 0x0D12 && c <= 0x0D28 ||
3910
+ c >= 0x0D2A && c <= 0x0D39 ||
3911
+ c >= 0x0D60 && c <= 0x0D61 ||
3912
+ c >= 0x0E01 && c <= 0x0E2E ||
3913
+ c == 0x0E30 ||
3914
+ c >= 0x0E32 && c <= 0x0E33 ||
3915
+ c >= 0x0E40 && c <= 0x0E45 ||
3916
+ c >= 0x0E81 && c <= 0x0E82 ||
3917
+ c == 0x0E84 ||
3918
+ c >= 0x0E87 && c <= 0x0E88 ||
3919
+ c == 0x0E8A ||
3920
+ c == 0x0E8D ||
3921
+ c >= 0x0E94 && c <= 0x0E97 ||
3922
+ c >= 0x0E99 && c <= 0x0E9F ||
3923
+ c >= 0x0EA1 && c <= 0x0EA3 ||
3924
+ c == 0x0EA5 ||
3925
+ c == 0x0EA7 ||
3926
+ c >= 0x0EAA && c <= 0x0EAB ||
3927
+ c >= 0x0EAD && c <= 0x0EAE ||
3928
+ c == 0x0EB0 ||
3929
+ c >= 0x0EB2 && c <= 0x0EB3 ||
3930
+ c == 0x0EBD ||
3931
+ c >= 0x0EC0 && c <= 0x0EC4 ||
3932
+ c >= 0x0F40 && c <= 0x0F47 ||
3933
+ c >= 0x0F49 && c <= 0x0F69 ||
3934
+ c >= 0x10A0 && c <= 0x10C5 ||
3935
+ c >= 0x10D0 && c <= 0x10F6 ||
3936
+ c == 0x1100 ||
3937
+ c >= 0x1102 && c <= 0x1103 ||
3938
+ c >= 0x1105 && c <= 0x1107 ||
3939
+ c == 0x1109 ||
3940
+ c >= 0x110B && c <= 0x110C ||
3941
+ c >= 0x110E && c <= 0x1112 ||
3942
+ c == 0x113C ||
3943
+ c == 0x113E ||
3944
+ c == 0x1140 ||
3945
+ c == 0x114C ||
3946
+ c == 0x114E ||
3947
+ c == 0x1150 ||
3948
+ c >= 0x1154 && c <= 0x1155 ||
3949
+ c == 0x1159 ||
3950
+ c >= 0x115F && c <= 0x1161 ||
3951
+ c == 0x1163 ||
3952
+ c == 0x1165 ||
3953
+ c == 0x1167 ||
3954
+ c == 0x1169 ||
3955
+ c >= 0x116D && c <= 0x116E ||
3956
+ c >= 0x1172 && c <= 0x1173 ||
3957
+ c == 0x1175 ||
3958
+ c == 0x119E ||
3959
+ c == 0x11A8 ||
3960
+ c == 0x11AB ||
3961
+ c >= 0x11AE && c <= 0x11AF ||
3962
+ c >= 0x11B7 && c <= 0x11B8 ||
3963
+ c == 0x11BA ||
3964
+ c >= 0x11BC && c <= 0x11C2 ||
3965
+ c == 0x11EB ||
3966
+ c == 0x11F0 ||
3967
+ c == 0x11F9 ||
3968
+ c >= 0x1E00 && c <= 0x1E9B ||
3969
+ c >= 0x1EA0 && c <= 0x1EF9 ||
3970
+ c >= 0x1F00 && c <= 0x1F15 ||
3971
+ c >= 0x1F18 && c <= 0x1F1D ||
3972
+ c >= 0x1F20 && c <= 0x1F45 ||
3973
+ c >= 0x1F48 && c <= 0x1F4D ||
3974
+ c >= 0x1F50 && c <= 0x1F57 ||
3975
+ c == 0x1F59 ||
3976
+ c == 0x1F5B ||
3977
+ c == 0x1F5D ||
3978
+ c >= 0x1F5F && c <= 0x1F7D ||
3979
+ c >= 0x1F80 && c <= 0x1FB4 ||
3980
+ c >= 0x1FB6 && c <= 0x1FBC ||
3981
+ c == 0x1FBE ||
3982
+ c >= 0x1FC2 && c <= 0x1FC4 ||
3983
+ c >= 0x1FC6 && c <= 0x1FCC ||
3984
+ c >= 0x1FD0 && c <= 0x1FD3 ||
3985
+ c >= 0x1FD6 && c <= 0x1FDB ||
3986
+ c >= 0x1FE0 && c <= 0x1FEC ||
3987
+ c >= 0x1FF2 && c <= 0x1FF4 ||
3988
+ c >= 0x1FF6 && c <= 0x1FFC ||
3989
+ c == 0x2126 ||
3990
+ c >= 0x212A && c <= 0x212B ||
3991
+ c == 0x212E ||
3992
+ c >= 0x2180 && c <= 0x2182 ||
3993
+ c >= 0x3041 && c <= 0x3094 ||
3994
+ c >= 0x30A1 && c <= 0x30FA ||
3995
+ c >= 0x3105 && c <= 0x312C ||
3996
+ c >= 0xAC00 && c <= 0xD7A3 ||
3997
+ c >= 0x4E00 && c <= 0x9FA5 ||
3998
+ c == 0x3007 ||
3999
+ c >= 0x3021 && c <= 0x3029;
4000
+ };
4001
+
4002
+ Utilities.isNCNameChar = function(c) {
4003
+ return c >= 0x0030 && c <= 0x0039
4004
+ || c >= 0x0660 && c <= 0x0669
4005
+ || c >= 0x06F0 && c <= 0x06F9
4006
+ || c >= 0x0966 && c <= 0x096F
4007
+ || c >= 0x09E6 && c <= 0x09EF
4008
+ || c >= 0x0A66 && c <= 0x0A6F
4009
+ || c >= 0x0AE6 && c <= 0x0AEF
4010
+ || c >= 0x0B66 && c <= 0x0B6F
4011
+ || c >= 0x0BE7 && c <= 0x0BEF
4012
+ || c >= 0x0C66 && c <= 0x0C6F
4013
+ || c >= 0x0CE6 && c <= 0x0CEF
4014
+ || c >= 0x0D66 && c <= 0x0D6F
4015
+ || c >= 0x0E50 && c <= 0x0E59
4016
+ || c >= 0x0ED0 && c <= 0x0ED9
4017
+ || c >= 0x0F20 && c <= 0x0F29
4018
+ || c == 0x002E
4019
+ || c == 0x002D
4020
+ || c == 0x005F
4021
+ || Utilities.isLetter(c)
4022
+ || c >= 0x0300 && c <= 0x0345
4023
+ || c >= 0x0360 && c <= 0x0361
4024
+ || c >= 0x0483 && c <= 0x0486
4025
+ || c >= 0x0591 && c <= 0x05A1
4026
+ || c >= 0x05A3 && c <= 0x05B9
4027
+ || c >= 0x05BB && c <= 0x05BD
4028
+ || c == 0x05BF
4029
+ || c >= 0x05C1 && c <= 0x05C2
4030
+ || c == 0x05C4
4031
+ || c >= 0x064B && c <= 0x0652
4032
+ || c == 0x0670
4033
+ || c >= 0x06D6 && c <= 0x06DC
4034
+ || c >= 0x06DD && c <= 0x06DF
4035
+ || c >= 0x06E0 && c <= 0x06E4
4036
+ || c >= 0x06E7 && c <= 0x06E8
4037
+ || c >= 0x06EA && c <= 0x06ED
4038
+ || c >= 0x0901 && c <= 0x0903
4039
+ || c == 0x093C
4040
+ || c >= 0x093E && c <= 0x094C
4041
+ || c == 0x094D
4042
+ || c >= 0x0951 && c <= 0x0954
4043
+ || c >= 0x0962 && c <= 0x0963
4044
+ || c >= 0x0981 && c <= 0x0983
4045
+ || c == 0x09BC
4046
+ || c == 0x09BE
4047
+ || c == 0x09BF
4048
+ || c >= 0x09C0 && c <= 0x09C4
4049
+ || c >= 0x09C7 && c <= 0x09C8
4050
+ || c >= 0x09CB && c <= 0x09CD
4051
+ || c == 0x09D7
4052
+ || c >= 0x09E2 && c <= 0x09E3
4053
+ || c == 0x0A02
4054
+ || c == 0x0A3C
4055
+ || c == 0x0A3E
4056
+ || c == 0x0A3F
4057
+ || c >= 0x0A40 && c <= 0x0A42
4058
+ || c >= 0x0A47 && c <= 0x0A48
4059
+ || c >= 0x0A4B && c <= 0x0A4D
4060
+ || c >= 0x0A70 && c <= 0x0A71
4061
+ || c >= 0x0A81 && c <= 0x0A83
4062
+ || c == 0x0ABC
4063
+ || c >= 0x0ABE && c <= 0x0AC5
4064
+ || c >= 0x0AC7 && c <= 0x0AC9
4065
+ || c >= 0x0ACB && c <= 0x0ACD
4066
+ || c >= 0x0B01 && c <= 0x0B03
4067
+ || c == 0x0B3C
4068
+ || c >= 0x0B3E && c <= 0x0B43
4069
+ || c >= 0x0B47 && c <= 0x0B48
4070
+ || c >= 0x0B4B && c <= 0x0B4D
4071
+ || c >= 0x0B56 && c <= 0x0B57
4072
+ || c >= 0x0B82 && c <= 0x0B83
4073
+ || c >= 0x0BBE && c <= 0x0BC2
4074
+ || c >= 0x0BC6 && c <= 0x0BC8
4075
+ || c >= 0x0BCA && c <= 0x0BCD
4076
+ || c == 0x0BD7
4077
+ || c >= 0x0C01 && c <= 0x0C03
4078
+ || c >= 0x0C3E && c <= 0x0C44
4079
+ || c >= 0x0C46 && c <= 0x0C48
4080
+ || c >= 0x0C4A && c <= 0x0C4D
4081
+ || c >= 0x0C55 && c <= 0x0C56
4082
+ || c >= 0x0C82 && c <= 0x0C83
4083
+ || c >= 0x0CBE && c <= 0x0CC4
4084
+ || c >= 0x0CC6 && c <= 0x0CC8
4085
+ || c >= 0x0CCA && c <= 0x0CCD
4086
+ || c >= 0x0CD5 && c <= 0x0CD6
4087
+ || c >= 0x0D02 && c <= 0x0D03
4088
+ || c >= 0x0D3E && c <= 0x0D43
4089
+ || c >= 0x0D46 && c <= 0x0D48
4090
+ || c >= 0x0D4A && c <= 0x0D4D
4091
+ || c == 0x0D57
4092
+ || c == 0x0E31
4093
+ || c >= 0x0E34 && c <= 0x0E3A
4094
+ || c >= 0x0E47 && c <= 0x0E4E
4095
+ || c == 0x0EB1
4096
+ || c >= 0x0EB4 && c <= 0x0EB9
4097
+ || c >= 0x0EBB && c <= 0x0EBC
4098
+ || c >= 0x0EC8 && c <= 0x0ECD
4099
+ || c >= 0x0F18 && c <= 0x0F19
4100
+ || c == 0x0F35
4101
+ || c == 0x0F37
4102
+ || c == 0x0F39
4103
+ || c == 0x0F3E
4104
+ || c == 0x0F3F
4105
+ || c >= 0x0F71 && c <= 0x0F84
4106
+ || c >= 0x0F86 && c <= 0x0F8B
4107
+ || c >= 0x0F90 && c <= 0x0F95
4108
+ || c == 0x0F97
4109
+ || c >= 0x0F99 && c <= 0x0FAD
4110
+ || c >= 0x0FB1 && c <= 0x0FB7
4111
+ || c == 0x0FB9
4112
+ || c >= 0x20D0 && c <= 0x20DC
4113
+ || c == 0x20E1
4114
+ || c >= 0x302A && c <= 0x302F
4115
+ || c == 0x3099
4116
+ || c == 0x309A
4117
+ || c == 0x00B7
4118
+ || c == 0x02D0
4119
+ || c == 0x02D1
4120
+ || c == 0x0387
4121
+ || c == 0x0640
4122
+ || c == 0x0E46
4123
+ || c == 0x0EC6
4124
+ || c == 0x3005
4125
+ || c >= 0x3031 && c <= 0x3035
4126
+ || c >= 0x309D && c <= 0x309E
4127
+ || c >= 0x30FC && c <= 0x30FE;
4128
+ };
4129
+
4130
+ Utilities.coalesceText = function(n) {
4131
+ for (var m = n.firstChild; m != null; m = m.nextSibling) {
4132
+ if (m.nodeType == 3 /*Node.TEXT_NODE*/ || m.nodeType == 4 /*Node.CDATA_SECTION_NODE*/) {
4133
+ var s = m.nodeValue;
4134
+ var first = m;
4135
+ m = m.nextSibling;
4136
+ while (m != null && (m.nodeType == 3 /*Node.TEXT_NODE*/ || m.nodeType == 4 /*Node.CDATA_SECTION_NODE*/)) {
4137
+ s += m.nodeValue;
4138
+ var del = m;
4139
+ m = m.nextSibling;
4140
+ del.parentNode.removeChild(del);
4141
+ }
4142
+ if (first.nodeType == 4 /*Node.CDATA_SECTION_NODE*/) {
4143
+ var p = first.parentNode;
4144
+ if (first.nextSibling == null) {
4145
+ p.removeChild(first);
4146
+ p.appendChild(p.ownerDocument.createTextNode(s));
4147
+ } else {
4148
+ var next = first.nextSibling;
4149
+ p.removeChild(first);
4150
+ p.insertBefore(p.ownerDocument.createTextNode(s), next);
4151
+ }
4152
+ } else {
4153
+ first.nodeValue = s;
4154
+ }
4155
+ if (m == null) {
4156
+ break;
4157
+ }
4158
+ } else if (m.nodeType == 1 /*Node.ELEMENT_NODE*/) {
4159
+ Utilities.coalesceText(m);
4160
+ }
4161
+ }
4162
+ };
4163
+
4164
+ Utilities.instance_of = function(o, c) {
4165
+ while (o != null) {
4166
+ if (o.constructor === c) {
4167
+ return true;
4168
+ }
4169
+ if (o === Object) {
4170
+ return false;
4171
+ }
4172
+ o = o.constructor.superclass;
4173
+ }
4174
+ return false;
4175
+ };
4176
+
4177
+ Utilities.getElementById = function(n, id) {
4178
+ // Note that this does not check the DTD to check for actual
4179
+ // attributes of type ID, so this may be a bit wrong.
4180
+ if (n.nodeType == 1 /*Node.ELEMENT_NODE*/) {
4181
+ if (n.getAttribute("id") == id
4182
+ || n.getAttributeNS(null, "id") == id) {
4183
+ return n;
4184
+ }
4185
+ }
4186
+ for (var m = n.firstChild; m != null; m = m.nextSibling) {
4187
+ var res = Utilities.getElementById(m, id);
4188
+ if (res != null) {
4189
+ return res;
4190
+ }
4191
+ }
4192
+ return null;
4193
+ };
4194
+
4195
+ // XPathException ////////////////////////////////////////////////////////////
4196
+
4197
+ var XPathException = (function () {
4198
+ function getMessage(code, exception) {
4199
+ var msg = exception ? ": " + exception.toString() : "";
4200
+ switch (code) {
4201
+ case XPathException.INVALID_EXPRESSION_ERR:
4202
+ return "Invalid expression" + msg;
4203
+ case XPathException.TYPE_ERR:
4204
+ return "Type error" + msg;
4205
+ }
4206
+ return null;
4207
+ }
4208
+
4209
+ function XPathException(code, error, message) {
4210
+ var err = Error.call(this, getMessage(code, error) || message);
4211
+
4212
+ err.code = code;
4213
+ err.exception = error;
4214
+
4215
+ return err;
4216
+ }
4217
+
4218
+ XPathException.prototype = Object.create(Error.prototype);
4219
+ XPathException.prototype.constructor = XPathException;
4220
+ XPathException.superclass = Error;
4221
+
4222
+ XPathException.prototype.toString = function() {
4223
+ return this.message;
4224
+ };
4225
+
4226
+ XPathException.fromMessage = function(message, error) {
4227
+ return new XPathException(null, error, message);
4228
+ };
4229
+
4230
+ XPathException.INVALID_EXPRESSION_ERR = 51;
4231
+ XPathException.TYPE_ERR = 52;
4232
+
4233
+ return XPathException;
4234
+ })();
4235
+
4236
+ // XPathExpression ///////////////////////////////////////////////////////////
4237
+
4238
+ XPathExpression.prototype = {};
4239
+ XPathExpression.prototype.constructor = XPathExpression;
4240
+ XPathExpression.superclass = Object.prototype;
4241
+
4242
+ function XPathExpression(e, r, p) {
4243
+ this.xpath = p.parse(e);
4244
+ this.context = new XPathContext();
4245
+ this.context.namespaceResolver = new XPathNSResolverWrapper(r);
4246
+ }
4247
+
4248
+ XPathExpression.prototype.evaluate = function(n, t, res) {
4249
+ this.context.expressionContextNode = n;
4250
+ var result = this.xpath.evaluate(this.context);
4251
+ return new XPathResult(result, t);
4252
+ }
4253
+
4254
+ // XPathNSResolverWrapper ////////////////////////////////////////////////////
4255
+
4256
+ XPathNSResolverWrapper.prototype = {};
4257
+ XPathNSResolverWrapper.prototype.constructor = XPathNSResolverWrapper;
4258
+ XPathNSResolverWrapper.superclass = Object.prototype;
4259
+
4260
+ function XPathNSResolverWrapper(r) {
4261
+ this.xpathNSResolver = r;
4262
+ }
4263
+
4264
+ XPathNSResolverWrapper.prototype.getNamespace = function(prefix, n) {
4265
+ if (this.xpathNSResolver == null) {
4266
+ return null;
4267
+ }
4268
+ return this.xpathNSResolver.lookupNamespaceURI(prefix);
4269
+ };
4270
+
4271
+ // NodeXPathNSResolver ///////////////////////////////////////////////////////
4272
+
4273
+ NodeXPathNSResolver.prototype = {};
4274
+ NodeXPathNSResolver.prototype.constructor = NodeXPathNSResolver;
4275
+ NodeXPathNSResolver.superclass = Object.prototype;
4276
+
4277
+ function NodeXPathNSResolver(n) {
4278
+ this.node = n;
4279
+ this.namespaceResolver = new NamespaceResolver();
4280
+ }
4281
+
4282
+ NodeXPathNSResolver.prototype.lookupNamespaceURI = function(prefix) {
4283
+ return this.namespaceResolver.getNamespace(prefix, this.node);
4284
+ };
4285
+
4286
+ // XPathResult ///////////////////////////////////////////////////////////////
4287
+
4288
+ XPathResult.prototype = {};
4289
+ XPathResult.prototype.constructor = XPathResult;
4290
+ XPathResult.superclass = Object.prototype;
4291
+
4292
+ function XPathResult(v, t) {
4293
+ if (t == XPathResult.ANY_TYPE) {
4294
+ if (v.constructor === XString) {
4295
+ t = XPathResult.STRING_TYPE;
4296
+ } else if (v.constructor === XNumber) {
4297
+ t = XPathResult.NUMBER_TYPE;
4298
+ } else if (v.constructor === XBoolean) {
4299
+ t = XPathResult.BOOLEAN_TYPE;
4300
+ } else if (v.constructor === XNodeSet) {
4301
+ t = XPathResult.UNORDERED_NODE_ITERATOR_TYPE;
4302
+ }
4303
+ }
4304
+ this.resultType = t;
4305
+ switch (t) {
4306
+ case XPathResult.NUMBER_TYPE:
4307
+ this.numberValue = v.numberValue();
4308
+ return;
4309
+ case XPathResult.STRING_TYPE:
4310
+ this.stringValue = v.stringValue();
4311
+ return;
4312
+ case XPathResult.BOOLEAN_TYPE:
4313
+ this.booleanValue = v.booleanValue();
4314
+ return;
4315
+ case XPathResult.ANY_UNORDERED_NODE_TYPE:
4316
+ case XPathResult.FIRST_ORDERED_NODE_TYPE:
4317
+ if (v.constructor === XNodeSet) {
4318
+ this.singleNodeValue = v.first();
4319
+ return;
4320
+ }
4321
+ break;
4322
+ case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
4323
+ case XPathResult.ORDERED_NODE_ITERATOR_TYPE:
4324
+ if (v.constructor === XNodeSet) {
4325
+ this.invalidIteratorState = false;
4326
+ this.nodes = v.toArray();
4327
+ this.iteratorIndex = 0;
4328
+ return;
4329
+ }
4330
+ break;
4331
+ case XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE:
4332
+ case XPathResult.ORDERED_NODE_SNAPSHOT_TYPE:
4333
+ if (v.constructor === XNodeSet) {
4334
+ this.nodes = v.toArray();
4335
+ this.snapshotLength = this.nodes.length;
4336
+ return;
4337
+ }
4338
+ break;
4339
+ }
4340
+ throw new XPathException(XPathException.TYPE_ERR);
4341
+ };
4342
+
4343
+ XPathResult.prototype.iterateNext = function() {
4344
+ if (this.resultType != XPathResult.UNORDERED_NODE_ITERATOR_TYPE
4345
+ && this.resultType != XPathResult.ORDERED_NODE_ITERATOR_TYPE) {
4346
+ throw new XPathException(XPathException.TYPE_ERR);
4347
+ }
4348
+ return this.nodes[this.iteratorIndex++];
4349
+ };
4350
+
4351
+ XPathResult.prototype.snapshotItem = function(i) {
4352
+ if (this.resultType != XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE
4353
+ && this.resultType != XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) {
4354
+ throw new XPathException(XPathException.TYPE_ERR);
4355
+ }
4356
+ return this.nodes[i];
4357
+ };
4358
+
4359
+ XPathResult.ANY_TYPE = 0;
4360
+ XPathResult.NUMBER_TYPE = 1;
4361
+ XPathResult.STRING_TYPE = 2;
4362
+ XPathResult.BOOLEAN_TYPE = 3;
4363
+ XPathResult.UNORDERED_NODE_ITERATOR_TYPE = 4;
4364
+ XPathResult.ORDERED_NODE_ITERATOR_TYPE = 5;
4365
+ XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE = 6;
4366
+ XPathResult.ORDERED_NODE_SNAPSHOT_TYPE = 7;
4367
+ XPathResult.ANY_UNORDERED_NODE_TYPE = 8;
4368
+ XPathResult.FIRST_ORDERED_NODE_TYPE = 9;
4369
+
4370
+ // DOM 3 XPath support ///////////////////////////////////////////////////////
4371
+
4372
+ function installDOM3XPathSupport(doc, p) {
4373
+ doc.createExpression = function(e, r) {
4374
+ try {
4375
+ return new XPathExpression(e, r, p);
4376
+ } catch (e) {
4377
+ throw new XPathException(XPathException.INVALID_EXPRESSION_ERR, e);
4378
+ }
4379
+ };
4380
+ doc.createNSResolver = function(n) {
4381
+ return new NodeXPathNSResolver(n);
4382
+ };
4383
+ doc.evaluate = function(e, cn, r, t, res) {
4384
+ if (t < 0 || t > 9) {
4385
+ throw { code: 0, toString: function() { return "Request type not supported"; } };
4386
+ }
4387
+ return doc.createExpression(e, r, p).evaluate(cn, t, res);
4388
+ };
4389
+ };
4390
+
4391
+ // ---------------------------------------------------------------------------
4392
+
4393
+ // Install DOM 3 XPath support for the current document.
4394
+ try {
4395
+ var shouldInstall = true;
4396
+ try {
4397
+ if (document.implementation
4398
+ && document.implementation.hasFeature
4399
+ && document.implementation.hasFeature("XPath", null)) {
4400
+ shouldInstall = false;
4401
+ }
4402
+ } catch (e) {
4403
+ }
4404
+ if (shouldInstall) {
4405
+ installDOM3XPathSupport(document, new XPathParser());
4406
+ }
4407
+ } catch (e) {
4408
+ }
4409
+
4410
+ // ---------------------------------------------------------------------------
4411
+ // exports for node.js
4412
+
4413
+ installDOM3XPathSupport(exports, new XPathParser());
4414
+
4415
+ (function() {
4416
+ var parser = new XPathParser();
4417
+
4418
+ var defaultNSResolver = new NamespaceResolver();
4419
+ var defaultFunctionResolver = new FunctionResolver();
4420
+ var defaultVariableResolver = new VariableResolver();
4421
+
4422
+ function makeNSResolverFromFunction(func) {
4423
+ return {
4424
+ getNamespace: function (prefix, node) {
4425
+ var ns = func(prefix, node);
4426
+
4427
+ return ns || defaultNSResolver.getNamespace(prefix, node);
4428
+ }
4429
+ };
4430
+ }
4431
+
4432
+ function makeNSResolverFromObject(obj) {
4433
+ return makeNSResolverFromFunction(obj.getNamespace.bind(obj));
4434
+ }
4435
+
4436
+ function makeNSResolverFromMap(map) {
4437
+ return makeNSResolverFromFunction(function (prefix) {
4438
+ return map[prefix];
4439
+ });
4440
+ }
4441
+
4442
+ function makeNSResolver(resolver) {
4443
+ if (resolver && typeof resolver.getNamespace === "function") {
4444
+ return makeNSResolverFromObject(resolver);
4445
+ }
4446
+
4447
+ if (typeof resolver === "function") {
4448
+ return makeNSResolverFromFunction(resolver);
4449
+ }
4450
+
4451
+ // assume prefix -> uri mapping
4452
+ if (typeof resolver === "object") {
4453
+ return makeNSResolverFromMap(resolver);
4454
+ }
4455
+
4456
+ return defaultNSResolver;
4457
+ }
4458
+
4459
+ /** Converts native JavaScript types to their XPath library equivalent */
4460
+ function convertValue(value) {
4461
+ if (value === null ||
4462
+ typeof value === "undefined" ||
4463
+ value instanceof XString ||
4464
+ value instanceof XBoolean ||
4465
+ value instanceof XNumber ||
4466
+ value instanceof XNodeSet) {
4467
+ return value;
4468
+ }
4469
+
4470
+ switch (typeof value) {
4471
+ case "string": return new XString(value);
4472
+ case "boolean": return new XBoolean(value);
4473
+ case "number": return new XNumber(value);
4474
+ }
4475
+
4476
+ // assume node(s)
4477
+ var ns = new XNodeSet();
4478
+ ns.addArray([].concat(value));
4479
+ return ns;
4480
+ }
4481
+
4482
+ function makeEvaluator(func) {
4483
+ return function (context) {
4484
+ var args = Array.prototype.slice.call(arguments, 1).map(function (arg) {
4485
+ return arg.evaluate(context);
4486
+ });
4487
+ var result = func.apply(this, [].concat(context, args));
4488
+ return convertValue(result);
4489
+ };
4490
+ }
4491
+
4492
+ function makeFunctionResolverFromFunction(func) {
4493
+ return {
4494
+ getFunction: function (name, namespace) {
4495
+ var found = func(name, namespace);
4496
+ if (found) {
4497
+ return makeEvaluator(found);
4498
+ }
4499
+ return defaultFunctionResolver.getFunction(name, namespace);
4500
+ }
4501
+ };
4502
+ }
4503
+
4504
+ function makeFunctionResolverFromObject(obj) {
4505
+ return makeFunctionResolverFromFunction(obj.getFunction.bind(obj));
4506
+ }
4507
+
4508
+ function makeFunctionResolverFromMap(map) {
4509
+ return makeFunctionResolverFromFunction(function (name) {
4510
+ return map[name];
4511
+ });
4512
+ }
4513
+
4514
+ function makeFunctionResolver(resolver) {
4515
+ if (resolver && typeof resolver.getFunction === "function") {
4516
+ return makeFunctionResolverFromObject(resolver);
4517
+ }
4518
+
4519
+ if (typeof resolver === "function") {
4520
+ return makeFunctionResolverFromFunction(resolver);
4521
+ }
4522
+
4523
+ // assume map
4524
+ if (typeof resolver === "object") {
4525
+ return makeFunctionResolverFromMap(resolver);
4526
+ }
4527
+
4528
+ return defaultFunctionResolver;
4529
+ }
4530
+
4531
+ function makeVariableResolverFromFunction(func) {
4532
+ return {
4533
+ getVariable: function (name, namespace) {
4534
+ var value = func(name, namespace);
4535
+ return convertValue(value);
4536
+ }
4537
+ };
4538
+ }
4539
+
4540
+ function makeVariableResolver(resolver) {
4541
+ if (resolver) {
4542
+ if (typeof resolver.getVariable === "function") {
4543
+ return makeVariableResolverFromFunction(resolver.getVariable.bind(resolver));
4544
+ }
4545
+
4546
+ if (typeof resolver === "function") {
4547
+ return makeVariableResolverFromFunction(resolver);
4548
+ }
4549
+
4550
+ // assume map
4551
+ if (typeof resolver === "object") {
4552
+ return makeVariableResolverFromFunction(function (name) {
4553
+ return resolver[name];
4554
+ });
4555
+ }
4556
+ }
4557
+
4558
+ return defaultVariableResolver;
4559
+ }
4560
+
4561
+ function makeContext(options) {
4562
+ var context = new XPathContext();
4563
+
4564
+ if (options) {
4565
+ context.namespaceResolver = makeNSResolver(options.namespaces);
4566
+ context.functionResolver = makeFunctionResolver(options.functions);
4567
+ context.variableResolver = makeVariableResolver(options.variables);
4568
+ context.expressionContextNode = options.node;
4569
+ } else {
4570
+ context.namespaceResolver = defaultNSResolver;
4571
+ }
4572
+
4573
+ return context;
4574
+ }
4575
+
4576
+ function evaluate(parsedExpression, options) {
4577
+ var context = makeContext(options);
4578
+
4579
+ return parsedExpression.evaluate(context);
4580
+ }
4581
+
4582
+ var evaluatorPrototype = {
4583
+ evaluate: function (options) {
4584
+ return evaluate(this.expression, options);
4585
+ }
4586
+
4587
+ ,evaluateNumber: function (options) {
4588
+ return this.evaluate(options).numberValue();
4589
+ }
4590
+
4591
+ ,evaluateString: function (options) {
4592
+ return this.evaluate(options).stringValue();
4593
+ }
4594
+
4595
+ ,evaluateBoolean: function (options) {
4596
+ return this.evaluate(options).booleanValue();
4597
+ }
4598
+
4599
+ ,evaluateNodeSet: function (options) {
4600
+ return this.evaluate(options).nodeset();
4601
+ }
4602
+
4603
+ ,select: function (options) {
4604
+ return this.evaluateNodeSet(options).toArray()
4605
+ }
4606
+
4607
+ ,select1: function (options) {
4608
+ return this.select(options)[0];
4609
+ }
4610
+ };
4611
+
4612
+ function parse(xpath) {
4613
+ var parsed = parser.parse(xpath);
4614
+
4615
+ return Object.create(evaluatorPrototype, {
4616
+ expression: {
4617
+ value: parsed
4618
+ }
4619
+ });
4620
+ }
4621
+
4622
+ exports.parse = parse;
4623
+ })();
4624
+
4625
+ exports.XPath = XPath;
4626
+ exports.XPathParser = XPathParser;
4627
+ exports.XPathResult = XPathResult;
4628
+
4629
+ exports.Step = Step;
4630
+ exports.NodeTest = NodeTest;
4631
+ exports.BarOperation = BarOperation;
4632
+
4633
+ exports.NamespaceResolver = NamespaceResolver;
4634
+ exports.FunctionResolver = FunctionResolver;
4635
+ exports.VariableResolver = VariableResolver;
4636
+
4637
+ exports.Utilities = Utilities;
4638
+
4639
+ exports.XPathContext = XPathContext;
4640
+ exports.XNodeSet = XNodeSet;
4641
+ exports.XBoolean = XBoolean;
4642
+ exports.XString = XString;
4643
+ exports.XNumber = XNumber;
4644
+
4645
+ // helper
4646
+ exports.select = function(e, doc, single) {
4647
+ return exports.selectWithResolver(e, doc, null, single);
4648
+ };
4649
+
4650
+ exports.useNamespaces = function(mappings) {
4651
+ var resolver = {
4652
+ mappings: mappings || {},
4653
+ lookupNamespaceURI: function(prefix) {
4654
+ return this.mappings[prefix];
4655
+ }
4656
+ };
4657
+
4658
+ return function(e, doc, single) {
4659
+ return exports.selectWithResolver(e, doc, resolver, single);
4660
+ };
4661
+ };
4662
+
4663
+ exports.selectWithResolver = function(e, doc, resolver, single) {
4664
+ var expression = new XPathExpression(e, resolver, new XPathParser());
4665
+ var type = XPathResult.ANY_TYPE;
4666
+
4667
+ var result = expression.evaluate(doc, type, null);
4668
+
4669
+ if (result.resultType == XPathResult.STRING_TYPE) {
4670
+ result = result.stringValue;
4671
+ }
4672
+ else if (result.resultType == XPathResult.NUMBER_TYPE) {
4673
+ result = result.numberValue;
4674
+ }
4675
+ else if (result.resultType == XPathResult.BOOLEAN_TYPE) {
4676
+ result = result.booleanValue;
4677
+ }
4678
+ else {
4679
+ result = result.nodes;
4680
+ if (single) {
4681
+ result = result[0];
4682
+ }
4683
+ }
4684
+
4685
+ return result;
4686
+ };
4687
+
4688
+ exports.select1 = function(e, doc) {
4689
+ return exports.select(e, doc, true);
4690
+ };
4691
+
4692
+ // end non-node wrapper
4693
+ })(xpath);