6375d9d1f3f6e29c25161d8cb53911710793d026
[jquery.git] / traversing.js
1 var runtil = /Until$/,
2         rparentsprev = /^(?:parents|prevUntil|prevAll)/,
3         // Note: This RegExp should be improved, or likely pulled from Sizzle
4         rmultiselector = /,/,
5         slice = Array.prototype.slice;
6
7 // Implement the identical functionality for filter and not
8 var winnow = function( elements, qualifier, keep ) {
9         if ( jQuery.isFunction( qualifier ) ) {
10                 return jQuery.grep(elements, function(elem, i) {
11                         return !!qualifier.call( elem, i, elem ) === keep;
12                 });
13
14         } else if ( qualifier.nodeType ) {
15                 return jQuery.grep(elements, function(elem, i) {
16                         return (elem === qualifier) === keep;
17                 });
18
19         } else if ( typeof qualifier === "string" ) {
20                 var filtered = jQuery.grep(elements, function(elem) {
21                         return elem.nodeType === 1;
22                 });
23
24                 if ( isSimple.test( qualifier ) ) {
25                         return jQuery.filter(qualifier, filtered, !keep);
26                 } else {
27                         qualifier = jQuery.filter( qualifier, elements );
28                 }
29         }
30
31         return jQuery.grep(elements, function(elem, i) {
32                 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
33         });
34 };
35
36 jQuery.fn.extend({
37         find: function( selector ) {
38                 var ret = this.pushStack( "", "find", selector ), length = 0;
39
40                 for ( var i = 0, l = this.length; i < l; i++ ) {
41                         length = ret.length;
42                         jQuery.find( selector, this[i], ret );
43
44                         if ( i > 0 ) {
45                                 // Make sure that the results are unique
46                                 for ( var n = length; n < ret.length; n++ ) {
47                                         for ( var r = 0; r < length; r++ ) {
48                                                 if ( ret[r] === ret[n] ) {
49                                                         ret.splice(n--, 1);
50                                                         break;
51                                                 }
52                                         }
53                                 }
54                         }
55                 }
56
57                 return ret;
58         },
59
60         has: function( target ) {
61                 var targets = jQuery( target );
62                 return this.filter(function() {
63                         for ( var i = 0, l = targets.length; i < l; i++ ) {
64                                 if ( jQuery.contains( this, targets[i] ) ) {
65                                         return true;
66                                 }
67                         }
68                 });
69         },
70
71         contains: function( target ) {
72                 return this.has( target ).length > 0;
73         },
74
75         not: function( selector ) {
76                 return this.pushStack( winnow(this, selector, false), "not", selector);
77         },
78
79         filter: function( selector ) {
80                 return this.pushStack( winnow(this, selector, true), "filter", selector );
81         },
82         
83         is: function( selector ) {
84                 return !!selector && jQuery.filter( selector, this ).length > 0;
85         },
86
87         closest: function( selectors, context ) {
88                 if ( jQuery.isArray( selectors ) ) {
89                         var ret = [], cur = this[0], match, matches = {}, selector;
90
91                         if ( cur && selectors.length ) {
92                                 for ( var i = 0, l = selectors.length; i < l; i++ ) {
93                                         selector = selectors[i];
94
95                                         if ( !matches[selector] ) {
96                                                 matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
97                                                         jQuery( selector, context || this.context ) :
98                                                         selector;
99                                         }
100                                 }
101
102                                 while ( cur && cur.ownerDocument && cur !== context ) {
103                                         for ( selector in matches ) {
104                                                 match = matches[selector];
105
106                                                 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
107                                                         ret.push({ selector: selector, elem: cur });
108                                                         delete matches[selector];
109                                                 }
110                                         }
111                                         cur = cur.parentNode;
112                                 }
113                         }
114
115                         return ret;
116                 }
117
118                 var pos = jQuery.expr.match.POS.test( selectors ) ? 
119                         jQuery( selectors, context || this.context ) : null;
120
121                 return this.map(function(i, cur){
122                         while ( cur && cur.ownerDocument && cur !== context ) {
123                                 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
124                                         return cur;
125                                 }
126                                 cur = cur.parentNode;
127                         }
128                         return null;
129                 });
130         },
131         
132         // Determine the position of an element within
133         // the matched set of elements
134         index: function( elem ) {
135                 if ( !elem || typeof elem === "string" ) {
136                         return jQuery.inArray( this[0],
137                                 // If it receives a string, the selector is used
138                                 // If it receives nothing, the siblings are used
139                                 elem ? jQuery( elem ) : this.parent().children() );
140                 }
141                 // Locate the position of the desired element
142                 return jQuery.inArray(
143                         // If it receives a jQuery object, the first element is used
144                         elem.jquery ? elem[0] : elem, this );
145         },
146
147         add: function( selector, context ) {
148                 var set = typeof selector === "string" ?
149                                 jQuery( selector, context || this.context ) :
150                                 jQuery.makeArray( selector ),
151                         all = jQuery.merge( this.get(), set );
152
153                 return this.pushStack( set[0] && (set[0].setInterval || set[0].nodeType === 9 || (set[0].parentNode && set[0].parentNode.nodeType !== 11)) ?
154                         jQuery.unique( all ) :
155                         all );
156         },
157
158         andSelf: function() {
159                 return this.add( this.prevObject );
160         }
161 });
162
163 jQuery.each({
164         parent: function(elem){return elem.parentNode;},
165         parents: function(elem){return jQuery.dir(elem,"parentNode");},
166         parentsUntil: function(elem,i,until){return jQuery.dir(elem,"parentNode",until);},
167         next: function(elem){return jQuery.nth(elem,2,"nextSibling");},
168         prev: function(elem){return jQuery.nth(elem,2,"previousSibling");},
169         nextAll: function(elem){return jQuery.dir(elem,"nextSibling");},
170         prevAll: function(elem){return jQuery.dir(elem,"previousSibling");},
171         nextUntil: function(elem,i,until){return jQuery.dir(elem,"nextSibling",until);},
172         prevUntil: function(elem,i,until){return jQuery.dir(elem,"previousSibling",until);},
173         siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);},
174         children: function(elem){return jQuery.sibling(elem.firstChild);},
175         contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);}
176 }, function(name, fn){
177         jQuery.fn[ name ] = function( until, selector ) {
178                 var ret = jQuery.map( this, fn, until );
179                 
180                 if ( !runtil.test( name ) ) {
181                         selector = until;
182                 }
183
184                 if ( selector && typeof selector === "string" ) {
185                         ret = jQuery.filter( selector, ret );
186                 }
187
188                 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
189
190                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
191                         ret = ret.reverse();
192                 }
193
194                 return this.pushStack( ret, name, slice.call(arguments).join(",") );
195         };
196 });
197
198 jQuery.extend({
199         filter: function( expr, elems, not ) {
200                 if ( not ) {
201                         expr = ":not(" + expr + ")";
202                 }
203
204                 return jQuery.find.matches(expr, elems);
205         },
206         
207         dir: function( elem, dir, until ) {
208                 var matched = [], cur = elem[dir];
209                 while ( cur && cur.nodeType !== 9 && (until === undefined || !jQuery( cur ).is( until )) ) {
210                         if ( cur.nodeType === 1 ) {
211                                 matched.push( cur );
212                         }
213                         cur = cur[dir];
214                 }
215                 return matched;
216         },
217
218         nth: function( cur, result, dir, elem ) {
219                 result = result || 1;
220                 var num = 0;
221
222                 for ( ; cur; cur = cur[dir] ) {
223                         if ( cur.nodeType === 1 && ++num === result ) {
224                                 break;
225                         }
226                 }
227
228                 return cur;
229         },
230
231         sibling: function( n, elem ) {
232                 var r = [];
233
234                 for ( ; n; n = n.nextSibling ) {
235                         if ( n.nodeType === 1 && n !== elem ) {
236                                 r.push( n );
237                         }
238                 }
239
240                 return r;
241         }
242 });