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