ffffng/app/bower_components/jquery/src/serialize.js

133 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-05-13 13:25:33 +02:00
define( [
2014-05-12 20:08:19 +02:00
"./core",
2019-03-29 22:00:08 +01:00
"./core/toType",
2014-05-12 20:08:19 +02:00
"./manipulation/var/rcheckableType",
2019-03-29 22:00:08 +01:00
"./var/isFunction",
2014-05-12 20:08:19 +02:00
"./core/init",
"./traversing", // filter
"./attributes/prop"
2019-03-29 22:00:08 +01:00
], function( jQuery, toType, rcheckableType, isFunction ) {
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
"use strict";
var
2014-05-12 20:08:19 +02:00
rbracket = /\[\]$/,
rCRLF = /\r?\n/g,
rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
rsubmittable = /^(?:input|select|textarea|keygen)/i;
function buildParams( prefix, obj, traditional, add ) {
var name;
2017-05-13 13:25:33 +02:00
if ( Array.isArray( obj ) ) {
2014-05-12 20:08:19 +02:00
// Serialize array item.
jQuery.each( obj, function( i, v ) {
if ( traditional || rbracket.test( prefix ) ) {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Treat each array item as a scalar.
add( prefix, v );
} else {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Item is non-scalar (array or object), encode its numeric index.
2017-05-13 13:25:33 +02:00
buildParams(
prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
v,
traditional,
add
);
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2019-03-29 22:00:08 +01:00
} else if ( !traditional && toType( obj ) === "object" ) {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Serialize object item.
for ( name in obj ) {
buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
}
} else {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Serialize scalar item.
add( prefix, obj );
}
}
// Serialize an array of form elements or a set of
// key/values into a query string
jQuery.param = function( a, traditional ) {
var prefix,
s = [],
2017-05-13 13:25:33 +02:00
add = function( key, valueOrFunction ) {
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
// If value is a function, invoke it and use its return value
2019-03-29 22:00:08 +01:00
var value = isFunction( valueOrFunction ) ?
2017-05-13 13:25:33 +02:00
valueOrFunction() :
valueOrFunction;
s[ s.length ] = encodeURIComponent( key ) + "=" +
encodeURIComponent( value == null ? "" : value );
};
2014-05-12 20:08:19 +02:00
// If an array was passed in, assume that it is an array of form elements.
2017-05-13 13:25:33 +02:00
if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
2014-05-12 20:08:19 +02:00
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
} else {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for ( prefix in a ) {
buildParams( prefix, a[ prefix ], traditional, add );
}
}
// Return the resulting serialization
2017-05-13 13:25:33 +02:00
return s.join( "&" );
2014-05-12 20:08:19 +02:00
};
2017-05-13 13:25:33 +02:00
jQuery.fn.extend( {
2014-05-12 20:08:19 +02:00
serialize: function() {
return jQuery.param( this.serializeArray() );
},
serializeArray: function() {
2017-05-13 13:25:33 +02:00
return this.map( function() {
2014-05-12 20:08:19 +02:00
// Can add propHook for "elements" to filter or add form elements
var elements = jQuery.prop( this, "elements" );
return elements ? jQuery.makeArray( elements ) : this;
2017-05-13 13:25:33 +02:00
} )
.filter( function() {
2014-05-12 20:08:19 +02:00
var type = this.type;
// Use .is( ":disabled" ) so that fieldset[disabled] works
return this.name && !jQuery( this ).is( ":disabled" ) &&
rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
( this.checked || !rcheckableType.test( type ) );
2017-05-13 13:25:33 +02:00
} )
.map( function( i, elem ) {
2014-05-12 20:08:19 +02:00
var val = jQuery( this ).val();
2017-05-13 13:25:33 +02:00
if ( val == null ) {
return null;
}
if ( Array.isArray( val ) ) {
return jQuery.map( val, function( val ) {
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
} );
}
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
} ).get();
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
return jQuery;
2017-05-13 13:25:33 +02:00
} );