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 ) {
59 if ( jQuery.isArray( selectors ) ) {
60 var cur = this[0], match, matches = {}, selector, level = 1;
62 if ( cur && selectors.length ) {
63 for ( var i = 0, l = selectors.length; i < l; i++ ) {
64 selector = selectors[i];
66 if ( !matches[selector] ) {
67 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
68 jQuery( selector, context || this.context ) :
73 while ( cur && cur.ownerDocument && cur !== context ) {
74 for ( selector in matches ) {
75 match = matches[selector];
77 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
78 ret.push({ selector: selector, elem: cur, level: level });
86 return ret.length > 1 ? jQuery.unique(ret) : ret;
89 var pos = jQuery.expr.match.POS.test( selectors ) ?
90 jQuery( selectors, context || this.context ) : null;
91 ret = jQuery.map(this.get(),function( cur,i ) {
92 while ( cur && cur.ownerDocument && cur !== context ) {
93 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
101 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
103 return this.pushStack( ret, "closest", selectors );
106 // Determine the position of an element within
107 // the matched set of elements
108 index: function( elem ) {
109 if ( !elem || typeof elem === "string" ) {
110 return jQuery.inArray( this[0],
111 // If it receives a string, the selector is used
112 // If it receives nothing, the siblings are used
113 elem ? jQuery( elem ) : this.parent().children() );
115 // Locate the position of the desired element
116 return jQuery.inArray(
117 // If it receives a jQuery object, the first element is used
118 elem.jquery ? elem[0] : elem, this );
121 add: function( selector, context ) {
122 var set = typeof selector === "string" ?
123 jQuery( selector, context || this.context ) :
124 jQuery.makeArray( selector ),
125 all = jQuery.merge( this.get(), set );
127 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
129 jQuery.unique( all ) );
132 andSelf: function() {
133 return this.add( this.prevObject );
137 // A painfully simple check to see if an element is disconnected
138 // from a document (should be improved, where feasible).
139 function isDisconnected( node ) {
140 return !node || !node.parentNode || node.parentNode.nodeType === 11;
144 parent: function( elem ) {
145 var parent = elem.parentNode;
146 return parent && parent.nodeType !== 11 ? parent : null;
148 parents: function( elem ) {
149 return jQuery.dir( elem, "parentNode" );
151 parentsUntil: function( elem, i, until ) {
152 return jQuery.dir( elem, "parentNode", until );
154 next: function( elem ) {
155 return jQuery.nth( elem, 2, "nextSibling" );
157 prev: function( elem ) {
158 return jQuery.nth( elem, 2, "previousSibling" );
160 nextAll: function( elem ) {
161 return jQuery.dir( elem, "nextSibling" );
163 prevAll: function( elem ) {
164 return jQuery.dir( elem, "previousSibling" );
166 nextUntil: function( elem, i, until ) {
167 return jQuery.dir( elem, "nextSibling", until );
169 prevUntil: function( elem, i, until ) {
170 return jQuery.dir( elem, "previousSibling", until );
172 siblings: function( elem ) {
173 return jQuery.sibling( elem.parentNode.firstChild, elem );
175 children: function( elem ) {
176 return jQuery.sibling( elem.firstChild );
178 contents: function( elem ) {
179 return jQuery.nodeName( elem, "iframe" ) ?
180 elem.contentDocument || elem.contentWindow.document :
181 jQuery.makeArray( elem.childNodes );
183 }, function( name, fn ) {
184 jQuery.fn[ name ] = function( until, selector ) {
185 var ret = jQuery.map( this, fn, until );
187 if ( !runtil.test( name ) ) {
191 if ( selector && typeof selector === "string" ) {
192 ret = jQuery.filter( selector, ret );
195 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
197 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
201 return this.pushStack( ret, name, slice.call(arguments).join(",") );
206 filter: function( expr, elems, not ) {
208 expr = ":not(" + expr + ")";
211 return jQuery.find.matches(expr, elems);
214 dir: function( elem, dir, until ) {
215 var matched = [], cur = elem[dir];
216 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
217 if ( cur.nodeType === 1 ) {
225 nth: function( cur, result, dir, elem ) {
226 result = result || 1;
229 for ( ; cur; cur = cur[dir] ) {
230 if ( cur.nodeType === 1 && ++num === result ) {
238 sibling: function( n, elem ) {
241 for ( ; n; n = n.nextSibling ) {
242 if ( n.nodeType === 1 && n !== elem ) {
251 // Implement the identical functionality for filter and not
252 function winnow( elements, qualifier, keep ) {
253 if ( jQuery.isFunction( qualifier ) ) {
254 return jQuery.grep(elements, function( elem, i ) {
255 var retVal = !!qualifier.call( elem, i, elem );
256 return retVal === keep;
259 } else if ( qualifier.nodeType ) {
260 return jQuery.grep(elements, function( elem, i ) {
261 return (elem === qualifier) === keep;
264 } else if ( typeof qualifier === "string" ) {
265 var filtered = jQuery.grep(elements, function( elem ) {
266 return elem.nodeType === 1;
269 if ( isSimple.test( qualifier ) ) {
270 return jQuery.filter(qualifier, filtered, !keep);
272 qualifier = jQuery.filter( qualifier, filtered );
276 return jQuery.grep(elements, function( elem, i ) {
277 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;