Blame view

bower_components/jquery/src/serialize.js 3.14 KB
c5169e0e   Renato De Donato   a new hope
1
  define([
74249687   Luigi Serra   Cross browser con...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  	"./core",
  	"./manipulation/var/rcheckableType",
  	"./core/init",
  	"./traversing", // filter
  	"./attributes/prop"
  ], function( jQuery, rcheckableType ) {
  
  var r20 = /%20/g,
  	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;
  
  	if ( jQuery.isArray( obj ) ) {
74249687   Luigi Serra   Cross browser con...
19
20
21
  		// Serialize array item.
  		jQuery.each( obj, function( i, v ) {
  			if ( traditional || rbracket.test( prefix ) ) {
74249687   Luigi Serra   Cross browser con...
22
23
24
25
  				// Treat each array item as a scalar.
  				add( prefix, v );
  
  			} else {
74249687   Luigi Serra   Cross browser con...
26
  				// Item is non-scalar (array or object), encode its numeric index.
c5169e0e   Renato De Donato   a new hope
27
  				buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add );
74249687   Luigi Serra   Cross browser con...
28
  			}
c5169e0e   Renato De Donato   a new hope
29
  		});
74249687   Luigi Serra   Cross browser con...
30
31
  
  	} else if ( !traditional && jQuery.type( obj ) === "object" ) {
74249687   Luigi Serra   Cross browser con...
32
33
34
35
36
37
  		// Serialize object item.
  		for ( name in obj ) {
  			buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
  		}
  
  	} else {
74249687   Luigi Serra   Cross browser con...
38
39
40
41
42
43
44
45
46
47
48
  		// 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 = [],
  		add = function( key, value ) {
74249687   Luigi Serra   Cross browser con...
49
50
51
52
53
54
55
56
57
58
59
60
  			// If value is a function, invoke it and return its value
  			value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
  			s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
  		};
  
  	// Set traditional to true for jQuery <= 1.3.2 behavior.
  	if ( traditional === undefined ) {
  		traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
  	}
  
  	// If an array was passed in, assume that it is an array of form elements.
  	if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
74249687   Luigi Serra   Cross browser con...
61
62
63
  		// Serialize the form elements
  		jQuery.each( a, function() {
  			add( this.name, this.value );
c5169e0e   Renato De Donato   a new hope
64
  		});
74249687   Luigi Serra   Cross browser con...
65
66
  
  	} else {
74249687   Luigi Serra   Cross browser con...
67
68
69
70
71
72
73
74
75
76
77
  		// 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
  	return s.join( "&" ).replace( r20, "+" );
  };
  
c5169e0e   Renato De Donato   a new hope
78
  jQuery.fn.extend({
74249687   Luigi Serra   Cross browser con...
79
80
81
82
  	serialize: function() {
  		return jQuery.param( this.serializeArray() );
  	},
  	serializeArray: function() {
c5169e0e   Renato De Donato   a new hope
83
  		return this.map(function() {
74249687   Luigi Serra   Cross browser con...
84
85
86
  			// Can add propHook for "elements" to filter or add form elements
  			var elements = jQuery.prop( this, "elements" );
  			return elements ? jQuery.makeArray( elements ) : this;
c5169e0e   Renato De Donato   a new hope
87
88
  		})
  		.filter(function() {
74249687   Luigi Serra   Cross browser con...
89
90
91
92
93
94
  			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 ) );
c5169e0e   Renato De Donato   a new hope
95
96
  		})
  		.map(function( i, elem ) {
74249687   Luigi Serra   Cross browser con...
97
98
99
100
101
102
103
  			var val = jQuery( this ).val();
  
  			return val == null ?
  				null :
  				jQuery.isArray( val ) ?
  					jQuery.map( val, function( val ) {
  						return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
c5169e0e   Renato De Donato   a new hope
104
  					}) :
74249687   Luigi Serra   Cross browser con...
105
  					{ name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
c5169e0e   Renato De Donato   a new hope
106
  		}).get();
74249687   Luigi Serra   Cross browser con...
107
  	}
c5169e0e   Renato De Donato   a new hope
108
  });
74249687   Luigi Serra   Cross browser con...
109
110
  
  return jQuery;
c5169e0e   Renato De Donato   a new hope
111
  });