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 ) {
56 if ( jQuery.isArray( selectors ) ) {
57 var ret = [], cur = this[0], match, matches = {}, selector, level = 1;
59 if ( cur && selectors.length ) {
60 for ( var i = 0, l = selectors.length; i < l; i++ ) {
61 selector = selectors[i];
63 if ( !matches[selector] ) {
64 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
65 jQuery( selector, context || this.context ) :
70 while ( cur && cur.ownerDocument && cur !== context ) {
71 for ( selector in matches ) {
72 match = matches[selector];
74 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
75 ret.push({ selector: selector, elem: cur, level: level });
86 var pos = jQuery.expr.match.POS.test( selectors ) ?
87 jQuery( selectors, context || this.context ) : null;
89 return this.map(function( i, cur ) {
90 while ( cur && cur.ownerDocument && cur !== context ) {
91 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
100 // Determine the position of an element within
101 // the matched set of elements
102 index: function( elem ) {
103 if ( !elem || typeof elem === "string" ) {
104 return jQuery.inArray( this[0],
105 // If it receives a string, the selector is used
106 // If it receives nothing, the siblings are used
107 elem ? jQuery( elem ) : this.parent().children() );
109 // Locate the position of the desired element
110 return jQuery.inArray(
111 // If it receives a jQuery object, the first element is used
112 elem.jquery ? elem[0] : elem, this );
115 add: function( selector, context ) {
116 var set = typeof selector === "string" ?
117 jQuery( selector, context || this.context ) :
118 jQuery.makeArray( selector ),
119 all = jQuery.merge( this.get(), set );
121 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
123 jQuery.unique( all ) );
126 andSelf: function() {
127 return this.add( this.prevObject );
131 // A painfully simple check to see if an element is disconnected
132 // from a document (should be improved, where feasible).
133 function isDisconnected( node ) {
134 return !node || !node.parentNode || node.parentNode.nodeType === 11;
138 parent: function( elem ) {
139 var parent = elem.parentNode;
140 return parent && parent.nodeType !== 11 ? parent : null;
142 parents: function( elem ) {
143 return jQuery.dir( elem, "parentNode" );
145 parentsUntil: function( elem, i, until ) {
146 return jQuery.dir( elem, "parentNode", until );
148 next: function( elem ) {
149 return jQuery.nth( elem, 2, "nextSibling" );
151 prev: function( elem ) {
152 return jQuery.nth( elem, 2, "previousSibling" );
154 nextAll: function( elem ) {
155 return jQuery.dir( elem, "nextSibling" );
157 prevAll: function( elem ) {
158 return jQuery.dir( elem, "previousSibling" );
160 nextUntil: function( elem, i, until ) {
161 return jQuery.dir( elem, "nextSibling", until );
163 prevUntil: function( elem, i, until ) {
164 return jQuery.dir( elem, "previousSibling", until );
166 siblings: function( elem ) {
167 return jQuery.sibling( elem.parentNode.firstChild, elem );
169 children: function( elem ) {
170 return jQuery.sibling( elem.firstChild );
172 contents: function( elem ) {
173 return jQuery.nodeName( elem, "iframe" ) ?
174 elem.contentDocument || elem.contentWindow.document :
175 jQuery.makeArray( elem.childNodes );
177 }, function( name, fn ) {
178 jQuery.fn[ name ] = function( until, selector ) {
179 var ret = jQuery.map( this, fn, until );
181 if ( !runtil.test( name ) ) {
185 if ( selector && typeof selector === "string" ) {
186 ret = jQuery.filter( selector, ret );
189 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
191 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
195 return this.pushStack( ret, name, slice.call(arguments).join(",") );
200 filter: function( expr, elems, not ) {
202 expr = ":not(" + expr + ")";
205 return jQuery.find.matches(expr, elems);
208 dir: function( elem, dir, until ) {
209 var matched = [], cur = elem[dir];
210 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
211 if ( cur.nodeType === 1 ) {
219 nth: function( cur, result, dir, elem ) {
220 result = result || 1;
223 for ( ; cur; cur = cur[dir] ) {
224 if ( cur.nodeType === 1 && ++num === result ) {
232 sibling: function( n, elem ) {
235 for ( ; n; n = n.nextSibling ) {
236 if ( n.nodeType === 1 && n !== elem ) {
245 // Implement the identical functionality for filter and not
246 function winnow( elements, qualifier, keep ) {
247 if ( jQuery.isFunction( qualifier ) ) {
248 return jQuery.grep(elements, function( elem, i ) {
249 var retVal = !!qualifier.call( elem, i, elem );
250 return retVal === keep;
253 } else if ( qualifier.nodeType ) {
254 return jQuery.grep(elements, function( elem, i ) {
255 return (elem === qualifier) === keep;
258 } else if ( typeof qualifier === "string" ) {
259 var filtered = jQuery.grep(elements, function( elem ) {
260 return elem.nodeType === 1;
263 if ( isSimple.test( qualifier ) ) {
264 return jQuery.filter(qualifier, filtered, !keep);
266 qualifier = jQuery.filter( qualifier, filtered );
270 return jQuery.grep(elements, function( elem, i ) {
271 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;