2 * Sizzle CSS Selector Engine - v0.9.3
3 * Copyright 2009, The Dojo Foundation
4 * Released under the MIT, BSD, and GPL Licenses.
5 * More information: http://sizzlejs.com/
9 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g,
11 toString = Object.prototype.toString;
13 var Sizzle = function(selector, context, results, seed) {
14 results = results || [];
15 context = context || document;
17 if ( context.nodeType !== 1 && context.nodeType !== 9 )
20 if ( !selector || typeof selector !== "string" ) {
24 var parts = [], m, set, checkSet, check, mode, extra, prune = true;
26 // Reset the position of the chunker regexp (start from head)
27 chunker.lastIndex = 0;
29 while ( (m = chunker.exec(selector)) !== null ) {
33 extra = RegExp.rightContext;
38 if ( parts.length > 1 && origPOS.exec( selector ) ) {
39 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
40 set = posProcess( parts[0] + parts[1], context );
42 set = Expr.relative[ parts[0] ] ?
44 Sizzle( parts.shift(), context );
46 while ( parts.length ) {
47 selector = parts.shift();
49 if ( Expr.relative[ selector ] )
50 selector += parts.shift();
52 set = posProcess( selector, set );
57 { expr: parts.pop(), set: makeArray(seed) } :
58 Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) );
59 set = Sizzle.filter( ret.expr, ret.set );
61 if ( parts.length > 0 ) {
62 checkSet = makeArray(set);
67 while ( parts.length ) {
68 var cur = parts.pop(), pop = cur;
70 if ( !Expr.relative[ cur ] ) {
80 Expr.relative[ cur ]( checkSet, pop, isXML(context) );
89 throw "Syntax error, unrecognized expression: " + (cur || selector);
92 if ( toString.call(checkSet) === "[object Array]" ) {
94 results.push.apply( results, checkSet );
95 } else if ( context.nodeType === 1 ) {
96 for ( var i = 0; checkSet[i] != null; i++ ) {
97 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
98 results.push( set[i] );
102 for ( var i = 0; checkSet[i] != null; i++ ) {
103 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
104 results.push( set[i] );
109 makeArray( checkSet, results );
113 Sizzle( extra, context, results, seed );
119 Sizzle.matches = function(expr, set){
120 return Sizzle(expr, null, null, set);
123 Sizzle.find = function(expr, context, isXML){
130 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
131 var type = Expr.order[i], match;
133 if ( (match = Expr.match[ type ].exec( expr )) ) {
134 var left = RegExp.leftContext;
136 if ( left.substr( left.length - 1 ) !== "\\" ) {
137 match[1] = (match[1] || "").replace(/\\/g, "");
138 set = Expr.find[ type ]( match, context, isXML );
140 expr = expr.replace( Expr.match[ type ], "" );
148 set = context.getElementsByTagName("*");
151 return {set: set, expr: expr};
154 Sizzle.filter = function(expr, set, inplace, not){
155 var old = expr, result = [], curLoop = set, match, anyFound;
157 while ( expr && set.length ) {
158 for ( var type in Expr.filter ) {
159 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
160 var filter = Expr.filter[ type ], found, item;
163 if ( curLoop == result ) {
167 if ( Expr.preFilter[ type ] ) {
168 match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not );
171 anyFound = found = true;
172 } else if ( match === true ) {
178 for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
180 found = filter( item, match, i, curLoop );
181 var pass = not ^ !!found;
183 if ( inplace && found != null ) {
197 if ( found !== undefined ) {
202 expr = expr.replace( Expr.match[ type ], "" );
213 expr = expr.replace(/\s*,\s*/, "");
215 // Improper expression
217 if ( anyFound == null ) {
218 throw "Syntax error, unrecognized expression: " + expr;
230 var Expr = Sizzle.selectors = {
231 order: [ "ID", "NAME", "TAG" ],
233 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
234 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
235 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
236 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
237 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
238 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
239 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
240 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
243 "class": "className",
247 href: function(elem){
248 return elem.getAttribute("href");
252 "+": function(checkSet, part, isXML){
253 var isPartStr = typeof part === "string",
254 isTag = isPartStr && !/\W/.test(part),
255 isPartStrNotTag = isPartStr && !isTag;
256 if ( isTag && !isXML ) part = part.toUpperCase();
257 for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
258 if ( elem = checkSet[i] ) {
259 while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {};
260 checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
261 elem : elem === part;
264 if (isPartStrNotTag) {
265 Sizzle.filter( part, checkSet, true );
268 ">": function(checkSet, part, isXML){
269 if ( typeof part === "string" && !/\W/.test(part) ) {
270 part = isXML ? part : part.toUpperCase();
272 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
273 var elem = checkSet[i];
275 var parent = elem.parentNode;
276 checkSet[i] = parent.nodeName === part ? parent : false;
280 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
281 var elem = checkSet[i];
283 checkSet[i] = typeof part === "string" ?
285 elem.parentNode === part;
289 if ( typeof part === "string" ) {
290 Sizzle.filter( part, checkSet, true );
294 "": function(checkSet, part, isXML){
295 var doneName = done++, checkFn = dirCheck;
297 if ( !part.match(/\W/) ) {
298 var nodeCheck = part = isXML ? part : part.toUpperCase();
299 checkFn = dirNodeCheck;
302 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
304 "~": function(checkSet, part, isXML){
305 var doneName = done++, checkFn = dirCheck;
307 if ( typeof part === "string" && !part.match(/\W/) ) {
308 var nodeCheck = part = isXML ? part : part.toUpperCase();
309 checkFn = dirNodeCheck;
312 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
316 ID: function(match, context, isXML){
317 if ( typeof context.getElementById !== "undefined" && !isXML ) {
318 var m = context.getElementById(match[1]);
322 NAME: function(match, context, isXML){
323 if ( typeof context.getElementsByName !== "undefined" && !isXML ) {
324 return context.getElementsByName(match[1]);
327 TAG: function(match, context){
328 return context.getElementsByTagName(match[1]);
332 CLASS: function(match, curLoop, inplace, result, not){
333 match = " " + match[1].replace(/\\/g, "") + " ";
335 for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
337 if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
340 } else if ( inplace ) {
349 return match[1].replace(/\\/g, "");
351 TAG: function(match, curLoop){
352 for ( var i = 0; curLoop[i] === false; i++ ){}
353 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
355 CHILD: function(match){
356 if ( match[1] == "nth" ) {
357 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
358 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
359 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
360 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
362 // calculate the numbers (first)n+(last) including if they are negative
363 match[2] = (test[1] + (test[2] || 1)) - 0;
364 match[3] = test[3] - 0;
367 // TODO: Move to normal caching system
372 ATTR: function(match){
373 var name = match[1].replace(/\\/g, "");
375 if ( Expr.attrMap[name] ) {
376 match[1] = Expr.attrMap[name];
379 if ( match[2] === "~=" ) {
380 match[4] = " " + match[4] + " ";
385 PSEUDO: function(match, curLoop, inplace, result, not){
386 if ( match[1] === "not" ) {
387 // If we're dealing with a complex expression, or a simple one
388 if ( match[3].match(chunker).length > 1 ) {
389 match[3] = Sizzle(match[3], null, null, curLoop);
391 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
393 result.push.apply( result, ret );
397 } else if ( Expr.match.POS.test( match[0] ) ) {
403 POS: function(match){
404 match.unshift( true );
409 enabled: function(elem){
410 return elem.disabled === false && elem.type !== "hidden";
412 disabled: function(elem){
413 return elem.disabled === true;
415 checked: function(elem){
416 return elem.checked === true;
418 selected: function(elem){
419 // Accessing this property makes selected-by-default
420 // options in Safari work properly
421 elem.parentNode.selectedIndex;
422 return elem.selected === true;
424 parent: function(elem){
425 return !!elem.firstChild;
427 empty: function(elem){
428 return !elem.firstChild;
430 has: function(elem, i, match){
431 return !!Sizzle( match[3], elem ).length;
433 header: function(elem){
434 return /h\d/i.test( elem.nodeName );
436 text: function(elem){
437 return "text" === elem.type;
439 radio: function(elem){
440 return "radio" === elem.type;
442 checkbox: function(elem){
443 return "checkbox" === elem.type;
445 file: function(elem){
446 return "file" === elem.type;
448 password: function(elem){
449 return "password" === elem.type;
451 submit: function(elem){
452 return "submit" === elem.type;
454 image: function(elem){
455 return "image" === elem.type;
457 reset: function(elem){
458 return "reset" === elem.type;
460 button: function(elem){
461 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
463 input: function(elem){
464 return /input|select|textarea|button/i.test(elem.nodeName);
468 first: function(elem, i){
471 last: function(elem, i, match, array){
472 return i === array.length - 1;
474 even: function(elem, i){
477 odd: function(elem, i){
480 lt: function(elem, i, match){
481 return i < match[3] - 0;
483 gt: function(elem, i, match){
484 return i > match[3] - 0;
486 nth: function(elem, i, match){
487 return match[3] - 0 == i;
489 eq: function(elem, i, match){
490 return match[3] - 0 == i;
494 CHILD: function(elem, match){
495 var type = match[1], node = elem;
499 while (node = node.previousSibling) {
500 if ( node.nodeType === 1 ) return false;
502 if ( type == 'first') return true;
505 while (node = node.nextSibling) {
506 if ( node.nodeType === 1 ) return false;
510 var first = match[2], last = match[3];
512 if ( first == 1 && last == 0 ) {
516 var doneName = match[0],
517 parent = elem.parentNode;
519 if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
521 for ( node = parent.firstChild; node; node = node.nextSibling ) {
522 if ( node.nodeType === 1 ) {
523 node.nodeIndex = ++count;
526 parent.sizcache = doneName;
529 var diff = elem.nodeIndex - last;
533 return ( diff % first == 0 && diff / first >= 0 );
537 PSEUDO: function(elem, match, i, array){
538 var name = match[1], filter = Expr.filters[ name ];
541 return filter( elem, i, match, array );
542 } else if ( name === "contains" ) {
543 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
544 } else if ( name === "not" ) {
547 for ( var i = 0, l = not.length; i < l; i++ ) {
548 if ( not[i] === elem ) {
556 ID: function(elem, match){
557 return elem.nodeType === 1 && elem.getAttribute("id") === match;
559 TAG: function(elem, match){
560 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
562 CLASS: function(elem, match){
563 return match.test( elem.className );
565 ATTR: function(elem, match){
567 result = Expr.attrHandle[ name ] ?
568 Expr.attrHandle[ name ]( elem ) :
569 elem[ name ] != null ?
571 elem.getAttribute( name ),
576 return result == null ?
581 value.indexOf(check) >= 0 :
583 (" " + value + " ").indexOf(check) >= 0 :
585 value && result !== false :
589 value.indexOf(check) === 0 :
591 value.substr(value.length - check.length) === check :
593 value === check || value.substr(0, check.length + 1) === check + "-" :
596 POS: function(elem, match, i, array){
597 var name = match[2], filter = Expr.setFilters[ name ];
600 return filter( elem, i, match, array );
606 var origPOS = Expr.match.POS;
608 for ( var type in Expr.match ) {
609 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
612 var makeArray = function(array, results) {
613 array = Array.prototype.slice.call( array );
616 results.push.apply( results, array );
623 // Perform a simple check to determine if the browser is capable of
624 // converting a NodeList to an array using builtin methods.
626 Array.prototype.slice.call( document.documentElement.childNodes );
628 // Provide a fallback method if it does not work
630 makeArray = function(array, results) {
631 var ret = results || [];
633 if ( toString.call(array) === "[object Array]" ) {
634 Array.prototype.push.apply( ret, array );
636 if ( typeof array.length === "number" ) {
637 for ( var i = 0, l = array.length; i < l; i++ ) {
638 ret.push( array[i] );
641 for ( var i = 0; array[i]; i++ ) {
642 ret.push( array[i] );
651 // Check to see if the browser returns elements by name when
652 // querying by getElementById (and provide a workaround)
654 // We're going to inject a fake input element with a specified name
655 var form = document.createElement("form"),
656 id = "script" + (new Date).getTime();
657 form.innerHTML = "<input name='" + id + "'/>";
659 // Inject it into the root element, check its status, and remove it quickly
660 var root = document.documentElement;
661 root.insertBefore( form, root.firstChild );
663 // The workaround has to do additional checks after a getElementById
664 // Which slows things down for other browsers (hence the branching)
665 if ( !!document.getElementById( id ) ) {
666 Expr.find.ID = function(match, context, isXML){
667 if ( typeof context.getElementById !== "undefined" && !isXML ) {
668 var m = context.getElementById(match[1]);
669 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
673 Expr.filter.ID = function(elem, match){
674 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
675 return elem.nodeType === 1 && node && node.nodeValue === match;
679 root.removeChild( form );
683 // Check to see if the browser returns only elements
684 // when doing getElementsByTagName("*")
686 // Create a fake element
687 var div = document.createElement("div");
688 div.appendChild( document.createComment("") );
690 // Make sure no comments are found
691 if ( div.getElementsByTagName("*").length > 0 ) {
692 Expr.find.TAG = function(match, context){
693 var results = context.getElementsByTagName(match[1]);
695 // Filter out possible comments
696 if ( match[1] === "*" ) {
699 for ( var i = 0; results[i]; i++ ) {
700 if ( results[i].nodeType === 1 ) {
701 tmp.push( results[i] );
712 // Check to see if an attribute returns normalized href attributes
713 div.innerHTML = "<a href='#'></a>";
714 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
715 div.firstChild.getAttribute("href") !== "#" ) {
716 Expr.attrHandle.href = function(elem){
717 return elem.getAttribute("href", 2);
722 if ( document.querySelectorAll ) (function(){
723 var oldSizzle = Sizzle, div = document.createElement("div");
724 div.innerHTML = "<p class='TEST'></p>";
726 // Safari can't handle uppercase or unicode characters when
728 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
732 Sizzle = function(query, context, extra, seed){
733 context = context || document;
735 // Only use querySelectorAll on non-XML documents
736 // (ID selectors don't work in non-HTML documents)
737 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
739 return makeArray( context.querySelectorAll(query), extra );
743 return oldSizzle(query, context, extra, seed);
746 Sizzle.find = oldSizzle.find;
747 Sizzle.filter = oldSizzle.filter;
748 Sizzle.selectors = oldSizzle.selectors;
749 Sizzle.matches = oldSizzle.matches;
752 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
753 var div = document.createElement("div");
754 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
756 // Opera can't find a second classname (in 9.6)
757 if ( div.getElementsByClassName("e").length === 0 )
760 // Safari caches class attributes, doesn't catch changes (in 3.2)
761 div.lastChild.className = "e";
763 if ( div.getElementsByClassName("e").length === 1 )
766 Expr.order.splice(1, 0, "CLASS");
767 Expr.find.CLASS = function(match, context) {
768 return context.getElementsByClassName(match[1]);
772 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
773 var sibDir = dir == "previousSibling" && !isXML;
774 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
775 var elem = checkSet[i];
777 if ( sibDir && elem.nodeType === 1 ){
778 elem.sizcache = doneName;
785 if ( elem.sizcache === doneName ) {
786 match = checkSet[elem.sizset];
790 if ( elem.nodeType === 1 && !isXML ){
791 elem.sizcache = doneName;
795 if ( elem.nodeName === cur ) {
808 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
809 var sibDir = dir == "previousSibling" && !isXML;
810 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
811 var elem = checkSet[i];
813 if ( sibDir && elem.nodeType === 1 ) {
814 elem.sizcache = doneName;
821 if ( elem.sizcache === doneName ) {
822 match = checkSet[elem.sizset];
826 if ( elem.nodeType === 1 ) {
828 elem.sizcache = doneName;
831 if ( typeof cur !== "string" ) {
832 if ( elem === cur ) {
837 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
851 var contains = document.compareDocumentPosition ? function(a, b){
852 return a.compareDocumentPosition(b) & 16;
854 return a !== b && (a.contains ? a.contains(b) : true);
857 var isXML = function(elem){
858 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
859 !!elem.ownerDocument && isXML( elem.ownerDocument );
862 var posProcess = function(selector, context){
863 var tmpSet = [], later = "", match,
864 root = context.nodeType ? [context] : context;
866 // Position selectors must be done after the filter
867 // And so must :not(positional) so we move all PSEUDOs to the end
868 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
870 selector = selector.replace( Expr.match.PSEUDO, "" );
873 selector = Expr.relative[selector] ? selector + "*" : selector;
875 for ( var i = 0, l = root.length; i < l; i++ ) {
876 Sizzle( selector, root[i], tmpSet );
879 return Sizzle.filter( later, tmpSet );
883 jQuery.find = Sizzle;
884 jQuery.filter = Sizzle.filter;
885 jQuery.expr = Sizzle.selectors;
886 jQuery.expr[":"] = jQuery.expr.filters;
888 Sizzle.selectors.filters.hidden = function(elem){
889 return "hidden" === elem.type ||
890 jQuery.css(elem, "display") === "none" ||
891 jQuery.css(elem, "visibility") === "hidden";
894 Sizzle.selectors.filters.visible = function(elem){
895 return "hidden" !== elem.type &&
896 jQuery.css(elem, "display") !== "none" &&
897 jQuery.css(elem, "visibility") !== "hidden";
900 Sizzle.selectors.filters.animated = function(elem){
901 return jQuery.grep(jQuery.timers, function(fn){
902 return elem === fn.elem;
906 jQuery.multiFilter = function( expr, elems, not ) {
908 expr = ":not(" + expr + ")";
911 return Sizzle.matches(expr, elems);
914 jQuery.dir = function( elem, dir ){
915 var matched = [], cur = elem[dir];
916 while ( cur && cur != document ) {
917 if ( cur.nodeType == 1 )
924 jQuery.nth = function(cur, result, dir, elem){
925 result = result || 1;
928 for ( ; cur; cur = cur[dir] )
929 if ( cur.nodeType == 1 && ++num == result )
935 jQuery.sibling = function(n, elem){
938 for ( ; n; n = n.nextSibling ) {
939 if ( n.nodeType == 1 && n != elem )
948 window.Sizzle = Sizzle;