Moved to a generic solution for copying methods over for querySelectorAll-using browsers.
[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         arraySplice = Array.prototype.splice,
13         arrayPush = Array.prototype.push,
14         arraySort = Array.prototype.sort;
15
16 var Sizzle = function(selector, context, results, seed) {
17         results = results || [];
18         var origContext = context = context || document;
19
20         if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
21                 return [];
22         }
23         
24         if ( !selector || typeof selector !== "string" ) {
25                 return results;
26         }
27
28         var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
29         
30         // Reset the position of the chunker regexp (start from head)
31         chunker.lastIndex = 0;
32         
33         while ( (m = chunker.exec(selector)) !== null ) {
34                 parts.push( m[1] );
35                 
36                 if ( m[2] ) {
37                         extra = RegExp.rightContext;
38                         break;
39                 }
40         }
41
42         if ( parts.length > 1 && origPOS.exec( selector ) ) {
43                 if ( parts.length === 2 && Expr.relative[ parts[0] ] ) {
44                         set = posProcess( parts[0] + parts[1], context );
45                 } else {
46                         set = Expr.relative[ parts[0] ] ?
47                                 [ context ] :
48                                 Sizzle( parts.shift(), context );
49
50                         while ( parts.length ) {
51                                 selector = parts.shift();
52
53                                 if ( Expr.relative[ selector ] )
54                                         selector += parts.shift();
55
56                                 set = posProcess( selector, set );
57                         }
58                 }
59         } else {
60                 // Take a shortcut and set the context if the root selector is an ID
61                 // (but not if it'll be faster if the inner selector is an ID)
62                 if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
63                                 Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
64                         var ret = Sizzle.find( parts.shift(), context, contextXML );
65                         context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
66                 }
67
68                 if ( context ) {
69                         var ret = seed ?
70                                 { expr: parts.pop(), set: makeArray(seed) } :
71                                 Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML );
72                         set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : 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, contextXML );
94                         }
95                 } else {
96                         checkSet = parts = [];
97                 }
98         }
99
100         if ( !checkSet ) {
101                 checkSet = set;
102         }
103
104         if ( !checkSet ) {
105                 throw "Syntax error, unrecognized expression: " + (cur || selector);
106         }
107
108         if ( toString.call(checkSet) === "[object Array]" ) {
109                 if ( !prune ) {
110                         arrayPush.apply( results, checkSet );
111                 } else if ( context && context.nodeType === 1 ) {
112                         for ( var i = 0; checkSet[i] != null; i++ ) {
113                                 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
114                                         arrayPush.call( results, set[i] );
115                                 }
116                         }
117                 } else {
118                         for ( var i = 0; checkSet[i] != null; i++ ) {
119                                 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
120                                         arrayPush.call( results, set[i] );
121                                 }
122                         }
123                 }
124         } else {
125                 makeArray( checkSet, results );
126         }
127
128         if ( extra ) {
129                 Sizzle( extra, origContext, results, seed );
130                 Sizzle.uniqueSort( results );
131         }
132
133         return results;
134 };
135
136 Sizzle.uniqueSort = function(results){
137         if ( sortOrder ) {
138                 hasDuplicate = false;
139                 arraySort.call(results, sortOrder);
140
141                 if ( hasDuplicate ) {
142                         for ( var i = 1; i < results.length; i++ ) {
143                                 if ( results[i] === results[i-1] ) {
144                                         arraySplice.call(results, i--, 1);
145                                 }
146                         }
147                 }
148         }
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 ( !part.match(/\W/) ) {
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" && !part.match(/\W/) ) {
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 ( match[3].match(chunker).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 );
667
668         if ( results ) {
669                 arrayPush.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 );
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                 var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
709                 if ( ret === 0 ) {
710                         hasDuplicate = true;
711                 }
712                 return ret;
713         };
714 } else if ( "sourceIndex" in document.documentElement ) {
715         sortOrder = function( a, b ) {
716                 var ret = a.sourceIndex - b.sourceIndex;
717                 if ( ret === 0 ) {
718                         hasDuplicate = true;
719                 }
720                 return ret;
721         };
722 } else if ( document.createRange ) {
723         sortOrder = function( a, b ) {
724                 var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
725                 aRange.selectNode(a);
726                 aRange.collapse(true);
727                 bRange.selectNode(b);
728                 bRange.collapse(true);
729                 var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
730                 if ( ret === 0 ) {
731                         hasDuplicate = true;
732                 }
733                 return ret;
734         };
735 }
736
737 // Check to see if the browser returns elements by name when
738 // querying by getElementById (and provide a workaround)
739 (function(){
740         // We're going to inject a fake input element with a specified name
741         var form = document.createElement("form"),
742                 id = "script" + (new Date).getTime();
743         form.innerHTML = "<input name='" + id + "'/>";
744
745         // Inject it into the root element, check its status, and remove it quickly
746         var root = document.documentElement;
747         root.insertBefore( form, root.firstChild );
748
749         // The workaround has to do additional checks after a getElementById
750         // Which slows things down for other browsers (hence the branching)
751         if ( !!document.getElementById( id ) ) {
752                 Expr.find.ID = function(match, context, isXML){
753                         if ( typeof context.getElementById !== "undefined" && !isXML ) {
754                                 var m = context.getElementById(match[1]);
755                                 return m ? m.id === match[1] || typeof m.getAttributeNode !== "undefined" && m.getAttributeNode("id").nodeValue === match[1] ? [m] : undefined : [];
756                         }
757                 };
758
759                 Expr.filter.ID = function(elem, match){
760                         var node = typeof elem.getAttributeNode !== "undefined" && elem.getAttributeNode("id");
761                         return elem.nodeType === 1 && node && node.nodeValue === match;
762                 };
763         }
764
765         root.removeChild( form );
766 })();
767
768 (function(){
769         // Check to see if the browser returns only elements
770         // when doing getElementsByTagName("*")
771
772         // Create a fake element
773         var div = document.createElement("div");
774         div.appendChild( document.createComment("") );
775
776         // Make sure no comments are found
777         if ( div.getElementsByTagName("*").length > 0 ) {
778                 Expr.find.TAG = function(match, context){
779                         var results = context.getElementsByTagName(match[1]);
780
781                         // Filter out possible comments
782                         if ( match[1] === "*" ) {
783                                 var tmp = [];
784
785                                 for ( var i = 0; results[i]; i++ ) {
786                                         if ( results[i].nodeType === 1 ) {
787                                                 tmp.push( results[i] );
788                                         }
789                                 }
790
791                                 results = tmp;
792                         }
793
794                         return results;
795                 };
796         }
797
798         // Check to see if an attribute returns normalized href attributes
799         div.innerHTML = "<a href='#'></a>";
800         if ( div.firstChild && typeof div.firstChild.getAttribute !== "undefined" &&
801                         div.firstChild.getAttribute("href") !== "#" ) {
802                 Expr.attrHandle.href = function(elem){
803                         return elem.getAttribute("href", 2);
804                 };
805         }
806 })();
807
808 if ( document.querySelectorAll ) (function(){
809         var oldSizzle = Sizzle, div = document.createElement("div");
810         div.innerHTML = "<p class='TEST'></p>";
811
812         // Safari can't handle uppercase or unicode characters when
813         // in quirks mode.
814         if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
815                 return;
816         }
817         
818         Sizzle = function(query, context, extra, seed){
819                 context = context || document;
820
821                 // Only use querySelectorAll on non-XML documents
822                 // (ID selectors don't work in non-HTML documents)
823                 if ( !seed && context.nodeType === 9 && !isXML(context) ) {
824                         try {
825                                 return makeArray( context.querySelectorAll(query), extra );
826                         } catch(e){}
827                 }
828                 
829                 return oldSizzle(query, context, extra, seed);
830         };
831
832         for ( var prop in oldSizzle ) {
833                 Sizzle[ prop ] = oldSizzle[ prop ];
834         }
835 })();
836
837 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
838         var div = document.createElement("div");
839         div.innerHTML = "<div class='test e'></div><div class='test'></div>";
840
841         // Opera can't find a second classname (in 9.6)
842         if ( div.getElementsByClassName("e").length === 0 )
843                 return;
844
845         // Safari caches class attributes, doesn't catch changes (in 3.2)
846         div.lastChild.className = "e";
847
848         if ( div.getElementsByClassName("e").length === 1 )
849                 return;
850
851         Expr.order.splice(1, 0, "CLASS");
852         Expr.find.CLASS = function(match, context, isXML) {
853                 if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) {
854                         return context.getElementsByClassName(match[1]);
855                 }
856         };
857 })();
858
859 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
860         var sibDir = dir == "previousSibling" && !isXML;
861         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
862                 var elem = checkSet[i];
863                 if ( elem ) {
864                         if ( sibDir && elem.nodeType === 1 ){
865                                 elem.sizcache = doneName;
866                                 elem.sizset = i;
867                         }
868                         elem = elem[dir];
869                         var match = false;
870
871                         while ( elem ) {
872                                 if ( elem.sizcache === doneName ) {
873                                         match = checkSet[elem.sizset];
874                                         break;
875                                 }
876
877                                 if ( elem.nodeType === 1 && !isXML ){
878                                         elem.sizcache = doneName;
879                                         elem.sizset = i;
880                                 }
881
882                                 if ( elem.nodeName === cur ) {
883                                         match = elem;
884                                         break;
885                                 }
886
887                                 elem = elem[dir];
888                         }
889
890                         checkSet[i] = match;
891                 }
892         }
893 }
894
895 function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
896         var sibDir = dir == "previousSibling" && !isXML;
897         for ( var i = 0, l = checkSet.length; i < l; i++ ) {
898                 var elem = checkSet[i];
899                 if ( elem ) {
900                         if ( sibDir && elem.nodeType === 1 ) {
901                                 elem.sizcache = doneName;
902                                 elem.sizset = i;
903                         }
904                         elem = elem[dir];
905                         var match = false;
906
907                         while ( elem ) {
908                                 if ( elem.sizcache === doneName ) {
909                                         match = checkSet[elem.sizset];
910                                         break;
911                                 }
912
913                                 if ( elem.nodeType === 1 ) {
914                                         if ( !isXML ) {
915                                                 elem.sizcache = doneName;
916                                                 elem.sizset = i;
917                                         }
918                                         if ( typeof cur !== "string" ) {
919                                                 if ( elem === cur ) {
920                                                         match = true;
921                                                         break;
922                                                 }
923
924                                         } else if ( Sizzle.filter( cur, [elem] ).length > 0 ) {
925                                                 match = elem;
926                                                 break;
927                                         }
928                                 }
929
930                                 elem = elem[dir];
931                         }
932
933                         checkSet[i] = match;
934                 }
935         }
936 }
937
938 var contains = document.compareDocumentPosition ?  function(a, b){
939         return a.compareDocumentPosition(b) & 16;
940 } : function(a, b){
941         return a !== b && (a.contains ? a.contains(b) : true);
942 };
943
944 var isXML = function(elem){
945         return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
946                 !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
947 };
948
949 var posProcess = function(selector, context){
950         var tmpSet = [], later = "", match,
951                 root = context.nodeType ? [context] : context;
952
953         // Position selectors must be done after the filter
954         // And so must :not(positional) so we move all PSEUDOs to the end
955         while ( (match = Expr.match.PSEUDO.exec( selector )) ) {
956                 later += match[0];
957                 selector = selector.replace( Expr.match.PSEUDO, "" );
958         }
959
960         selector = Expr.relative[selector] ? selector + "*" : selector;
961
962         for ( var i = 0, l = root.length; i < l; i++ ) {
963                 Sizzle( selector, root[i], tmpSet );
964         }
965
966         return Sizzle.filter( later, tmpSet );
967 };
968
969 // EXPOSE
970 jQuery.find = Sizzle;
971 jQuery.expr = Sizzle.selectors;
972 jQuery.expr[":"] = jQuery.expr.filters;
973
974 Sizzle.selectors.filters.hidden = function(elem){
975         return elem.offsetWidth === 0 || elem.offsetHeight === 0;
976 };
977
978 Sizzle.selectors.filters.visible = function(elem){
979         return elem.offsetWidth > 0 || elem.offsetHeight > 0;
980 };
981
982 Sizzle.selectors.filters.animated = function(elem){
983         return jQuery.grep(jQuery.timers, function(fn){
984                 return elem === fn.elem;
985         }).length;
986 };
987
988 jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
989         if ( not ) {
990                 expr = ":not(" + expr + ")";
991         }
992
993         return Sizzle.matches(expr, elems);
994 };
995
996 jQuery.dir = function( elem, dir ){
997         var matched = [], cur = elem[dir];
998         while ( cur && cur != document ) {
999                 if ( cur.nodeType == 1 )
1000                         matched.push( cur );
1001                 cur = cur[dir];
1002         }
1003         return matched;
1004 };
1005
1006 jQuery.nth = function(cur, result, dir, elem){
1007         result = result || 1;
1008         var num = 0;
1009
1010         for ( ; cur; cur = cur[dir] )
1011                 if ( cur.nodeType == 1 && ++num == result )
1012                         break;
1013
1014         return cur;
1015 };
1016
1017 jQuery.sibling = function(n, elem){
1018         var r = [];
1019
1020         for ( ; n; n = n.nextSibling ) {
1021                 if ( n.nodeType == 1 && n != elem )
1022                         r.push( n );
1023         }
1024
1025         return r;
1026 };
1027
1028 return;
1029
1030 window.Sizzle = Sizzle;
1031
1032 })();