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