Changed the jQuery hidden/visible selectors to be a little more precise.
[jquery.git] / src / selector.js
1 /*!
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/
6  */
7 (function(){
8
9 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
10         done = 0,
11         toString = Object.prototype.toString;
12
13 var Sizzle = function(selector, context, results, seed) {
14         results = results || [];
15         context = context || document;
16
17         if ( context.nodeType !== 1 && context.nodeType !== 9 )
18                 return [];
19         
20         if ( !selector || typeof selector !== "string" ) {
21                 return results;
22         }
23
24         var parts = [], m, set, checkSet, check, mode, extra, prune = true;
25         
26         // Reset the position of the chunker regexp (start from head)
27         chunker.lastIndex = 0;
28         
29         while ( (m = chunker.exec(selector)) !== null ) {
30                 parts.push( m[1] );
31                 
32                 if ( m[2] ) {
33                         extra = RegExp.rightContext;
34                         break;
35                 }
36         }
37
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 );
41                 } else {
42                         set = Expr.relative[ parts[0] ] ?
43                                 [ context ] :
44                                 Sizzle( parts.shift(), context );
45
46                         while ( parts.length ) {
47                                 selector = parts.shift();
48
49                                 if ( Expr.relative[ selector ] )
50                                         selector += parts.shift();
51
52                                 set = posProcess( selector, set );
53                         }
54                 }
55         } else {
56                 var ret = seed ?
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 );
60
61                 if ( parts.length > 0 ) {
62                         checkSet = makeArray(set);
63                 } else {
64                         prune = false;
65                 }
66
67                 while ( parts.length ) {
68                         var cur = parts.pop(), pop = cur;
69
70                         if ( !Expr.relative[ cur ] ) {
71                                 cur = "";
72                         } else {
73                                 pop = parts.pop();
74                         }
75
76                         if ( pop == null ) {
77                                 pop = context;
78                         }
79
80                         Expr.relative[ cur ]( checkSet, pop, isXML(context) );
81                 }
82         }
83
84         if ( !checkSet ) {
85                 checkSet = set;
86         }
87
88         if ( !checkSet ) {
89                 throw "Syntax error, unrecognized expression: " + (cur || selector);
90         }
91
92         if ( toString.call(checkSet) === "[object Array]" ) {
93                 if ( !prune ) {
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] );
99                                 }
100                         }
101                 } else {
102                         for ( var i = 0; checkSet[i] != null; i++ ) {
103                                 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
104                                         results.push( set[i] );
105                                 }
106                         }
107                 }
108         } else {
109                 makeArray( checkSet, results );
110         }
111
112         if ( extra ) {
113                 Sizzle( extra, context, results, seed );
114
115                 if ( sortOrder ) {
116                         hasDuplicate = false;
117                         results.sort(sortOrder);
118
119                         if ( hasDuplicate ) {
120                                 for ( var i = 1; i < results.length; i++ ) {
121                                         if ( results[i] === results[i-1] ) {
122                                                 results.splice(i--, 1);
123                                         }
124                                 }
125                         }
126                 }
127         }
128
129         return results;
130 };
131
132 Sizzle.matches = function(expr, set){
133         return Sizzle(expr, null, null, set);
134 };
135
136 Sizzle.find = function(expr, context, isXML){
137         var set, match;
138
139         if ( !expr ) {
140                 return [];
141         }
142
143         for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
144                 var type = Expr.order[i], match;
145                 
146                 if ( (match = Expr.match[ type ].exec( expr )) ) {
147                         var left = RegExp.leftContext;
148
149                         if ( left.substr( left.length - 1 ) !== "\\" ) {
150                                 match[1] = (match[1] || "").replace(/\\/g, "");
151                                 set = Expr.find[ type ]( match, context, isXML );
152                                 if ( set != null ) {
153                                         expr = expr.replace( Expr.match[ type ], "" );
154                                         break;
155                                 }
156                         }
157                 }
158         }
159
160         if ( !set ) {
161                 set = context.getElementsByTagName("*");
162         }
163
164         return {set: set, expr: expr};
165 };
166
167 Sizzle.filter = function(expr, set, inplace, not){
168         var old = expr, result = [], curLoop = set, match, anyFound;
169
170         while ( expr && set.length ) {
171                 for ( var type in Expr.filter ) {
172                         if ( (match = Expr.match[ type ].exec( expr )) != null ) {
173                                 var filter = Expr.filter[ type ], found, item;
174                                 anyFound = false;
175
176                                 if ( curLoop == result ) {
177                                         result = [];
178                                 }
179
180                                 if ( Expr.preFilter[ type ] ) {
181                                         match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not );
182
183                                         if ( !match ) {
184                                                 anyFound = found = true;
185                                         } else if ( match === true ) {
186                                                 continue;
187                                         }
188                                 }
189
190                                 if ( match ) {
191                                         for ( var i = 0; (item = curLoop[i]) != null; i++ ) {
192                                                 if ( item ) {
193                                                         found = filter( item, match, i, curLoop );
194                                                         var pass = not ^ !!found;
195
196                                                         if ( inplace && found != null ) {
197                                                                 if ( pass ) {
198                                                                         anyFound = true;
199                                                                 } else {
200                                                                         curLoop[i] = false;
201                                                                 }
202                                                         } else if ( pass ) {
203                                                                 result.push( item );
204                                                                 anyFound = true;
205                                                         }
206                                                 }
207                                         }
208                                 }
209
210                                 if ( found !== undefined ) {
211                                         if ( !inplace ) {
212                                                 curLoop = result;
213                                         }
214
215                                         expr = expr.replace( Expr.match[ type ], "" );
216
217                                         if ( !anyFound ) {
218                                                 return [];
219                                         }
220
221                                         break;
222                                 }
223                         }
224                 }
225
226                 // Improper expression
227                 if ( expr == old ) {
228                         if ( anyFound == null ) {
229                                 throw "Syntax error, unrecognized expression: " + expr;
230                         } else {
231                                 break;
232                         }
233                 }
234
235                 old = expr;
236         }
237
238         return curLoop;
239 };
240
241 var Expr = Sizzle.selectors = {
242         order: [ "ID", "NAME", "TAG" ],
243         match: {
244                 ID: /#((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
245                 CLASS: /\.((?:[\w\u00c0-\uFFFF_-]|\\.)+)/,
246                 NAME: /\[name=['"]*((?:[\w\u00c0-\uFFFF_-]|\\.)+)['"]*\]/,
247                 ATTR: /\[\s*((?:[\w\u00c0-\uFFFF_-]|\\.)+)\s*(?:(\S?=)\s*(['"]*)(.*?)\3|)\s*\]/,
248                 TAG: /^((?:[\w\u00c0-\uFFFF\*_-]|\\.)+)/,
249                 CHILD: /:(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?/,
250                 POS: /:(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)/,
251                 PSEUDO: /:((?:[\w\u00c0-\uFFFF_-]|\\.)+)(?:\((['"]*)((?:\([^\)]+\)|[^\2\(\)]*)+)\2\))?/
252         },
253         attrMap: {
254                 "class": "className",
255                 "for": "htmlFor"
256         },
257         attrHandle: {
258                 href: function(elem){
259                         return elem.getAttribute("href");
260                 }
261         },
262         relative: {
263                 "+": function(checkSet, part, isXML){
264                         var isPartStr = typeof part === "string",
265                                 isTag = isPartStr && !/\W/.test(part),
266                                 isPartStrNotTag = isPartStr && !isTag;
267
268                         if ( isTag && !isXML ) {
269                                 part = part.toUpperCase();
270                         }
271
272                         for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
273                                 if ( (elem = checkSet[i]) ) {
274                                         while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}
275
276                                         checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
277                                                 elem || false :
278                                                 elem === part;
279                                 }
280                         }
281
282                         if ( isPartStrNotTag ) {
283                                 Sizzle.filter( part, checkSet, true );
284                         }
285                 },
286                 ">": function(checkSet, part, isXML){
287                         var isPartStr = typeof part === "string";
288
289                         if ( isPartStr && !/\W/.test(part) ) {
290                                 part = isXML ? part : part.toUpperCase();
291
292                                 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
293                                         var elem = checkSet[i];
294                                         if ( elem ) {
295                                                 var parent = elem.parentNode;
296                                                 checkSet[i] = parent.nodeName === part ? parent : false;
297                                         }
298                                 }
299                         } else {
300                                 for ( var i = 0, l = checkSet.length; i < l; i++ ) {
301                                         var elem = checkSet[i];
302                                         if ( elem ) {
303                                                 checkSet[i] = isPartStr ?
304                                                         elem.parentNode :
305                                                         elem.parentNode === part;
306                                         }
307                                 }
308
309                                 if ( isPartStr ) {
310                                         Sizzle.filter( part, checkSet, true );
311                                 }
312                         }
313                 },
314                 "": function(checkSet, part, isXML){
315                         var doneName = done++, checkFn = dirCheck;
316
317                         if ( !part.match(/\W/) ) {
318                                 var nodeCheck = part = isXML ? part : part.toUpperCase();
319                                 checkFn = dirNodeCheck;
320                         }
321
322                         checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
323                 },
324                 "~": function(checkSet, part, isXML){
325                         var doneName = done++, checkFn = dirCheck;
326
327                         if ( typeof part === "string" && !part.match(/\W/) ) {
328                                 var nodeCheck = part = isXML ? part : part.toUpperCase();
329                                 checkFn = dirNodeCheck;
330                         }
331
332                         checkFn("previousSibling", part, doneName, checkSet, nodeCheck, isXML);
333                 }
334         },
335         find: {
336                 ID: function(match, context, isXML){
337                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
338                                 var m = context.getElementById(match[1]);
339                                 return m ? [m] : [];
340                         }
341                 },
342                 NAME: function(match, context, isXML){
343                         if ( typeof context.getElementsByName !== "undefined" ) {
344                                 var ret = [], results = context.getElementsByName(match[1]);
345
346                                 for ( var i = 0, l = results.length; i < l; i++ ) {
347                                         if ( results[i].getAttribute("name") === match[1] ) {
348                                                 ret.push( results[i] );
349                                         }
350                                 }
351
352                                 return ret.length === 0 ? null : ret;
353                         }
354                 },
355                 TAG: function(match, context){
356                         return context.getElementsByTagName(match[1]);
357                 }
358         },
359         preFilter: {
360                 CLASS: function(match, curLoop, inplace, result, not){
361                         match = " " + match[1].replace(/\\/g, "") + " ";
362
363                         for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
364                                 if ( elem ) {
365                                         if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
366                                                 if ( !inplace )
367                                                         result.push( elem );
368                                         } else if ( inplace ) {
369                                                 curLoop[i] = false;
370                                         }
371                                 }
372                         }
373
374                         return false;
375                 },
376                 ID: function(match){
377                         return match[1].replace(/\\/g, "");
378                 },
379                 TAG: function(match, curLoop){
380                         for ( var i = 0; curLoop[i] === false; i++ ){}
381                         return curLoop[i] && isXML(curLoop[i]) ? match[1] : match[1].toUpperCase();
382                 },
383                 CHILD: function(match){
384                         if ( match[1] == "nth" ) {
385                                 // parse equations like 'even', 'odd', '5', '2n', '3n+2', '4n-1', '-n+6'
386                                 var test = /(-?)(\d*)n((?:\+|-)?\d*)/.exec(
387                                         match[2] == "even" && "2n" || match[2] == "odd" && "2n+1" ||
388                                         !/\D/.test( match[2] ) && "0n+" + match[2] || match[2]);
389
390                                 // calculate the numbers (first)n+(last) including if they are negative
391                                 match[2] = (test[1] + (test[2] || 1)) - 0;
392                                 match[3] = test[3] - 0;
393                         }
394
395                         // TODO: Move to normal caching system
396                         match[0] = done++;
397
398                         return match;
399                 },
400                 ATTR: function(match){
401                         var name = match[1].replace(/\\/g, "");
402                         
403                         if ( Expr.attrMap[name] ) {
404                                 match[1] = Expr.attrMap[name];
405                         }
406
407                         if ( match[2] === "~=" ) {
408                                 match[4] = " " + match[4] + " ";
409                         }
410
411                         return match;
412                 },
413                 PSEUDO: function(match, curLoop, inplace, result, not){
414                         if ( match[1] === "not" ) {
415                                 // If we're dealing with a complex expression, or a simple one
416                                 if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
417                                         match[3] = Sizzle(match[3], null, null, curLoop);
418                                 } else {
419                                         var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
420                                         if ( !inplace ) {
421                                                 result.push.apply( result, ret );
422                                         }
423                                         return false;
424                                 }
425                         } else if ( Expr.match.POS.test( match[0] ) ) {
426                                 return true;
427                         }
428                         
429                         return match;
430                 },
431                 POS: function(match){
432                         match.unshift( true );
433                         return match;
434                 }
435         },
436         filters: {
437                 enabled: function(elem){
438                         return elem.disabled === false && elem.type !== "hidden";
439                 },
440                 disabled: function(elem){
441                         return elem.disabled === true;
442                 },
443                 checked: function(elem){
444                         return elem.checked === true;
445                 },
446                 selected: function(elem){
447                         // Accessing this property makes selected-by-default
448                         // options in Safari work properly
449                         elem.parentNode.selectedIndex;
450                         return elem.selected === true;
451                 },
452                 parent: function(elem){
453                         return !!elem.firstChild;
454                 },
455                 empty: function(elem){
456                         return !elem.firstChild;
457                 },
458                 has: function(elem, i, match){
459                         return !!Sizzle( match[3], elem ).length;
460                 },
461                 header: function(elem){
462                         return /h\d/i.test( elem.nodeName );
463                 },
464                 text: function(elem){
465                         return "text" === elem.type;
466                 },
467                 radio: function(elem){
468                         return "radio" === elem.type;
469                 },
470                 checkbox: function(elem){
471                         return "checkbox" === elem.type;
472                 },
473                 file: function(elem){
474                         return "file" === elem.type;
475                 },
476                 password: function(elem){
477                         return "password" === elem.type;
478                 },
479                 submit: function(elem){
480                         return "submit" === elem.type;
481                 },
482                 image: function(elem){
483                         return "image" === elem.type;
484                 },
485                 reset: function(elem){
486                         return "reset" === elem.type;
487                 },
488                 button: function(elem){
489                         return "button" === elem.type || elem.nodeName.toUpperCase() === "BUTTON";
490                 },
491                 input: function(elem){
492                         return /input|select|textarea|button/i.test(elem.nodeName);
493                 }
494         },
495         setFilters: {
496                 first: function(elem, i){
497                         return i === 0;
498                 },
499                 last: function(elem, i, match, array){
500                         return i === array.length - 1;
501                 },
502                 even: function(elem, i){
503                         return i % 2 === 0;
504                 },
505                 odd: function(elem, i){
506                         return i % 2 === 1;
507                 },
508                 lt: function(elem, i, match){
509                         return i < match[3] - 0;
510                 },
511                 gt: function(elem, i, match){
512                         return i > match[3] - 0;
513                 },
514                 nth: function(elem, i, match){
515                         return match[3] - 0 == i;
516                 },
517                 eq: function(elem, i, match){
518                         return match[3] - 0 == i;
519                 }
520         },
521         filter: {
522                 CHILD: function(elem, match){
523                         var type = match[1], node = elem;
524                         switch (type) {
525                                 case 'only':
526                                 case 'first':
527                                         while (node = node.previousSibling)  {
528                                                 if ( node.nodeType === 1 ) return false;
529                                         }
530                                         if ( type == 'first') return true;
531                                         node = elem;
532                                 case 'last':
533                                         while (node = node.nextSibling)  {
534                                                 if ( node.nodeType === 1 ) return false;
535                                         }
536                                         return true;
537                                 case 'nth':
538                                         var first = match[2], last = match[3];
539
540                                         if ( first == 1 && last == 0 ) {
541                                                 return true;
542                                         }
543                                         
544                                         var doneName = match[0],
545                                                 parent = elem.parentNode;
546         
547                                         if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
548                                                 var count = 0;
549                                                 for ( node = parent.firstChild; node; node = node.nextSibling ) {
550                                                         if ( node.nodeType === 1 ) {
551                                                                 node.nodeIndex = ++count;
552                                                         }
553                                                 } 
554                                                 parent.sizcache = doneName;
555                                         }
556                                         
557                                         var diff = elem.nodeIndex - last;
558                                         if ( first == 0 ) {
559                                                 return diff == 0;
560                                         } else {
561                                                 return ( diff % first == 0 && diff / first >= 0 );
562                                         }
563                         }
564                 },
565                 PSEUDO: function(elem, match, i, array){
566                         var name = match[1], filter = Expr.filters[ name ];
567
568                         if ( filter ) {
569                                 return filter( elem, i, match, array );
570                         } else if ( name === "contains" ) {
571                                 return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0;
572                         } else if ( name === "not" ) {
573                                 var not = match[3];
574
575                                 for ( var i = 0, l = not.length; i < l; i++ ) {
576                                         if ( not[i] === elem ) {
577                                                 return false;
578                                         }
579                                 }
580
581                                 return true;
582                         }
583                 },
584                 ID: function(elem, match){
585                         return elem.nodeType === 1 && elem.getAttribute("id") === match;
586                 },
587                 TAG: function(elem, match){
588                         return (match === "*" && elem.nodeType === 1) || elem.nodeName === match;
589                 },
590                 CLASS: function(elem, match){
591                         return match.test( elem.className );
592                 },
593                 ATTR: function(elem, match){
594                         var name = match[1],
595                                 result = Expr.attrHandle[ name ] ?
596                                         Expr.attrHandle[ name ]( elem ) :
597                                         elem[ name ] != null ?
598                                                 elem[ name ] :
599                                                 elem.getAttribute( name ),
600                                 value = result + "",
601                                 type = match[2],
602                                 check = match[4];
603
604                         return result == null ?
605                                 type === "!=" :
606                                 type === "=" ?
607                                 value === check :
608                                 type === "*=" ?
609                                 value.indexOf(check) >= 0 :
610                                 type === "~=" ?
611                                 (" " + value + " ").indexOf(check) >= 0 :
612                                 !check ?
613                                 value && result !== false :
614                                 type === "!=" ?
615                                 value != check :
616                                 type === "^=" ?
617                                 value.indexOf(check) === 0 :
618                                 type === "$=" ?
619                                 value.substr(value.length - check.length) === check :
620                                 type === "|=" ?
621                                 value === check || value.substr(0, check.length + 1) === check + "-" :
622                                 false;
623                 },
624                 POS: function(elem, match, i, array){
625                         var name = match[2], filter = Expr.setFilters[ name ];
626
627                         if ( filter ) {
628                                 return filter( elem, i, match, array );
629                         }
630                 }
631         }
632 };
633
634 var origPOS = Expr.match.POS;
635
636 for ( var type in Expr.match ) {
637         Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source );
638 }
639
640 var makeArray = function(array, results) {
641         array = Array.prototype.slice.call( array );
642
643         if ( results ) {
644                 results.push.apply( results, array );
645                 return results;
646         }
647         
648         return array;
649 };
650
651 // Perform a simple check to determine if the browser is capable of
652 // converting a NodeList to an array using builtin methods.
653 try {
654         Array.prototype.slice.call( document.documentElement.childNodes );
655
656 // Provide a fallback method if it does not work
657 } catch(e){
658         makeArray = function(array, results) {
659                 var ret = results || [];
660
661                 if ( toString.call(array) === "[object Array]" ) {
662                         Array.prototype.push.apply( ret, array );
663                 } else {
664                         if ( typeof array.length === "number" ) {
665                                 for ( var i = 0, l = array.length; i < l; i++ ) {
666                                         ret.push( array[i] );
667                                 }
668                         } else {
669                                 for ( var i = 0; array[i]; i++ ) {
670                                         ret.push( array[i] );
671                                 }
672                         }
673                 }
674
675                 return ret;
676         };
677 }
678
679 var sortOrder;
680
681 if ( Array.prototype.indexOf ) {
682         var indexOf = Array.prototype.indexOf,
683                 allSort = document.getElementsByTagName("*");
684
685         sortOrder = function( a, b ) {
686                 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
687                 if ( ret === 0 ) {
688                         hasDuplicate = true;
689                 }
690                 return ret;
691         };
692 } else if ( document.documentElement.sourceIndex === 1 ) {
693         sortOrder = function( a, b ) {
694                 var ret = a.sourceIndex - b.sourceIndex;
695                 if ( ret === 0 ) {
696                         hasDuplicate = true;
697                 }
698                 return ret;
699         };
700 }
701
702 // Check to see if the browser returns elements by name when
703 // querying by getElementById (and provide a workaround)
704 (function(){
705         // We're going to inject a fake input element with a specified name
706         var form = document.createElement("form"),
707                 id = "script" + (new Date).getTime();
708         form.innerHTML = "<input name='" + id + "'/>";
709
710         // Inject it into the root element, check its status, and remove it quickly
711         var root = document.documentElement;
712         root.insertBefore( form, root.firstChild );
713
714         // The workaround has to do additional checks after a getElementById
715         // Which slows things down for other browsers (hence the branching)
716         if ( !!document.getElementById( id ) ) {
717                 Expr.find.ID = function(match, context, isXML){
718                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
719                                 var m = context.getElementById(match[1]);
720                                 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
721                         }
722                 };
723
724                 Expr.filter.ID = function(elem, match){
725                         var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
726                         return elem.nodeType === 1 && node && node.nodeValue === match;
727                 };
728         }
729
730         root.removeChild( form );
731 })();
732
733 (function(){
734         // Check to see if the browser returns only elements
735         // when doing getElementsByTagName("*")
736
737         // Create a fake element
738         var div = document.createElement("div");
739         div.appendChild( document.createComment("") );
740
741         // Make sure no comments are found
742         if ( div.getElementsByTagName("*").length > 0 ) {
743                 Expr.find.TAG = function(match, context){
744                         var results = context.getElementsByTagName(match[1]);
745
746                         // Filter out possible comments
747                         if ( match[1] === "*" ) {
748                                 var tmp = [];
749
750                                 for ( var i = 0; results[i]; i++ ) {
751                                         if ( results[i].nodeType === 1 ) {
752                                                 tmp.push( results[i] );
753                                         }
754                                 }
755
756                                 results = tmp;
757                         }
758
759                         return results;
760                 };
761         }
762
763         // Check to see if an attribute returns normalized href attributes
764         div.innerHTML = "<a href='#'></a>";
765         if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
766                         div.firstChild.getAttribute("href") !== "#" ) {
767                 Expr.attrHandle.href = function(elem){
768                         return elem.getAttribute("href", 2);
769                 };
770         }
771 })();
772
773 if ( document.querySelectorAll ) (function(){
774         var oldSizzle = Sizzle, div = document.createElement("div");
775         div.innerHTML = "<p class='TEST'></p>";
776
777         // Safari can't handle uppercase or unicode characters when
778         // in quirks mode.
779         if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
780                 return;
781         }
782         
783         Sizzle = function(query, context, extra, seed){
784                 context = context || document;
785
786                 // Only use querySelectorAll on non-XML documents
787                 // (ID selectors don't work in non-HTML documents)
788                 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
789                         try {
790                                 return makeArray( context.querySelectorAll(query), extra );
791                         } catch(e){}
792                 }
793                 
794                 return oldSizzle(query, context, extra, seed);
795         };
796
797         Sizzle.find = oldSizzle.find;
798         Sizzle.filter = oldSizzle.filter;
799         Sizzle.selectors = oldSizzle.selectors;
800         Sizzle.matches = oldSizzle.matches;
801 })();
802
803 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
804         var div = document.createElement("div");
805         div.innerHTML = "<div class='test e'></div><div class='test'></div>";
806
807         // Opera can't find a second classname (in 9.6)
808         if ( div.getElementsByClassName("e").length === 0 )
809                 return;
810
811         // Safari caches class attributes, doesn't catch changes (in 3.2)
812         div.lastChild.className = "e";
813
814         if ( div.getElementsByClassName("e").length === 1 )
815                 return;
816
817         Expr.order.splice(1, 0, "CLASS");
818         Expr.find.CLASS = function(match, context) {
819                 return context.getElementsByClassName(match[1]);
820         };
821 })();
822
823 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
824         var sibDir = dir == "previousSibling" && !isXML;
825         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
826                 var elem = checkSet[i];
827                 if ( elem ) {
828                         if ( sibDir && elem.nodeType === 1 ){
829                                 elem.sizcache = doneName;
830                                 elem.sizset = i;
831                         }
832                         elem = elem[dir];
833                         var match = false;
834
835                         while ( elem ) {
836                                 if ( elem.sizcache === doneName ) {
837                                         match = checkSet[elem.sizset];
838                                         break;
839                                 }
840
841                                 if ( elem.nodeType === 1 && !isXML ){
842                                         elem.sizcache = doneName;
843                                         elem.sizset = i;
844                                 }
845
846                                 if ( elem.nodeName === cur ) {
847                                         match = elem;
848                                         break;
849                                 }
850
851                                 elem = elem[dir];
852                         }
853
854                         checkSet[i] = match;
855                 }
856         }
857 }
858
859 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
860         var sibDir = dir == "previousSibling" && !isXML;
861         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
862                 var elem = checkSet[i];
863                 if ( elem ) {
864                         if ( sibDir && elem.nodeType === 1 ) {
865                                 elem.sizcache = doneName;
866                                 elem.sizset = i;
867                         }
868                         elem = elem[dir];
869                         var match = false;
870
871                         while ( elem ) {
872                                 if ( elem.sizcache === doneName ) {
873                                         match = checkSet[elem.sizset];
874                                         break;
875                                 }
876
877                                 if ( elem.nodeType === 1 ) {
878                                         if ( !isXML ) {
879                                                 elem.sizcache = doneName;
880                                                 elem.sizset = i;
881                                         }
882                                         if ( typeof cur !== "string" ) {
883                                                 if ( elem === cur ) {
884                                                         match = true;
885                                                         break;
886                                                 }
887
888                                         } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
889                                                 match = elem;
890                                                 break;
891                                         }
892                                 }
893
894                                 elem = elem[dir];
895                         }
896
897                         checkSet[i] = match;
898                 }
899         }
900 }
901
902 var contains = document.compareDocumentPosition ?  function(a, b){
903         return a.compareDocumentPosition(b) & 16;
904 } : function(a, b){
905         return a !== b && (a.contains ? a.contains(b) : true);
906 };
907
908 var isXML = function(elem){
909         return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
910                 !!elem.ownerDocument && isXML( elem.ownerDocument );
911 };
912
913 var posProcess = function(selector, context){
914         var tmpSet = [], later = "", match,
915                 root = context.nodeType ? [context] : context;
916
917         // Position selectors must be done after the filter
918         // And so must :not(positional) so we move all PSEUDOs to the end
919         while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
920                 later += match[0];
921                 selector = selector.replace( Expr.match.PSEUDO, "" );
922         }
923
924         selector = Expr.relative[selector] ? selector + "*" : selector;
925
926         for ( var i = 0, l = root.length; i < l; i++ ) {
927                 Sizzle( selector, root[i], tmpSet );
928         }
929
930         return Sizzle.filter( later, tmpSet );
931 };
932
933 // EXPOSE
934 jQuery.find = Sizzle;
935 jQuery.filter = Sizzle.filter;
936 jQuery.expr = Sizzle.selectors;
937 jQuery.expr[":"] = jQuery.expr.filters;
938
939 Sizzle.selectors.filters.hidden = function(elem){
940         return elem.offsetWidth === 0 || elem.offsetHeight === 0;
941 };
942
943 Sizzle.selectors.filters.visible = function(elem){
944         return elem.offsetWidth > 0 || elem.offsetHeight > 0;
945 };
946
947 Sizzle.selectors.filters.animated = function(elem){
948         return jQuery.grep(jQuery.timers, function(fn){
949                 return elem === fn.elem;
950         }).length;
951 };
952
953 jQuery.multiFilter = function( expr, elems, not ) {
954         if ( not ) {
955                 expr = ":not(" + expr + ")";
956         }
957
958         return Sizzle.matches(expr, elems);
959 };
960
961 jQuery.dir = function( elem, dir ){
962         var matched = [], cur = elem[dir];
963         while ( cur && cur != document ) {
964                 if ( cur.nodeType == 1 )
965                         matched.push( cur );
966                 cur = cur[dir];
967         }
968         return matched;
969 };
970
971 jQuery.nth = function(cur, result, dir, elem){
972         result = result || 1;
973         var num = 0;
974
975         for ( ; cur; cur = cur[dir] )
976                 if ( cur.nodeType == 1 && ++num == result )
977                         break;
978
979         return cur;
980 };
981
982 jQuery.sibling = function(n, elem){
983         var r = [];
984
985         for ( ; n; n = n.nextSibling ) {
986                 if ( n.nodeType == 1 && n != elem )
987                         r.push( n );
988         }
989
990         return r;
991 };
992
993 return;
994
995 window.Sizzle = Sizzle;
996
997 })();