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