Build: replace CRLF with LF during minify
[jquery.git] / src / serialize.js
blobebe73345011ba730f0c8d80dcf6b157220833aa0
1 import jQuery from "./core.js";
2 import toType from "./core/toType.js";
3 import rcheckableType from "./var/rcheckableType.js";
5 import "./core/init.js";
6 import "./traversing.js"; // filter
7 import "./attributes/prop.js";
9 var
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 ( Array.isArray( obj ) ) {
20 // Serialize array item.
21 jQuery.each( obj, function( i, v ) {
22 if ( traditional || rbracket.test( prefix ) ) {
24 // Treat each array item as a scalar.
25 add( prefix, v );
27 } else {
29 // Item is non-scalar (array or object), encode its numeric index.
30 buildParams(
31 prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
33 traditional,
34 add
37 } );
39 } else if ( !traditional && toType( obj ) === "object" ) {
41 // Serialize object item.
42 for ( name in obj ) {
43 buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
46 } else {
48 // Serialize scalar item.
49 add( prefix, obj );
53 // Serialize an array of form elements or a set of
54 // key/values into a query string
55 jQuery.param = function( a, traditional ) {
56 var prefix,
57 s = [],
58 add = function( key, valueOrFunction ) {
60 // If value is a function, invoke it and use its return value
61 var value = typeof valueOrFunction === "function" ?
62 valueOrFunction() :
63 valueOrFunction;
65 s[ s.length ] = encodeURIComponent( key ) + "=" +
66 encodeURIComponent( value == null ? "" : value );
69 if ( a == null ) {
70 return "";
73 // If an array was passed in, assume that it is an array of form elements.
74 if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
76 // Serialize the form elements
77 jQuery.each( a, function() {
78 add( this.name, this.value );
79 } );
81 } else {
83 // If traditional, encode the "old" way (the way 1.3.2 or older
84 // did it), otherwise encode params recursively.
85 for ( prefix in a ) {
86 buildParams( prefix, a[ prefix ], traditional, add );
90 // Return the resulting serialization
91 return s.join( "&" );
94 jQuery.fn.extend( {
95 serialize: function() {
96 return jQuery.param( this.serializeArray() );
98 serializeArray: function() {
99 return this.map( function() {
101 // Can add propHook for "elements" to filter or add form elements
102 var elements = jQuery.prop( this, "elements" );
103 return elements ? jQuery.makeArray( elements ) : this;
104 } ).filter( function() {
105 var type = this.type;
107 // Use .is( ":disabled" ) so that fieldset[disabled] works
108 return this.name && !jQuery( this ).is( ":disabled" ) &&
109 rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
110 ( this.checked || !rcheckableType.test( type ) );
111 } ).map( function( _i, elem ) {
112 var val = jQuery( this ).val();
114 if ( val == null ) {
115 return null;
118 if ( Array.isArray( val ) ) {
119 return jQuery.map( val, function( val ) {
120 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
121 } );
124 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
125 } ).get();
127 } );
129 export default jQuery;