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