Skip to content

Commit 9fdbdd3

Browse files
jtrumbullgibson042
authored andcommitted
Serialize: Treat literal and function-returned null/undefined the same
Fixes gh-3005 Closes gh-3007
1 parent 5d20a3c commit 9fdbdd3

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/serialize.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ jQuery.param = function( a, traditional ) {
5858
add = function( key, value ) {
5959

6060
// If value is a function, invoke it and return its value
61-
value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
61+
value = jQuery.isFunction( value ) ? value() : value;
62+
if ( value == null ) {
63+
value = "";
64+
}
6265
s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
6366
};
6467

test/unit/serialize.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
QUnit.module( "serialize", { teardown: moduleTeardown } );
22

33
QUnit.test( "jQuery.param()", function( assert ) {
4-
assert.expect( 23 );
4+
assert.expect( 24 );
55

66
var params, settings;
77

@@ -74,6 +74,9 @@ QUnit.test( "jQuery.param()", function( assert ) {
7474
params = { "param1": null };
7575
assert.equal( jQuery.param( params, false ), "param1=", "Make sure that null params aren't traversed." );
7676

77+
params = { "param1": function() {}, "param2": function() { return null; } };
78+
assert.equal( jQuery.param( params, false ), "param1=&param2=", "object with function property that returns null value" );
79+
7780
params = { "test": { "length": 3, "foo": "bar" } };
7881
assert.equal( jQuery.param( params, false ), "test%5Blength%5D=3&test%5Bfoo%5D=bar", "Sub-object with a length property" );
7982

0 commit comments

Comments
 (0)