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