Release: push dist to same remote as project
[jquery.git] / src / serialize.js
blobd6fedb6a19dd51419e3225bec98693012afadb97
1 define([
2         "./core",
3         "./manipulation/var/rcheckableType",
4         "./core/init",
5         "./traversing", // filter
6         "./attributes/prop"
7 ], function( jQuery, rcheckableType ) {
9 var r20 = /%20/g,
10         rbracket = /\[\]$/,
11         rCRLF = /\r?\n/g,
12         rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
13         rsubmittable = /^(?:input|select|textarea|keygen)/i;
15 function buildParams( prefix, obj, traditional, add ) {
16         var name;
18         if ( jQuery.isArray( obj ) ) {
19                 // Serialize array item.
20                 jQuery.each( obj, function( i, v ) {
21                         if ( traditional || rbracket.test( prefix ) ) {
22                                 // Treat each array item as a scalar.
23                                 add( prefix, v );
25                         } else {
26                                 // Item is non-scalar (array or object), encode its numeric index.
27                                 buildParams(
28                                         prefix + "[" + ( typeof v === "object" ? i : "" ) + "]",
29                                         v,
30                                         traditional,
31                                         add
32                                 );
33                         }
34                 });
36         } else if ( !traditional && jQuery.type( obj ) === "object" ) {
37                 // Serialize object item.
38                 for ( name in obj ) {
39                         buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
40                 }
42         } else {
43                 // Serialize scalar item.
44                 add( prefix, obj );
45         }
48 // Serialize an array of form elements or a set of
49 // key/values into a query string
50 jQuery.param = function( a, traditional ) {
51         var prefix,
52                 s = [],
53                 add = function( key, value ) {
54                         // If value is a function, invoke it and return its value
55                         value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
56                         s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
57                 };
59         // Set traditional to true for jQuery <= 1.3.2 behavior.
60         if ( traditional === undefined ) {
61                 traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
62         }
64         // If an array was passed in, assume that it is an array of form elements.
65         if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
66                 // Serialize the form elements
67                 jQuery.each( a, function() {
68                         add( this.name, this.value );
69                 });
71         } else {
72                 // If traditional, encode the "old" way (the way 1.3.2 or older
73                 // did it), otherwise encode params recursively.
74                 for ( prefix in a ) {
75                         buildParams( prefix, a[ prefix ], traditional, add );
76                 }
77         }
79         // Return the resulting serialization
80         return s.join( "&" ).replace( r20, "+" );
83 jQuery.fn.extend({
84         serialize: function() {
85                 return jQuery.param( this.serializeArray() );
86         },
87         serializeArray: function() {
88                 return this.map(function() {
89                         // Can add propHook for "elements" to filter or add form elements
90                         var elements = jQuery.prop( this, "elements" );
91                         return elements ? jQuery.makeArray( elements ) : this;
92                 })
93                 .filter(function() {
94                         var type = this.type;
96                         // Use .is( ":disabled" ) so that fieldset[disabled] works
97                         return this.name && !jQuery( this ).is( ":disabled" ) &&
98                                 rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
99                                 ( this.checked || !rcheckableType.test( type ) );
100                 })
101                 .map(function( i, elem ) {
102                         var val = jQuery( this ).val();
104                         return val == null ?
105                                 null :
106                                 jQuery.isArray( val ) ?
107                                         jQuery.map( val, function( val ) {
108                                                 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
109                                         }) :
110                                         { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
111                 }).get();
112         }
115 return jQuery;