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