2 rparentsprev = /^(?:parents|prevUntil|prevAll)/,
3 // Note: This RegExp should be improved, or likely pulled from Sizzle
5 isSimple = /^.[^:#\[\.,]*$/,
6 slice = Array.prototype.slice;
9 find: function( selector ) {
10 var ret = this.pushStack( "", "find", selector ), length = 0;
12 for ( var i = 0, l = this.length; i < l; i++ ) {
14 jQuery.find( selector, this[i], ret );
17 // Make sure that the results are unique
18 for ( var n = length; n < ret.length; n++ ) {
19 for ( var r = 0; r < length; r++ ) {
20 if ( ret[r] === ret[n] ) {
32 has: function( target ) {
33 var targets = jQuery( target );
34 return this.filter(function() {
35 for ( var i = 0, l = targets.length; i < l; i++ ) {
36 if ( jQuery.contains( this, targets[i] ) ) {
43 not: function( selector ) {
44 return this.pushStack( winnow(this, selector, false), "not", selector);
47 filter: function( selector ) {
48 return this.pushStack( winnow(this, selector, true), "filter", selector );
51 is: function( selector ) {
52 return !!selector && jQuery.filter( selector, this ).length > 0;
55 closest: function( selectors, context ) {
57 if ( jQuery.isArray( selectors ) ) {
58 var cur = this[0], match, matches = {}, selector, level = 1;
60 if ( cur && selectors.length ) {
61 for ( var i = 0, l = selectors.length; i < l; i++ ) {
62 selector = selectors[i];
64 if ( !matches[selector] ) {
65 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
66 jQuery( selector, context || this.context ) :
71 while ( cur && cur.ownerDocument && cur !== context ) {
72 for ( selector in matches ) {
73 match = matches[selector];
75 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
76 ret.push({ selector: selector, elem: cur, level: level });
84 return ret.length > 1 ? jQuery.unique(ret) : ret;
87 var pos = jQuery.expr.match.POS.test( selectors ) ?
88 jQuery( selectors, context || this.context ) : null;
89 ret = jQuery.map(this.get(),function( cur,i ) {
90 while ( cur && cur.ownerDocument && cur !== context ) {
91 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
99 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
101 return this.pushStack( ret, "closest", selectors );
104 // Determine the position of an element within
105 // the matched set of elements
106 index: function( elem ) {
107 if ( !elem || typeof elem === "string" ) {
108 return jQuery.inArray( this[0],
109 // If it receives a string, the selector is used
110 // If it receives nothing, the siblings are used
111 elem ? jQuery( elem ) : this.parent().children() );
113 // Locate the position of the desired element
114 return jQuery.inArray(
115 // If it receives a jQuery object, the first element is used
116 elem.jquery ? elem[0] : elem, this );
119 add: function( selector, context ) {
120 var set = typeof selector === "string" ?
121 jQuery( selector, context || this.context ) :
122 jQuery.makeArray( selector ),
123 all = jQuery.merge( this.get(), set );
125 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
127 jQuery.unique( all ) );
130 andSelf: function() {
131 return this.add( this.prevObject );
135 // A painfully simple check to see if an element is disconnected
136 // from a document (should be improved, where feasible).
137 function isDisconnected( node ) {
138 return !node || !node.parentNode || node.parentNode.nodeType === 11;
142 parent: function( elem ) {
143 var parent = elem.parentNode;
144 return parent && parent.nodeType !== 11 ? parent : null;
146 parents: function( elem ) {
147 return jQuery.dir( elem, "parentNode" );
149 parentsUntil: function( elem, i, until ) {
150 return jQuery.dir( elem, "parentNode", until );
152 next: function( elem ) {
153 return jQuery.nth( elem, 2, "nextSibling" );
155 prev: function( elem ) {
156 return jQuery.nth( elem, 2, "previousSibling" );
158 nextAll: function( elem ) {
159 return jQuery.dir( elem, "nextSibling" );
161 prevAll: function( elem ) {
162 return jQuery.dir( elem, "previousSibling" );
164 nextUntil: function( elem, i, until ) {
165 return jQuery.dir( elem, "nextSibling", until );
167 prevUntil: function( elem, i, until ) {
168 return jQuery.dir( elem, "previousSibling", until );
170 siblings: function( elem ) {
171 return jQuery.sibling( elem.parentNode.firstChild, elem );
173 children: function( elem ) {
174 return jQuery.sibling( elem.firstChild );
176 contents: function( elem ) {
177 return jQuery.nodeName( elem, "iframe" ) ?
178 elem.contentDocument || elem.contentWindow.document :
179 jQuery.makeArray( elem.childNodes );
181 }, function( name, fn ) {
182 jQuery.fn[ name ] = function( until, selector ) {
183 var ret = jQuery.map( this, fn, until );
185 if ( !runtil.test( name ) ) {
189 if ( selector && typeof selector === "string" ) {
190 ret = jQuery.filter( selector, ret );
193 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
195 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
199 return this.pushStack( ret, name, slice.call(arguments).join(",") );
204 filter: function( expr, elems, not ) {
206 expr = ":not(" + expr + ")";
209 return jQuery.find.matches(expr, elems);
212 dir: function( elem, dir, until ) {
213 var matched = [], cur = elem[dir];
214 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
215 if ( cur.nodeType === 1 ) {
223 nth: function( cur, result, dir, elem ) {
224 result = result || 1;
227 for ( ; cur; cur = cur[dir] ) {
228 if ( cur.nodeType === 1 && ++num === result ) {
236 sibling: function( n, elem ) {
239 for ( ; n; n = n.nextSibling ) {
240 if ( n.nodeType === 1 && n !== elem ) {
249 // Implement the identical functionality for filter and not
250 function winnow( elements, qualifier, keep ) {
251 if ( jQuery.isFunction( qualifier ) ) {
252 return jQuery.grep(elements, function( elem, i ) {
253 var retVal = !!qualifier.call( elem, i, elem );
254 return retVal === keep;
257 } else if ( qualifier.nodeType ) {
258 return jQuery.grep(elements, function( elem, i ) {
259 return (elem === qualifier) === keep;
262 } else if ( typeof qualifier === "string" ) {
263 var filtered = jQuery.grep(elements, function( elem ) {
264 return elem.nodeType === 1;
267 if ( isSimple.test( qualifier ) ) {
268 return jQuery.filter(qualifier, filtered, !keep);
270 qualifier = jQuery.filter( qualifier, filtered );
274 return jQuery.grep(elements, function( elem, i ) {
275 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;