From 8e3987e0fe7941d530c83a207f7c22e1ac475083 Mon Sep 17 00:00:00 2001
From: Frederick Polgardy
Date: Wed, 8 Feb 2012 08:10:57 -0600
Subject: [PATCH 01/23] Fixed $.Class function to return class definition (from
@fmarsoni)
---
class/class.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/class/class.js b/class/class.js
index 18854c74..8df6f95d 100644
--- a/class/class.js
+++ b/class/class.js
@@ -349,7 +349,7 @@ steal("jquery","jquery/lang/string",function( $ ) {
clss = $.Class = function() {
if (arguments.length) {
- clss.extend.apply(clss, arguments);
+ return clss.extend.apply(clss, arguments);
}
};
From 5b30e30f6a47a7f6e219b5afc3bf4d2b3f6f83dc Mon Sep 17 00:00:00 2001
From: TQ White II/qbook
Date: Wed, 15 Feb 2012 10:33:48 -0600
Subject: [PATCH 02/23] form_params() was incorrectly returning scalar form
values as arrays, now it only returns array for checkbox
---
dom/form_params/form_params.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dom/form_params/form_params.js b/dom/form_params/form_params.js
index 69f56ac2..bbf78325 100644
--- a/dom/form_params/form_params.js
+++ b/dom/form_params/form_params.js
@@ -145,7 +145,7 @@ steal("jquery/dom").then(function( $ ) {
//now we are on the last part, set the value
if (current[lastPart]) {
- if (!$.isArray(current[lastPart]) ) {
+ if (!$.isArray(current[lastPart] && type === "checkbox") ) {
current[lastPart] = current[lastPart] === undefined ? [] : [current[lastPart]];
}
if ( write ) {
From 766abeb9f60abef2b9b09c5e740b3d84b06250b4 Mon Sep 17 00:00:00 2001
From: Ralph Holzmann
Date: Wed, 15 Feb 2012 13:19:00 -0600
Subject: [PATCH 03/23] Fixed bug where last checkbox of checkboxes with
multiple names would return a scalar instead of an array. Tests attached,
100% passing.
---
dom/form_params/form_params.js | 137 +++++++++++++++-------------
dom/form_params/form_params_test.js | 7 +-
dom/form_params/test/basics.micro | 10 ++
3 files changed, 92 insertions(+), 62 deletions(-)
diff --git a/dom/form_params/form_params.js b/dom/form_params/form_params.js
index bbf78325..7e7a66cd 100644
--- a/dom/form_params/form_params.js
+++ b/dom/form_params/form_params.js
@@ -2,22 +2,58 @@
* @add jQuery.fn
*/
steal("jquery/dom").then(function( $ ) {
- var radioCheck = /radio|checkbox/i,
- keyBreaker = /[^\[\]]+/g,
- numberMatcher = /^[\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?$/;
+ var keyBreaker = /[^\[\]]+/g,
+ convertValue = function( value ) {
+ if ( $.isNumeric( value )) {
+ return parseFloat( value );
+ } else if ( value === 'true') {
+ return true;
+ } else if ( value === 'false' ) {
+ return false;
+ } else if ( value === '' ) {
+ return undefined;
+ }
+ return value;
+ },
+ nestData = function( elem, type, data, parts, value, seen ) {
+ var name = parts.shift();
+
+ if ( parts.length ) {
+ if ( ! data[ name ] ) {
+ data[ name ] = {};
+ }
+ // Recursive call
+ nestData( elem, type, data[ name ], parts, value, seen );
+ } else {
+
+ // Handle same name case, as well as "last checkbox checked"
+ // case
+ if ( name in seen && type != "radio" && ! $.isArray( data[ name ] )) {
+ if ( name in data ) {
+ data[ name ] = [ data[name] ];
+ } else {
+ data[ name ] = [];
+ }
+ } else {
+ seen[ name ] = true;
+ }
- var isNumber = function( value ) {
- if ( typeof value == 'number' ) {
- return true;
- }
+ // Finally, assign data
+ if ( ( type == "radio" || type == "checkbox" ) && ! elem.is(":checked") ) {
+ return
+ }
- if ( typeof value != 'string' ) {
- return false;
- }
+ if ( ! data[ name ] ) {
+ data[ name ] = value;
+ } else {
+ data[ name ].push( value );
+ }
+
- return value.match(numberMatcher);
- };
+ }
+ };
+
$.fn.extend({
/**
* @parent dom
@@ -53,7 +89,9 @@ steal("jquery/dom").then(function( $ ) {
* to the result. Defaults to false.
* @return {Object} An object of name-value pairs.
*/
- formParams: function( params, convert ) {
+ formParams: function( params ) {
+
+ var convert;
// Quick way to determine if something is a boolean
if ( !! params === params ) {
@@ -63,10 +101,9 @@ steal("jquery/dom").then(function( $ ) {
if ( params ) {
return this.setParams( params );
- } else if ( this[0].nodeName.toLowerCase() == 'form' && this[0].elements ) {
- return jQuery(jQuery.makeArray(this[0].elements)).getParams(convert);
+ } else if ( this.is("form") ) {
+ return this.getParams( convert );
}
- return jQuery("input[name], textarea[name], select[name]", this[0]).getParams(convert);
},
setParams: function( params ) {
@@ -100,63 +137,41 @@ steal("jquery/dom").then(function( $ ) {
},
getParams: function( convert ) {
var data = {},
+ // This is used to keep track of the checkbox names that we've
+ // already seen, so we know that we should return an array if
+ // we see it multiple times. Fixes last checkbox checked bug.
+ seen = {},
current;
- convert = convert === undefined ? false : convert;
- this.each(function() {
- var el = this,
- type = el.type && el.type.toLowerCase();
- //if we are submit, ignore
- if ((type == 'submit') || !el.name ) {
+ this.find("[name]").each(function() {
+ var $this = $(this),
+ type = $this.attr("type"),
+ name = $this.attr("name"),
+ value = $this.val(),
+ parts;
+
+ // Don't accumulate submit buttons and nameless elements
+ if ( type == "submit" || ! name ) {
return;
}
- var key = el.name,
- value = $.data(el, "value") || $.fn.val.call([el]),
- isRadioCheck = radioCheck.test(el.type),
- parts = key.match(keyBreaker),
- write = !isRadioCheck || !! el.checked,
- //make an array of values
- lastPart;
-
- if ( convert ) {
- if ( isNumber(value) ) {
- value = parseFloat(value);
- } else if ( value === 'true') {
- value = true;
- } else if ( value === 'false' ) {
- value = false;
- }
- if(value === '') {
- value = undefined;
- }
+ // Figure out name parts
+ parts = name.match( keyBreaker );
+ if ( ! parts.length ) {
+ parts = [name];
}
- // go through and create nested objects
- current = data;
- for ( var i = 0; i < parts.length - 1; i++ ) {
- if (!current[parts[i]] ) {
- current[parts[i]] = {};
- }
- current = current[parts[i]];
+ // Convert the value
+ if ( convert ) {
+ value = convertValue( value );
}
- lastPart = parts[parts.length - 1];
-
- //now we are on the last part, set the value
- if (current[lastPart]) {
- if (!$.isArray(current[lastPart] && type === "checkbox") ) {
- current[lastPart] = current[lastPart] === undefined ? [] : [current[lastPart]];
- }
- if ( write ) {
- current[lastPart].push(value);
- }
- } else if ( write || !current[lastPart] ) {
- current[lastPart] = write ? value : undefined;
- }
+ // Assign data recursively
+ nestData( $this, type, data, parts, value, seen );
});
+
return data;
}
});
diff --git a/dom/form_params/form_params_test.js b/dom/form_params/form_params_test.js
index 93d50fd1..ff158c11 100644
--- a/dom/form_params/form_params_test.js
+++ b/dom/form_params/form_params_test.js
@@ -10,6 +10,7 @@ module("jquery/dom/form_params")
test("with a form", function(){
$("#qunit-test-area").html("//jquery/dom/form_params/test/basics.micro",{})
+
var formParams = $("#qunit-test-area form").formParams() ;
ok(formParams.params.one === "1","one is right");
@@ -17,9 +18,13 @@ test("with a form", function(){
ok(formParams.params.three === "3","three is right");
same(formParams.params.four,["4","1"],"four is right");
same(formParams.params.five,["2","3"],"five is right");
-
equal(typeof formParams.id , 'string', "Id value is empty");
+ equal( typeof formParams.singleRadio, "string", "Type of single named radio is string" );
+ equal( formParams.singleRadio, "2", "Value of single named radio is right" );
+
+ ok( $.isArray(formParams.lastOneChecked), "Type of checkbox with last option checked is array" );
+ equal( formParams.lastOneChecked, "4", "Value of checkbox with the last option checked is 4" );
});
diff --git a/dom/form_params/test/basics.micro b/dom/form_params/test/basics.micro
index dfc247ff..14e76957 100644
--- a/dom/form_params/test/basics.micro
+++ b/dom/form_params/test/basics.micro
@@ -11,6 +11,10 @@
+
+
+
+