Make sure that the contents of the element is still in place when html(Function)...
[jquery.git] / src / traversing.js
1 (function( jQuery ) {
2
3 var runtil = /Until$/,
4         rparentsprev = /^(?:parents|prevUntil|prevAll)/,
5         // Note: This RegExp should be improved, or likely pulled from Sizzle
6         rmultiselector = /,/,
7         isSimple = /^.[^:#\[\.,]*$/,
8         slice = Array.prototype.slice;
9
10 jQuery.fn.extend({
11         find: function( selector ) {
12                 var ret = this.pushStack( "", "find", selector ), length = 0;
13
14                 for ( var i = 0, l = this.length; i < l; i++ ) {
15                         length = ret.length;
16                         jQuery.find( selector, this[i], ret );
17
18                         if ( i > 0 ) {
19                                 // Make sure that the results are unique
20                                 for ( var n = length; n < ret.length; n++ ) {
21                                         for ( var r = 0; r < length; r++ ) {
22                                                 if ( ret[r] === ret[n] ) {
23                                                         ret.splice(n--, 1);
24                                                         break;
25                                                 }
26                                         }
27                                 }
28                         }
29                 }
30
31                 return ret;
32         },
33
34         has: function( target ) {
35                 var targets = jQuery( target );
36                 return this.filter(function() {
37                         for ( var i = 0, l = targets.length; i < l; i++ ) {
38                                 if ( jQuery.contains( this, targets[i] ) ) {
39                                         return true;
40                                 }
41                         }
42                 });
43         },
44
45         not: function( selector ) {
46                 return this.pushStack( winnow(this, selector, false), "not", selector);
47         },
48
49         filter: function( selector ) {
50                 return this.pushStack( winnow(this, selector, true), "filter", selector );
51         },
52         
53         is: function( selector ) {
54                 return !!selector && jQuery.filter( selector, this ).length > 0;
55         },
56
57         closest: function( selectors, context ) {
58                 var ret;
59
60                 if ( jQuery.isArray( selectors ) ) {
61                         var cur = this[0], match, matches = {}, selector, level = 1;
62                         ret = [];
63
64                         if ( cur && selectors.length ) {
65                                 for ( var i = 0, l = selectors.length; i < l; i++ ) {
66                                         selector = selectors[i];
67
68                                         if ( !matches[selector] ) {
69                                                 matches[selector] = jQuery.expr.match.POS.test( selector ) ? 
70                                                         jQuery( selector, context || this.context ) :
71                                                         selector;
72                                         }
73                                 }
74
75                                 while ( cur && cur.ownerDocument && cur !== context ) {
76                                         for ( selector in matches ) {
77                                                 match = matches[selector];
78
79                                                 if ( match.jquery ? match.index(cur) > -1 : jQuery(cur).is(match) ) {
80                                                         ret.push({ selector: selector, elem: cur, level: level });
81                                                 }
82                                         }
83
84                                         cur = cur.parentNode;
85                                         level++;
86                                 }
87                         }
88
89                         return ret;
90                 }
91
92                 var pos = jQuery.expr.match.POS.test( selectors ) ? 
93                         jQuery( selectors, context || this.context ) : null;
94
95                 ret = jQuery.map(this.get(),function( cur,i ) {
96                         while ( cur && cur.ownerDocument && cur !== context ) {
97                                 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selectors) ) {
98                                         return cur;
99                                 }
100
101                                 cur = cur.parentNode;
102                         }
103
104                         return null;
105                 });
106                 
107                 ret = ret.length > 1 ? jQuery.unique(ret) : ret;
108                 
109                 return this.pushStack( ret, "closest", selectors );
110         },
111         
112         // Determine the position of an element within
113         // the matched set of elements
114         index: function( elem ) {
115                 if ( !elem || typeof elem === "string" ) {
116                         return jQuery.inArray( this[0],
117                                 // If it receives a string, the selector is used
118                                 // If it receives nothing, the siblings are used
119                                 elem ? jQuery( elem ) : this.parent().children() );
120                 }
121                 // Locate the position of the desired element
122                 return jQuery.inArray(
123                         // If it receives a jQuery object, the first element is used
124                         elem.jquery ? elem[0] : elem, this );
125         },
126
127         add: function( selector, context ) {
128                 var set = typeof selector === "string" ?
129                                 jQuery( selector, context || this.context ) :
130                                 jQuery.makeArray( selector ),
131                         all = jQuery.merge( this.get(), set );
132
133                 return this.pushStack( isDisconnected( set[0] ) || isDisconnected( all[0] ) ?
134                         all :
135                         jQuery.unique( all ) );
136         },
137
138         andSelf: function() {
139                 return this.add( this.prevObject );
140         }
141 });
142
143 // A painfully simple check to see if an element is disconnected
144 // from a document (should be improved, where feasible).
145 function isDisconnected( node ) {
146         return !node || !node.parentNode || node.parentNode.nodeType === 11;
147 }
148
149 jQuery.each({
150         parent: function( elem ) {
151                 var parent = elem.parentNode;
152                 return parent && parent.nodeType !== 11 ? parent : null;
153         },
154         parents: function( elem ) {
155                 return jQuery.dir( elem, "parentNode" );
156         },
157         parentsUntil: function( elem, i, until ) {
158                 return jQuery.dir( elem, "parentNode", until );
159         },
160         next: function( elem ) {
161                 return jQuery.nth( elem, 2, "nextSibling" );
162         },
163         prev: function( elem ) {
164                 return jQuery.nth( elem, 2, "previousSibling" );
165         },
166         nextAll: function( elem ) {
167                 return jQuery.dir( elem, "nextSibling" );
168         },
169         prevAll: function( elem ) {
170                 return jQuery.dir( elem, "previousSibling" );
171         },
172         nextUntil: function( elem, i, until ) {
173                 return jQuery.dir( elem, "nextSibling", until );
174         },
175         prevUntil: function( elem, i, until ) {
176                 return jQuery.dir( elem, "previousSibling", until );
177         },
178         siblings: function( elem ) {
179                 return jQuery.sibling( elem.parentNode.firstChild, elem );
180         },
181         children: function( elem ) {
182                 return jQuery.sibling( elem.firstChild );
183         },
184         contents: function( elem ) {
185                 return jQuery.nodeName( elem, "iframe" ) ?
186                         elem.contentDocument || elem.contentWindow.document :
187                         jQuery.makeArray( elem.childNodes );
188         }
189 }, function( name, fn ) {
190         jQuery.fn[ name ] = function( until, selector ) {
191                 var ret = jQuery.map( this, fn, until );
192                 
193                 if ( !runtil.test( name ) ) {
194                         selector = until;
195                 }
196
197                 if ( selector && typeof selector === "string" ) {
198                         ret = jQuery.filter( selector, ret );
199                 }
200
201                 ret = this.length > 1 ? jQuery.unique( ret ) : ret;
202
203                 if ( (this.length > 1 || rmultiselector.test( selector )) && rparentsprev.test( name ) ) {
204                         ret = ret.reverse();
205                 }
206
207                 return this.pushStack( ret, name, slice.call(arguments).join(",") );
208         };
209 });
210
211 jQuery.extend({
212         filter: function( expr, elems, not ) {
213                 if ( not ) {
214                         expr = ":not(" + expr + ")";
215                 }
216
217                 return jQuery.find.matches(expr, elems);
218         },
219         
220         dir: function( elem, dir, until ) {
221                 var matched = [], cur = elem[dir];
222                 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
223                         if ( cur.nodeType === 1 ) {
224                                 matched.push( cur );
225                         }
226                         cur = cur[dir];
227                 }
228                 return matched;
229         },
230
231         nth: function( cur, result, dir, elem ) {
232                 result = result || 1;
233                 var num = 0;
234
235                 for ( ; cur; cur = cur[dir] ) {
236                         if ( cur.nodeType === 1 && ++num === result ) {
237                                 break;
238                         }
239                 }
240
241                 return cur;
242         },
243
244         sibling: function( n, elem ) {
245                 var r = [];
246
247                 for ( ; n; n = n.nextSibling ) {
248                         if ( n.nodeType === 1 && n !== elem ) {
249                                 r.push( n );
250                         }
251                 }
252
253                 return r;
254         }
255 });
256
257 // Implement the identical functionality for filter and not
258 function winnow( elements, qualifier, keep ) {
259         if ( jQuery.isFunction( qualifier ) ) {
260                 return jQuery.grep(elements, function( elem, i ) {
261                         var retVal = !!qualifier.call( elem, i, elem );
262                         return retVal === keep;
263                 });
264
265         } else if ( qualifier.nodeType ) {
266                 return jQuery.grep(elements, function( elem, i ) {
267                         return (elem === qualifier) === keep;
268                 });
269
270         } else if ( typeof qualifier === "string" ) {
271                 var filtered = jQuery.grep(elements, function( elem ) {
272                         return elem.nodeType === 1;
273                 });
274
275                 if ( isSimple.test( qualifier ) ) {
276                         return jQuery.filter(qualifier, filtered, !keep);
277                 } else {
278                         qualifier = jQuery.filter( qualifier, filtered );
279                 }
280         }
281
282         return jQuery.grep(elements, function( elem, i ) {
283                 return (jQuery.inArray( elem, qualifier ) >= 0) === keep;
284         });
285 }
286
287 })( jQuery );