special event refactor left over code
[jquery.git] / src / traversing.js
1 jQuery.fn.extend({
2         find: function( selector ) {
3                 var ret = this.pushStack( "", "find", selector ), length = 0;
4
5                 for ( var i = 0, l = this.length; i < l; i++ ) {
6                         length = ret.length;
7                         jQuery.find( selector, this[i], ret );
8
9                         if ( i > 0 ) {
10                                 // Make sure that the results are unique
11                                 for ( var n = length; n < ret.length; n++ ) {
12                                         for ( var r = 0; r < length; r++ ) {
13                                                 if ( ret[r] === ret[n] ) {
14                                                         ret.splice(n--, 1);
15                                                         break;
16                                                 }
17                                         }
18                                 }
19                         }
20                 }
21
22                 return ret;
23         },
24
25         filter: function( selector ) {
26                 return this.pushStack(
27                         jQuery.isFunction( selector ) &&
28                         jQuery.grep(this, function(elem, i){
29                                 return selector.call( elem, i );
30                         }) ||
31
32                         jQuery.multiFilter( selector, jQuery.grep(this, function(elem){
33                                 return elem.nodeType === 1;
34                         }) ), "filter", selector );
35         },
36
37         closest: function( selector ) {
38                 var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null,
39                         closer = 0;
40
41                 return this.map(function(){
42                         var cur = this;
43                         while ( cur && cur.ownerDocument ) {
44                                 if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
45                                         jQuery.data(cur, "closest", closer);
46                                         return cur;
47                                 }
48                                 cur = cur.parentNode;
49                                 closer++;
50                         }
51                 });
52         },
53
54         not: function( selector ) {
55                 if ( typeof selector === "string" )
56                         // test special case where just one selector is passed in
57                         if ( isSimple.test( selector ) )
58                                 return this.pushStack( jQuery.multiFilter( selector, this, true ), "not", selector );
59                         else
60                                 selector = jQuery.multiFilter( selector, this );
61
62                 var isArrayLike = selector.length && selector[selector.length - 1] !== undefined && !selector.nodeType;
63                 return this.filter(function() {
64                         return isArrayLike ? jQuery.inArray( this, selector ) < 0 : this != selector;
65                 });
66         },
67
68         add: function( selector ) {
69                 return this.pushStack( jQuery.unique( jQuery.merge(
70                         this.get(),
71                         typeof selector === "string" ?
72                                 jQuery( selector ) :
73                                 jQuery.makeArray( selector )
74                 )));
75         },
76
77         eq: function( i ) {
78                 return this.slice( i, +i + 1 );
79         },
80
81         slice: function() {
82                 return this.pushStack( Array.prototype.slice.apply( this, arguments ),
83                         "slice", Array.prototype.slice.call(arguments).join(",") );
84         },
85
86         map: function( callback ) {
87                 return this.pushStack( jQuery.map(this, function(elem, i){
88                         return callback.call( elem, i, elem );
89                 }));
90         },
91
92         andSelf: function() {
93                 return this.add( this.prevObject );
94         },
95
96         end: function() {
97                 return this.prevObject || jQuery(null);
98         }
99 });
100
101 jQuery.each({
102         parent: function(elem){return elem.parentNode;},
103         parents: function(elem){return jQuery.dir(elem,"parentNode");},
104         next: function(elem){return jQuery.nth(elem,2,"nextSibling");},
105         prev: function(elem){return jQuery.nth(elem,2,"previousSibling");},
106         nextAll: function(elem){return jQuery.dir(elem,"nextSibling");},
107         prevAll: function(elem){return jQuery.dir(elem,"previousSibling");},
108         siblings: function(elem){return jQuery.sibling(elem.parentNode.firstChild,elem);},
109         children: function(elem){return jQuery.sibling(elem.firstChild);},
110         contents: function(elem){return jQuery.nodeName(elem,"iframe")?elem.contentDocument||elem.contentWindow.document:jQuery.makeArray(elem.childNodes);}
111 }, function(name, fn){
112         jQuery.fn[ name ] = function( selector ) {
113                 var ret = jQuery.map( this, fn );
114
115                 if ( selector && typeof selector == "string" )
116                         ret = jQuery.multiFilter( selector, ret );
117
118                 return this.pushStack( jQuery.unique( ret ), name, selector );
119         };
120 });