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