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