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