The script prefilter now forces cross-domain requests type to GET.
[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 || 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         args = slice.call(arguments);
208
209                 if ( !runtil.test( name ) ) {
210                         selector = until;
211                 }
212
213                 if ( selector && typeof selector === "string" ) {
214                         ret = jQuery.filter( selector, ret );
215                 }
216
217                 ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret;
218
219                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
220                         ret = ret.reverse();
221                 }
222
223                 return this.pushStack( ret, name, args.join(",") );
224         };
225 });
226
227 jQuery.extend({
228         filter: function( expr, elems, not ) {
229                 if ( not ) {
230                         expr = ":not(" + expr + ")";
231                 }
232
233                 return elems.length === 1 ?
234                         jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
235                         jQuery.find.matches(expr, elems);
236         },
237
238         dir: function( elem, dir, until ) {
239                 var matched = [],
240                         cur = elem[ dir ];
241
242                 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
243                         if ( cur.nodeType === 1 ) {
244                                 matched.push( cur );
245                         }
246                         cur = cur[dir];
247                 }
248                 return matched;
249         },
250
251         nth: function( cur, result, dir, elem ) {
252                 result = result || 1;
253                 var num = 0;
254
255                 for ( ; cur; cur = cur[dir] ) {
256                         if ( cur.nodeType === 1 && ++num === result ) {
257                                 break;
258                         }
259                 }
260
261                 return cur;
262         },
263
264         sibling: function( n, elem ) {
265                 var r = [];
266
267                 for ( ; n; n = n.nextSibling ) {
268                         if ( n.nodeType === 1 && n !== elem ) {
269                                 r.push( n );
270                         }
271                 }
272
273                 return r;
274         }
275 });
276
277 // Implement the identical functionality for filter and not
278 function winnow( elements, qualifier, keep ) {
279         if ( jQuery.isFunction( qualifier ) ) {
280                 return jQuery.grep(elements, function( elem, i ) {
281                         var retVal = !!qualifier.call( elem, i, elem );
282                         return retVal === keep;
283                 });
284
285         } else if ( qualifier.nodeType ) {
286                 return jQuery.grep(elements, function( elem, i ) {
287                         return (elem === qualifier) === keep;
288                 });
289
290         } else if ( typeof qualifier === "string" ) {
291                 var filtered = jQuery.grep(elements, function( elem ) {
292                         return elem.nodeType === 1;
293                 });
294
295                 if ( isSimple.test( qualifier ) ) {
296                         return jQuery.filter(qualifier, filtered, !keep);
297                 } else {
298                         qualifier = jQuery.filter( qualifier, filtered );
299                 }
300         }
301
302         return jQuery.grep(elements, function( elem, i ) {
303                 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
304         });
305 }
306
307 })( jQuery );