Skip to content

Commit

Permalink
Fix #12134. Make .serialize() HTML5-compliant; provide a propHook for…
Browse files Browse the repository at this point in the history
… shimming.
  • Loading branch information
dmethvin committed Nov 25, 2012
1 parent a938d7b commit ae215fd
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/serialize.js
@@ -1,21 +1,26 @@
var r20 = /%20/g, var r20 = /%20/g,
rbracket = /\[\]$/, rbracket = /\[\]$/,
rCRLF = /\r?\n/g, rCRLF = /\r?\n/g,
rinput = /^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i, rcheckTypes = /^(?:checkbox|radio)$/i,
rselectTextarea = /^(?:select|textarea)/i; rsubmitterTypes = /^(?:submit|button|image|reset)$/i,
rsubmittable = /^(?:select|textarea|input|keygen)/i;


jQuery.fn.extend({ jQuery.fn.extend({
serialize: function() { serialize: function() {
return jQuery.param( this.serializeArray() ); return jQuery.param( this.serializeArray() );
}, },
serializeArray: function() { serializeArray: function() {
return this.map(function(){ return this.map(function(){
return this.elements ? jQuery.makeArray( this.elements ) : this; // Can add propHook for "elements" to filter or add form elements
var elements = jQuery.prop( this, "elements" );
return elements ? jQuery.makeArray( elements ) : this;
}) })
.filter(function(){ .filter(function(){
return this.name && !this.disabled && var type = this.type;
( this.checked || rselectTextarea.test( this.nodeName ) || // Use .is(":disabled") so that fieldset[disabled] works

This comment has been minimized.

Copy link
@Krinkle

Krinkle Nov 25, 2012

Member

Can you elaborate on this? How does it help that by making a jQuery object and using a selector match instead of a property check?

This comment has been minimized.

Copy link
@dmethvin

dmethvin Nov 25, 2012

Author Member

This comment has been minimized.

Copy link
@Krinkle

Krinkle Nov 25, 2012

Member

Thanks! I understand now. Comment could've been a more elaborate by mentioning the parent relationship to fieldset.

rinput.test( this.type ) ); return this.name && !jQuery( this ).is( ":disabled" ) &&
rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
( this.checked || !rcheckTypes.test( type ) );
}) })
.map(function( i, elem ){ .map(function( i, elem ){
var val = jQuery( this ).val(); var val = jQuery( this ).val();
Expand Down Expand Up @@ -77,13 +82,7 @@ function buildParams( prefix, obj, traditional, add ) {
add( prefix, v ); add( prefix, v );


} else { } else {
// If array item is non-scalar (array or object), encode its // Item is non-scalar (array or object), encode its numeric index.
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add ); buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add );
} }
}); });
Expand Down

0 comments on commit ae215fd

Please sign in to comment.