Make sure that live events bubble unless explicitly told not to, like a normal event...
[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, filtered );
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, level = 1;
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, level: level });
104                                                 }
105                                         }
106                                         cur = cur.parentNode;
107                                         level++;
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( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
150                         all :
151                         jQuery.unique( all ) );
152         },
153
154         andSelf: function() {
155                 return this.add( this.prevObject );
156         }
157 });
158
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;
163 }
164
165 jQuery.each({
166         parent: function( elem ) {
167                 var parent = elem.parentNode;
168                 return parent && parent.nodeType !== 11 ? parent : null;
169         },
170         parents: function( elem ) {
171                 return jQuery.dir( elem, "parentNode" );
172         },
173         parentsUntil: function( elem, i, until ) {
174                 return jQuery.dir( elem, "parentNode", until );
175         },
176         next: function( elem ) {
177                 return jQuery.nth( elem, 2, "nextSibling" );
178         },
179         prev: function( elem ) {
180                 return jQuery.nth( elem, 2, "previousSibling" );
181         },
182         nextAll: function( elem ) {
183                 return jQuery.dir( elem, "nextSibling" );
184         },
185         prevAll: function( elem ) {
186                 return jQuery.dir( elem, "previousSibling" );
187         },
188         nextUntil: function( elem, i, until ) {
189                 return jQuery.dir( elem, "nextSibling", until );
190         },
191         prevUntil: function( elem, i, until ) {
192                 return jQuery.dir( elem, "previousSibling", until );
193         },
194         siblings: function( elem ) {
195                 return jQuery.sibling( elem.parentNode.firstChild, elem );
196         },
197         children: function( elem ) {
198                 return jQuery.sibling( elem.firstChild );
199         },
200         contents: function( elem ) {
201                 return jQuery.nodeName( elem, "iframe" ) ?
202                         elem.contentDocument || elem.contentWindow.document :
203                         jQuery.makeArray( elem.childNodes );
204         }
205 }, function( name, fn ) {
206         jQuery.fn[ name ] = function( until, selector ) {
207                 var ret = jQuery.map( this, fn, until );
208                 
209                 if ( !runtil.test( name ) ) {
210                         selector = until;
211                 }
212
213                 if ( selector && typeof selector === "string" ) {
214                         ret = jQuery.filter( selector, ret );
215                 }
216
217                 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
218
219                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
220                         ret = ret.reverse();
221                 }
222
223                 return this.pushStack( ret, name, slice.call(arguments).join(",") );
224         };
225 });
226
227 jQuery.extend({
228         filter: function( expr, elems, not ) {
229                 if ( not ) {
230                         expr = ":not(" + expr + ")";
231                 }
232
233                 return jQuery.find.matches(expr, elems);
234         },
235         
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 ) {
240                                 matched.push( cur );
241                         }
242                         cur = cur[dir];
243                 }
244                 return matched;
245         },
246
247         nth: function( cur, result, dir, elem ) {
248                 result = result || 1;
249                 var num = 0;
250
251                 for ( ; cur; cur = cur[dir] ) {
252                         if ( cur.nodeType === 1 && ++num === result ) {
253                                 break;
254                         }
255                 }
256
257                 return cur;
258         },
259
260         sibling: function( n, elem ) {
261                 var r = [];
262
263                 for ( ; n; n = n.nextSibling ) {
264                         if ( n.nodeType === 1 && n !== elem ) {
265                                 r.push( n );
266                         }
267                 }
268
269                 return r;
270         }
271 });