4 rparentsprev = /^(?:parents|prevUntil|prevAll)/,
5 // Note: This RegExp should be improved, or likely pulled from Sizzle
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
19 find: function( selector ) {
20 var ret = this.pushStack( "", "find", selector ),
23 for ( var i = 0, l = this.length; i < l; i++ ) {
25 jQuery.find( selector, this[i], ret );
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] ) {
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] ) ) {
54 not: function( selector ) {
55 return this.pushStack( winnow(this, selector, false), "not", selector);
58 filter: function( selector ) {
59 return this.pushStack( winnow(this, selector, true), "filter", selector );
62 is: function( selector ) {
63 return !!selector && jQuery.filter( selector, this ).length > 0;
66 closest: function( selectors, context ) {
67 var ret = [], i, l, cur = this[0];
69 if ( jQuery.isArray( selectors ) ) {
74 if ( cur && selectors.length ) {
75 for ( i = 0, l = selectors.length; i < l; i++ ) {
76 selector = selectors[i];
78 if ( !matches[selector] ) {
79 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
80 jQuery( selector, context || this.context ) :
85 while ( cur && cur.ownerDocument && cur !== context ) {
86 for ( selector in matches ) {
87 match = matches[selector];
89 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
90 ret.push({ selector: selector, elem: cur, level: level });
102 var pos = POS.test( selectors ) ?
103 jQuery( selectors, context || this.context ) : null;
105 for ( i = 0, l = this.length; i < l; i++ ) {
109 if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
114 cur = cur.parentNode;
115 if ( !cur || !cur.ownerDocument || cur === context ) {
122 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
124 return this.pushStack( ret, "closest", selectors );
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() );
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 );
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 );
148 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
150 jQuery.unique( all ) );
153 andSelf: function() {
154 return this.add( this.prevObject );
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;
165 parent: function( elem ) {
166 var parent = elem.parentNode;
167 return parent && parent.nodeType !== 11 ? parent : null;
169 parents: function( elem ) {
170 return jQuery.dir( elem, "parentNode" );
172 parentsUntil: function( elem, i, until ) {
173 return jQuery.dir( elem, "parentNode", until );
175 next: function( elem ) {
176 return jQuery.nth( elem, 2, "nextSibling" );
178 prev: function( elem ) {
179 return jQuery.nth( elem, 2, "previousSibling" );
181 nextAll: function( elem ) {
182 return jQuery.dir( elem, "nextSibling" );
184 prevAll: function( elem ) {
185 return jQuery.dir( elem, "previousSibling" );
187 nextUntil: function( elem, i, until ) {
188 return jQuery.dir( elem, "nextSibling", until );
190 prevUntil: function( elem, i, until ) {
191 return jQuery.dir( elem, "previousSibling", until );
193 siblings: function( elem ) {
194 return jQuery.sibling( elem.parentNode.firstChild, elem );
196 children: function( elem ) {
197 return jQuery.sibling( elem.firstChild );
199 contents: function( elem ) {
200 return jQuery.nodeName( elem, "iframe" ) ?
201 elem.contentDocument || elem.contentWindow.document :
202 jQuery.makeArray( elem.childNodes );
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);
213 if ( !runtil.test( name ) ) {
217 if ( selector && typeof selector === "string" ) {
218 ret = jQuery.filter( selector, ret );
221 ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret;
223 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
227 return this.pushStack( ret, name, args.join(",") );
232 filter: function( expr, elems, not ) {
234 expr = ":not(" + expr + ")";
237 return elems.length === 1 ?
238 jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
239 jQuery.find.matches(expr, elems);
242 dir: function( elem, dir, until ) {
246 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
247 if ( cur.nodeType === 1 ) {
255 nth: function( cur, result, dir, elem ) {
256 result = result || 1;
259 for ( ; cur; cur = cur[dir] ) {
260 if ( cur.nodeType === 1 && ++num === result ) {
268 sibling: function( n, elem ) {
271 for ( ; n; n = n.nextSibling ) {
272 if ( n.nodeType === 1 && n !== elem ) {
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;
289 } else if ( qualifier.nodeType ) {
290 return jQuery.grep(elements, function( elem, i ) {
291 return (elem === qualifier) === keep;
294 } else if ( typeof qualifier === "string" ) {
295 var filtered = jQuery.grep(elements, function( elem ) {
296 return elem.nodeType === 1;
299 if ( isSimple.test( qualifier ) ) {
300 return jQuery.filter(qualifier, filtered, !keep);
302 qualifier = jQuery.filter( qualifier, filtered );
306 return jQuery.grep(elements, function( elem, i ) {
307 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;