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