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