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