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