diff --git a/lib/api/forms.js b/lib/api/forms.js index 5f4a883ee2..9e335c9a1c 100644 --- a/lib/api/forms.js +++ b/lib/api/forms.js @@ -2,9 +2,7 @@ // https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js var _ = require('lodash'), submittableSelector = 'input,select,textarea,keygen', - rCRLF = /\r?\n/g, - rcheckableType = /^(?:checkbox|radio)$/i, - rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i; + rCRLF = /\r?\n/g; exports.serializeArray = function() { // Resolve all form elements from either forms or collections of form elements @@ -17,18 +15,15 @@ exports.serializeArray = function() { } else { return $elem.filter(submittableSelector).toArray(); } - }).filter(function() { - var $elem = Cheerio(this); - var type = $elem.attr('type'); - - // Verify elements have a name (`attr.name`) and are not disabled (`:disabled`) - return $elem.attr('name') && !$elem.is(':disabled') && + }).filter( + // Verify elements have a name (`attr.name`) and are not disabled (`:disabled`) + '[name!=""]:not(:disabled)' // and cannot be clicked (`[type=submit]`) or are used in `x-www-form-urlencoded` (`[type=file]`) - !rsubmitterTypes.test(type) && + + ':not(:submit, :button, :image, :reset, :file)' // and are either checked/don't have a checkable state - ($elem.attr('checked') || !rcheckableType.test(type)); + + ':matches([checked], :not(:checkbox, :radio))' // Convert each of the elements to its value(s) - }).map(function(i, elem) { + ).map(function(i, elem) { var $elem = Cheerio(elem); var name = $elem.attr('name'); var val = $elem.val(); diff --git a/test/api/forms.js b/test/api/forms.js index e531b07ca7..2812ec7d3b 100644 --- a/test/api/forms.js +++ b/test/api/forms.js @@ -115,6 +115,17 @@ describe('$(...)', function() { ]); }); + it('() : shouldn\'t serialize the empty string', function() { + expect($('').serializeArray()).to.eql([]); + expect($('').serializeArray()).to.eql([]); + expect($('').serializeArray()).to.eql([ + { + name: 'fruit', + value: 'pineapple' + } + ]); + }); + }); });