Explicitly re-bind the events on clone. Copying over the data isn't enough. Fixes...
[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, elem ) === 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         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] ) ) {
65                                         return true;
66                                 }
67                         }
68                 });
69         },
70
71         not: function( selector ) {
72                 return this.pushStack( winnow(this, selector, false), "not", selector);
73         },
74
75         filter: function( selector ) {
76                 return this.pushStack( winnow(this, selector, true), "filter", selector );
77         },
78         
79         is: function( selector ) {
80                 return !!selector && jQuery.filter( selector, this ).length > 0;
81         },
82
83         closest: function( selectors, context ) {
84                 if ( jQuery.isArray( selectors ) ) {
85                         var ret = [], cur = this[0], match, matches = {}, selector;
86
87                         if ( cur && selectors.length ) {
88                                 for ( var i = 0, l = selectors.length; i < l; i++ ) {
89                                         selector = selectors[i];
90
91                                         if ( !matches[selector] ) {
92                                                 matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
93                                                         jQuery( selector, context || this.context ) :
94                                                         selector;
95                                         }
96                                 }
97
98                                 while ( cur && cur.ownerDocument && cur !== context ) {
99                                         for ( selector in matches ) {
100                                                 match = matches[selector];
101
102                                                 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
103                                                         ret.push({ selector: selector, elem: cur });
104                                                         delete matches[selector];
105                                                 }
106                                         }
107                                         cur = cur.parentNode;
108                                 }
109                         }
110
111                         return ret;
112                 }
113
114                 var pos = jQuery.expr.match.POS.test( selectors ) ? 
115                         jQuery( selectors, context || this.context ) : null;
116
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) ) {
120                                         return cur;
121                                 }
122                                 cur = cur.parentNode;
123                         }
124                         return null;
125                 });
126         },
127         
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() );
136                 }
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 );
141         },
142
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 );
148
149                 return this.pushStack( set[0] && (set[0].setInterval || set[0].nodeType === 9 || (set[0].parentNode && set[0].parentNode.nodeType !== 11)) ?
150                         jQuery.unique( all ) :
151                         all );
152         },
153
154         andSelf: function() {
155                 return this.add( this.prevObject );
156         }
157 });
158
159 jQuery.each({
160         parent: function( elem ) {
161                 var parent = elem.parentNode;
162                 return parent && parent.nodeType !== 11 ? parent : null;
163         },
164         parents: function( elem ) {
165                 return jQuery.dir( elem, "parentNode" );
166         },
167         parentsUntil: function( elem, i, until ) {
168                 return jQuery.dir( elem, "parentNode", until );
169         },
170         next: function( elem ) {
171                 return jQuery.nth( elem, 2, "nextSibling" );
172         },
173         prev: function( elem ) {
174                 return jQuery.nth( elem, 2, "previousSibling" );
175         },
176         nextAll: function( elem ) {
177                 return jQuery.dir( elem, "nextSibling" );
178         },
179         prevAll: function( elem ) {
180                 return jQuery.dir( elem, "previousSibling" );
181         },
182         nextUntil: function( elem, i, until ) {
183                 return jQuery.dir( elem, "nextSibling", until );
184         },
185         prevUntil: function( elem, i, until ) {
186                 return jQuery.dir( elem, "previousSibling", until );
187         },
188         siblings: function( elem ) {
189                 return jQuery.sibling( elem.parentNode.firstChild, elem );
190         },
191         children: function( elem ) {
192                 return jQuery.sibling( elem.firstChild );
193         },
194         contents: function( elem ) {
195                 return jQuery.nodeName( elem, "iframe" ) ?
196                         elem.contentDocument || elem.contentWindow.document :
197                         jQuery.makeArray( elem.childNodes );
198         }
199 }, function( name, fn ) {
200         jQuery.fn[ name ] = function( until, selector ) {
201                 var ret = jQuery.map( this, fn, until );
202                 
203                 if ( !runtil.test( name ) ) {
204                         selector = until;
205                 }
206
207                 if ( selector && typeof selector === "string" ) {
208                         ret = jQuery.filter( selector, ret );
209                 }
210
211                 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
212
213                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
214                         ret = ret.reverse();
215                 }
216
217                 return this.pushStack( ret, name, slice.call(arguments).join(",") );
218         };
219 });
220
221 jQuery.extend({
222         filter: function( expr, elems, not ) {
223                 if ( not ) {
224                         expr = ":not(" + expr + ")";
225                 }
226
227                 return jQuery.find.matches(expr, elems);
228         },
229         
230         dir: function( elem, dir, until ) {
231                 var matched = [], cur = elem[dir];
232                 while ( cur && cur.nodeType !== 9 && (until === undefined || !jQuery( cur ).is( until )) ) {
233                         if ( cur.nodeType === 1 ) {
234                                 matched.push( cur );
235                         }
236                         cur = cur[dir];
237                 }
238                 return matched;
239         },
240
241         nth: function( cur, result, dir, elem ) {
242                 result = result || 1;
243                 var num = 0;
244
245                 for ( ; cur; cur = cur[dir] ) {
246                         if ( cur.nodeType === 1 && ++num === result ) {
247                                 break;
248                         }
249                 }
250
251                 return cur;
252         },
253
254         sibling: function( n, elem ) {
255                 var r = [];
256
257                 for ( ; n; n = n.nextSibling ) {
258                         if ( n.nodeType === 1 && n !== elem ) {
259                                 r.push( n );
260                         }
261                 }
262
263                 return r;
264         }
265 });