4 rparentsprev = /^(?:parents|prevUntil|prevAll)/,
5 // Note: This RegExp should be improved, or likely pulled from Sizzle
8 isSimple = /^.[^:#\[\.,]*$/,
9 slice = Array.prototype.slice;
12 find: function( selector ) {
13 // Handle "> div" child selectors and pass them to .children()
14 if ( typeof selector === "string" && rchild.test( selector ) ) {
15 return this.children( selector.replace( rchild, "" ) );
18 var ret = this.pushStack( "", "find", selector ), length = 0;
20 for ( var i = 0, l = this.length; i < l; i++ ) {
22 jQuery.find( selector, this[i], ret );
25 // Make sure that the results are unique
26 for ( var n = length; n < ret.length; n++ ) {
27 for ( var r = 0; r < length; r++ ) {
28 if ( ret[r] === ret[n] ) {
40 has: function( target ) {
41 var targets = jQuery( target );
42 return this.filter(function() {
43 for ( var i = 0, l = targets.length; i < l; i++ ) {
44 if ( jQuery.contains( this, targets[i] ) ) {
51 not: function( selector ) {
52 return this.pushStack( winnow(this, selector, false), "not", selector);
55 filter: function( selector ) {
56 return this.pushStack( winnow(this, selector, true), "filter", selector );
59 is: function( selector ) {
60 return !!selector && jQuery.filter( selector, this ).length > 0;
63 closest: function( selectors, context ) {
66 if ( jQuery.isArray( selectors ) ) {
67 var cur = this[0], match, matches = {}, selector, level = 1;
70 if ( cur && selectors.length ) {
71 for ( var i = 0, l = selectors.length; i < l; i++ ) {
72 selector = selectors[i];
74 if ( !matches[selector] ) {
75 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
76 jQuery( selector, context || this.context ) :
81 while ( cur && cur.ownerDocument && cur !== context ) {
82 for ( selector in matches ) {
83 match = matches[selector];
85 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
86 ret.push({ selector: selector, elem: cur, level: level });
98 var pos = jQuery.expr.match.POS.test( selectors ) ?
99 jQuery( selectors, context || this.context ) : null;
101 ret = jQuery.map(this.get(),function( cur,i ) {
102 while ( cur && cur.ownerDocument && cur !== context ) {
103 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
107 cur = cur.parentNode;
113 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
115 return this.pushStack( ret, "closest", selectors );
118 // Determine the position of an element within
119 // the matched set of elements
120 index: function( elem ) {
121 if ( !elem || typeof elem === "string" ) {
122 return jQuery.inArray( this[0],
123 // If it receives a string, the selector is used
124 // If it receives nothing, the siblings are used
125 elem ? jQuery( elem ) : this.parent().children() );
127 // Locate the position of the desired element
128 return jQuery.inArray(
129 // If it receives a jQuery object, the first element is used
130 elem.jquery ? elem[0] : elem, this );
133 add: function( selector, context ) {
134 var set = typeof selector === "string" ?
135 jQuery( selector, context || this.context ) :
136 jQuery.makeArray( selector ),
137 all = jQuery.merge( this.get(), set );
139 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
141 jQuery.unique( all ) );
144 andSelf: function() {
145 return this.add( this.prevObject );
149 // A painfully simple check to see if an element is disconnected
150 // from a document (should be improved, where feasible).
151 function isDisconnected( node ) {
152 return !node || !node.parentNode || node.parentNode.nodeType === 11;
156 parent: function( elem ) {
157 var parent = elem.parentNode;
158 return parent && parent.nodeType !== 11 ? parent : null;
160 parents: function( elem ) {
161 return jQuery.dir( elem, "parentNode" );
163 parentsUntil: function( elem, i, until ) {
164 return jQuery.dir( elem, "parentNode", until );
166 next: function( elem ) {
167 return jQuery.nth( elem, 2, "nextSibling" );
169 prev: function( elem ) {
170 return jQuery.nth( elem, 2, "previousSibling" );
172 nextAll: function( elem ) {
173 return jQuery.dir( elem, "nextSibling" );
175 prevAll: function( elem ) {
176 return jQuery.dir( elem, "previousSibling" );
178 nextUntil: function( elem, i, until ) {
179 return jQuery.dir( elem, "nextSibling", until );
181 prevUntil: function( elem, i, until ) {
182 return jQuery.dir( elem, "previousSibling", until );
184 siblings: function( elem ) {
185 return jQuery.sibling( elem.parentNode.firstChild, elem );
187 children: function( elem ) {
188 return jQuery.sibling( elem.firstChild );
190 contents: function( elem ) {
191 return jQuery.nodeName( elem, "iframe" ) ?
192 elem.contentDocument || elem.contentWindow.document :
193 jQuery.makeArray( elem.childNodes );
195 }, function( name, fn ) {
196 jQuery.fn[ name ] = function( until, selector ) {
197 var ret = jQuery.map( this, fn, until );
199 if ( !runtil.test( name ) ) {
203 if ( selector && typeof selector === "string" ) {
204 ret = jQuery.filter( selector, ret );
207 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
209 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
213 return this.pushStack( ret, name, slice.call(arguments).join(",") );
218 filter: function( expr, elems, not ) {
220 expr = ":not(" + expr + ")";
223 return jQuery.find.matches(expr, elems);
226 dir: function( elem, dir, until ) {
227 var matched = [], cur = elem[dir];
228 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
229 if ( cur.nodeType === 1 ) {
237 nth: function( cur, result, dir, elem ) {
238 result = result || 1;
241 for ( ; cur; cur = cur[dir] ) {
242 if ( cur.nodeType === 1 && ++num === result ) {
250 sibling: function( n, elem ) {
253 for ( ; n; n = n.nextSibling ) {
254 if ( n.nodeType === 1 && n !== elem ) {
263 // Implement the identical functionality for filter and not
264 function winnow( elements, qualifier, keep ) {
265 if ( jQuery.isFunction( qualifier ) ) {
266 return jQuery.grep(elements, function( elem, i ) {
267 var retVal = !!qualifier.call( elem, i, elem );
268 return retVal === keep;
271 } else if ( qualifier.nodeType ) {
272 return jQuery.grep(elements, function( elem, i ) {
273 return (elem === qualifier) === keep;
276 } else if ( typeof qualifier === "string" ) {
277 var filtered = jQuery.grep(elements, function( elem ) {
278 return elem.nodeType === 1;
281 if ( isSimple.test( qualifier ) ) {
282 return jQuery.filter(qualifier, filtered, !keep);
284 qualifier = jQuery.filter( qualifier, filtered );
288 return jQuery.grep(elements, function( elem, i ) {
289 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;