The compareDocumentPosition check was extraneous - indexOf works in all the other...
[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 ( Array.prototype.indexOf ) {
667         var indexOf = Array.prototype.indexOf,
668                 allSort = document.getElementsByTagName("*");
669
670         sortOrder = function( a, b ) {
671                 var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, b );
672                 if ( ret === 0 ) {
673                         hasDuplicate = true;
674                 }
675                 return ret;
676         };
677 } else if ( document.documentElement.sourceIndex === 1 ) {
678         sortOrder = function( a, b ) {
679                 var ret = a.sourceIndex - b.sourceIndex;
680                 if ( ret === 0 ) {
681                         hasDuplicate = true;
682                 }
683                 return ret;
684         };
685 }
686
687 // Check to see if the browser returns elements by name when
688 // querying by getElementById (and provide a workaround)
689 (function(){
690         // We're going to inject a fake input element with a specified name
691         var form = document.createElement("form"),
692                 id = "script" + (new Date).getTime();
693         form.innerHTML = "<input name='" + id + "'/>";
694
695         // Inject it into the root element, check its status, and remove it quickly
696         var root = document.documentElement;
697         root.insertBefore( form, root.firstChild );
698
699         // The workaround has to do additional checks after a getElementById
700         // Which slows things down for other browsers (hence the branching)
701         if ( !!document.getElementById( id ) ) {
702                 Expr.find.ID = function(match, context, isXML){
703                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
704                                 var m = context.getElementById(match[1]);
705                                 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
706                         }
707                 };
708
709                 Expr.filter.ID = function(elem, match){
710                         var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
711                         return elem.nodeType === 1 && node && node.nodeValue === match;
712                 };
713         }
714
715         root.removeChild( form );
716 })();
717
718 (function(){
719         // Check to see if the browser returns only elements
720         // when doing getElementsByTagName("*")
721
722         // Create a fake element
723         var div = document.createElement("div");
724         div.appendChild( document.createComment("") );
725
726         // Make sure no comments are found
727         if ( div.getElementsByTagName("*").length > 0 ) {
728                 Expr.find.TAG = function(match, context){
729                         var results = context.getElementsByTagName(match[1]);
730
731                         // Filter out possible comments
732                         if ( match[1] === "*" ) {
733                                 var tmp = [];
734
735                                 for ( var i = 0; results[i]; i++ ) {
736                                         if ( results[i].nodeType === 1 ) {
737                                                 tmp.push( results[i] );
738                                         }
739                                 }
740
741                                 results = tmp;
742                         }
743
744                         return results;
745                 };
746         }
747
748         // Check to see if an attribute returns normalized href attributes
749         div.innerHTML = "<a href='#'></a>";
750         if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
751                         div.firstChild.getAttribute("href") !== "#" ) {
752                 Expr.attrHandle.href = function(elem){
753                         return elem.getAttribute("href", 2);
754                 };
755         }
756 })();
757
758 if ( document.querySelectorAll ) (function(){
759         var oldSizzle = Sizzle, div = document.createElement("div");
760         div.innerHTML = "<p class='TEST'></p>";
761
762         // Safari can't handle uppercase or unicode characters when
763         // in quirks mode.
764         if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
765                 return;
766         }
767         
768         Sizzle = function(query, context, extra, seed){
769                 context = context || document;
770
771                 // Only use querySelectorAll on non-XML documents
772                 // (ID selectors don't work in non-HTML documents)
773                 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
774                         try {
775                                 return makeArray( context.querySelectorAll(query), extra );
776                         } catch(e){}
777                 }
778                 
779                 return oldSizzle(query, context, extra, seed);
780         };
781
782         Sizzle.find = oldSizzle.find;
783         Sizzle.filter = oldSizzle.filter;
784         Sizzle.selectors = oldSizzle.selectors;
785         Sizzle.matches = oldSizzle.matches;
786 })();
787
788 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
789         var div = document.createElement("div");
790         div.innerHTML = "<div class='test e'></div><div class='test'></div>";
791
792         // Opera can't find a second classname (in 9.6)
793         if ( div.getElementsByClassName("e").length === 0 )
794                 return;
795
796         // Safari caches class attributes, doesn't catch changes (in 3.2)
797         div.lastChild.className = "e";
798
799         if ( div.getElementsByClassName("e").length === 1 )
800                 return;
801
802         Expr.order.splice(1, 0, "CLASS");
803         Expr.find.CLASS = function(match, context) {
804                 return context.getElementsByClassName(match[1]);
805         };
806 })();
807
808 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
809         var sibDir = dir == "previousSibling" && !isXML;
810         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
811                 var elem = checkSet[i];
812                 if ( elem ) {
813                         if ( sibDir && elem.nodeType === 1 ){
814                                 elem.sizcache = doneName;
815                                 elem.sizset = i;
816                         }
817                         elem = elem[dir];
818                         var match = false;
819
820                         while ( elem ) {
821                                 if ( elem.sizcache === doneName ) {
822                                         match = checkSet[elem.sizset];
823                                         break;
824                                 }
825
826                                 if ( elem.nodeType === 1 && !isXML ){
827                                         elem.sizcache = doneName;
828                                         elem.sizset = i;
829                                 }
830
831                                 if ( elem.nodeName === cur ) {
832                                         match = elem;
833                                         break;
834                                 }
835
836                                 elem = elem[dir];
837                         }
838
839                         checkSet[i] = match;
840                 }
841         }
842 }
843
844 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
845         var sibDir = dir == "previousSibling" && !isXML;
846         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
847                 var elem = checkSet[i];
848                 if ( elem ) {
849                         if ( sibDir && elem.nodeType === 1 ) {
850                                 elem.sizcache = doneName;
851                                 elem.sizset = i;
852                         }
853                         elem = elem[dir];
854                         var match = false;
855
856                         while ( elem ) {
857                                 if ( elem.sizcache === doneName ) {
858                                         match = checkSet[elem.sizset];
859                                         break;
860                                 }
861
862                                 if ( elem.nodeType === 1 ) {
863                                         if ( !isXML ) {
864                                                 elem.sizcache = doneName;
865                                                 elem.sizset = i;
866                                         }
867                                         if ( typeof cur !== "string" ) {
868                                                 if ( elem === cur ) {
869                                                         match = true;
870                                                         break;
871                                                 }
872
873                                         } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
874                                                 match = elem;
875                                                 break;
876                                         }
877                                 }
878
879                                 elem = elem[dir];
880                         }
881
882                         checkSet[i] = match;
883                 }
884         }
885 }
886
887 var contains = document.compareDocumentPosition ?  function(a, b){
888         return a.compareDocumentPosition(b) & 16;
889 } : function(a, b){
890         return a !== b && (a.contains ? a.contains(b) : true);
891 };
892
893 var isXML = function(elem){
894         return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
895                 !!elem.ownerDocument && isXML( elem.ownerDocument );
896 };
897
898 var posProcess = function(selector, context){
899         var tmpSet = [], later = "", match,
900                 root = context.nodeType ? [context] : context;
901
902         // Position selectors must be done after the filter
903         // And so must :not(positional) so we move all PSEUDOs to the end
904         while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
905                 later += match[0];
906                 selector = selector.replace( Expr.match.PSEUDO, "" );
907         }
908
909         selector = Expr.relative[selector] ? selector + "*" : selector;
910
911         for ( var i = 0, l = root.length; i < l; i++ ) {
912                 Sizzle( selector, root[i], tmpSet );
913         }
914
915         return Sizzle.filter( later, tmpSet );
916 };
917
918 // EXPOSE
919 jQuery.find = Sizzle;
920 jQuery.filter = Sizzle.filter;
921 jQuery.expr = Sizzle.selectors;
922 jQuery.expr[":"] = jQuery.expr.filters;
923
924 Sizzle.selectors.filters.hidden = function(elem){
925         return "hidden" === elem.type ||
926                 jQuery.css(elem, "display") === "none" ||
927                 jQuery.css(elem, "visibility") === "hidden";
928 };
929
930 Sizzle.selectors.filters.visible = function(elem){
931         return "hidden" !== elem.type &&
932                 jQuery.css(elem, "display") !== "none" &&
933                 jQuery.css(elem, "visibility") !== "hidden";
934 };
935
936 Sizzle.selectors.filters.animated = function(elem){
937         return jQuery.grep(jQuery.timers, function(fn){
938                 return elem === fn.elem;
939         }).length;
940 };
941
942 jQuery.multiFilter = function( expr, elems, not ) {
943         if ( not ) {
944                 expr = ":not(" + expr + ")";
945         }
946
947         return Sizzle.matches(expr, elems);
948 };
949
950 jQuery.dir = function( elem, dir ){
951         var matched = [], cur = elem[dir];
952         while ( cur && cur != document ) {
953                 if ( cur.nodeType == 1 )
954                         matched.push( cur );
955                 cur = cur[dir];
956         }
957         return matched;
958 };
959
960 jQuery.nth = function(cur, result, dir, elem){
961         result = result || 1;
962         var num = 0;
963
964         for ( ; cur; cur = cur[dir] )
965                 if ( cur.nodeType == 1 && ++num == result )
966                         break;
967
968         return cur;
969 };
970
971 jQuery.sibling = function(n, elem){
972         var r = [];
973
974         for ( ; n; n = n.nextSibling ) {
975                 if ( n.nodeType == 1 && n != elem )
976                         r.push( n );
977         }
978
979         return r;
980 };
981
982 return;
983
984 window.Sizzle = Sizzle;
985
986 })();