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