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