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