forked from assaf/zombie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
548 lines (481 loc) · 14.4 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright 2005 Google
//
// Author: Steffen Meschkat <mesch@google.com>
//
// Miscellaneous utility and placeholder functions.
// Dummy implmentation for the logging functions. Replace by something
// useful when you want to debug.
function xpathLog(msg) {};
function xsltLog(msg) {};
function xsltLogXml(msg) {};
// zombie.js change
var ajaxsltIsIE6 = false;
// Throws an exception if false.
function assert(b) {
if (!b) {
throw "Assertion failed";
}
}
// Splits a string s at all occurrences of character c. This is like
// the split() method of the string object, but IE omits empty
// strings, which violates the invariant (s.split(x).join(x) == s).
function stringSplit(s, c) {
var a = s.indexOf(c);
if (a == -1) {
return [ s ];
}
var parts = [];
parts.push(s.substr(0,a));
while (a != -1) {
var a1 = s.indexOf(c, a + 1);
if (a1 != -1) {
parts.push(s.substr(a + 1, a1 - a - 1));
} else {
parts.push(s.substr(a + 1));
}
a = a1;
}
return parts;
}
// The following function does what document.importNode(node, true)
// would do for us here; however that method is broken in Safari/1.3,
// so we have to emulate it.
function xmlImportNode(doc, node) {
if (node.nodeType == DOM_TEXT_NODE) {
return domCreateTextNode(doc, node.nodeValue);
} else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
return domCreateCDATASection(doc, node.nodeValue);
} else if (node.nodeType == DOM_ELEMENT_NODE) {
var newNode = domCreateElement(doc, node.nodeName);
for (var i = 0; i < node.attributes.length; ++i) {
var an = node.attributes[i];
var name = an.nodeName;
var value = an.nodeValue;
domSetAttribute(newNode, name, value);
}
for (var c = node.firstChild; c; c = c.nextSibling) {
var cn = arguments.callee(doc, c);
domAppendChild(newNode, cn);
}
return newNode;
} else {
return domCreateComment(doc, node.nodeName);
}
}
// A set data structure. It can also be used as a map (i.e. the keys
// can have values other than 1), but we don't call it map because it
// would be ambiguous in this context. Also, the map is iterable, so
// we can use it to replace for-in loops over core javascript Objects.
// For-in iteration breaks when Object.prototype is modified, which
// some clients of the maps API do.
//
// NOTE(mesch): The set keys by the string value of its element, NOT
// by the typed value. In particular, objects can't be used as keys.
//
// @constructor
function Set() {
this.keys = [];
}
Set.prototype.size = function() {
return this.keys.length;
}
// Adds the entry to the set, ignoring if it is present.
Set.prototype.add = function(key, opt_value) {
var value = opt_value || 1;
if (!this.contains(key)) {
this[':' + key] = value;
this.keys.push(key);
}
}
// Sets the entry in the set, adding if it is not yet present.
Set.prototype.set = function(key, opt_value) {
var value = opt_value || 1;
if (!this.contains(key)) {
this[':' + key] = value;
this.keys.push(key);
} else {
this[':' + key] = value;
}
}
// Increments the key's value by 1. This works around the fact that
// numbers are always passed by value, never by reference, so that we
// can't increment the value returned by get(), or the iterator
// argument. Sets the key's value to 1 if it doesn't exist yet.
Set.prototype.inc = function(key) {
if (!this.contains(key)) {
this[':' + key] = 1;
this.keys.push(key);
} else {
this[':' + key]++;
}
}
Set.prototype.get = function(key) {
if (this.contains(key)) {
return this[':' + key];
} else {
var undefined;
return undefined;
}
}
// Removes the entry from the set.
Set.prototype.remove = function(key) {
if (this.contains(key)) {
delete this[':' + key];
removeFromArray(this.keys, key, true);
}
}
// Tests if an entry is in the set.
Set.prototype.contains = function(entry) {
return typeof this[':' + entry] != 'undefined';
}
// Gets a list of values in the set.
Set.prototype.items = function() {
var list = [];
for (var i = 0; i < this.keys.length; ++i) {
var k = this.keys[i];
var v = this[':' + k];
list.push(v);
}
return list;
}
// Invokes function f for every key value pair in the set as a method
// of the set.
Set.prototype.map = function(f) {
for (var i = 0; i < this.keys.length; ++i) {
var k = this.keys[i];
f.call(this, k, this[':' + k]);
}
}
Set.prototype.clear = function() {
for (var i = 0; i < this.keys.length; ++i) {
delete this[':' + this.keys[i]];
}
this.keys.length = 0;
}
// Applies the given function to each element of the array, preserving
// this, and passing the index.
function mapExec(array, func) {
for (var i = 0; i < array.length; ++i) {
func.call(this, array[i], i);
}
}
// Returns an array that contains the return value of the given
// function applied to every element of the input array.
function mapExpr(array, func) {
var ret = [];
for (var i = 0; i < array.length; ++i) {
ret.push(func(array[i]));
}
return ret;
};
// Reverses the given array in place.
function reverseInplace(array) {
for (var i = 0; i < array.length / 2; ++i) {
var h = array[i];
var ii = array.length - i - 1;
array[i] = array[ii];
array[ii] = h;
}
}
// Removes value from array. Returns the number of instances of value
// that were removed from array.
function removeFromArray(array, value, opt_notype) {
var shift = 0;
for (var i = 0; i < array.length; ++i) {
if (array[i] === value || (opt_notype && array[i] == value)) {
array.splice(i--, 1);
shift++;
}
}
return shift;
}
// Shallow-copies an array to the end of another array
// Basically Array.concat, but works with other non-array collections
function copyArray(dst, src) {
if (!src) return;
var dstLength = dst.length;
for (var i = src.length - 1; i >= 0; --i) {
dst[i+dstLength] = src[i];
}
}
/**
* This is an optimization for copying attribute lists in IE. IE includes many
* extraneous properties in its DOM attribute lists, which take require
* significant extra processing when evaluating attribute steps. With this
* function, we ignore any such attributes that has an empty string value.
*/
function copyArrayIgnoringAttributesWithoutValue(dst, src)
{
if (!src) return;
for (var i = src.length - 1; i >= 0; --i) {
// this test will pass so long as the attribute has a non-empty string
// value, even if that value is "false", "0", "undefined", etc.
if (src[i].nodeValue) {
dst.push(src[i]);
}
}
}
// Returns the text value of a node; for nodes without children this
// is the nodeValue, for nodes with children this is the concatenation
// of the value of all children. Browser-specific optimizations are used by
// default; they can be disabled by passing "true" in as the second parameter.
function xmlValue(node, disallowBrowserSpecificOptimization) {
if (!node) {
return '';
}
var ret = '';
if (node.nodeType == DOM_TEXT_NODE ||
node.nodeType == DOM_CDATA_SECTION_NODE) {
ret += node.nodeValue;
} else if (node.nodeType == DOM_ATTRIBUTE_NODE) {
if (ajaxsltIsIE6) {
ret += xmlValueIE6Hack(node);
} else {
ret += node.nodeValue;
}
} else if (node.nodeType == DOM_ELEMENT_NODE ||
node.nodeType == DOM_DOCUMENT_NODE ||
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
if (!disallowBrowserSpecificOptimization) {
// IE, Safari, Opera, and friends
var innerText = node.innerText;
if (innerText != undefined) {
return innerText;
}
// Firefox
var textContent = node.textContent;
if (textContent != undefined) {
return textContent;
}
}
// pobrecito!
var len = node.childNodes.length;
for (var i = 0; i < len; ++i) {
ret += arguments.callee(node.childNodes[i]);
}
}
return ret;
}
function xmlValueIE6Hack(node) {
// Issue 19, IE6 mangles href attribute when it's a javascript: url
var nodeName = node.nodeName;
var nodeValue = node.nodeValue;
if (nodeName.length != 4) return nodeValue;
if (!/^href$/i.test(nodeName)) return nodeValue;
if (!/^javascript:/.test(nodeValue)) return nodeValue;
return unescape(nodeValue);
}
// Returns the representation of a node as XML text.
function xmlText(node, opt_cdata) {
var buf = [];
xmlTextR(node, buf, opt_cdata);
return buf.join('');
}
function xmlTextR(node, buf, cdata) {
if (node.nodeType == DOM_TEXT_NODE) {
buf.push(xmlEscapeText(node.nodeValue));
} else if (node.nodeType == DOM_CDATA_SECTION_NODE) {
if (cdata) {
buf.push(node.nodeValue);
} else {
buf.push('<![CDATA[' + node.nodeValue + ']]>');
}
} else if (node.nodeType == DOM_COMMENT_NODE) {
buf.push('<!--' + node.nodeValue + '-->');
} else if (node.nodeType == DOM_ELEMENT_NODE) {
buf.push('<' + xmlFullNodeName(node));
for (var i = 0; i < node.attributes.length; ++i) {
var a = node.attributes[i];
if (a && a.nodeName && a.nodeValue) {
buf.push(' ' + xmlFullNodeName(a) + '="' +
xmlEscapeAttr(a.nodeValue) + '"');
}
}
if (node.childNodes.length == 0) {
buf.push('/>');
} else {
buf.push('>');
for (var i = 0; i < node.childNodes.length; ++i) {
arguments.callee(node.childNodes[i], buf, cdata);
}
buf.push('</' + xmlFullNodeName(node) + '>');
}
} else if (node.nodeType == DOM_DOCUMENT_NODE ||
node.nodeType == DOM_DOCUMENT_FRAGMENT_NODE) {
for (var i = 0; i < node.childNodes.length; ++i) {
arguments.callee(node.childNodes[i], buf, cdata);
}
}
}
function xmlFullNodeName(n) {
if (n.prefix && n.nodeName.indexOf(n.prefix + ':') != 0) {
return n.prefix + ':' + n.nodeName;
} else {
return n.nodeName;
}
}
// Escape XML special markup chracters: tag delimiter < > and entity
// reference start delimiter &. The escaped string can be used in XML
// text portions (i.e. between tags).
function xmlEscapeText(s) {
return ('' + s).replace(/&/g, '&').replace(/</g, '<').
replace(/>/g, '>');
}
// Escape XML special markup characters: tag delimiter < > entity
// reference start delimiter & and quotes ". The escaped string can be
// used in double quoted XML attribute value portions (i.e. in
// attributes within start tags).
function xmlEscapeAttr(s) {
return xmlEscapeText(s).replace(/\"/g, '"');
}
// Escape markup in XML text, but don't touch entity references. The
// escaped string can be used as XML text (i.e. between tags).
function xmlEscapeTags(s) {
return s.replace(/</g, '<').replace(/>/g, '>');
}
/**
* Wrapper function to access the owner document uniformly for document
* and other nodes: for the document node, the owner document is the
* node itself, for all others it's the ownerDocument property.
*
* @param {Node} node
* @return {Document}
*/
function xmlOwnerDocument(node) {
if (node.nodeType == DOM_DOCUMENT_NODE) {
return node;
} else {
return node.ownerDocument;
}
}
// Wrapper around DOM methods so we can condense their invocations.
function domGetAttribute(node, name) {
return node.getAttribute(name);
}
function domSetAttribute(node, name, value) {
return node.setAttribute(name, value);
}
function domRemoveAttribute(node, name) {
return node.removeAttribute(name);
}
function domAppendChild(node, child) {
return node.appendChild(child);
}
function domRemoveChild(node, child) {
return node.removeChild(child);
}
function domReplaceChild(node, newChild, oldChild) {
return node.replaceChild(newChild, oldChild);
}
function domInsertBefore(node, newChild, oldChild) {
return node.insertBefore(newChild, oldChild);
}
function domRemoveNode(node) {
return domRemoveChild(node.parentNode, node);
}
function domCreateTextNode(doc, text) {
return doc.createTextNode(text);
}
function domCreateElement(doc, name) {
return doc.createElement(name);
}
function domCreateAttribute(doc, name) {
return doc.createAttribute(name);
}
function domCreateCDATASection(doc, data) {
return doc.createCDATASection(data);
}
function domCreateComment(doc, text) {
return doc.createComment(text);
}
function domCreateDocumentFragment(doc) {
return doc.createDocumentFragment();
}
function domGetElementById(doc, id) {
return doc.getElementById(id);
}
// Same for window methods.
function windowSetInterval(win, fun, time) {
return win.setInterval(fun, time);
}
function windowClearInterval(win, id) {
return win.clearInterval(id);
}
/**
* Escape the special regular expression characters when the regular expression
* is specified as a string.
*
* Based on: http://simonwillison.net/2006/Jan/20/escape/
*/
RegExp.escape = (function() {
var specials = [
'/', '.', '*', '+', '?', '|', '^', '$',
'(', ')', '[', ']', '{', '}', '\\'
];
var sRE = new RegExp(
'(\\' + specials.join('|\\') + ')', 'g'
);
return function(text) {
return text.replace(sRE, '\\$1');
}
})();
/**
* Determines whether a predicate expression contains a "positional selector".
* A positional selector filters nodes from the nodelist input based on their
* position within that list. When such selectors are encountered, the
* evaluation of the predicate cannot be depth-first, because the positional
* selector may be based on the result of evaluating predicates that precede
* it.
*/
function predicateExprHasPositionalSelector(expr, isRecursiveCall) {
if (!expr) {
return false;
}
if (!isRecursiveCall && exprReturnsNumberValue(expr)) {
// this is a "proximity position"-based predicate
return true;
}
if (expr instanceof FunctionCallExpr) {
var value = expr.name.value;
return (value == 'last' || value == 'position');
}
if (expr instanceof BinaryExpr) {
return (
predicateExprHasPositionalSelector(expr.expr1, true) ||
predicateExprHasPositionalSelector(expr.expr2, true));
}
return false;
}
function exprReturnsNumberValue(expr) {
if (expr instanceof FunctionCallExpr) {
var isMember = {
last: true
, position: true
, count: true
, 'string-length': true
, number: true
, sum: true
, floor: true
, ceiling: true
, round: true
};
return isMember[expr.name.value];
}
else if (expr instanceof UnaryMinusExpr) {
return true;
}
else if (expr instanceof BinaryExpr) {
var isMember = {
'+': true
, '-': true
, '*': true
, mod: true
, div: true
};
return isMember[expr.op.value];
}
else if (expr instanceof NumberExpr) {
return true;
}
return false;
}