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