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){
253 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
254 var elem = checkSet[i];
256 var cur = elem.previousSibling;
257 while ( cur && cur.nodeType !== 1 ) {
258 cur = cur.previousSibling;
260 checkSet[i] = typeof part === "string" ?
266 if ( typeof part === "string" ) {
267 Sizzle.filter( part, checkSet, true );
270 ">": function(checkSet, part, isXML){
271 if ( typeof part === "string" && !/\W/.test(part) ) {
272 part = isXML ? part : part.toUpperCase();
274 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
275 var elem = checkSet[i];
277 var parent = elem.parentNode;
278 checkSet[i] = parent.nodeName === part ? parent : false;
282 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
283 var elem = checkSet[i];
285 checkSet[i] = typeof part === "string" ?
287 elem.parentNode === part;
291 if ( typeof part === "string" ) {
292 Sizzle.filter( part, checkSet, true );
296 "": function(checkSet, part, isXML){
297 var doneName = "done" + (done++), checkFn = dirCheck;
299 if ( !part.match(/\W/) ) {
300 var nodeCheck = part = isXML ? part : part.toUpperCase();
301 checkFn = dirNodeCheck;
304 checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
306 "~": function(checkSet, part, isXML){
307 var doneName = "done" + (done++), checkFn = dirCheck;
309 if ( typeof part === "string" && !part.match(/\W/) ) {
310 var nodeCheck = part = isXML ? part : part.toUpperCase();
311 checkFn = dirNodeCheck;
314 checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
318 ID: function(match, context, isXML){
319 if ( typeof context.getElementById !== "undefined" && !isXML ) {
320 var m = context.getElementById(match[1]);
324 NAME: function(match, context, isXML){
325 if ( typeof context.getElementsByName !== "undefined" && !isXML ) {
326 return context.getElementsByName(match[1]);
329 TAG: function(match, context){
330 return context.getElementsByTagName(match[1]);
334 CLASS: function(match, curLoop, inplace, result, not){
335 match = " " + match[1].replace(/\\/g, "") + " ";
338 for ( var i = 0; (elem = curLoop[i]) != null; i++ ) {
340 if ( not ^ (" " + elem.className + " ").indexOf(match) >= 0 ) {
343 } else if ( inplace ) {
352 return match[1].replace(/\\/g, "");
354 TAG: function(match, curLoop){
355 for ( var i = 0; curLoop[i] === false; i++ ){}
356 return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
358 CHILD: function(match){
359 if ( match[1] == "nth" ) {
360 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
361 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
362 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
363 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
365 // calculate the numbers (first)n+(last) including if they are negative
366 match[2] = (test[1] + (test[2] || 1)) - 0;
367 match[3] = test[3] - 0;
370 // TODO: Move to normal caching system
371 match[0] = "done" + (done++);
375 ATTR: function(match){
376 var name = match[1].replace(/\\/g, "");
378 if ( Expr.attrMap[name] ) {
379 match[1] = Expr.attrMap[name];
382 if ( match[2] === "~=" ) {
383 match[4] = " " + match[4] + " ";
388 PSEUDO: function(match, curLoop, inplace, result, not){
389 if ( match[1] === "not" ) {
390 // If we're dealing with a complex expression, or a simple one
391 if ( match[3].match(chunker).length > 1 ) {
392 match[3] = Sizzle(match[3], null, null, curLoop);
394 var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
396 result.push.apply( result, ret );
400 } else if ( Expr.match.POS.test( match[0] ) ) {
406 POS: function(match){
407 match.unshift( true );
412 enabled: function(elem){
413 return elem.disabled === false && elem.type !== "hidden";
415 disabled: function(elem){
416 return elem.disabled === true;
418 checked: function(elem){
419 return elem.checked === true;
421 selected: function(elem){
422 // Accessing this property makes selected-by-default
423 // options in Safari work properly
424 elem.parentNode.selectedIndex;
425 return elem.selected === true;
427 parent: function(elem){
428 return !!elem.firstChild;
430 empty: function(elem){
431 return !elem.firstChild;
433 has: function(elem, i, match){
434 return !!Sizzle( match[3], elem ).length;
436 header: function(elem){
437 return /h\d/i.test( elem.nodeName );
439 text: function(elem){
440 return "text" === elem.type;
442 radio: function(elem){
443 return "radio" === elem.type;
445 checkbox: function(elem){
446 return "checkbox" === elem.type;
448 file: function(elem){
449 return "file" === elem.type;
451 password: function(elem){
452 return "password" === elem.type;
454 submit: function(elem){
455 return "submit" === elem.type;
457 image: function(elem){
458 return "image" === elem.type;
460 reset: function(elem){
461 return "reset" === elem.type;
463 button: function(elem){
464 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
466 input: function(elem){
467 return /input|select|textarea|button/i.test(elem.nodeName);
471 first: function(elem, i){
474 last: function(elem, i, match, array){
475 return i === array.length - 1;
477 even: function(elem, i){
480 odd: function(elem, i){
483 lt: function(elem, i, match){
484 return i < match[3] - 0;
486 gt: function(elem, i, match){
487 return i > match[3] - 0;
489 nth: function(elem, i, match){
490 return match[3] - 0 == i;
492 eq: function(elem, i, match){
493 return match[3] - 0 == i;
497 CHILD: function(elem, match){
498 var type = match[1], parent = elem.parentNode;
500 var doneName = match[0];
502 if ( parent && (!parent[ doneName ] || !elem.nodeIndex) ) {
505 for ( var node = parent.firstChild; node; node = node.nextSibling ) {
506 if ( node.nodeType == 1 ) {
507 node.nodeIndex = count++;
511 parent[ doneName ] = count - 1;
514 if ( type == "first" ) {
515 return elem.nodeIndex == 1;
516 } else if ( type == "last" ) {
517 return elem.nodeIndex == parent[ doneName ];
518 } else if ( type == "only" ) {
519 return parent[ doneName ] == 1;
520 } else if ( type == "nth" ) {
521 var add = false, first = match[2], last = match[3];
523 if ( first == 1 && last == 0 ) {
528 if ( elem.nodeIndex == last ) {
531 } else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
538 PSEUDO: function(elem, match, i, array){
539 var name = match[1], filter = Expr.filters[ name ];
542 return filter( elem, i, match, array );
543 } else if ( name === "contains" ) {
544 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
545 } else if ( name === "not" ) {
548 for ( var i = 0, l = not.length; i < l; i++ ) {
549 if ( not[i] === elem ) {
557 ID: function(elem, match){
558 return elem.nodeType === 1 && elem.getAttribute("id") === match;
560 TAG: function(elem, match){
561 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
563 CLASS: function(elem, match){
564 return match.test( elem.className );
566 ATTR: function(elem, match){
567 var result = Expr.attrHandle[ match[1] ] ? Expr.attrHandle[ match[1] ]( elem ) : elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
568 return result == null ?
573 value.indexOf(check) >= 0 :
575 (" " + value + " ").indexOf(check) >= 0 :
581 value.indexOf(check) === 0 :
583 value.substr(value.length - check.length) === check :
585 value === check || value.substr(0, check.length + 1) === check + "-" :
588 POS: function(elem, match, i, array){
589 var name = match[2], filter = Expr.setFilters[ name ];
592 return filter( elem, i, match, array );
598 var origPOS = Expr.match.POS;
600 for ( var type in Expr.match ) {
601 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
604 var makeArray = function(array, results) {
605 array = Array.prototype.slice.call( array );
608 results.push.apply( results, array );
615 // Perform a simple check to determine if the browser is capable of
616 // converting a NodeList to an array using builtin methods.
618 Array.prototype.slice.call( document.documentElement.childNodes );
620 // Provide a fallback method if it does not work
622 makeArray = function(array, results) {
623 var ret = results || [];
625 if ( toString.call(array) === "[object Array]" ) {
626 Array.prototype.push.apply( ret, array );
628 if ( typeof array.length === "number" ) {
629 for ( var i = 0, l = array.length; i < l; i++ ) {
630 ret.push( array[i] );
633 for ( var i = 0; array[i]; i++ ) {
634 ret.push( array[i] );
643 // Check to see if the browser returns elements by name when
644 // querying by getElementById (and provide a workaround)
646 // We're going to inject a fake input element with a specified name
647 var form = document.createElement("form"),
648 id = "script" + (new Date).getTime();
649 form.innerHTML = "<input name='" + id + "'/>";
651 // Inject it into the root element, check its status, and remove it quickly
652 var root = document.documentElement;
653 root.insertBefore( form, root.firstChild );
655 // The workaround has to do additional checks after a getElementById
656 // Which slows things down for other browsers (hence the branching)
657 if ( !!document.getElementById( id ) ) {
658 Expr.find.ID = function(match, context, isXML){
659 if ( typeof context.getElementById !== "undefined" && !isXML ) {
660 var m = context.getElementById(match[1]);
661 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
665 Expr.filter.ID = function(elem, match){
666 var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
667 return elem.nodeType === 1 && node && node.nodeValue === match;
671 root.removeChild( form );
675 // Check to see if the browser returns only elements
676 // when doing getElementsByTagName("*")
678 // Create a fake element
679 var div = document.createElement("div");
680 div.appendChild( document.createComment("") );
682 // Make sure no comments are found
683 if ( div.getElementsByTagName("*").length > 0 ) {
684 Expr.find.TAG = function(match, context){
685 var results = context.getElementsByTagName(match[1]);
687 // Filter out possible comments
688 if ( match[1] === "*" ) {
691 for ( var i = 0; results[i]; i++ ) {
692 if ( results[i].nodeType === 1 ) {
693 tmp.push( results[i] );
704 // Check to see if an attribute returns normalized href attributes
705 div.innerHTML = "<a href='#'></a>";
706 if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
707 div.firstChild.getAttribute("href") !== "#" ) {
708 Expr.attrHandle.href = function(elem){
709 return elem.getAttribute("href", 2);
714 if ( document.querySelectorAll ) (function(){
715 var oldSizzle = Sizzle, div = document.createElement("div");
716 div.innerHTML = "<p class='TEST'></p>";
718 // Safari can't handle uppercase or unicode characters when
720 if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
724 Sizzle = function(query, context, extra, seed){
725 context = context || document;
727 // Only use querySelectorAll on non-XML documents
728 // (ID selectors don't work in non-HTML documents)
729 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
731 return makeArray( context.querySelectorAll(query), extra );
735 return oldSizzle(query, context, extra, seed);
738 Sizzle.find = oldSizzle.find;
739 Sizzle.filter = oldSizzle.filter;
740 Sizzle.selectors = oldSizzle.selectors;
741 Sizzle.matches = oldSizzle.matches;
744 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
745 var div = document.createElement("div");
746 div.innerHTML = "<div class='test e'></div><div class='test'></div>";
748 // Opera can't find a second classname (in 9.6)
749 if ( div.getElementsByClassName("e").length === 0 )
752 // Safari caches class attributes, doesn't catch changes (in 3.2)
753 div.lastChild.className = "e";
755 if ( div.getElementsByClassName("e").length === 1 )
758 Expr.order.splice(1, 0, "CLASS");
759 Expr.find.CLASS = function(match, context) {
760 return context.getElementsByClassName(match[1]);
764 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
765 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
766 var elem = checkSet[i];
771 while ( elem && elem.nodeType ) {
772 var done = elem[doneName];
774 match = checkSet[ done ];
778 if ( elem.nodeType === 1 && !isXML )
781 if ( elem.nodeName === cur ) {
794 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
795 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
796 var elem = checkSet[i];
801 while ( elem && elem.nodeType ) {
802 if ( elem[doneName] ) {
803 match = checkSet[ elem[doneName] ];
807 if ( elem.nodeType === 1 ) {
811 if ( typeof cur !== "string" ) {
812 if ( elem === cur ) {
817 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
831 var contains = document.compareDocumentPosition ? function(a, b){
832 return a.compareDocumentPosition(b) & 16;
834 return a !== b && (a.contains ? a.contains(b) : true);
837 var isXML = function(elem){
838 return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
839 !!elem.ownerDocument && isXML( elem.ownerDocument );
842 var posProcess = function(selector, context){
843 var tmpSet = [], later = "", match,
844 root = context.nodeType ? [context] : context;
846 // Position selectors must be done after the filter
847 // And so must :not(positional) so we move all PSEUDOs to the end
848 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
850 selector = selector.replace( Expr.match.PSEUDO, "" );
853 selector = Expr.relative[selector] ? selector + "*" : selector;
855 for ( var i = 0, l = root.length; i < l; i++ ) {
856 Sizzle( selector, root[i], tmpSet );
859 return Sizzle.filter( later, tmpSet );
863 jQuery.find = Sizzle;
864 jQuery.filter = Sizzle.filter;
865 jQuery.expr = Sizzle.selectors;
866 jQuery.expr[":"] = jQuery.expr.filters;
868 Sizzle.selectors.filters.hidden = function(elem){
869 return "hidden" === elem.type ||
870 jQuery.css(elem, "display") === "none" ||
871 jQuery.css(elem, "visibility") === "hidden";
874 Sizzle.selectors.filters.visible = function(elem){
875 return "hidden" !== elem.type &&
876 jQuery.css(elem, "display") !== "none" &&
877 jQuery.css(elem, "visibility") !== "hidden";
880 Sizzle.selectors.filters.animated = function(elem){
881 return jQuery.grep(jQuery.timers, function(fn){
882 return elem === fn.elem;
886 jQuery.multiFilter = function( expr, elems, not ) {
888 expr = ":not(" + expr + ")";
891 return Sizzle.matches(expr, elems);
894 jQuery.dir = function( elem, dir ){
895 var matched = [], cur = elem[dir];
896 while ( cur && cur != document ) {
897 if ( cur.nodeType == 1 )
904 jQuery.nth = function(cur, result, dir, elem){
905 result = result || 1;
908 for ( ; cur; cur = cur[dir] )
909 if ( cur.nodeType == 1 && ++num == result )
915 jQuery.sibling = function(n, elem){
918 for ( ; n; n = n.nextSibling ) {
919 if ( n.nodeType == 1 && n != elem )
928 window.Sizzle = Sizzle;