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 ), length = 0;
15 for ( var i = 0, l = this.length; i < l; i++ ) {
17 jQuery.find( selector, this[i], ret );
20 // Make sure that the results are unique
21 for ( var n = length; n < ret.length; n++ ) {
22 for ( var r = 0; r < length; r++ ) {
23 if ( ret[r] === ret[n] ) {
35 has: function( target ) {
36 var targets = jQuery( target );
37 return this.filter(function() {
38 for ( var i = 0, l = targets.length; i < l; i++ ) {
39 if ( jQuery.contains( this, targets[i] ) ) {
46 not: function( selector ) {
47 return this.pushStack( winnow(this, selector, false), "not", selector);
50 filter: function( selector ) {
51 return this.pushStack( winnow(this, selector, true), "filter", selector );
54 is: function( selector ) {
55 return !!selector && jQuery.filter( selector, this ).length > 0;
58 closest: function( selectors, context ) {
59 var ret = [], i, l, cur = this[0];
61 if ( jQuery.isArray( selectors ) ) {
62 var match, matches = {}, selector, level = 1;
64 if ( cur && selectors.length ) {
65 for ( 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 = POS.test( selectors ) ?
93 jQuery( selectors, context || this.context ) : null;
95 for ( i = 0, l = this.length; i < l; i++ ) {
99 if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) {
104 cur = cur.parentNode;
105 if ( !cur || !cur.ownerDocument || cur === context ) {
112 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
114 return this.pushStack( ret, "closest", selectors );
117 // Determine the position of an element within
118 // the matched set of elements
119 index: function( elem ) {
120 if ( !elem || typeof elem === "string" ) {
121 return jQuery.inArray( this[0],
122 // If it receives a string, the selector is used
123 // If it receives nothing, the siblings are used
124 elem ? jQuery( elem ) : this.parent().children() );
126 // Locate the position of the desired element
127 return jQuery.inArray(
128 // If it receives a jQuery object, the first element is used
129 elem.jquery ? elem[0] : elem, this );
132 add: function( selector, context ) {
133 var set = typeof selector === "string" ?
134 jQuery( selector, context || this.context ) :
135 jQuery.makeArray( selector ),
136 all = jQuery.merge( this.get(), set );
138 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
140 jQuery.unique( all ) );
143 andSelf: function() {
144 return this.add( this.prevObject );
148 // A painfully simple check to see if an element is disconnected
149 // from a document (should be improved, where feasible).
150 function isDisconnected( node ) {
151 return !node || !node.parentNode || node.parentNode.nodeType === 11;
155 parent: function( elem ) {
156 var parent = elem.parentNode;
157 return parent && parent.nodeType !== 11 ? parent : null;
159 parents: function( elem ) {
160 return jQuery.dir( elem, "parentNode" );
162 parentsUntil: function( elem, i, until ) {
163 return jQuery.dir( elem, "parentNode", until );
165 next: function( elem ) {
166 return jQuery.nth( elem, 2, "nextSibling" );
168 prev: function( elem ) {
169 return jQuery.nth( elem, 2, "previousSibling" );
171 nextAll: function( elem ) {
172 return jQuery.dir( elem, "nextSibling" );
174 prevAll: function( elem ) {
175 return jQuery.dir( elem, "previousSibling" );
177 nextUntil: function( elem, i, until ) {
178 return jQuery.dir( elem, "nextSibling", until );
180 prevUntil: function( elem, i, until ) {
181 return jQuery.dir( elem, "previousSibling", until );
183 siblings: function( elem ) {
184 return jQuery.sibling( elem.parentNode.firstChild, elem );
186 children: function( elem ) {
187 return jQuery.sibling( elem.firstChild );
189 contents: function( elem ) {
190 return jQuery.nodeName( elem, "iframe" ) ?
191 elem.contentDocument || elem.contentWindow.document :
192 jQuery.makeArray( elem.childNodes );
194 }, function( name, fn ) {
195 jQuery.fn[ name ] = function( until, selector ) {
196 var ret = jQuery.map( this, fn, until );
198 if ( !runtil.test( name ) ) {
202 if ( selector && typeof selector === "string" ) {
203 ret = jQuery.filter( selector, ret );
206 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
208 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
212 return this.pushStack( ret, name, slice.call(arguments).join(",") );
217 filter: function( expr, elems, not ) {
219 expr = ":not(" + expr + ")";
222 return elems.length === 1 ?
223 jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] :
224 jQuery.find.matches(expr, elems);
227 dir: function( elem, dir, until ) {
228 var matched = [], cur = elem[dir];
229 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
230 if ( cur.nodeType === 1 ) {
238 nth: function( cur, result, dir, elem ) {
239 result = result || 1;
242 for ( ; cur; cur = cur[dir] ) {
243 if ( cur.nodeType === 1 && ++num === result ) {
251 sibling: function( n, elem ) {
254 for ( ; n; n = n.nextSibling ) {
255 if ( n.nodeType === 1 && n !== elem ) {
264 // Implement the identical functionality for filter and not
265 function winnow( elements, qualifier, keep ) {
266 if ( jQuery.isFunction( qualifier ) ) {
267 return jQuery.grep(elements, function( elem, i ) {
268 var retVal = !!qualifier.call( elem, i, elem );
269 return retVal === keep;
272 } else if ( qualifier.nodeType ) {
273 return jQuery.grep(elements, function( elem, i ) {
274 return (elem === qualifier) === keep;
277 } else if ( typeof qualifier === "string" ) {
278 var filtered = jQuery.grep(elements, function( elem ) {
279 return elem.nodeType === 1;
282 if ( isSimple.test( qualifier ) ) {
283 return jQuery.filter(qualifier, filtered, !keep);
285 qualifier = jQuery.filter( qualifier, filtered );
289 return jQuery.grep(elements, function( elem, i ) {
290 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;