2 * Sizzle CSS Selector Engine - v0.9
3 * Copyright 2009, John Resig (http://ejohn.org/)
4 * released under the MIT License
8 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]+\]|[^[\]]+)+\]|\\.|[^ >+~,(\[]+)+|[>+~])(\s*,\s*)?/g;
12 var Sizzle = function(selector, context, results, seed) {
13 results = results || [];
14 context = context || document;
16 if ( context.nodeType !== 1 && context.nodeType !== 9 )
19 if ( !selector || typeof selector !== "string" ) {
23 var parts = [], m, set, checkSet, check, mode, extra;
25 // Reset the position of the chunker regexp (start from head)
26 chunker.lastIndex = 0;
28 while ( (m = chunker.exec(selector)) !== null ) {
32 extra = RegExp.rightContext;
37 if ( parts.length > 1 && Expr.match.POS.exec( selector ) ) {
38 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
39 var later = "", match;
41 // Position selectors must be done after the filter
42 while ( (match = Expr.match.POS.exec( selector )) ) {
44 selector = selector.replace( Expr.match.POS, "" );
47 set = Sizzle.filter( later, Sizzle( selector, context ) );
49 set = Expr.relative[ parts[0] ] ?
51 Sizzle( parts.shift(), context );
53 while ( parts.length ) {
56 selector = parts.shift();
57 if ( Expr.relative[ selector ] )
58 selector += parts.shift();
60 for ( var i = 0, l = set.length; i < l; i++ ) {
61 Sizzle( selector, set[i], tmpSet );
69 { expr: parts.pop(), set: makeArray(seed) } :
70 Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context );
71 set = Sizzle.filter( ret.expr, ret.set );
73 if ( parts.length > 0 ) {
74 checkSet = makeArray(set);
77 while ( parts.length ) {
78 var cur = parts.pop(), pop = cur;
80 if ( !Expr.relative[ cur ] ) {
90 Expr.relative[ cur ]( checkSet, pop );
99 throw "Syntax error, unrecognized expression: " + (cur || selector);
102 if ( checkSet instanceof Array ) {
103 if ( context.nodeType === 1 ) {
104 for ( var i = 0; checkSet[i] != null; i++ ) {
105 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
106 results.push( set[i] );
110 for ( var i = 0; checkSet[i] != null; i++ ) {
111 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
112 results.push( set[i] );
117 makeArray( checkSet, results );
121 Sizzle( extra, context, results );
127 Sizzle.matches = function(expr, set){
128 return Sizzle(expr, null, null, set);
131 Sizzle.find = function(expr, context){
138 var later = "", match;
140 // Pseudo-selectors could contain other selectors (like :not)
141 while ( (match = Expr.match.PSEUDO.exec( expr )) ) {
142 var left = RegExp.leftContext;
144 if ( left.substr( left.length - 1 ) !== "\\" ) {
146 expr = expr.replace( Expr.match.PSEUDO, "" );
148 // TODO: Need a better solution, fails: .class\:foo:realfoo(#id)
153 for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
154 var type = Expr.order[i];
156 if ( (match = Expr.match[ type ].exec( expr )) ) {
157 var left = RegExp.leftContext;
159 if ( left.substr( left.length - 1 ) !== "\\" ) {
160 match[1] = (match[1] || "").replace(/\\/g, "");
161 set = Expr.find[ type ]( match, context );
164 expr = expr.replace( Expr.match[ type ], "" );
172 set = context.getElementsByTagName("*");
177 return {set: set, expr: expr};
180 Sizzle.filter = function(expr, set, inplace){
181 var old = expr, result = [], curLoop = set, match;
183 while ( expr && set.length ) {
184 for ( var type in Expr.filter ) {
185 if ( (match = Expr.match[ type ].exec( expr )) != null ) {
186 var anyFound = false, filter = Expr.filter[ type ], goodArray = null;
188 if ( curLoop == result ) {
192 if ( Expr.preFilter[ type ] ) {
193 match = Expr.preFilter[ type ]( match, curLoop );
195 if ( match[0] === true ) {
197 var last = null, elem;
198 for ( var i = 0; (elem = curLoop[i]) !== undefined; i++ ) {
199 if ( elem && last !== elem ) {
200 goodArray.push( elem );
208 var goodPos = 0, found, item;
210 for ( var i = 0; (item = curLoop[i]) !== undefined; i++ ) {
212 if ( goodArray && item != goodArray[goodPos] ) {
216 found = filter( item, match, goodPos, goodArray );
217 if ( inplace && found != null ) {
218 curLoop[i] = found ? curLoop[i] : false;
222 } else if ( found ) {
229 if ( found !== undefined ) {
234 expr = expr.replace( Expr.match[ type ], "" );
246 expr = expr.replace(/\s*,\s*/, "");
248 // Improper expression
250 throw "Syntax error, unrecognized expression: " + expr;
259 var Expr = Sizzle.selectors = {
260 order: [ "ID", "NAME", "TAG" ],
262 ID: /#((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
263 CLASS: /\.((?:[\w\u0128-\uFFFF_-]|\\.)+)/,
264 NAME: /\[name=((?:[\w\u0128-\uFFFF_-]|\\.)+)\]/,
265 ATTR: /\[((?:[\w\u0128-\uFFFF_-]|\\.)+)\s*(?:(\S{0,1}=)\s*(['"]*)(.*?)\3|)\]/,
266 TAG: /^((?:[\w\u0128-\uFFFF\*_-]|\\.)+)/,
267 CHILD: /:(only|nth|last|first)-child\(?(even|odd|[\dn+-]*)\)?/,
268 POS: /:(nth|eq|gt|lt|first|last|even|odd)\(?(\d*)\)?(?:[^-]|$)/,
269 PSEUDO: /:((?:[\w\u0128-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
275 "+": function(checkSet, part){
276 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
277 var elem = checkSet[i];
279 var cur = elem.previousSibling;
280 while ( cur && cur.nodeType !== 1 ) {
281 cur = cur.previousSibling;
283 checkSet[i] = typeof part === "string" ?
289 if ( typeof part === "string" ) {
290 Sizzle.filter( part, checkSet, true );
293 ">": function(checkSet, part){
294 if ( typeof part === "string" && !/\W/.test(part) ) {
295 part = part.toUpperCase();
297 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
298 var elem = checkSet[i];
300 var parent = elem.parentNode;
301 checkSet[i] = parent.nodeName === part ? parent : false;
305 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
306 var elem = checkSet[i];
308 checkSet[i] = typeof part === "string" ?
310 elem.parentNode === part;
314 if ( typeof part === "string" ) {
315 Sizzle.filter( part, checkSet, true );
319 "": function(checkSet, part){
320 var doneName = "done" + (done++), checkFn = dirCheck;
322 if ( !part.match(/\W/) ) {
323 var nodeCheck = part = part.toUpperCase();
324 checkFn = dirNodeCheck;
327 checkFn("parentNode", part, doneName, checkSet, nodeCheck);
329 "~": function(checkSet, part){
330 var doneName = "done" + (done++), checkFn = dirCheck;
332 if ( typeof part === "string" && !part.match(/\W/) ) {
333 var nodeCheck = part = part.toUpperCase();
334 checkFn = dirNodeCheck;
337 checkFn("previousSibling", part, doneName, checkSet, nodeCheck);
341 ID: function(match, context){
342 if ( context.getElementById ) {
343 var m = context.getElementById(match[1]);
347 NAME: function(match, context){
348 return context.getElementsByName(match[1]);
350 TAG: function(match, context){
351 return context.getElementsByTagName(match[1]);
355 CLASS: function(match){
356 return new RegExp( "(?:^|\\s)" + match[1] + "(?:\\s|$)" );
361 TAG: function(match){
362 return match[1].toUpperCase();
364 CHILD: function(match){
365 if ( match[1] == "nth" ) {
366 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
367 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
368 match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
369 !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
371 // calculate the numbers (first)n+(last) including if they are negative
372 match[2] = (test[1] + (test[2] || 1)) - 0;
373 match[3] = test[3] - 0;
376 // TODO: Move to normal caching system
377 match[0] = "done" + (done++);
381 ATTR: function(match){
384 if ( Expr.attrMap[name] ) {
385 match[1] = Expr.attrMap[name];
388 if ( match[2] === "~=" ) {
389 match[4] = " " + match[4] + " ";
394 PSEUDO: function(match){
395 if ( match[1] === "not" ) {
396 match[3] = match[3].split(/\s*,\s*/);
401 POS: function(match){
402 match.unshift( true );
407 enabled: function(elem){
408 return elem.disabled === false && elem.type !== "hidden";
410 disabled: function(elem){
411 return elem.disabled === true;
413 checked: function(elem){
414 return elem.checked === true;
416 selected: function(elem){
417 // Accessing this property makes selected-by-default
418 // options in Safari work properly
419 elem.parentNode.selectedIndex;
420 return elem.selected === true;
422 parent: function(elem){
423 return !!elem.firstChild;
425 empty: function(elem){
426 return !elem.firstChild;
428 has: function(elem, i, match){
429 return !!Sizzle( match[3], elem ).length;
431 header: function(elem){
432 return /h\d/i.test( elem.nodeName );
434 text: function(elem){
435 return "text" === elem.type;
437 radio: function(elem){
438 return "radio" === elem.type;
440 checkbox: function(elem){
441 return "checkbox" === elem.type;
443 file: function(elem){
444 return "file" === elem.type;
446 password: function(elem){
447 return "password" === elem.type;
449 submit: function(elem){
450 return "submit" === elem.type;
452 image: function(elem){
453 return "image" === elem.type;
455 reset: function(elem){
456 return "reset" === elem.type;
458 button: function(elem){
459 return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
461 input: function(elem){
462 return /input|select|textarea|button/i.test(elem.nodeName);
466 first: function(elem, i){
469 last: function(elem, i, match, array){
470 return i === array.length - 1;
472 even: function(elem, i){
475 odd: function(elem, i){
478 lt: function(elem, i, match){
479 return i < match[3] - 0;
481 gt: function(elem, i, match){
482 return i > match[3] - 0;
484 nth: function(elem, i, match){
485 return match[3] - 0 == i;
487 eq: function(elem, i, match){
488 return match[3] - 0 == i;
492 CHILD: function(elem, match){
493 var type = match[1], parent = elem.parentNode;
495 var doneName = match[0];
497 if ( parent && !parent[ doneName ] ) {
500 for ( var node = parent.firstChild; node; node = node.nextSibling ) {
501 if ( node.nodeType == 1 ) {
502 node.nodeIndex = count++;
506 parent[ doneName ] = count - 1;
509 if ( type == "first" ) {
510 return elem.nodeIndex == 1;
511 } else if ( type == "last" ) {
512 return elem.nodeIndex == parent[ doneName ];
513 } else if ( type == "only" ) {
514 return parent[ doneName ] == 1;
515 } else if ( type == "nth" ) {
516 var add = false, first = match[2], last = match[3];
518 if ( first == 1 && last == 0 ) {
523 if ( elem.nodeIndex == last ) {
526 } else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
533 PSEUDO: function(elem, match, i, array){
534 var name = match[1], filter = Expr.filters[ name ];
537 return filter( elem, i, match, array )
538 } else if ( name === "contains" ) {
539 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
540 } else if ( name === "not" ) {
543 for ( var i = 0, l = not.length; i < l; i++ ) {
544 if ( Sizzle.filter(not[i], [elem]).length > 0 ) {
552 ID: function(elem, match){
553 return elem.nodeType === 1 && elem.getAttribute("id") === match;
555 TAG: function(elem, match){
556 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
558 CLASS: function(elem, match){
559 return match.test( elem.className );
561 ATTR: function(elem, match){
562 var result = elem[ match[1] ] || elem.getAttribute( match[1] ), value = result + "", type = match[2], check = match[4];
563 return result == null ?
568 value.indexOf(check) >= 0 :
570 (" " + value + " ").indexOf(check) >= 0 :
576 value.indexOf(check) === 0 :
578 value.substr(value.length - check.length) === check :
580 value === check || value.substr(0, check.length + 1) === check + "-" :
583 POS: function(elem, match, i, array){
584 var name = match[2], filter = Expr.setFilters[ name ];
587 return filter( elem, i, match, array );
593 var makeArray = function(array, results) {
594 array = Array.prototype.slice.call( array );
597 results.push.apply( results, array );
604 // Perform a simple check to determine if the browser is capable of
605 // converting a NodeList to an array using builtin methods.
607 Array.prototype.slice.call( document.documentElement.childNodes );
609 // Provide a fallback method if it does not work
611 makeArray = function(array, results) {
612 var ret = results || [];
614 if ( array instanceof Array ) {
615 Array.prototype.push.apply( ret, array );
617 if ( typeof array.length === "number" ) {
618 for ( var i = 0, l = array.length; i < l; i++ ) {
619 ret.push( array[i] );
622 for ( var i = 0; array[i]; i++ ) {
623 ret.push( array[i] );
632 // Check to see if the browser returns elements by name when
633 // querying by getElementById (and provide a workaround)
635 // We're going to inject a fake input element with a specified name
636 var form = document.createElement("form"),
637 id = "script" + (new Date).getTime();
638 form.innerHTML = "<input name='" + id + "'/>";
640 // Inject it into the root element, check its status, and remove it quickly
641 var root = document.documentElement;
642 root.insertBefore( form, root.firstChild );
644 // The workaround has to do additional checks after a getElementById
645 // Which slows things down for other browsers (hence the branching)
646 if ( !!document.getElementById( id ) ) {
647 Expr.find.ID = function(match, context){
648 if ( context.getElementById ) {
649 var m = context.getElementById(match[1]);
650 return m ? m.id === match[1] || m.getAttributeNode && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
654 Expr.filter.ID = function(elem, match){
655 var node = elem.getAttributeNode && elem.getAttributeNode("id");
656 return elem.nodeType === 1 && node && node.nodeValue === match;
660 root.removeChild( form );
663 // Check to see if the browser returns only elements
664 // when doing getElementsByTagName("*")
666 // Create a fake element
667 var div = document.createElement("div");
668 div.appendChild( document.createComment("") );
670 // Make sure no comments are found
671 if ( div.getElementsByTagName("*").length > 0 ) {
672 Expr.find.TAG = function(match, context){
673 var results = context.getElementsByTagName(match[1]);
675 // Filter out possible comments
676 if ( match[1] === "*" ) {
679 for ( var i = 0; results[i]; i++ ) {
680 if ( results[i].nodeType === 1 ) {
681 tmp.push( results[i] );
693 if ( document.querySelectorAll ) (function(){
694 var oldSizzle = Sizzle;
696 Sizzle = function(query, context, extra){
697 context = context || document;
699 if ( context.nodeType === 9 ) {
701 return makeArray( context.querySelectorAll(query) );
705 return oldSizzle(query, context, extra);
708 Sizzle.find = oldSizzle.find;
709 Sizzle.filter = oldSizzle.filter;
710 Sizzle.selectors = oldSizzle.selectors;
713 if ( document.documentElement.getElementsByClassName ) {
714 Expr.order.splice(1, 0, "CLASS");
715 Expr.find.CLASS = function(match, context) {
716 return context.getElementsByClassName(match[1]);
720 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck ) {
721 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
722 var elem = checkSet[i];
727 while ( elem && elem.nodeType ) {
728 var done = elem[doneName];
730 match = checkSet[ done ];
734 if ( elem.nodeType === 1 )
737 if ( elem.nodeName === cur ) {
750 function dirCheck( dir, cur, doneName, checkSet, nodeCheck ) {
751 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
752 var elem = checkSet[i];
757 while ( elem && elem.nodeType ) {
758 if ( elem[doneName] ) {
759 match = checkSet[ elem[doneName] ];
763 if ( elem.nodeType === 1 ) {
766 if ( typeof cur !== "string" ) {
767 if ( elem === cur ) {
772 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
786 var contains = document.compareDocumentPosition ? function(a, b){
787 return a.compareDocumentPosition(b) & 16;
789 return a !== b && a.contains(b);
793 jQuery.find = Sizzle;
794 jQuery.filter = Sizzle.filter;
795 jQuery.expr = Sizzle.selectors;
796 jQuery.expr[":"] = jQuery.expr.filters;
798 Sizzle.selectors.filters.hidden = function(elem){
799 return "hidden" === elem.type ||
800 jQuery.css(elem, "display") === "none" ||
801 jQuery.css(elem, "visibility") === "hidden";
804 Sizzle.selectors.filters.visible = function(elem){
805 return "hidden" !== elem.type &&
806 jQuery.css(elem, "display") !== "none" &&
807 jQuery.css(elem, "visibility") !== "hidden";
810 jQuery.multiFilter = function( expr, elems, not ) {
812 return jQuery.multiFilter( ":not(" + expr + ")", elems );
815 var exprs = expr.split(/\s*,\s*/), cur = [];
817 for ( var i = 0; i < exprs.length; i++ ) {
818 cur = jQuery.merge( cur, jQuery.filter( exprs[i], elems ) );
824 jQuery.dir = function( elem, dir ){
825 var matched = [], cur = elem[dir];
826 while ( cur && cur != document ) {
827 if ( cur.nodeType == 1 )
834 jQuery.nth = function(cur, result, dir, elem){
835 result = result || 1;
838 for ( ; cur; cur = cur[dir] )
839 if ( cur.nodeType == 1 && ++num == result )
845 jQuery.sibling = function(n, elem){
848 for ( ; n; n = n.nextSibling ) {
849 if ( n.nodeType == 1 && n != elem )
858 window.Sizzle = Sizzle;