4 rparentsprev = /^(?:parents|prevUntil|prevAll)/,
5 // Note: This RegExp should be improved, or likely pulled from Sizzle
7 isSimple = /^.[^:#\[\.,]*$/,
8 slice = Array.prototype.slice;
11 find: function( selector ) {
12 var ret = this.pushStack( "", "find", selector ), length = 0;
14 for ( var i = 0, l = this.length; i < l; i++ ) {
16 jQuery.find( selector, this[i], ret );
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] ) {
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] ) ) {
45 not: function( selector ) {
46 return this.pushStack( winnow(this, selector, false), "not", selector);
49 filter: function( selector ) {
50 return this.pushStack( winnow(this, selector, true), "filter", selector );
53 is: function( selector ) {
54 return !!selector && jQuery.filter( selector, this ).length > 0;
57 closest: function( selectors, context ) {
60 if ( jQuery.isArray( selectors ) ) {
61 var cur = this[0], match, matches = {}, selector, level = 1;
64 if ( cur && selectors.length ) {
65 for ( var i = 0, l = selectors.length; i < l; i++ ) {
66 selector = selectors[i];
68 if ( !matches[selector] ) {
69 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
70 jQuery( selector, context || this.context ) :
75 while ( cur && cur.ownerDocument && cur !== context ) {
76 for ( selector in matches ) {
77 match = matches[selector];
79 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
80 ret.push({ selector: selector, elem: cur, level: level });
92 var pos = jQuery.expr.match.POS.test( selectors ) ?
93 jQuery( selectors, context || this.context ) : null;
95 ret = jQuery.map(this.get(),function( cur,i ) {
96 while ( cur && cur.ownerDocument && cur !== context ) {
97 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
101 cur = cur.parentNode;
107 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
109 return this.pushStack( ret, "closest", selectors );
112 // Determine the position of an element within
113 // the matched set of elements
114 index: function( elem ) {
115 if ( !elem || typeof elem === "string" ) {
116 return jQuery.inArray( this[0],
117 // If it receives a string, the selector is used
118 // If it receives nothing, the siblings are used
119 elem ? jQuery( elem ) : this.parent().children() );
121 // Locate the position of the desired element
122 return jQuery.inArray(
123 // If it receives a jQuery object, the first element is used
124 elem.jquery ? elem[0] : elem, this );
127 add: function( selector, context ) {
128 var set = typeof selector === "string" ?
129 jQuery( selector, context || this.context ) :
130 jQuery.makeArray( selector ),
131 all = jQuery.merge( this.get(), set );
133 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
135 jQuery.unique( all ) );
138 andSelf: function() {
139 return this.add( this.prevObject );
143 // A painfully simple check to see if an element is disconnected
144 // from a document (should be improved, where feasible).
145 function isDisconnected( node ) {
146 return !node || !node.parentNode || node.parentNode.nodeType === 11;
150 parent: function( elem ) {
151 var parent = elem.parentNode;
152 return parent && parent.nodeType !== 11 ? parent : null;
154 parents: function( elem ) {
155 return jQuery.dir( elem, "parentNode" );
157 parentsUntil: function( elem, i, until ) {
158 return jQuery.dir( elem, "parentNode", until );
160 next: function( elem ) {
161 return jQuery.nth( elem, 2, "nextSibling" );
163 prev: function( elem ) {
164 return jQuery.nth( elem, 2, "previousSibling" );
166 nextAll: function( elem ) {
167 return jQuery.dir( elem, "nextSibling" );
169 prevAll: function( elem ) {
170 return jQuery.dir( elem, "previousSibling" );
172 nextUntil: function( elem, i, until ) {
173 return jQuery.dir( elem, "nextSibling", until );
175 prevUntil: function( elem, i, until ) {
176 return jQuery.dir( elem, "previousSibling", until );
178 siblings: function( elem ) {
179 return jQuery.sibling( elem.parentNode.firstChild, elem );
181 children: function( elem ) {
182 return jQuery.sibling( elem.firstChild );
184 contents: function( elem ) {
185 return jQuery.nodeName( elem, "iframe" ) ?
186 elem.contentDocument || elem.contentWindow.document :
187 jQuery.makeArray( elem.childNodes );
189 }, function( name, fn ) {
190 jQuery.fn[ name ] = function( until, selector ) {
191 var ret = jQuery.map( this, fn, until );
193 if ( !runtil.test( name ) ) {
197 if ( selector && typeof selector === "string" ) {
198 ret = jQuery.filter( selector, ret );
201 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
203 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
207 return this.pushStack( ret, name, slice.call(arguments).join(",") );
212 filter: function( expr, elems, not ) {
214 expr = ":not(" + expr + ")";
217 return jQuery.find.matches(expr, elems);
220 dir: function( elem, dir, until ) {
221 var matched = [], cur = elem[dir];
222 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
223 if ( cur.nodeType === 1 ) {
231 nth: function( cur, result, dir, elem ) {
232 result = result || 1;
235 for ( ; cur; cur = cur[dir] ) {
236 if ( cur.nodeType === 1 && ++num === result ) {
244 sibling: function( n, elem ) {
247 for ( ; n; n = n.nextSibling ) {
248 if ( n.nodeType === 1 && n !== elem ) {
257 // Implement the identical functionality for filter and not
258 function winnow( elements, qualifier, keep ) {
259 if ( jQuery.isFunction( qualifier ) ) {
260 return jQuery.grep(elements, function( elem, i ) {
261 var retVal = !!qualifier.call( elem, i, elem );
262 return retVal === keep;
265 } else if ( qualifier.nodeType ) {
266 return jQuery.grep(elements, function( elem, i ) {
267 return (elem === qualifier) === keep;
270 } else if ( typeof qualifier === "string" ) {
271 var filtered = jQuery.grep(elements, function( elem ) {
272 return elem.nodeType === 1;
275 if ( isSimple.test( qualifier ) ) {
276 return jQuery.filter(qualifier, filtered, !keep);
278 qualifier = jQuery.filter( qualifier, filtered );
282 return jQuery.grep(elements, function( elem, i ) {
283 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;