Adds missing crossDomain test.
[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         // methods guaranteed to produce a unique set when starting from a unique set
11         guaranteedUnique = {
12                 children: true,
13                 contents: true,
14                 next: true,
15                 prev: true
16         };
17
18 jQuery.fn.extend({
19         find: function( selector ) {
20                 var ret = this.pushStack( "", "find", selector ),
21                         length = 0;
22
23                 for ( var i = 0, l = this.length; i < l; i++ ) {
24                         length = ret.length;
25                         jQuery.find( selector, this[i], ret );
26
27                         if ( i > 0 ) {
28                                 // Make sure that the results are unique
29                                 for ( var n = length; n < ret.length; n++ ) {
30                                         for ( var r = 0; r < length; r++ ) {
31                                                 if ( ret[r] === ret[n] ) {
32                                                         ret.splice(n--, 1);
33                                                         break;
34                                                 }
35                                         }
36                                 }
37                         }
38                 }
39
40                 return ret;
41         },
42
43         has: function( target ) {
44                 var targets = jQuery( target );
45                 return this.filter(function() {
46                         for ( var i = 0, l = targets.length; i < l; i++ ) {
47                                 if ( jQuery.contains( this, targets[i] ) ) {
48                                         return true;
49                                 }
50                         }
51                 });
52         },
53
54         not: function( selector ) {
55                 return this.pushStack( winnow(this, selector, false), "not", selector);
56         },
57
58         filter: function( selector ) {
59                 return this.pushStack( winnow(this, selector, true), "filter", selector );
60         },
61
62         is: function( selector ) {
63                 return !!selector && jQuery.filter( selector, this ).length > 0;
64         },
65
66         closest: function( selectors, context ) {
67                 var ret = [], i, l, cur = this[0];
68
69                 if ( jQuery.isArray( selectors ) ) {
70                         var match, selector,
71                                 matches = {},
72                                 level = 1;
73
74                         if ( cur && selectors.length ) {
75                                 for ( i = 0, l = selectors.length; i < l; i++ ) {
76                                         selector = selectors[i];
77
78                                         if ( !matches[selector] ) {
79                                                 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
80                                                         jQuery( selector, context || this.context ) :
81                                                         selector;
82                                         }
83                                 }
84
85                                 while ( cur && cur.ownerDocument && cur !== context ) {
86                                         for ( selector in matches ) {
87                                                 match = matches[selector];
88
89                                                 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
90                                                         ret.push({ selector: selector, elem: cur, level: level });
91                                                 }
92                                         }
93
94                                         cur = cur.parentNode;
95                                         level++;
96                                 }
97                         }
98
99                         return ret;
100                 }
101
102                 var pos = POS.test( selectors ) ?
103                         jQuery( selectors, context || this.context ) : null;
104
105                 for ( i = 0, l = this.length; i < l; i++ ) {
106                         cur = this[i];
107
108                         while ( cur ) {
109                                 if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
110                                         ret.push( cur );
111                                         break;
112
113                                 } else {
114                                         cur = cur.parentNode;
115                                         if ( !cur || !cur.ownerDocument || cur === context ) {
116                                                 break;
117                                         }
118                                 }
119                         }
120                 }
121
122                 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
123
124                 return this.pushStack( ret, "closest", selectors );
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 ) :
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                 // The variable 'args' was introduced in
208                 // https://github.com/jquery/jquery/commit/52a0238
209                 // to work around a bug in Chrome 10 (Dev) and should be removed when the bug is fixed.
210                 // http://code.google.com/p/v8/issues/detail?id=1050
211                     args = slice.call(arguments);
212
213                 if ( !runtil.test( name ) ) {
214                         selector = until;
215                 }
216
217                 if ( selector && typeof selector === "string" ) {
218                         ret = jQuery.filter( selector, ret );
219                 }
220
221                 ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret;
222
223                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
224                         ret = ret.reverse();
225                 }
226
227                 return this.pushStack( ret, name, args.join(",") );
228         };
229 });
230
231 jQuery.extend({
232         filter: function( expr, elems, not ) {
233                 if ( not ) {
234                         expr = ":not(" + expr + ")";
235                 }
236
237                 return elems.length === 1 ?
238                         jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
239                         jQuery.find.matches(expr, elems);
240         },
241
242         dir: function( elem, dir, until ) {
243                 var matched = [],
244                         cur = elem[ dir ];
245
246                 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
247                         if ( cur.nodeType === 1 ) {
248                                 matched.push( cur );
249                         }
250                         cur = cur[dir];
251                 }
252                 return matched;
253         },
254
255         nth: function( cur, result, dir, elem ) {
256                 result = result || 1;
257                 var num = 0;
258
259                 for ( ; cur; cur = cur[dir] ) {
260                         if ( cur.nodeType === 1 && ++num === result ) {
261                                 break;
262                         }
263                 }
264
265                 return cur;
266         },
267
268         sibling: function( n, elem ) {
269                 var r = [];
270
271                 for ( ; n; n = n.nextSibling ) {
272                         if ( n.nodeType === 1 && n !== elem ) {
273                                 r.push( n );
274                         }
275                 }
276
277                 return r;
278         }
279 });
280
281 // Implement the identical functionality for filter and not
282 function winnow( elements, qualifier, keep ) {
283         if ( jQuery.isFunction( qualifier ) ) {
284                 return jQuery.grep(elements, function( elem, i ) {
285                         var retVal = !!qualifier.call( elem, i, elem );
286                         return retVal === keep;
287                 });
288
289         } else if ( qualifier.nodeType ) {
290                 return jQuery.grep(elements, function( elem, i ) {
291                         return (elem === qualifier) === keep;
292                 });
293
294         } else if ( typeof qualifier === "string" ) {
295                 var filtered = jQuery.grep(elements, function( elem ) {
296                         return elem.nodeType === 1;
297                 });
298
299                 if ( isSimple.test( qualifier ) ) {
300                         return jQuery.filter(qualifier, filtered, !keep);
301                 } else {
302                         qualifier = jQuery.filter( qualifier, filtered );
303                 }
304         }
305
306         return jQuery.grep(elements, function( elem, i ) {
307                 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
308         });
309 }
310
311 })( jQuery );