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