blob: cd4e99f9f4ae5732534c1c53922825eacaa2e5a8 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001define( [
2 "./core",
3 "./core/toType",
4 "./var/rcheckableType",
5 "./var/isFunction",
6 "./core/init",
7 "./traversing", // filter
8 "./attributes/prop"
9], function( jQuery, toType, rcheckableType, isFunction ) {
10
11"use strict";
12
13var
14 rbracket = /\[\]$/,
15 rCRLF = /\r?\n/g,
16 rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
17 rsubmittable = /^(?:input|select|textarea|keygen)/i;
18
19function buildParams( prefix, obj, traditional, add ) {
20 var name;
21
22 if ( Array.isArray( obj ) ) {
23
24 // Serialize array item.
25 jQuery.each( obj, function( i, v ) {
26 if ( traditional || rbracket.test( prefix ) ) {
27
28 // Treat each array item as a scalar.
29 add( prefix, v );
30
31 } else {
32
33 // Item is non-scalar (array or object), encode its numeric index.
34 buildParams(
35 prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
36 v,
37 traditional,
38 add
39 );
40 }
41 } );
42
43 } else if ( !traditional && toType( obj ) === "object" ) {
44
45 // Serialize object item.
46 for ( name in obj ) {
47 buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
48 }
49
50 } else {
51
52 // Serialize scalar item.
53 add( prefix, obj );
54 }
55}
56
57// Serialize an array of form elements or a set of
58// key/values into a query string
59jQuery.param = function( a, traditional ) {
60 var prefix,
61 s = [],
62 add = function( key, valueOrFunction ) {
63
64 // If value is a function, invoke it and use its return value
65 var value = isFunction( valueOrFunction ) ?
66 valueOrFunction() :
67 valueOrFunction;
68
69 s[ s.length ] = encodeURIComponent( key ) + "=" +
70 encodeURIComponent( value == null ? "" : value );
71 };
72
73 if ( a == null ) {
74 return "";
75 }
76
77 // If an array was passed in, assume that it is an array of form elements.
78 if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
79
80 // Serialize the form elements
81 jQuery.each( a, function() {
82 add( this.name, this.value );
83 } );
84
85 } else {
86
87 // If traditional, encode the "old" way (the way 1.3.2 or older
88 // did it), otherwise encode params recursively.
89 for ( prefix in a ) {
90 buildParams( prefix, a[ prefix ], traditional, add );
91 }
92 }
93
94 // Return the resulting serialization
95 return s.join( "&" );
96};
97
98jQuery.fn.extend( {
99 serialize: function() {
100 return jQuery.param( this.serializeArray() );
101 },
102 serializeArray: function() {
103 return this.map( function() {
104
105 // Can add propHook for "elements" to filter or add form elements
106 var elements = jQuery.prop( this, "elements" );
107 return elements ? jQuery.makeArray( elements ) : this;
108 } )
109 .filter( function() {
110 var type = this.type;
111
112 // Use .is( ":disabled" ) so that fieldset[disabled] works
113 return this.name && !jQuery( this ).is( ":disabled" ) &&
114 rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
115 ( this.checked || !rcheckableType.test( type ) );
116 } )
Renovate botf591dcf2023-12-30 14:13:54 +0000117 .map( function( _i, elem ) {
Copybara botbe50d492023-11-30 00:16:42 +0100118 var val = jQuery( this ).val();
119
120 if ( val == null ) {
121 return null;
122 }
123
124 if ( Array.isArray( val ) ) {
125 return jQuery.map( val, function( val ) {
126 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
127 } );
128 }
129
130 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
131 } ).get();
132 }
133} );
134
135return jQuery;
136} );