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