2 * Sizzle CSS Selector Engine - v0.9.1
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 );
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){
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 );
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){
319 if ( context.getElementById ) {
320 var m = context.getElementById(match[1]);
324 NAME: function(match, context){
325 return context.getElementsByName ? context.getElementsByName(match[1]) : null;
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; curLoop[i] != null; i++ ) {
337 if ( not ^ (" " + curLoop[i].className + " ").indexOf(match) >= 0 ) {
339 result.push( curLoop[i] );
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
368 match[0] = "done" + (done++);
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], parent = elem.parentNode;
497 var doneName = "child" + parent.childNodes.length;
499 if ( parent && (!parent[ doneName ] || !elem.nodeIndex) ) {
502 for ( var node = parent.firstChild; node; node = node.nextSibling ) {
503 if ( node.nodeType == 1 ) {
504 node.nodeIndex = count++;
508 parent[ doneName ] = count - 1;
511 if ( type == "first" ) {
512 return elem.nodeIndex == 1;
513 } else if ( type == "last" ) {
514 return elem.nodeIndex == parent[ doneName ];
515 } else if ( type == "only" ) {
516 return parent[ doneName ] == 1;
517 } else if ( type == "nth" ) {
518 var add = false, first = match[2], last = match[3];
520 if ( first == 1 && last == 0 ) {
525 if ( elem.nodeIndex == last ) {
528 } else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) {
535 PSEUDO: function(elem, match, i, array){
536 var name = match[1], filter = Expr.filters[ name ];
539 return filter( elem, i, match, array );
540 } else if ( name === "contains" ) {
541 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
542 } else if ( name === "not" ) {
545 for ( var i = 0, l = not.length; i < l; i++ ) {
546 if ( not[i] === elem ) {
554 ID: function(elem, match){
555 return elem.nodeType === 1 && elem.getAttribute("id") === match;
557 TAG: function(elem, match){
558 return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
560 CLASS: function(elem, match){
561 return match.test( elem.className );
563 ATTR: function(elem, match){
564 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];
565 return result == null ?
570 value.indexOf(check) >= 0 :
572 (" " + value + " ").indexOf(check) >= 0 :
578 value.indexOf(check) === 0 :
580 value.substr(value.length - check.length) === check :
582 value === check || value.substr(0, check.length + 1) === check + "-" :
585 POS: function(elem, match, i, array){
586 var name = match[2], filter = Expr.setFilters[ name ];
589 return filter( elem, i, match, array );
595 var origPOS = Expr.match.POS;
597 for ( var type in Expr.match ) {
598 Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
601 var makeArray = function(array, results) {
602 array = Array.prototype.slice.call( array );
605 results.push.apply( results, array );
612 // Perform a simple check to determine if the browser is capable of
613 // converting a NodeList to an array using builtin methods.
615 Array.prototype.slice.call( document.documentElement.childNodes );
617 // Provide a fallback method if it does not work
619 makeArray = function(array, results) {
620 var ret = results || [];
622 if ( toString.call(array) === "[object Array]" ) {
623 Array.prototype.push.apply( ret, array );
625 if ( typeof array.length === "number" ) {
626 for ( var i = 0, l = array.length; i < l; i++ ) {
627 ret.push( array[i] );
630 for ( var i = 0; array[i]; i++ ) {
631 ret.push( array[i] );
640 // Check to see if the browser returns elements by name when
641 // querying by getElementById (and provide a workaround)
643 // We're going to inject a fake input element with a specified name
644 var form = document.createElement("form"),
645 id = "script" + (new Date).getTime();
646 form.innerHTML = "<input name='" + id + "'/>";
648 // Inject it into the root element, check its status, and remove it quickly
649 var root = document.documentElement;
650 root.insertBefore( form, root.firstChild );
652 // The workaround has to do additional checks after a getElementById
653 // Which slows things down for other browsers (hence the branching)
654 if ( !!document.getElementById( id ) ) {
655 Expr.find.ID = function(match, context){
656 if ( context.getElementById ) {
657 var m = context.getElementById(match[1]);
658 return m ? m.id === match[1] || m.getAttributeNode && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
662 Expr.filter.ID = function(elem, match){
663 var node = elem.getAttributeNode && elem.getAttributeNode("id");
664 return elem.nodeType === 1 && node && node.nodeValue === match;
668 root.removeChild( form );
672 // Check to see if the browser returns only elements
673 // when doing getElementsByTagName("*")
675 // Create a fake element
676 var div = document.createElement("div");
677 div.appendChild( document.createComment("") );
679 // Make sure no comments are found
680 if ( div.getElementsByTagName("*").length > 0 ) {
681 Expr.find.TAG = function(match, context){
682 var results = context.getElementsByTagName(match[1]);
684 // Filter out possible comments
685 if ( match[1] === "*" ) {
688 for ( var i = 0; results[i]; i++ ) {
689 if ( results[i].nodeType === 1 ) {
690 tmp.push( results[i] );
701 // Check to see if an attribute returns normalized href attributes
702 div.innerHTML = "<a href='#'></a>";
703 if ( div.firstChild.getAttribute("href") !== "#" ) {
704 Expr.attrHandle.href = function(elem){
705 return elem.getAttribute("href", 2);
710 if ( document.querySelectorAll ) (function(){
711 var oldSizzle = Sizzle;
713 Sizzle = function(query, context, extra, seed){
714 context = context || document;
716 if ( !seed && context.nodeType === 9 ) {
718 return makeArray( context.querySelectorAll(query), extra );
722 return oldSizzle(query, context, extra, seed);
725 Sizzle.find = oldSizzle.find;
726 Sizzle.filter = oldSizzle.filter;
727 Sizzle.selectors = oldSizzle.selectors;
728 Sizzle.matches = oldSizzle.matches;
731 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) {
732 Expr.order.splice(1, 0, "CLASS");
733 Expr.find.CLASS = function(match, context) {
734 return context.getElementsByClassName(match[1]);
738 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
739 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
740 var elem = checkSet[i];
745 while ( elem && elem.nodeType ) {
746 var done = elem[doneName];
748 match = checkSet[ done ];
752 if ( elem.nodeType === 1 && !isXML )
755 if ( elem.nodeName === cur ) {
768 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
769 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
770 var elem = checkSet[i];
775 while ( elem && elem.nodeType ) {
776 if ( elem[doneName] ) {
777 match = checkSet[ elem[doneName] ];
781 if ( elem.nodeType === 1 ) {
785 if ( typeof cur !== "string" ) {
786 if ( elem === cur ) {
791 } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
805 var contains = document.compareDocumentPosition ? function(a, b){
806 return a.compareDocumentPosition(b) & 16;
808 return a !== b && (a.contains ? a.contains(b) : true);
811 var isXML = function(elem){
812 return elem.documentElement && !elem.body ||
813 elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
816 var posProcess = function(selector, context){
817 var tmpSet = [], later = "", match,
818 root = context.nodeType ? [context] : context;
820 // Position selectors must be done after the filter
821 // And so must :not(positional) so we move all PSEUDOs to the end
822 while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
824 selector = selector.replace( Expr.match.PSEUDO, "" );
827 selector = Expr.relative[selector] ? selector + "*" : selector;
829 for ( var i = 0, l = root.length; i < l; i++ ) {
830 Sizzle( selector, root[i], tmpSet );
833 return Sizzle.filter( later, tmpSet );
837 jQuery.find = Sizzle;
838 jQuery.filter = Sizzle.filter;
839 jQuery.expr = Sizzle.selectors;
840 jQuery.expr[":"] = jQuery.expr.filters;
842 Sizzle.selectors.filters.hidden = function(elem){
843 return "hidden" === elem.type ||
844 jQuery.css(elem, "display") === "none" ||
845 jQuery.css(elem, "visibility") === "hidden";
848 Sizzle.selectors.filters.visible = function(elem){
849 return "hidden" !== elem.type &&
850 jQuery.css(elem, "display") !== "none" &&
851 jQuery.css(elem, "visibility") !== "hidden";
854 Sizzle.selectors.filters.animated = function(elem){
855 return jQuery.grep(jQuery.timers, function(fn){
856 return elem === fn.elem;
860 jQuery.multiFilter = function( expr, elems, not ) {
862 expr = ":not(" + expr + ")";
865 return Sizzle.matches(expr, elems);
868 jQuery.dir = function( elem, dir ){
869 var matched = [], cur = elem[dir];
870 while ( cur && cur != document ) {
871 if ( cur.nodeType == 1 )
878 jQuery.nth = function(cur, result, dir, elem){
879 result = result || 1;
882 for ( ; cur; cur = cur[dir] )
883 if ( cur.nodeType == 1 && ++num == result )
889 jQuery.sibling = function(n, elem){
892 for ( ; n; n = n.nextSibling ) {
893 if ( n.nodeType == 1 && n != elem )
902 window.Sizzle = Sizzle;