| 1 |
"use strict"; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
var window = Object.create(null, { |
| 15 |
location: { get: function() { |
| 16 |
throw new Error('window.location is not supported.'); |
| 17 |
} } |
| 18 |
}); |
| 19 |
|
| 20 |
var compareDocumentPosition = function(a, b) { |
| 21 |
return a.compareDocumentPosition(b); |
| 22 |
}; |
| 23 |
|
| 24 |
var order = function(a, b) { |
| 25 |
|
| 26 |
return compareDocumentPosition(a, b) & 2 ? 1 : -1; |
| 27 |
}; |
| 28 |
|
| 29 |
var next = function(el) { |
| 30 |
while ((el = el.nextSibling) |
| 31 |
&& el.nodeType !== 1); |
| 32 |
return el; |
| 33 |
}; |
| 34 |
|
| 35 |
var prev = function(el) { |
| 36 |
while ((el = el.previousSibling) |
| 37 |
&& el.nodeType !== 1); |
| 38 |
return el; |
| 39 |
}; |
| 40 |
|
| 41 |
var child = function(el) { |
| 42 |
|
| 43 |
if (el = el.firstChild) { |
| 44 |
while (el.nodeType !== 1 |
| 45 |
&& (el = el.nextSibling)); |
| 46 |
} |
| 47 |
return el; |
| 48 |
}; |
| 49 |
|
| 50 |
var lastChild = function(el) { |
| 51 |
|
| 52 |
if (el = el.lastChild) { |
| 53 |
while (el.nodeType !== 1 |
| 54 |
&& (el = el.previousSibling)); |
| 55 |
} |
| 56 |
return el; |
| 57 |
}; |
| 58 |
|
| 59 |
var parentIsElement = function(n) { |
| 60 |
if (!n.parentNode) { return false; } |
| 61 |
var nodeType = n.parentNode.nodeType; |
| 62 |
|
| 63 |
return nodeType === 1 || nodeType === 9; |
| 64 |
}; |
| 65 |
|
| 66 |
var unquote = function(str) { |
| 67 |
if (!str) return str; |
| 68 |
var ch = str[0]; |
| 69 |
if (ch === '"' || ch === '\'') { |
| 70 |
if (str[str.length-1] === ch) { |
| 71 |
str = str.slice(1, -1); |
| 72 |
} else { |
| 73 |
|
| 74 |
str = str.slice(1); |
| 75 |
} |
| 76 |
return str.replace(rules.str_escape, function(s) { |
| 77 |
var m = /^\\(?:([0-9A-Fa-f]+)|([\r\n\f]+))/.exec(s); |
| 78 |
if (!m) { return s.slice(1); } |
| 79 |
if (m[2]) { return ''; } |
| 80 |
var cp = parseInt(m[1], 16); |
| 81 |
return String.fromCodePoint ? String.fromCodePoint(cp) : |
| 82 |
|
| 83 |
String.fromCharCode(cp); |
| 84 |
}); |
| 85 |
} else if (rules.ident.test(str)) { |
| 86 |
return decodeid(str); |
| 87 |
} else { |
| 88 |
|
| 89 |
return str; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
var decodeid = function(str) { |
| 94 |
return str.replace(rules.escape, function(s) { |
| 95 |
var m = /^\\([0-9A-Fa-f]+)/.exec(s); |
| 96 |
if (!m) { return s[1]; } |
| 97 |
var cp = parseInt(m[1], 16); |
| 98 |
return String.fromCodePoint ? String.fromCodePoint(cp) : |
| 99 |
|
| 100 |
String.fromCharCode(cp); |
| 101 |
}); |
| 102 |
}; |
| 103 |
|
| 104 |
var indexOf = (function() { |
| 105 |
if (Array.prototype.indexOf) { |
| 106 |
return Array.prototype.indexOf; |
| 107 |
} |
| 108 |
return function(obj, item) { |
| 109 |
var i = this.length; |
| 110 |
while (i--) { |
| 111 |
if (this[i] === item) return i; |
| 112 |
} |
| 113 |
return -1; |
| 114 |
}; |
| 115 |
})(); |
| 116 |
|
| 117 |
var makeInside = function(start, end) { |
| 118 |
var regex = rules.inside.source |
| 119 |
.replace(/</g, start) |
| 120 |
.replace(/>/g, end); |
| 121 |
|
| 122 |
return new RegExp(regex); |
| 123 |
}; |
| 124 |
|
| 125 |
var replace = function(regex, name, val) { |
| 126 |
regex = regex.source; |
| 127 |
regex = regex.replace(name, val.source || val); |
| 128 |
return new RegExp(regex); |
| 129 |
}; |
| 130 |
|
| 131 |
var truncateUrl = function(url, num) { |
| 132 |
return url |
| 133 |
.replace(/^(?:\w+:\/\/|\/+)/, '') |
| 134 |
.replace(/(?:\/+|\/*#.*?)$/, '') |
| 135 |
.split('/', num) |
| 136 |
.join('/'); |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
var parseNth = function(param_, test) { |
| 144 |
var param = param_.replace(/\s+/g, '') |
| 145 |
, cap; |
| 146 |
|
| 147 |
if (param === 'even') { |
| 148 |
param = '2n+0'; |
| 149 |
} else if (param === 'odd') { |
| 150 |
param = '2n+1'; |
| 151 |
} else if (param.indexOf('n') === -1) { |
| 152 |
param = '0n' + param; |
| 153 |
} |
| 154 |
|
| 155 |
cap = /^([+-])?(\d+)?n([+-])?(\d+)?$/.exec(param); |
| 156 |
|
| 157 |
return { |
| 158 |
group: cap[1] === '-' |
| 159 |
? -(cap[2] || 1) |
| 160 |
: +(cap[2] || 1), |
| 161 |
offset: cap[4] |
| 162 |
? (cap[3] === '-' ? -cap[4] : +cap[4]) |
| 163 |
: 0 |
| 164 |
}; |
| 165 |
}; |
| 166 |
|
| 167 |
var nth = function(param_, test, last) { |
| 168 |
var param = parseNth(param_) |
| 169 |
, group = param.group |
| 170 |
, offset = param.offset |
| 171 |
, find = !last ? child : lastChild |
| 172 |
, advance = !last ? next : prev; |
| 173 |
|
| 174 |
return function(el) { |
| 175 |
if (!parentIsElement(el)) return; |
| 176 |
|
| 177 |
var rel = find(el.parentNode) |
| 178 |
, pos = 0; |
| 179 |
|
| 180 |
while (rel) { |
| 181 |
if (test(rel, el)) pos++; |
| 182 |
if (rel === el) { |
| 183 |
pos -= offset; |
| 184 |
return group && pos |
| 185 |
? (pos % group) === 0 && (pos < 0 === group < 0) |
| 186 |
: !pos; |
| 187 |
} |
| 188 |
rel = advance(rel); |
| 189 |
} |
| 190 |
}; |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
var selectors = { |
| 198 |
'*': (function() { |
| 199 |
if (false |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
) { |
| 204 |
return function(el) { |
| 205 |
if (el.nodeType === 1) return true; |
| 206 |
}; |
| 207 |
} |
| 208 |
return function() { |
| 209 |
return true; |
| 210 |
}; |
| 211 |
})(), |
| 212 |
'type': function(type) { |
| 213 |
type = type.toLowerCase(); |
| 214 |
return function(el) { |
| 215 |
return el.nodeName.toLowerCase() === type; |
| 216 |
}; |
| 217 |
}, |
| 218 |
'attr': function(key, op, val, i) { |
| 219 |
op = operators[op]; |
| 220 |
return function(el) { |
| 221 |
var attr; |
| 222 |
switch (key) { |
| 223 |
case 'for': |
| 224 |
attr = el.htmlFor; |
| 225 |
break; |
| 226 |
case 'class': |
| 227 |
|
| 228 |
|
| 229 |
attr = el.className; |
| 230 |
if (attr === '' && el.getAttribute('class') == null) { |
| 231 |
attr = null; |
| 232 |
} |
| 233 |
break; |
| 234 |
case 'href': |
| 235 |
case 'src': |
| 236 |
attr = el.getAttribute(key, 2); |
| 237 |
break; |
| 238 |
case 'title': |
| 239 |
|
| 240 |
attr = el.getAttribute('title') || null; |
| 241 |
break; |
| 242 |
|
| 243 |
case 'id': |
| 244 |
case 'lang': |
| 245 |
case 'dir': |
| 246 |
case 'accessKey': |
| 247 |
case 'hidden': |
| 248 |
case 'tabIndex': |
| 249 |
case 'style': |
| 250 |
if (el.getAttribute) { |
| 251 |
attr = el.getAttribute(key); |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
default: |
| 256 |
if (el.hasAttribute && !el.hasAttribute(key)) { |
| 257 |
break; |
| 258 |
} |
| 259 |
attr = el[key] != null |
| 260 |
? el[key] |
| 261 |
: el.getAttribute && el.getAttribute(key); |
| 262 |
break; |
| 263 |
} |
| 264 |
if (attr == null) return; |
| 265 |
attr = attr + ''; |
| 266 |
if (i) { |
| 267 |
attr = attr.toLowerCase(); |
| 268 |
val = val.toLowerCase(); |
| 269 |
} |
| 270 |
return op(attr, val); |
| 271 |
}; |
| 272 |
}, |
| 273 |
':first-child': function(el) { |
| 274 |
return !prev(el) && parentIsElement(el); |
| 275 |
}, |
| 276 |
':last-child': function(el) { |
| 277 |
return !next(el) && parentIsElement(el); |
| 278 |
}, |
| 279 |
':only-child': function(el) { |
| 280 |
return !prev(el) && !next(el) && parentIsElement(el); |
| 281 |
}, |
| 282 |
':nth-child': function(param, last) { |
| 283 |
return nth(param, function() { |
| 284 |
return true; |
| 285 |
}, last); |
| 286 |
}, |
| 287 |
':nth-last-child': function(param) { |
| 288 |
return selectors[':nth-child'](param, true); |
| 289 |
}, |
| 290 |
':root': function(el) { |
| 291 |
return el.ownerDocument.documentElement === el; |
| 292 |
}, |
| 293 |
':empty': function(el) { |
| 294 |
return !el.firstChild; |
| 295 |
}, |
| 296 |
':not': function(sel) { |
| 297 |
var test = compileGroup(sel); |
| 298 |
return function(el) { |
| 299 |
return !test(el); |
| 300 |
}; |
| 301 |
}, |
| 302 |
':first-of-type': function(el) { |
| 303 |
if (!parentIsElement(el)) return; |
| 304 |
var type = el.nodeName; |
| 305 |
|
| 306 |
while (el = prev(el)) { |
| 307 |
if (el.nodeName === type) return; |
| 308 |
} |
| 309 |
return true; |
| 310 |
}, |
| 311 |
':last-of-type': function(el) { |
| 312 |
if (!parentIsElement(el)) return; |
| 313 |
var type = el.nodeName; |
| 314 |
|
| 315 |
while (el = next(el)) { |
| 316 |
if (el.nodeName === type) return; |
| 317 |
} |
| 318 |
return true; |
| 319 |
}, |
| 320 |
':only-of-type': function(el) { |
| 321 |
return selectors[':first-of-type'](el) |
| 322 |
&& selectors[':last-of-type'](el); |
| 323 |
}, |
| 324 |
':nth-of-type': function(param, last) { |
| 325 |
return nth(param, function(rel, el) { |
| 326 |
return rel.nodeName === el.nodeName; |
| 327 |
}, last); |
| 328 |
}, |
| 329 |
':nth-last-of-type': function(param) { |
| 330 |
return selectors[':nth-of-type'](param, true); |
| 331 |
}, |
| 332 |
':checked': function(el) { |
| 333 |
return !!(el.checked || el.selected); |
| 334 |
}, |
| 335 |
':indeterminate': function(el) { |
| 336 |
return !selectors[':checked'](el); |
| 337 |
}, |
| 338 |
':enabled': function(el) { |
| 339 |
return !el.disabled && el.type !== 'hidden'; |
| 340 |
}, |
| 341 |
':disabled': function(el) { |
| 342 |
return !!el.disabled; |
| 343 |
}, |
| 344 |
':target': function(el) { |
| 345 |
return el.id === window.location.hash.substring(1); |
| 346 |
}, |
| 347 |
':focus': function(el) { |
| 348 |
return el === el.ownerDocument.activeElement; |
| 349 |
}, |
| 350 |
':is': function(sel) { |
| 351 |
return compileGroup(sel); |
| 352 |
}, |
| 353 |
|
| 354 |
|
| 355 |
':matches': function(sel) { |
| 356 |
return selectors[':is'](sel); |
| 357 |
}, |
| 358 |
':nth-match': function(param, last) { |
| 359 |
var args = param.split(/\s*,\s*/) |
| 360 |
, arg = args.shift() |
| 361 |
, test = compileGroup(args.join(',')); |
| 362 |
|
| 363 |
return nth(arg, test, last); |
| 364 |
}, |
| 365 |
':nth-last-match': function(param) { |
| 366 |
return selectors[':nth-match'](param, true); |
| 367 |
}, |
| 368 |
':links-here': function(el) { |
| 369 |
return el + '' === window.location + ''; |
| 370 |
}, |
| 371 |
':lang': function(param) { |
| 372 |
return function(el) { |
| 373 |
while (el) { |
| 374 |
if (el.lang) return el.lang.indexOf(param) === 0; |
| 375 |
el = el.parentNode; |
| 376 |
} |
| 377 |
}; |
| 378 |
}, |
| 379 |
':dir': function(param) { |
| 380 |
return function(el) { |
| 381 |
while (el) { |
| 382 |
if (el.dir) return el.dir === param; |
| 383 |
el = el.parentNode; |
| 384 |
} |
| 385 |
}; |
| 386 |
}, |
| 387 |
':scope': function(el, con) { |
| 388 |
var context = con || el.ownerDocument; |
| 389 |
if (context.nodeType === 9) { |
| 390 |
return el === context.documentElement; |
| 391 |
} |
| 392 |
return el === context; |
| 393 |
}, |
| 394 |
':any-link': function(el) { |
| 395 |
return typeof el.href === 'string'; |
| 396 |
}, |
| 397 |
':local-link': function(el) { |
| 398 |
if (el.nodeName) { |
| 399 |
return el.href && el.host === window.location.host; |
| 400 |
} |
| 401 |
var param = +el + 1; |
| 402 |
return function(el) { |
| 403 |
if (!el.href) return; |
| 404 |
|
| 405 |
var url = window.location + '' |
| 406 |
, href = el + ''; |
| 407 |
|
| 408 |
return truncateUrl(url, param) === truncateUrl(href, param); |
| 409 |
}; |
| 410 |
}, |
| 411 |
':default': function(el) { |
| 412 |
return !!el.defaultSelected; |
| 413 |
}, |
| 414 |
':valid': function(el) { |
| 415 |
return el.willValidate || (el.validity && el.validity.valid); |
| 416 |
}, |
| 417 |
':invalid': function(el) { |
| 418 |
return !selectors[':valid'](el); |
| 419 |
}, |
| 420 |
':in-range': function(el) { |
| 421 |
return el.value > el.min && el.value <= el.max; |
| 422 |
}, |
| 423 |
':out-of-range': function(el) { |
| 424 |
return !selectors[':in-range'](el); |
| 425 |
}, |
| 426 |
':required': function(el) { |
| 427 |
return !!el.required; |
| 428 |
}, |
| 429 |
':optional': function(el) { |
| 430 |
return !el.required; |
| 431 |
}, |
| 432 |
':read-only': function(el) { |
| 433 |
if (el.readOnly) return true; |
| 434 |
|
| 435 |
var attr = el.getAttribute('contenteditable') |
| 436 |
, prop = el.contentEditable |
| 437 |
, name = el.nodeName.toLowerCase(); |
| 438 |
|
| 439 |
name = name !== 'input' && name !== 'textarea'; |
| 440 |
|
| 441 |
return (name || el.disabled) && attr == null && prop !== 'true'; |
| 442 |
}, |
| 443 |
':read-write': function(el) { |
| 444 |
return !selectors[':read-only'](el); |
| 445 |
}, |
| 446 |
':hover': function() { |
| 447 |
throw new Error(':hover is not supported.'); |
| 448 |
}, |
| 449 |
':active': function() { |
| 450 |
throw new Error(':active is not supported.'); |
| 451 |
}, |
| 452 |
':link': function() { |
| 453 |
throw new Error(':link is not supported.'); |
| 454 |
}, |
| 455 |
':visited': function() { |
| 456 |
throw new Error(':visited is not supported.'); |
| 457 |
}, |
| 458 |
':column': function() { |
| 459 |
throw new Error(':column is not supported.'); |
| 460 |
}, |
| 461 |
':nth-column': function() { |
| 462 |
throw new Error(':nth-column is not supported.'); |
| 463 |
}, |
| 464 |
':nth-last-column': function() { |
| 465 |
throw new Error(':nth-last-column is not supported.'); |
| 466 |
}, |
| 467 |
':current': function() { |
| 468 |
throw new Error(':current is not supported.'); |
| 469 |
}, |
| 470 |
':past': function() { |
| 471 |
throw new Error(':past is not supported.'); |
| 472 |
}, |
| 473 |
':future': function() { |
| 474 |
throw new Error(':future is not supported.'); |
| 475 |
}, |
| 476 |
|
| 477 |
':contains': function(param) { |
| 478 |
return function(el) { |
| 479 |
var text = el.innerText || el.textContent || el.value || ''; |
| 480 |
return text.indexOf(param) !== -1; |
| 481 |
}; |
| 482 |
}, |
| 483 |
':has': function(param) { |
| 484 |
return function(el) { |
| 485 |
return find(param, el).length > 0; |
| 486 |
}; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
}; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
var operators = { |
| 498 |
'-': function() { |
| 499 |
return true; |
| 500 |
}, |
| 501 |
'=': function(attr, val) { |
| 502 |
return attr === val; |
| 503 |
}, |
| 504 |
'*=': function(attr, val) { |
| 505 |
return attr.indexOf(val) !== -1; |
| 506 |
}, |
| 507 |
'~=': function(attr, val) { |
| 508 |
var i |
| 509 |
, s |
| 510 |
, f |
| 511 |
, l; |
| 512 |
|
| 513 |
for (s = 0; true; s = i + 1) { |
| 514 |
i = attr.indexOf(val, s); |
| 515 |
if (i === -1) return false; |
| 516 |
f = attr[i - 1]; |
| 517 |
l = attr[i + val.length]; |
| 518 |
if ((!f || f === ' ') && (!l || l === ' ')) return true; |
| 519 |
} |
| 520 |
}, |
| 521 |
'|=': function(attr, val) { |
| 522 |
var i = attr.indexOf(val) |
| 523 |
, l; |
| 524 |
|
| 525 |
if (i !== 0) return; |
| 526 |
l = attr[i + val.length]; |
| 527 |
|
| 528 |
return l === '-' || !l; |
| 529 |
}, |
| 530 |
'^=': function(attr, val) { |
| 531 |
return attr.indexOf(val) === 0; |
| 532 |
}, |
| 533 |
'$=': function(attr, val) { |
| 534 |
var i = attr.lastIndexOf(val); |
| 535 |
return i !== -1 && i + val.length === attr.length; |
| 536 |
}, |
| 537 |
|
| 538 |
'!=': function(attr, val) { |
| 539 |
return attr !== val; |
| 540 |
} |
| 541 |
}; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
var combinators = { |
| 548 |
' ': function(test) { |
| 549 |
return function(el) { |
| 550 |
|
| 551 |
while (el = el.parentNode) { |
| 552 |
if (test(el)) return el; |
| 553 |
} |
| 554 |
}; |
| 555 |
}, |
| 556 |
'>': function(test) { |
| 557 |
return function(el) { |
| 558 |
|
| 559 |
if (el = el.parentNode) { |
| 560 |
return test(el) && el; |
| 561 |
} |
| 562 |
}; |
| 563 |
}, |
| 564 |
'+': function(test) { |
| 565 |
return function(el) { |
| 566 |
|
| 567 |
if (el = prev(el)) { |
| 568 |
return test(el) && el; |
| 569 |
} |
| 570 |
}; |
| 571 |
}, |
| 572 |
'~': function(test) { |
| 573 |
return function(el) { |
| 574 |
|
| 575 |
while (el = prev(el)) { |
| 576 |
if (test(el)) return el; |
| 577 |
} |
| 578 |
}; |
| 579 |
}, |
| 580 |
'noop': function(test) { |
| 581 |
return function(el) { |
| 582 |
return test(el) && el; |
| 583 |
}; |
| 584 |
}, |
| 585 |
'ref': function(test, name) { |
| 586 |
var node; |
| 587 |
|
| 588 |
function ref(el) { |
| 589 |
var doc = el.ownerDocument |
| 590 |
, nodes = doc.getElementsByTagName('*') |
| 591 |
, i = nodes.length; |
| 592 |
|
| 593 |
while (i--) { |
| 594 |
node = nodes[i]; |
| 595 |
if (ref.test(el)) { |
| 596 |
node = null; |
| 597 |
return true; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
node = null; |
| 602 |
} |
| 603 |
|
| 604 |
ref.combinator = function(el) { |
| 605 |
if (!node || !node.getAttribute) return; |
| 606 |
|
| 607 |
var attr = node.getAttribute(name) || ''; |
| 608 |
if (attr[0] === '#') attr = attr.substring(1); |
| 609 |
|
| 610 |
if (attr === el.id && test(node)) { |
| 611 |
return node; |
| 612 |
} |
| 613 |
}; |
| 614 |
|
| 615 |
return ref; |
| 616 |
} |
| 617 |
}; |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
var rules = { |
| 624 |
escape: /\\(?:[^0-9A-Fa-f\r\n]|[0-9A-Fa-f]{1,6}[\r\n\t ]?)/g, |
| 625 |
str_escape: /(escape)|\\(\n|\r\n?|\f)/g, |
| 626 |
nonascii: /[\u00A0-\uFFFF]/, |
| 627 |
cssid: /(?:(?!-?[0-9])(?:escape|nonascii|[-_a-zA-Z0-9])+)/, |
| 628 |
qname: /^ *(cssid|\*)/, |
| 629 |
simple: /^(?:([.#]cssid)|pseudo|attr)/, |
| 630 |
ref: /^ *\/(cssid)\/ */, |
| 631 |
combinator: /^(?: +([^ \w*.#\\]) +|( )+|([^ \w*.#\\]))(?! *$)/, |
| 632 |
attr: /^\[(cssid)(?:([^\w]?=)(inside))?\]/, |
| 633 |
pseudo: /^(:cssid)(?:\((inside)\))?/, |
| 634 |
inside: /(?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|<[^"'>]*>|\\["'>]|[^"'>])*/, |
| 635 |
ident: /^(cssid)$/ |
| 636 |
}; |
| 637 |
|
| 638 |
rules.cssid = replace(rules.cssid, 'nonascii', rules.nonascii); |
| 639 |
rules.cssid = replace(rules.cssid, 'escape', rules.escape); |
| 640 |
rules.qname = replace(rules.qname, 'cssid', rules.cssid); |
| 641 |
rules.simple = replace(rules.simple, 'cssid', rules.cssid); |
| 642 |
rules.ref = replace(rules.ref, 'cssid', rules.cssid); |
| 643 |
rules.attr = replace(rules.attr, 'cssid', rules.cssid); |
| 644 |
rules.pseudo = replace(rules.pseudo, 'cssid', rules.cssid); |
| 645 |
rules.inside = replace(rules.inside, '[^"\'>]*', rules.inside); |
| 646 |
rules.attr = replace(rules.attr, 'inside', makeInside('\\[', '\\]')); |
| 647 |
rules.pseudo = replace(rules.pseudo, 'inside', makeInside('\\(', '\\)')); |
| 648 |
rules.simple = replace(rules.simple, 'pseudo', rules.pseudo); |
| 649 |
rules.simple = replace(rules.simple, 'attr', rules.attr); |
| 650 |
rules.ident = replace(rules.ident, 'cssid', rules.cssid); |
| 651 |
rules.str_escape = replace(rules.str_escape, 'escape', rules.escape); |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
var compile = function(sel_) { |
| 658 |
var sel = sel_.replace(/^\s+|\s+$/g, '') |
| 659 |
, test |
| 660 |
, filter = [] |
| 661 |
, buff = [] |
| 662 |
, subject |
| 663 |
, qname |
| 664 |
, cap |
| 665 |
, op |
| 666 |
, ref; |
| 667 |
|
| 668 |
|
| 669 |
while (sel) { |
| 670 |
if (cap = rules.qname.exec(sel)) { |
| 671 |
sel = sel.substring(cap[0].length); |
| 672 |
qname = decodeid(cap[1]); |
| 673 |
buff.push(tok(qname, true)); |
| 674 |
} else if (cap = rules.simple.exec(sel)) { |
| 675 |
sel = sel.substring(cap[0].length); |
| 676 |
qname = '*'; |
| 677 |
buff.push(tok(qname, true)); |
| 678 |
buff.push(tok(cap)); |
| 679 |
} else { |
| 680 |
throw new SyntaxError('Invalid selector.'); |
| 681 |
} |
| 682 |
|
| 683 |
while (cap = rules.simple.exec(sel)) { |
| 684 |
sel = sel.substring(cap[0].length); |
| 685 |
buff.push(tok(cap)); |
| 686 |
} |
| 687 |
|
| 688 |
if (sel[0] === '!') { |
| 689 |
sel = sel.substring(1); |
| 690 |
subject = makeSubject(); |
| 691 |
subject.qname = qname; |
| 692 |
buff.push(subject.simple); |
| 693 |
} |
| 694 |
|
| 695 |
if (cap = rules.ref.exec(sel)) { |
| 696 |
sel = sel.substring(cap[0].length); |
| 697 |
ref = combinators.ref(makeSimple(buff), decodeid(cap[1])); |
| 698 |
filter.push(ref.combinator); |
| 699 |
buff = []; |
| 700 |
continue; |
| 701 |
} |
| 702 |
|
| 703 |
if (cap = rules.combinator.exec(sel)) { |
| 704 |
sel = sel.substring(cap[0].length); |
| 705 |
op = cap[1] || cap[2] || cap[3]; |
| 706 |
if (op === ',') { |
| 707 |
filter.push(combinators.noop(makeSimple(buff))); |
| 708 |
break; |
| 709 |
} |
| 710 |
} else { |
| 711 |
op = 'noop'; |
| 712 |
} |
| 713 |
|
| 714 |
if (!combinators[op]) { throw new SyntaxError('Bad combinator.'); } |
| 715 |
filter.push(combinators[op](makeSimple(buff))); |
| 716 |
buff = []; |
| 717 |
} |
| 718 |
|
| 719 |
test = makeTest(filter); |
| 720 |
test.qname = qname; |
| 721 |
test.sel = sel; |
| 722 |
|
| 723 |
if (subject) { |
| 724 |
subject.lname = test.qname; |
| 725 |
|
| 726 |
subject.test = test; |
| 727 |
subject.qname = subject.qname; |
| 728 |
subject.sel = test.sel; |
| 729 |
test = subject; |
| 730 |
} |
| 731 |
|
| 732 |
if (ref) { |
| 733 |
ref.test = test; |
| 734 |
ref.qname = test.qname; |
| 735 |
ref.sel = test.sel; |
| 736 |
test = ref; |
| 737 |
} |
| 738 |
|
| 739 |
return test; |
| 740 |
}; |
| 741 |
|
| 742 |
var tok = function(cap, qname) { |
| 743 |
|
| 744 |
if (qname) { |
| 745 |
return cap === '*' |
| 746 |
? selectors['*'] |
| 747 |
: selectors.type(cap); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
if (cap[1]) { |
| 752 |
return cap[1][0] === '.' |
| 753 |
|
| 754 |
? selectors.attr('class', '~=', decodeid(cap[1].substring(1)), false) |
| 755 |
: selectors.attr('id', '=', decodeid(cap[1].substring(1)), false); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
if (cap[2]) { |
| 761 |
return cap[3] |
| 762 |
? selectors[decodeid(cap[2])](unquote(cap[3])) |
| 763 |
: selectors[decodeid(cap[2])]; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
if (cap[4]) { |
| 770 |
var value = cap[6]; |
| 771 |
var i = /["'\s]\s*I$/i.test(value); |
| 772 |
if (i) { |
| 773 |
value = value.replace(/\s*I$/i, ''); |
| 774 |
} |
| 775 |
return selectors.attr(decodeid(cap[4]), cap[5] || '-', unquote(value), i); |
| 776 |
} |
| 777 |
|
| 778 |
throw new SyntaxError('Unknown Selector.'); |
| 779 |
}; |
| 780 |
|
| 781 |
var makeSimple = function(func) { |
| 782 |
var l = func.length |
| 783 |
, i; |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
if (l < 2) return func[0]; |
| 788 |
|
| 789 |
return function(el) { |
| 790 |
if (!el) return; |
| 791 |
for (i = 0; i < l; i++) { |
| 792 |
if (!func[i](el)) return; |
| 793 |
} |
| 794 |
return true; |
| 795 |
}; |
| 796 |
}; |
| 797 |
|
| 798 |
var makeTest = function(func) { |
| 799 |
if (func.length < 2) { |
| 800 |
return function(el) { |
| 801 |
return !!func[0](el); |
| 802 |
}; |
| 803 |
} |
| 804 |
return function(el) { |
| 805 |
var i = func.length; |
| 806 |
while (i--) { |
| 807 |
if (!(el = func[i](el))) return; |
| 808 |
} |
| 809 |
return true; |
| 810 |
}; |
| 811 |
}; |
| 812 |
|
| 813 |
var makeSubject = function() { |
| 814 |
var target; |
| 815 |
|
| 816 |
function subject(el) { |
| 817 |
var node = el.ownerDocument |
| 818 |
, scope = node.getElementsByTagName(subject.lname) |
| 819 |
, i = scope.length; |
| 820 |
|
| 821 |
while (i--) { |
| 822 |
if (subject.test(scope[i]) && target === el) { |
| 823 |
target = null; |
| 824 |
return true; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
target = null; |
| 829 |
} |
| 830 |
|
| 831 |
subject.simple = function(el) { |
| 832 |
target = el; |
| 833 |
return true; |
| 834 |
}; |
| 835 |
|
| 836 |
return subject; |
| 837 |
}; |
| 838 |
|
| 839 |
var compileGroup = function(sel) { |
| 840 |
var test = compile(sel) |
| 841 |
, tests = [ test ]; |
| 842 |
|
| 843 |
while (test.sel) { |
| 844 |
test = compile(test.sel); |
| 845 |
tests.push(test); |
| 846 |
} |
| 847 |
|
| 848 |
if (tests.length < 2) return test; |
| 849 |
|
| 850 |
return function(el) { |
| 851 |
var l = tests.length |
| 852 |
, i = 0; |
| 853 |
|
| 854 |
for (; i < l; i++) { |
| 855 |
if (tests[i](el)) return true; |
| 856 |
} |
| 857 |
}; |
| 858 |
}; |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
var find = function(sel, node) { |
| 865 |
var results = [] |
| 866 |
, test = compile(sel) |
| 867 |
, scope = node.getElementsByTagName(test.qname) |
| 868 |
, i = 0 |
| 869 |
, el; |
| 870 |
|
| 871 |
|
| 872 |
while (el = scope[i++]) { |
| 873 |
if (test(el)) results.push(el); |
| 874 |
} |
| 875 |
|
| 876 |
if (test.sel) { |
| 877 |
while (test.sel) { |
| 878 |
test = compile(test.sel); |
| 879 |
scope = node.getElementsByTagName(test.qname); |
| 880 |
i = 0; |
| 881 |
|
| 882 |
while (el = scope[i++]) { |
| 883 |
if (test(el) && indexOf.call(results, el) === -1) { |
| 884 |
results.push(el); |
| 885 |
} |
| 886 |
} |
| 887 |
} |
| 888 |
results.sort(order); |
| 889 |
} |
| 890 |
|
| 891 |
return results; |
| 892 |
}; |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
module.exports = exports = function(sel, context) { |
| 899 |
|
| 900 |
var id, r; |
| 901 |
if (context.nodeType !== 11 && sel.indexOf(' ') === -1) { |
| 902 |
if (sel[0] === '#' && context.rooted && /^#[A-Z_][-A-Z0-9_]*$/i.test(sel)) { |
| 903 |
if (context.doc._hasMultipleElementsWithId) { |
| 904 |
id = sel.substring(1); |
| 905 |
if (!context.doc._hasMultipleElementsWithId(id)) { |
| 906 |
r = context.doc.getElementById(id); |
| 907 |
return r ? [r] : []; |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
if (sel[0] === '.' && /^\.\w+$/.test(sel)) { |
| 912 |
return context.getElementsByClassName(sel.substring(1)); |
| 913 |
} |
| 914 |
if (/^\w+$/.test(sel)) { |
| 915 |
return context.getElementsByTagName(sel); |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
return find(sel, context); |
| 920 |
}; |
| 921 |
|
| 922 |
exports.selectors = selectors; |
| 923 |
exports.operators = operators; |
| 924 |
exports.combinators = combinators; |
| 925 |
|
| 926 |
exports.matches = function(el, sel) { |
| 927 |
var test = { sel: sel }; |
| 928 |
do { |
| 929 |
test = compile(test.sel); |
| 930 |
if (test(el)) { return true; } |
| 931 |
} while (test.sel); |
| 932 |
return false; |
| 933 |
}; |