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 ) {
58 if ( jQuery.isArray( selectors ) ) {
59 var ret = [], cur = this[0], match, matches = {}, selector, level = 1;
61 if ( cur && selectors.length ) {
62 for ( var i = 0, l = selectors.length; i < l; i++ ) {
63 selector = selectors[i];
65 if ( !matches[selector] ) {
66 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
67 jQuery( selector, context || this.context ) :
72 while ( cur && cur.ownerDocument && cur !== context ) {
73 for ( selector in matches ) {
74 match = matches[selector];
76 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
77 ret.push({ selector: selector, elem: cur, level: level });
88 var pos = jQuery.expr.match.POS.test( selectors ) ?
89 jQuery( selectors, context || this.context ) : null;
91 return this.map(function( i, cur ) {
92 while ( cur && cur.ownerDocument && cur !== context ) {
93 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
102 // Determine the position of an element within
103 // the matched set of elements
104 index: function( elem ) {
105 if ( !elem || typeof elem === "string" ) {
106 return jQuery.inArray( this[0],
107 // If it receives a string, the selector is used
108 // If it receives nothing, the siblings are used
109 elem ? jQuery( elem ) : this.parent().children() );
111 // Locate the position of the desired element
112 return jQuery.inArray(
113 // If it receives a jQuery object, the first element is used
114 elem.jquery ? elem[0] : elem, this );
117 add: function( selector, context ) {
118 var set = typeof selector === "string" ?
119 jQuery( selector, context || this.context ) :
120 jQuery.makeArray( selector ),
121 all = jQuery.merge( this.get(), set );
123 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
125 jQuery.unique( all ) );
128 andSelf: function() {
129 return this.add( this.prevObject );
133 // A painfully simple check to see if an element is disconnected
134 // from a document (should be improved, where feasible).
135 function isDisconnected( node ) {
136 return !node || !node.parentNode || node.parentNode.nodeType === 11;
140 parent: function( elem ) {
141 var parent = elem.parentNode;
142 return parent && parent.nodeType !== 11 ? parent : null;
144 parents: function( elem ) {
145 return jQuery.dir( elem, "parentNode" );
147 parentsUntil: function( elem, i, until ) {
148 return jQuery.dir( elem, "parentNode", until );
150 next: function( elem ) {
151 return jQuery.nth( elem, 2, "nextSibling" );
153 prev: function( elem ) {
154 return jQuery.nth( elem, 2, "previousSibling" );
156 nextAll: function( elem ) {
157 return jQuery.dir( elem, "nextSibling" );
159 prevAll: function( elem ) {
160 return jQuery.dir( elem, "previousSibling" );
162 nextUntil: function( elem, i, until ) {
163 return jQuery.dir( elem, "nextSibling", until );
165 prevUntil: function( elem, i, until ) {
166 return jQuery.dir( elem, "previousSibling", until );
168 siblings: function( elem ) {
169 return jQuery.sibling( elem.parentNode.firstChild, elem );
171 children: function( elem ) {
172 return jQuery.sibling( elem.firstChild );
174 contents: function( elem ) {
175 return jQuery.nodeName( elem, "iframe" ) ?
176 elem.contentDocument || elem.contentWindow.document :
177 jQuery.makeArray( elem.childNodes );
179 }, function( name, fn ) {
180 jQuery.fn[ name ] = function( until, selector ) {
181 var ret = jQuery.map( this, fn, until );
183 if ( !runtil.test( name ) ) {
187 if ( selector && typeof selector === "string" ) {
188 ret = jQuery.filter( selector, ret );
191 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
193 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
197 return this.pushStack( ret, name, slice.call(arguments).join(",") );
202 filter: function( expr, elems, not ) {
204 expr = ":not(" + expr + ")";
207 return jQuery.find.matches(expr, elems);
210 dir: function( elem, dir, until ) {
211 var matched = [], cur = elem[dir];
212 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
213 if ( cur.nodeType === 1 ) {
221 nth: function( cur, result, dir, elem ) {
222 result = result || 1;
225 for ( ; cur; cur = cur[dir] ) {
226 if ( cur.nodeType === 1 && ++num === result ) {
234 sibling: function( n, elem ) {
237 for ( ; n; n = n.nextSibling ) {
238 if ( n.nodeType === 1 && n !== elem ) {
247 // Implement the identical functionality for filter and not
248 function winnow( elements, qualifier, keep ) {
249 if ( jQuery.isFunction( qualifier ) ) {
250 return jQuery.grep(elements, function( elem, i ) {
251 var retVal = !!qualifier.call( elem, i, elem );
252 return retVal === keep;
255 } else if ( qualifier.nodeType ) {
256 return jQuery.grep(elements, function( elem, i ) {
257 return (elem === qualifier) === keep;
260 } else if ( typeof qualifier === "string" ) {
261 var filtered = jQuery.grep(elements, function( elem ) {
262 return elem.nodeType === 1;
265 if ( isSimple.test( qualifier ) ) {
266 return jQuery.filter(qualifier, filtered, !keep);
268 qualifier = jQuery.filter( qualifier, filtered );
272 return jQuery.grep(elements, function( elem, i ) {
273 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;