2 rparentsprev = /^(?:parents|prevUntil|prevAll)/,
3 // Note: This RegExp should be improved, or likely pulled from Sizzle
5 slice = Array.prototype.slice;
7 // Implement the identical functionality for filter and not
8 var winnow = function( elements, qualifier, keep ) {
9 if ( jQuery.isFunction( qualifier ) ) {
10 return jQuery.grep(elements, function( elem, i ) {
11 return !!qualifier.call( elem, i, elem ) === keep;
14 } else if ( qualifier.nodeType ) {
15 return jQuery.grep(elements, function( elem, i ) {
16 return (elem === qualifier) === keep;
19 } else if ( typeof qualifier === "string" ) {
20 var filtered = jQuery.grep(elements, function( elem ) {
21 return elem.nodeType === 1;
24 if ( isSimple.test( qualifier ) ) {
25 return jQuery.filter(qualifier, filtered, !keep);
27 qualifier = jQuery.filter( qualifier, filtered );
31 return jQuery.grep(elements, function( elem, i ) {
32 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
37 find: function( selector ) {
38 var ret = this.pushStack( "", "find", selector ), length = 0;
40 for ( var i = 0, l = this.length; i < l; i++ ) {
42 jQuery.find( selector, this[i], ret );
45 // Make sure that the results are unique
46 for ( var n = length; n < ret.length; n++ ) {
47 for ( var r = 0; r < length; r++ ) {
48 if ( ret[r] === ret[n] ) {
60 has: function( target ) {
61 var targets = jQuery( target );
62 return this.filter(function() {
63 for ( var i = 0, l = targets.length; i < l; i++ ) {
64 if ( jQuery.contains( this, targets[i] ) ) {
71 not: function( selector ) {
72 return this.pushStack( winnow(this, selector, false), "not", selector);
75 filter: function( selector ) {
76 return this.pushStack( winnow(this, selector, true), "filter", selector );
79 is: function( selector ) {
80 return !!selector && jQuery.filter( selector, this ).length > 0;
83 closest: function( selectors, context ) {
84 if ( jQuery.isArray( selectors ) ) {
85 var ret = [], cur = this[0], match, matches = {}, selector, level = 1;
87 if ( cur && selectors.length ) {
88 for ( var i = 0, l = selectors.length; i < l; i++ ) {
89 selector = selectors[i];
91 if ( !matches[selector] ) {
92 matches[selector] = jQuery.expr.match.POS.test( selector ) ?
93 jQuery( selector, context || this.context ) :
98 while ( cur && cur.ownerDocument && cur !== context ) {
99 for ( selector in matches ) {
100 match = matches[selector];
102 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
103 ret.push({ selector: selector, elem: cur, level: level });
106 cur = cur.parentNode;
114 var pos = jQuery.expr.match.POS.test( selectors ) ?
115 jQuery( selectors, context || this.context ) : null;
117 return this.map(function( i, cur ) {
118 while ( cur && cur.ownerDocument && cur !== context ) {
119 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
122 cur = cur.parentNode;
128 // Determine the position of an element within
129 // the matched set of elements
130 index: function( elem ) {
131 if ( !elem || typeof elem === "string" ) {
132 return jQuery.inArray( this[0],
133 // If it receives a string, the selector is used
134 // If it receives nothing, the siblings are used
135 elem ? jQuery( elem ) : this.parent().children() );
137 // Locate the position of the desired element
138 return jQuery.inArray(
139 // If it receives a jQuery object, the first element is used
140 elem.jquery ? elem[0] : elem, this );
143 add: function( selector, context ) {
144 var set = typeof selector === "string" ?
145 jQuery( selector, context || this.context ) :
146 jQuery.makeArray( selector ),
147 all = jQuery.merge( this.get(), set );
149 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
151 jQuery.unique( all ) );
154 andSelf: function() {
155 return this.add( this.prevObject );
159 // A painfully simple check to see if an element is disconnected
160 // from a document (should be improved, where feasible).
161 function isDisconnected( node ) {
162 return !node || !node.parentNode || node.parentNode.nodeType === 11;
166 parent: function( elem ) {
167 var parent = elem.parentNode;
168 return parent && parent.nodeType !== 11 ? parent : null;
170 parents: function( elem ) {
171 return jQuery.dir( elem, "parentNode" );
173 parentsUntil: function( elem, i, until ) {
174 return jQuery.dir( elem, "parentNode", until );
176 next: function( elem ) {
177 return jQuery.nth( elem, 2, "nextSibling" );
179 prev: function( elem ) {
180 return jQuery.nth( elem, 2, "previousSibling" );
182 nextAll: function( elem ) {
183 return jQuery.dir( elem, "nextSibling" );
185 prevAll: function( elem ) {
186 return jQuery.dir( elem, "previousSibling" );
188 nextUntil: function( elem, i, until ) {
189 return jQuery.dir( elem, "nextSibling", until );
191 prevUntil: function( elem, i, until ) {
192 return jQuery.dir( elem, "previousSibling", until );
194 siblings: function( elem ) {
195 return jQuery.sibling( elem.parentNode.firstChild, elem );
197 children: function( elem ) {
198 return jQuery.sibling( elem.firstChild );
200 contents: function( elem ) {
201 return jQuery.nodeName( elem, "iframe" ) ?
202 elem.contentDocument || elem.contentWindow.document :
203 jQuery.makeArray( elem.childNodes );
205 }, function( name, fn ) {
206 jQuery.fn[ name ] = function( until, selector ) {
207 var ret = jQuery.map( this, fn, until );
209 if ( !runtil.test( name ) ) {
213 if ( selector && typeof selector === "string" ) {
214 ret = jQuery.filter( selector, ret );
217 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
219 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
223 return this.pushStack( ret, name, slice.call(arguments).join(",") );
228 filter: function( expr, elems, not ) {
230 expr = ":not(" + expr + ")";
233 return jQuery.find.matches(expr, elems);
236 dir: function( elem, dir, until ) {
237 var matched = [], cur = elem[dir];
238 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
239 if ( cur.nodeType === 1 ) {
247 nth: function( cur, result, dir, elem ) {
248 result = result || 1;
251 for ( ; cur; cur = cur[dir] ) {
252 if ( cur.nodeType === 1 && ++num === result ) {
260 sibling: function( n, elem ) {
263 for ( ; n; n = n.nextSibling ) {
264 if ( n.nodeType === 1 && n !== elem ) {