Permalink
Browse files

Ensure that named[0] is truthy. Fixes #1

  • Loading branch information...
1 parent 241c748 commit 023ff0a4b7c708a48edb2735e2a5825aa588b0b5 @rwaldron rwaldron committed Jul 22, 2011
Showing with 21 additions and 17 deletions.
  1. +19 −15 jquery.serializeObject.js
  2. +2 −2 test/unit/jquery.serializeObject.js
View
34 jquery.serializeObject.js
@@ -1,7 +1,7 @@
-/*
+/*
* .serializeObject (c) Dan Heberden
* danheberden.com
-*
+*
* Gives you a pretty object for your form elements
*/
(function($){
@@ -18,21 +18,25 @@
cap = named.length - 1,
i = 0;
- for ( ; i < cap; i++ ) {
- // move down the tree - create objects or array if necessary
- lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
- ( named[i+1] == "" ? [] : {} );
- }
+ // Ensure that only elements with valid `name` properties will be serialized
+ if ( named[ 0 ] ) {
+ for ( ; i < cap; i++ ) {
+ // move down the tree - create objects or array if necessary
+ lookup = lookup[ named[i] ] = lookup[ named[i] ] ||
+ ( named[i+1] == "" ? [] : {} );
+ }
- // at the end, psuh or assign the value
- if ( lookup.length != undefined ) {
- lookup.push( $(this).val() );
- }else {
- lookup[ named[ cap ] ] = $(this).val();
- }
+ // at the end, psuh or assign the value
+ if ( lookup.length != undefined ) {
+ lookup.push( $(this).val() );
+ }else {
+ lookup[ named[ cap ] ] = $(this).val();
+ }
- // assign the reference back to root
- lookup = data;
+ // assign the reference back to root
+ lookup = data;
+
+ }
});
return data;
View
4 test/unit/jquery.serializeObject.js
@@ -1,6 +1,6 @@
// Supporting Issue #1
-test("Serialize elements with a name property", function() {
+test("Only serializes elements with `name` property", function() {
expect( 1 );
@@ -10,5 +10,5 @@ test("Serialize elements with a name property", function() {
password: "seriously"
};
- deepEqual( $("#test-form").serializeObject(), looksLike, "jQuery.fn.serializeObject() serializes elements with name property" );
+ deepEqual( $("#test-form").serializeObject(), looksLike, "jQuery.fn.serializeObject() only serializes elements with `name` property" );
});

0 comments on commit 023ff0a

Please sign in to comment.