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