Make sure that the previous element is removed from the page before the next is inser...
[jquery.git] / src / traversing.js
1 var runtil = /Until$/,
2         rparentsprev = /^(?:parents|prevUntil|prevAll)/,
3         // Note: This RegExp should be improved, or likely pulled from Sizzle
4         rmultiselector = /,/,
5         slice = Array.prototype.slice;
6
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 ) === keep;
12                 });
13
14         } else if ( qualifier.nodeType ) {
15                 return jQuery.grep(elements, function(elem, i) {
16                         return (elem === qualifier) === keep;
17                 });
18
19         } else if ( typeof qualifier === "string" ) {
20                 var filtered = jQuery.grep(elements, function(elem) {
21                         return elem.nodeType === 1;
22                 });
23
24                 if ( isSimple.test( qualifier ) ) {
25                         return jQuery.filter(qualifier, filtered, !keep);
26                 } else {
27                         qualifier = jQuery.filter( qualifier, elements );
28                 }
29         }
30
31         return jQuery.grep(elements, function(elem, i) {
32                 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
33         });
34 };
35
36 jQuery.fn.extend({
37         find: function( selector ) {
38                 var ret = this.pushStack( "", "find", selector ), length = 0;
39
40                 for ( var i = 0, l = this.length; i < l; i++ ) {
41                         length = ret.length;
42                         jQuery.find( selector, this[i], ret );
43
44                         if ( i > 0 ) {
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] ) {
49                                                         ret.splice(n--, 1);
50                                                         break;
51                                                 }
52                                         }
53                                 }
54                         }
55                 }
56
57                 return ret;
58         },
59
60         not: function( selector ) {
61                 return this.pushStack( winnow(this, selector, false), "not", selector);
62         },
63
64         filter: function( selector ) {
65                 return this.pushStack( winnow(this, selector, true), "filter", selector );
66         },
67
68         closest: function( selectors, context ) {
69                 if ( jQuery.isArray( selectors ) ) {
70                         var ret = [], cur = this[0], match, matches = {}, selector;
71
72                         if ( cur && selectors.length ) {
73                                 for ( var i = 0, l = selectors.length; i < l; i++ ) {
74                                         selector = selectors[i];
75
76                                         if ( !matches[selector] ) {
77                                                 matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
78                                                         jQuery( selector, context || this.context ) :
79                                                         selector;
80                                         }
81                                 }
82
83                                 while ( cur && cur.ownerDocument && cur !== context ) {
84                                         for ( selector in matches ) {
85                                                 match = matches[selector];
86
87                                                 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
88                                                         ret.push({ selector: selector, elem: cur });
89                                                         delete matches[selector];
90                                                 }
91                                         }
92                                         cur = cur.parentNode;
93                                 }
94                         }
95
96                         return ret;
97                 }
98
99                 var pos = jQuery.expr.match.POS.test( selectors ) ? 
100                         jQuery( selectors, context || this.context ) : null;
101
102                 return this.map(function(i, cur){
103                         while ( cur && cur.ownerDocument && cur !== context ) {
104                                 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
105                                         return cur;
106                                 }
107                                 cur = cur.parentNode;
108                         }
109                         return null;
110                 });
111         },
112
113         add: function( selector, context ) {
114                 var set = typeof selector === "string" ?
115                                 jQuery( selector, context || this.context ) :
116                                 jQuery.makeArray( selector ),
117                         all = jQuery.merge( this.get(), set );
118
119                 return this.pushStack( set[0] && (set[0].setInterval || set[0].nodeType === 9 || (set[0].parentNode && set[0].parentNode.nodeType !== 11)) ?
120                         jQuery.unique( all ) :
121                         all );
122         },
123
124         eq: function( i ) {
125                 return i === -1 ?
126                         this.slice( i ) :
127                         this.slice( i, +i + 1 );
128         },
129
130         first: function() {
131                 return this.eq( 0 );
132         },
133
134         last: function() {
135                 return this.eq( -1 );
136         },
137
138         slice: function() {
139                 return this.pushStack( slice.apply( this, arguments ),
140                         "slice", slice.call(arguments).join(",") );
141         },
142
143         map: function( callback ) {
144                 return this.pushStack( jQuery.map(this, function(elem, i){
145                         return callback.call( elem, i, elem );
146                 }));
147         },
148
149         andSelf: function() {
150                 return this.add( this.prevObject );
151         },
152
153         end: function() {
154                 return this.prevObject || jQuery(null);
155         }
156 });
157
158 jQuery.each({
159         parent: function(elem){return elem.parentNode;},
160         parents: function(elem){return jQuery.dir(elem,"parentNode");},
161         parentsUntil: function(elem,i,until){return jQuery.dir(elem,"parentNode",until);},
162         next: function(elem){return jQuery.nth(elem,2,"nextSibling");},
163         prev: function(elem){return jQuery.nth(elem,2,"previousSibling");},
164         nextAll: function(elem){return jQuery.dir(elem,"nextSibling");},
165         prevAll: function(elem){return jQuery.dir(elem,"previousSibling");},
166         nextUntil: function(elem,i,until){return jQuery.dir(elem,"nextSibling",until);},
167         prevUntil: function(elem,i,until){return jQuery.dir(elem,"previousSibling",until);},
168         siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);},
169         children: function(elem){return jQuery.sibling(elem.firstChild);},
170         contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);}
171 }, function(name, fn){
172         jQuery.fn[ name ] = function( until, selector ) {
173                 var ret = jQuery.map( this, fn, until );
174                 
175                 if ( !runtil.test( name ) ) {
176                         selector = until;
177                 }
178
179                 if ( selector && typeof selector === "string" ) {
180                         ret = jQuery.filter( selector, ret );
181                 }
182
183                 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
184
185                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
186                         ret = ret.reverse();
187                 }
188
189                 return this.pushStack( ret, name, slice.call(arguments).join(",") );
190         };
191 });
192
193 jQuery.extend({
194         filter: function( expr, elems, not ) {
195                 if ( not ) {
196                         expr = ":not(" + expr + ")";
197                 }
198
199                 return jQuery.find.matches(expr, elems);
200         },
201         
202         dir: function( elem, dir, until ) {
203                 var matched = [], cur = elem[dir];
204                 while ( cur && cur.nodeType !== 9 && (until === undefined || !jQuery( cur ).is( until )) ) {
205                         if ( cur.nodeType === 1 ) {
206                                 matched.push( cur );
207                         }
208                         cur = cur[dir];
209                 }
210                 return matched;
211         },
212
213         nth: function( cur, result, dir, elem ) {
214                 result = result || 1;
215                 var num = 0;
216
217                 for ( ; cur; cur = cur[dir] ) {
218                         if ( cur.nodeType === 1 && ++num === result ) {
219                                 break;
220                         }
221                 }
222
223                 return cur;
224         },
225
226         sibling: function( n, elem ) {
227                 var r = [];
228
229                 for ( ; n; n = n.nextSibling ) {
230                         if ( n.nodeType === 1 && n !== elem ) {
231                                 r.push( n );
232                         }
233                 }
234
235                 return r;
236         }
237 });