Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81141 views
1
/*
2
* Copyright (C) 2007-2015 Diego Perini
3
* All rights reserved.
4
*
5
* nwmatcher-noqsa.js - A fast CSS selector engine and matcher
6
*
7
* Author: Diego Perini <diego.perini at gmail com>
8
* Version: 1.3.4
9
* Created: 20070722
10
* Release: 20150101
11
*
12
* License:
13
* http://javascript.nwbox.com/NWMatcher/MIT-LICENSE
14
* Download:
15
* http://javascript.nwbox.com/NWMatcher/nwmatcher.js
16
*/
17
18
(function(global, factory) {
19
20
if (typeof module == 'object' && typeof exports == 'object') {
21
module.exports = function (browserGlobal) {
22
// passed global does not contain
23
// references to native objects
24
browserGlobal.console = console;
25
browserGlobal.parseInt = parseInt;
26
browserGlobal.Function = Function;
27
browserGlobal.Boolean = Boolean;
28
browserGlobal.Number = Number;
29
browserGlobal.RegExp = RegExp;
30
browserGlobal.String = String;
31
browserGlobal.Object = Object;
32
browserGlobal.Array = Array;
33
browserGlobal.Error = Error;
34
browserGlobal.Date = Date;
35
browserGlobal.Math = Math;
36
var exports = browserGlobal.Object();
37
factory(browserGlobal, exports);
38
return exports;
39
};
40
module.factory = factory;
41
} else {
42
factory(global,
43
(global.NW || (global.NW = global.Object())) &&
44
(global.NW.Dom || (global.NW.Dom = global.Object())));
45
global.NW.Dom.factory = factory;
46
}
47
48
})(this, function(global, exports) {
49
50
var version = 'nwmatcher-1.3.4',
51
52
Dom = exports,
53
54
doc = global.document,
55
root = doc.documentElement,
56
57
isSingleMatch,
58
isSingleSelect,
59
60
lastSlice,
61
lastContext,
62
lastPosition,
63
64
lastMatcher,
65
lastSelector,
66
67
lastPartsMatch,
68
lastPartsSelect,
69
70
operators = '([~*^$|!]?={1})',
71
combinators = '[\\s]|[>+~][^>+~]',
72
pseudoparms = '(?:[-+]?\\d*n)?[-+]?\\d*',
73
74
quotedvalue = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"' + "|'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'",
75
skipgroup = '\\[.*\\]|\\(.*\\)|\\{.*\\}',
76
77
encoding = '(?:[-\\w]|[^\\x00-\\xa0]|\\\\.)',
78
identifier = '(?:-?[_a-zA-Z]{1}[-\\w]*|[^\\x00-\\xa0]+|\\\\.+)+',
79
80
attrcheck = '(' + quotedvalue + '|' + identifier + ')',
81
attributes = '\\s*(' + encoding + '*:?' + encoding + '+)\\s*(?:' + operators + '\\s*' + attrcheck + ')?\\s*',
82
83
attrmatcher = attributes.replace(attrcheck, '([\\x22\\x27]*)((?:\\\\?.)*?)\\3'),
84
85
pseudoclass = '((?:' +
86
pseudoparms + '|' + quotedvalue + '|' +
87
'[#.:]?|' + encoding + '+|' +
88
'\\[' + attributes + '\\]|' +
89
'\\(.+\\)|\\s*|' +
90
',)+)',
91
92
extensions = '.+',
93
94
standardValidator =
95
'(?=\\s*[^>+~(){}<>])' +
96
'(' +
97
'\\*' +
98
'|(?:[#.:]?' + identifier + ')' +
99
'|' + combinators +
100
'|\\[' + attributes + '\\]' +
101
'|\\(' + pseudoclass + '\\)' +
102
'|\\{' + extensions + '\\}' +
103
'|(?:,|\\s*)' +
104
')+',
105
106
extendedValidator = standardValidator.replace(pseudoclass, '.*'),
107
108
reValidator = global.RegExp(standardValidator, 'g'),
109
110
reTrimSpaces = /^\s*|\s*$/g,
111
112
reSimpleNot = global.RegExp('^(' +
113
'(?!:not)' +
114
'([#.:]?' +
115
'|' + identifier +
116
'|\\([^()]*\\))+' +
117
'|\\[' + attributes + '\\]' +
118
')$'),
119
120
reSplitGroup = /([^,\\()[\]]+|\[[^[\]]*\]|\[.*\]|\([^()]+\)|\(.*\)|\{[^{}]+\}|\{.*\}|\\.)+/g,
121
122
reSplitToken = global.RegExp('(' +
123
'\\[' + attributes + '\\]|' +
124
'\\(' + pseudoclass + '\\)|' +
125
'\\\\.|[^\\s>+~])+', 'g'),
126
127
reOptimizeSelector = global.RegExp(identifier + '|^$'),
128
129
QUIRKS_MODE,
130
XML_DOCUMENT,
131
132
GEBTN = 'getElementsByTagName' in doc,
133
GEBCN = 'getElementsByClassName' in doc,
134
135
LINK_NODES = global.Object({ a: 1, A: 1, area: 1, AREA: 1, link: 1, LINK: 1 }),
136
137
ATTR_BOOLEAN = global.Object({
138
checked: 1, disabled: 1, ismap: 1,
139
multiple: 1, readonly: 1, selected: 1
140
}),
141
142
ATTR_DEFAULT = global.Object({
143
value: 'defaultValue',
144
checked: 'defaultChecked',
145
selected: 'defaultSelected'
146
}),
147
148
ATTR_URIDATA = global.Object({
149
action: 2, cite: 2, codebase: 2, data: 2, href: 2,
150
longdesc: 2, lowsrc: 2, src: 2, usemap: 2
151
}),
152
153
Selectors = global.Object({
154
}),
155
156
Operators = global.Object({
157
'=': "n=='%m'",
158
'^=': "n.indexOf('%m')==0",
159
'*=': "n.indexOf('%m')>-1",
160
'|=': "(n+'-').indexOf('%m-')==0",
161
'~=': "(' '+n+' ').indexOf(' %m ')>-1",
162
'$=': "n.substr(n.length-'%m'.length)=='%m'"
163
}),
164
165
Optimize = global.Object({
166
ID: global.RegExp('^\\*?#(' + encoding + '+)|' + skipgroup),
167
TAG: global.RegExp('^(' + encoding + '+)|' + skipgroup),
168
CLASS: global.RegExp('^\\*?\\.(' + encoding + '+$)|' + skipgroup)
169
}),
170
171
Patterns = global.Object({
172
spseudos: /^\:(root|empty|(?:first|last|only)(?:-child|-of-type)|nth(?:-last)?(?:-child|-of-type)\(\s*(even|odd|(?:[-+]{0,1}\d*n\s*)?[-+]{0,1}\s*\d*)\s*\))?(.*)/i,
173
dpseudos: /^\:(link|visited|target|active|focus|hover|checked|disabled|enabled|selected|lang\(([-\w]{2,})\)|not\(([^()]*|.*)\))?(.*)/i,
174
attribute: global.RegExp('^\\[' + attrmatcher + '\\](.*)'),
175
children: /^\s*\>\s*(.*)/,
176
adjacent: /^\s*\+\s*(.*)/,
177
relative: /^\s*\~\s*(.*)/,
178
ancestor: /^\s+(.*)/,
179
universal: /^\*(.*)/,
180
id: global.RegExp('^#(' + encoding + '+)(.*)'),
181
tagName: global.RegExp('^(' + encoding + '+)(.*)'),
182
className: global.RegExp('^\\.(' + encoding + '+)(.*)')
183
}),
184
185
concatCall =
186
function(data, elements, callback) {
187
var i = -1, element;
188
while ((element = elements[++i])) {
189
if (false === callback(data[data.length] = element)) { break; }
190
}
191
return data;
192
},
193
194
switchContext =
195
function(from, force) {
196
var oldDoc = doc;
197
lastContext = from;
198
doc = from.ownerDocument || from;
199
if (force || oldDoc !== doc) {
200
root = doc.documentElement;
201
XML_DOCUMENT = doc.createElement('DiV').nodeName == 'DiV';
202
QUIRKS_MODE = !XML_DOCUMENT &&
203
typeof doc.compatMode == 'string' ?
204
doc.compatMode.indexOf('CSS') < 0 :
205
(function() {
206
var style = doc.createElement('div').style;
207
return style && (style.width = 1) && style.width == '1px';
208
})();
209
210
Config.CACHING && Dom.setCache(true, doc);
211
}
212
},
213
214
convertEscapes =
215
function(str) {
216
return str.replace(/\\([0-9a-fA-F]{1,6}\x20?|.)|([\x22\x27])/g, function(substring, p1, p2) {
217
var codePoint, highHex, highSurrogate, lowHex, lowSurrogate;
218
219
if (p2) {
220
return '\\' + p2;
221
}
222
223
if (/^[0-9a-fA-F]/.test(p1)) {
224
codePoint = parseInt(p1, 16);
225
226
if (codePoint < 0 || codePoint > 0x10ffff) {
227
return '\\ufffd';
228
}
229
230
if (codePoint <= 0xffff) {
231
lowHex = '000' + codePoint.toString(16);
232
return '\\u' + lowHex.substr(lowHex.length - 4);
233
}
234
235
codePoint -= 0x10000;
236
highSurrogate = (codePoint >> 10) + 0xd800;
237
lowSurrogate = (codePoint % 0x400) + 0xdc00;
238
highHex = '000' + highSurrogate.toString(16);
239
lowHex = '000' + lowSurrogate.toString(16);
240
241
return '\\u' + highHex.substr(highHex.length - 4) +
242
'\\u' + lowHex.substr(lowHex.length - 4);
243
}
244
245
if (/^[\\\x22\x27]/.test(p1)) {
246
return substring;
247
}
248
249
return p1;
250
});
251
},
252
253
byIdRaw =
254
function(id, elements) {
255
var i = -1, element = null;
256
while ((element = elements[++i])) {
257
if (element.getAttribute('id') == id) {
258
break;
259
}
260
}
261
return element;
262
},
263
264
_byId = !('fileSize' in doc) ?
265
function(id, from) {
266
id = id.replace(/\\([^\\]{1})/g, '$1');
267
return from.getElementById && from.getElementById(id) ||
268
byIdRaw(id, from.getElementsByTagName('*'));
269
} :
270
function(id, from) {
271
var element = null;
272
id = id.replace(/\\([^\\]{1})/g, '$1');
273
if (XML_DOCUMENT || from.nodeType != 9) {
274
return byIdRaw(id, from.getElementsByTagName('*'));
275
}
276
if ((element = from.getElementById(id)) &&
277
element.name == id && from.getElementsByName) {
278
return byIdRaw(id, from.getElementsByName(id));
279
}
280
return element;
281
},
282
283
byId =
284
function(id, from) {
285
from || (from = doc);
286
if (lastContext !== from) { switchContext(from); }
287
return _byId(id, from);
288
},
289
290
byTagRaw =
291
function(tag, from) {
292
var any = tag == '*', element = from, elements = global.Array(), next = element.firstChild;
293
any || (tag = tag.toUpperCase());
294
while ((element = next)) {
295
if (element.tagName > '@' && (any || element.tagName.toUpperCase() == tag)) {
296
elements[elements.length] = element;
297
}
298
if ((next = element.firstChild || element.nextSibling)) continue;
299
while (!next && (element = element.parentNode) && element !== from) {
300
next = element.nextSibling;
301
}
302
}
303
return elements;
304
},
305
306
contains = 'compareDocumentPosition' in root ?
307
function(container, element) {
308
return (container.compareDocumentPosition(element) & 16) == 16;
309
} : 'contains' in root ?
310
function(container, element) {
311
return element.nodeType == 1 && container.contains(element);
312
} :
313
function(container, element) {
314
while ((element = element.parentNode) && element.nodeType == 1) {
315
if (element === container) return true;
316
}
317
return false;
318
},
319
320
getAttribute =
321
function(node, attribute) {
322
attribute = attribute.toLowerCase();
323
if (typeof node[attribute] == 'object') {
324
return node.attributes[attribute] &&
325
node.attributes[attribute].value || '';
326
}
327
return (
328
attribute == 'type' ? node.getAttribute(attribute) || '' :
329
ATTR_URIDATA[attribute] ? node.getAttribute(attribute, 2) || '' :
330
ATTR_BOOLEAN[attribute] ? node.getAttribute(attribute) ? attribute : 'false' :
331
((node = node.getAttributeNode(attribute)) && node.value) || '');
332
},
333
334
hasAttribute = root.hasAttribute ?
335
function(node, attribute) {
336
return node.hasAttribute(attribute);
337
} :
338
function(node, attribute) {
339
attribute = attribute.toLowerCase();
340
if (ATTR_DEFAULT[attribute]) {
341
return !!node[ATTR_DEFAULT[attribute]];
342
}
343
node = node.getAttributeNode(attribute);
344
return !!(node && node.specified);
345
},
346
347
isLink =
348
function(element) {
349
return element.getAttribute('href') && LINK_NODES[element.nodeName];
350
},
351
352
isEmpty =
353
function(node) {
354
node = node.firstChild;
355
while (node) {
356
if (node.nodeType == 3 || node.nodeName > '@') return false;
357
node = node.nextSibling;
358
}
359
return true;
360
},
361
362
nthElement =
363
function(element, last) {
364
var count = 1, succ = last ? 'nextSibling' : 'previousSibling';
365
while ((element = element[succ])) {
366
if (element.nodeName > '@') ++count;
367
}
368
return count;
369
},
370
371
nthOfType =
372
function(element, last) {
373
var count = 1, succ = last ? 'nextSibling' : 'previousSibling', type = element.nodeName;
374
while ((element = element[succ])) {
375
if (element.nodeName == type) ++count;
376
}
377
return count;
378
},
379
380
configure =
381
function(option) {
382
if (typeof option == 'string') { return Config[option] || Config; }
383
if (typeof option != 'object') { return false; }
384
for (var i in option) {
385
Config[i] = !!option[i];
386
if (i == 'SIMPLENOT') {
387
matchContexts = global.Object();
388
matchResolvers = global.Object();
389
selectContexts = global.Object();
390
selectResolvers = global.Object();
391
}
392
}
393
reValidator = global.RegExp(Config.SIMPLENOT ?
394
standardValidator : extendedValidator, 'g');
395
return true;
396
},
397
398
emit =
399
function(message) {
400
if (Config.VERBOSITY) { throw global.Error(message); }
401
if (global.console && global.console.log) {
402
global.console.log(message);
403
}
404
},
405
406
Config = global.Object({
407
CACHING: false,
408
SIMPLENOT: true,
409
UNIQUE_ID: true,
410
USE_HTML5: true,
411
VERBOSITY: true
412
}),
413
414
IE_LT_9 = typeof doc.addEventListener != 'function',
415
416
INSENSITIVE_MAP = global.Object({
417
href: 1, lang: 1, src: 1, style: 1, title: 1,
418
type: 1, xmlns: 1, 'xml:lang': 1, 'xml:space': 1
419
}),
420
421
TO_UPPER_CASE = IE_LT_9 ? '.toUpperCase()' : '',
422
423
ACCEPT_NODE = 'r[r.length]=c[k];if(f&&false===f(c[k]))break main;else continue main;',
424
REJECT_NODE = IE_LT_9 ? 'if(e.nodeName<"A")continue;' : '',
425
426
compile =
427
function(selector, source, mode) {
428
429
var parts = typeof selector == 'string' ? selector.match(reSplitGroup) : selector;
430
431
typeof source == 'string' || (source = '');
432
433
if (parts.length == 1) {
434
source += compileSelector(parts[0], mode ? ACCEPT_NODE : 'f&&f(k);return true;', mode);
435
} else {
436
var i = -1, seen = global.Object(), token;
437
while ((token = parts[++i])) {
438
token = token.replace(reTrimSpaces, '');
439
if (!seen[token] && (seen[token] = true)) {
440
source += compileSelector(token, mode ? ACCEPT_NODE : 'f&&f(k);return true;', mode);
441
}
442
}
443
}
444
445
if (mode) {
446
return global.Function('c,s,r,d,h,g,f,v',
447
'var N,n,x=0,k=-1,e;main:while((e=c[++k])){' + source + '}return r;');
448
} else {
449
return global.Function('e,s,r,d,h,g,f,v',
450
'var N,n,x=0,k=e;' + source + 'return false;');
451
}
452
},
453
454
FILTER =
455
'var z=v[@]||(v[@]=[]),l=z.length-1;' +
456
'while(l>=0&&z[l]!==e)--l;' +
457
'if(l!==-1){break;}' +
458
'z[z.length]=e;',
459
460
compileSelector =
461
function(selector, source, mode) {
462
463
var a, b, n, k = 0, expr, match, name, result, status, test, type;
464
465
while (selector) {
466
467
k++;
468
469
if ((match = selector.match(Patterns.universal))) {
470
expr = '';
471
}
472
473
else if ((match = selector.match(Patterns.id))) {
474
source = 'if(' + (XML_DOCUMENT ?
475
's.getAttribute(e,"id")' :
476
'(e.submit?s.getAttribute(e,"id"):e.id)') +
477
'=="' + match[1] + '"' +
478
'){' + source + '}';
479
}
480
481
else if ((match = selector.match(Patterns.tagName))) {
482
source = 'if(e.nodeName' + (XML_DOCUMENT ?
483
'=="' + match[1] + '"' : TO_UPPER_CASE +
484
'=="' + match[1].toUpperCase() + '"') +
485
'){' + source + '}';
486
}
487
488
else if ((match = selector.match(Patterns.className))) {
489
source = 'if((n=' + (XML_DOCUMENT ?
490
'e.getAttribute("class")' : 'e.className') +
491
')&&n.length&&(" "+' + (QUIRKS_MODE ? 'n.toLowerCase()' : 'n') +
492
'.replace(/\\s+/g," ")+" ").indexOf(" ' +
493
(QUIRKS_MODE ? match[1].toLowerCase() : match[1]) + ' ")>-1' +
494
'){' + source + '}';
495
}
496
497
else if ((match = selector.match(Patterns.attribute))) {
498
if (match[2] && !Operators[match[2]]) {
499
emit('Unsupported operator in attribute selectors "' + selector + '"');
500
return '';
501
}
502
test = 'false';
503
if (match[2] && match[4] && (test = Operators[match[2]])) {
504
match[4] = convertEscapes(match[4]);
505
type = INSENSITIVE_MAP[match[1].toLowerCase()];
506
test = test.replace(/\%m/g, type ? match[4].toLowerCase() : match[4]);
507
} else if (match[2] == '!=' || match[2] == '=') {
508
test = 'n' + match[2] + '=""';
509
}
510
expr = 'n=s.' + (match[2] ? 'get' : 'has') + 'Attribute(e,"' + match[1] + '")' + (type && match[2] ? '.toLowerCase();' : ';');
511
source = expr + 'if(' + (match[2] ? test : 'n') + '){' + source + '}';
512
}
513
514
else if ((match = selector.match(Patterns.adjacent))) {
515
source = (mode ? '' : FILTER.replace(/@/g, k)) + source;
516
source = 'var N' + k + '=e;while(e&&(e=e.previousSibling)){if(e.nodeName>"@"){' + source + 'break;}}e=N' + k + ';';
517
}
518
519
else if ((match = selector.match(Patterns.relative))) {
520
source = (mode ? '' : FILTER.replace(/@/g, k)) + source;
521
source = 'var N' + k + '=e;e=e.parentNode.firstChild;while(e&&e!==N' + k + '){if(e.nodeName>"@"){' + source + '}e=e.nextSibling;}e=N' + k + ';';
522
}
523
524
else if ((match = selector.match(Patterns.children))) {
525
source = (mode ? '' : FILTER.replace(/@/g, k)) + source;
526
source = 'var N' + k + '=e;while(e&&e!==h&&e!==g&&(e=e.parentNode)){' + source + 'break;}e=N' + k + ';';
527
}
528
529
else if ((match = selector.match(Patterns.ancestor))) {
530
source = (mode ? '' : FILTER.replace(/@/g, k)) + source;
531
source = 'var N' + k + '=e;while(e&&e!==h&&e!==g&&(e=e.parentNode)){' + source + '}e=N' + k + ';';
532
}
533
534
else if ((match = selector.match(Patterns.spseudos)) && match[1]) {
535
switch (match[1]) {
536
case 'root':
537
if (match[3]) {
538
source = 'if(e===h||s.contains(h,e)){' + source + '}';
539
} else {
540
source = 'if(e===h){' + source + '}';
541
}
542
break;
543
case 'empty':
544
source = 'if(s.isEmpty(e)){' + source + '}';
545
break;
546
default:
547
if (match[1] && match[2]) {
548
if (match[2] == 'n') {
549
source = 'if(e!==h){' + source + '}';
550
break;
551
} else if (match[2] == 'even') {
552
a = 2;
553
b = 0;
554
} else if (match[2] == 'odd') {
555
a = 2;
556
b = 1;
557
} else {
558
b = ((n = match[2].match(/(-?\d+)$/)) ? global.parseInt(n[1], 10) : 0);
559
a = ((n = match[2].match(/(-?\d*)n/i)) ? global.parseInt(n[1], 10) : 0);
560
if (n && n[1] == '-') a = -1;
561
}
562
test = a > 1 ?
563
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
564
'n>=' + b + '&&(n-(' + b + '))%' + a + '==0' : a < -1 ?
565
(/last/i.test(match[1])) ? '(n-(' + b + '))%' + a + '==0' :
566
'n<=' + b + '&&(n-(' + b + '))%' + a + '==0' : a === 0 ?
567
'n==' + b : a == -1 ? 'n<=' + b : 'n>=' + b;
568
source =
569
'if(e!==h){' +
570
'n=s[' + (/-of-type/i.test(match[1]) ? '"nthOfType"' : '"nthElement"') + ']' +
571
'(e,' + (/last/i.test(match[1]) ? 'true' : 'false') + ');' +
572
'if(' + test + '){' + source + '}' +
573
'}';
574
} else {
575
a = /first/i.test(match[1]) ? 'previous' : 'next';
576
n = /only/i.test(match[1]) ? 'previous' : 'next';
577
b = /first|last/i.test(match[1]);
578
type = /-of-type/i.test(match[1]) ? '&&n.nodeName!=e.nodeName' : '&&n.nodeName<"@"';
579
source = 'if(e!==h){' +
580
( 'n=e;while((n=n.' + a + 'Sibling)' + type + ');if(!n){' + (b ? source :
581
'n=e;while((n=n.' + n + 'Sibling)' + type + ');if(!n){' + source + '}') + '}' ) + '}';
582
}
583
break;
584
}
585
}
586
587
else if ((match = selector.match(Patterns.dpseudos)) && match[1]) {
588
switch (match[1].match(/^\w+/)[0]) {
589
case 'not':
590
expr = match[3].replace(reTrimSpaces, '');
591
if (Config.SIMPLENOT && !reSimpleNot.test(expr)) {
592
emit('Negation pseudo-class only accepts simple selectors "' + match.join('') + '"');
593
return '';
594
} else {
595
if ('compatMode' in doc) {
596
source = 'if(!' + compile(expr, '', false) + '(e,s,r,d,h,g)){' + source + '}';
597
} else {
598
source = 'if(!s.match(e, "' + expr.replace(/\x22/g, '\\"') + '",g)){' + source +'}';
599
}
600
}
601
break;
602
case 'checked':
603
source = 'if((typeof e.form!=="undefined"&&(/^(?:radio|checkbox)$/i).test(e.type)&&e.checked)' +
604
(Config.USE_HTML5 ? '||(/^option$/i.test(e.nodeName)&&(e.selected||e.checked))' : '') +
605
'){' + source + '}';
606
break;
607
case 'disabled':
608
source = 'if(((typeof e.form!=="undefined"' +
609
(Config.USE_HTML5 ? '' : '&&!(/^hidden$/i).test(e.type)') +
610
')||s.isLink(e))&&e.disabled===true){' + source + '}';
611
break;
612
case 'enabled':
613
source = 'if(((typeof e.form!=="undefined"' +
614
(Config.USE_HTML5 ? '' : '&&!(/^hidden$/i).test(e.type)') +
615
')||s.isLink(e))&&e.disabled===false){' + source + '}';
616
break;
617
case 'lang':
618
test = '';
619
if (match[2]) test = match[2].substr(0, 2) + '-';
620
source = 'do{(n=e.lang||"").toLowerCase();' +
621
'if((n==""&&h.lang=="' + match[2].toLowerCase() + '")||' +
622
'(n&&(n=="' + match[2].toLowerCase() +
623
'"||n.substr(0,3)=="' + test.toLowerCase() + '")))' +
624
'{' + source + 'break;}}while((e=e.parentNode)&&e!==g);';
625
break;
626
case 'target':
627
source = 'if(e.id==d.location.hash.slice(1)){' + source + '}';
628
break;
629
case 'link':
630
source = 'if(s.isLink(e)&&!e.visited){' + source + '}';
631
break;
632
case 'visited':
633
source = 'if(s.isLink(e)&&e.visited){' + source + '}';
634
break;
635
case 'active':
636
source = 'if(e===d.activeElement){' + source + '}';
637
break;
638
case 'hover':
639
source = 'if(e===d.hoverElement){' + source + '}';
640
break;
641
case 'focus':
642
source = 'hasFocus' in doc ?
643
'if(e===d.activeElement&&d.hasFocus()&&(e.type||e.href||typeof e.tabIndex=="number")){' + source + '}' :
644
'if(e===d.activeElement&&(e.type||e.href)){' + source + '}';
645
break;
646
case 'selected':
647
source = 'if(/^option$/i.test(e.nodeName)&&(e.selected||e.checked)){' + source + '}';
648
break;
649
default:
650
break;
651
}
652
}
653
654
else {
655
656
expr = false;
657
status = false;
658
for (expr in Selectors) {
659
if ((match = selector.match(Selectors[expr].Expression)) && match[1]) {
660
result = Selectors[expr].Callback(match, source);
661
source = result.source;
662
status = result.status;
663
if (status) { break; }
664
}
665
}
666
667
if (!status) {
668
emit('Unknown pseudo-class selector "' + selector + '"');
669
return '';
670
}
671
672
if (!expr) {
673
emit('Unknown token in selector "' + selector + '"');
674
return '';
675
}
676
677
}
678
679
if (!match) {
680
emit('Invalid syntax in selector "' + selector + '"');
681
return '';
682
}
683
684
selector = match && match[match.length - 1];
685
}
686
687
return source;
688
},
689
690
match =
691
function(element, selector, from, callback) {
692
693
var parts;
694
695
if (!(element && element.nodeType == 1)) {
696
emit('Invalid element argument');
697
return false;
698
} else if (typeof selector != 'string') {
699
emit('Invalid selector argument');
700
return false;
701
} else if (lastContext !== from) {
702
switchContext(from || (from = element.ownerDocument));
703
}
704
705
selector = selector.replace(reTrimSpaces, '');
706
707
Config.SHORTCUTS && (selector = Dom.shortcuts(selector, element, from));
708
709
if (lastMatcher != selector) {
710
if ((parts = selector.match(reValidator)) && parts[0] == selector) {
711
isSingleMatch = (parts = selector.match(reSplitGroup)).length < 2;
712
lastMatcher = selector;
713
lastPartsMatch = parts;
714
} else {
715
emit('The string "' + selector + '", is not a valid CSS selector');
716
return false;
717
}
718
} else parts = lastPartsMatch;
719
720
if (!matchResolvers[selector] || matchContexts[selector] !== from) {
721
matchResolvers[selector] = compile(isSingleMatch ? [selector] : parts, '', false);
722
matchContexts[selector] = from;
723
}
724
725
return matchResolvers[selector](element, Snapshot, [ ], doc, root, from, callback, { });
726
},
727
728
first =
729
function(selector, from) {
730
return select(selector, from, function() { return false; })[0] || null;
731
},
732
733
select =
734
function(selector, from, callback) {
735
736
var i, changed, element, elements, parts, token, original = selector;
737
738
if (arguments.length === 0) {
739
emit('Not enough arguments');
740
return [ ];
741
} else if (typeof selector != 'string') {
742
return [ ];
743
} else if (from && !(/1|9|11/).test(from.nodeType)) {
744
emit('Invalid or illegal context element');
745
return [ ];
746
} else if (lastContext !== from) {
747
switchContext(from || (from = doc));
748
}
749
750
if (Config.CACHING && (elements = Dom.loadResults(original, from, doc, root))) {
751
return callback ? concatCall([ ], elements, callback) : elements;
752
}
753
754
selector = selector.replace(reTrimSpaces, '');
755
756
Config.SHORTCUTS && (selector = Dom.shortcuts(selector, from));
757
758
if ((changed = lastSelector != selector)) {
759
if ((parts = selector.match(reValidator)) && parts[0] == selector) {
760
isSingleSelect = (parts = selector.match(reSplitGroup)).length < 2;
761
lastSelector = selector;
762
lastPartsSelect = parts;
763
} else {
764
emit('The string "' + selector + '", is not a valid CSS selector');
765
return [ ];
766
}
767
} else parts = lastPartsSelect;
768
769
if (from.nodeType == 11) {
770
771
elements = byTagRaw('*', from);
772
773
} else if (isSingleSelect) {
774
775
if (changed) {
776
parts = selector.match(reSplitToken);
777
token = parts[parts.length - 1];
778
lastSlice = token.split(':not')[0];
779
lastPosition = selector.length - token.length;
780
}
781
782
if (Config.UNIQUE_ID && (parts = lastSlice.match(Optimize.ID)) && (token = parts[1])) {
783
if ((element = _byId(token, from))) {
784
if (match(element, selector)) {
785
callback && callback(element);
786
elements = global.Array(element);
787
} else elements = global.Array();
788
}
789
}
790
791
else if (Config.UNIQUE_ID && (parts = selector.match(Optimize.ID)) && (token = parts[1])) {
792
if ((element = _byId(token, doc))) {
793
if ('#' + token == selector) {
794
callback && callback(element);
795
elements = global.Array(element);
796
} else if (/[>+~]/.test(selector)) {
797
from = element.parentNode;
798
} else {
799
from = element;
800
}
801
} else elements = global.Array();
802
}
803
804
if (elements) {
805
Config.CACHING && Dom.saveResults(original, from, doc, elements);
806
return elements;
807
}
808
809
if (!XML_DOCUMENT && GEBTN && (parts = lastSlice.match(Optimize.TAG)) && (token = parts[1])) {
810
if ((elements = from.getElementsByTagName(token)).length === 0) { return [ ]; }
811
selector = selector.slice(0, lastPosition) + selector.slice(lastPosition).replace(token, '*');
812
}
813
814
else if (!XML_DOCUMENT && GEBCN && (parts = lastSlice.match(Optimize.CLASS)) && (token = parts[1])) {
815
if ((elements = from.getElementsByClassName(token.replace(/\\([^\\]{1})/g, '$1'))).length === 0) { return [ ]; }
816
selector = selector.slice(0, lastPosition) + selector.slice(lastPosition).replace('.' + token,
817
reOptimizeSelector.test(selector.charAt(selector.indexOf(token) - 1)) ? '' : '*');
818
}
819
820
}
821
822
if (!elements) {
823
if (IE_LT_9) {
824
elements = /^(?:applet|object)$/i.test(from.nodeName) ? from.childNodes : from.all;
825
} else {
826
elements = from.getElementsByTagName('*');
827
}
828
}
829
830
if (!selectResolvers[selector] || selectContexts[selector] !== from) {
831
selectResolvers[selector] = compile(isSingleSelect ? [selector] : parts, REJECT_NODE, true);
832
selectContexts[selector] = from;
833
}
834
835
elements = selectResolvers[selector](elements, Snapshot, [ ], doc, root, from, callback, { });
836
837
Config.CACHING && Dom.saveResults(original, from, doc, elements);
838
839
return elements;
840
},
841
842
FN = function(x) { return x; },
843
844
matchContexts = global.Object(),
845
matchResolvers = global.Object(),
846
847
selectContexts = global.Object(),
848
selectResolvers = global.Object(),
849
850
Snapshot = global.Object({
851
byId: _byId,
852
match: match,
853
select: select,
854
isLink: isLink,
855
isEmpty: isEmpty,
856
contains: contains,
857
nthOfType: nthOfType,
858
nthElement: nthElement,
859
getAttribute: getAttribute,
860
hasAttribute: hasAttribute
861
});
862
863
Dom.ACCEPT_NODE = ACCEPT_NODE;
864
865
Dom.byId = byId;
866
Dom.match = match;
867
Dom.first = first;
868
Dom.select = select;
869
Dom.compile = compile;
870
Dom.contains = contains;
871
Dom.configure = configure;
872
Dom.getAttribute = getAttribute;
873
Dom.hasAttribute = hasAttribute;
874
875
Dom.setCache = FN;
876
Dom.shortcuts = FN;
877
Dom.loadResults = FN;
878
Dom.saveResults = FN;
879
880
Dom.emit = emit;
881
Dom.Config = Config;
882
Dom.Snapshot = Snapshot;
883
884
Dom.Operators = Operators;
885
Dom.Selectors = Selectors;
886
887
Dom.Version = version;
888
889
Dom.registerOperator =
890
function(symbol, resolver) {
891
Operators[symbol] || (Operators[symbol] = resolver);
892
};
893
894
Dom.registerSelector =
895
function(name, rexp, func) {
896
Selectors[name] || (Selectors[name] = global.Object({
897
Expression: rexp,
898
Callback: func
899
}));
900
};
901
902
switchContext(doc, true);
903
904
});
905
906