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;
12 find: function( selector ) {
13 var ret = this.pushStack( "", "find", selector ),
16 for ( var i = 0, l = this.length; i < l; i++ ) {
18 jQuery.find( selector, this[i], ret );
21 // Make sure that the results are unique
22 for ( var n = length; n < ret.length; n++ ) {
23 for ( var r = 0; r < length; r++ ) {
24 if ( ret[r] === ret[n] ) {
36 has: function( target ) {
37 var targets = jQuery( target );
38 return this.filter(function() {
39 for ( var i = 0, l = targets.length; i < l; i++ ) {
40 if ( jQuery.contains( this, targets[i] ) ) {
47 not: function( selector ) {
48 return this.pushStack( winnow(this, selector, false), "not", selector);
51 filter: function( selector ) {
52 return this.pushStack( winnow(this, selector, true), "filter", selector );
55 is: function( selector ) {
56 return !!selector && jQuery.filter( selector, this ).length > 0;
59 closest: function( selectors, context ) {
60 var ret = [], i, l, cur = this[0];
62 if ( jQuery.isArray( selectors ) ) {
67 if ( cur && selectors.length ) {
68 for ( i = 0, l = selectors.length; i < l; i++ ) {
69 selector = selectors[i];
71 if ( !matches[selector] ) {
72 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
73 jQuery( selector, context || this.context ) :
78 while ( cur && cur.ownerDocument && cur !== context ) {
79 for ( selector in matches ) {
80 match = matches[selector];
82 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
83 ret.push({ selector: selector, elem: cur, level: level });
95 var pos = POS.test( selectors ) ?
96 jQuery( selectors, context || this.context ) : null;
98 for ( i = 0, l = this.length; i < l; i++ ) {
102 if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
107 cur = cur.parentNode;
108 if ( !cur || !cur.ownerDocument || cur === context ) {
115 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
117 return this.pushStack( ret, "closest", selectors );
120 // Determine the position of an element within
121 // the matched set of elements
122 index: function( elem ) {
123 if ( !elem || typeof elem === "string" ) {
124 return jQuery.inArray( this[0],
125 // If it receives a string, the selector is used
126 // If it receives nothing, the siblings are used
127 elem ? jQuery( elem ) : this.parent().children() );
129 // Locate the position of the desired element
130 return jQuery.inArray(
131 // If it receives a jQuery object, the first element is used
132 elem.jquery ? elem[0] : elem, this );
135 add: function( selector, context ) {
136 var set = typeof selector === "string" ?
137 jQuery( selector, context || this.context ) :
138 jQuery.makeArray( selector ),
139 all = jQuery.merge( this.get(), set );
141 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
143 jQuery.unique( all ) );
146 andSelf: function() {
147 return this.add( this.prevObject );
151 // A painfully simple check to see if an element is disconnected
152 // from a document (should be improved, where feasible).
153 function isDisconnected( node ) {
154 return !node || !node.parentNode || node.parentNode.nodeType === 11;
158 parent: function( elem ) {
159 var parent = elem.parentNode;
160 return parent && parent.nodeType !== 11 ? parent : null;
162 parents: function( elem ) {
163 return jQuery.dir( elem, "parentNode" );
165 parentsUntil: function( elem, i, until ) {
166 return jQuery.dir( elem, "parentNode", until );
168 next: function( elem ) {
169 return jQuery.nth( elem, 2, "nextSibling" );
171 prev: function( elem ) {
172 return jQuery.nth( elem, 2, "previousSibling" );
174 nextAll: function( elem ) {
175 return jQuery.dir( elem, "nextSibling" );
177 prevAll: function( elem ) {
178 return jQuery.dir( elem, "previousSibling" );
180 nextUntil: function( elem, i, until ) {
181 return jQuery.dir( elem, "nextSibling", until );
183 prevUntil: function( elem, i, until ) {
184 return jQuery.dir( elem, "previousSibling", until );
186 siblings: function( elem ) {
187 return jQuery.sibling( elem.parentNode.firstChild, elem );
189 children: function( elem ) {
190 return jQuery.sibling( elem.firstChild );
192 contents: function( elem ) {
193 return jQuery.nodeName( elem, "iframe" ) ?
194 elem.contentDocument || elem.contentWindow.document :
195 jQuery.makeArray( elem.childNodes );
197 }, function( name, fn ) {
198 jQuery.fn[ name ] = function( until, selector ) {
199 var ret = jQuery.map( this, fn, until );
201 if ( !runtil.test( name ) ) {
205 if ( selector && typeof selector === "string" ) {
206 ret = jQuery.filter( selector, ret );
209 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
211 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
215 return this.pushStack( ret, name, slice.call(arguments).join(",") );
220 filter: function( expr, elems, not ) {
222 expr = ":not(" + expr + ")";
225 return elems.length === 1 ?
226 jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
227 jQuery.find.matches(expr, elems);
230 dir: function( elem, dir, until ) {
234 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
235 if ( cur.nodeType === 1 ) {
243 nth: function( cur, result, dir, elem ) {
244 result = result || 1;
247 for ( ; cur; cur = cur[dir] ) {
248 if ( cur.nodeType === 1 && ++num === result ) {
256 sibling: function( n, elem ) {
259 for ( ; n; n = n.nextSibling ) {
260 if ( n.nodeType === 1 && n !== elem ) {
269 // Implement the identical functionality for filter and not
270 function winnow( elements, qualifier, keep ) {
271 if ( jQuery.isFunction( qualifier ) ) {
272 return jQuery.grep(elements, function( elem, i ) {
273 var retVal = !!qualifier.call( elem, i, elem );
274 return retVal === keep;
277 } else if ( qualifier.nodeType ) {
278 return jQuery.grep(elements, function( elem, i ) {
279 return (elem === qualifier) === keep;
282 } else if ( typeof qualifier === "string" ) {
283 var filtered = jQuery.grep(elements, function( elem ) {
284 return elem.nodeType === 1;
287 if ( isSimple.test( qualifier ) ) {
288 return jQuery.filter(qualifier, filtered, !keep);
290 qualifier = jQuery.filter( qualifier, filtered );
294 return jQuery.grep(elements, function( elem, i ) {
295 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;