Skip to content

Commit

Permalink
Added .serialize() support. Fixes #69 (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
twolfson authored and fb55 committed May 8, 2016
1 parent df39f33 commit e65ad72
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/api/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
// https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
var _ = require('lodash'),
submittableSelector = 'input,select,textarea,keygen',
r20 = /%20/g,
rCRLF = /\r?\n/g;

exports.serialize = function() {
// Convert form elements into name/value objects
var arr = this.serializeArray();

// Serialize each element into a key/value string
var retArr = _.map(arr, function(data) {
return encodeURIComponent(data.name) + '=' + encodeURIComponent(data.value);
});

// Return the resulting serialization
return retArr.join('&').replace(r20, '+');
};

exports.serializeArray = function() {
// Resolve all form elements from either forms or collections of form elements
var Cheerio = this.constructor;
Expand Down
24 changes: 24 additions & 0 deletions test/api/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,28 @@ describe('$(...)', function() {

});

describe('.serialize', function() {

it('() : should get form controls', function() {
expect($('form#simple').serialize()).to.equal('fruit=Apple');
});

it('() : should get nested form controls', function() {
expect($('form#nested').serialize()).to.equal('fruit=Apple&vegetable=Carrot');
});

it('() : should not get disabled form controls', function() {
expect($('form#disabled').serialize()).to.equal('');
});

it('() : should get multiple selected options', function() {
expect($('form#multiple').serialize()).to.equal('fruit=Apple&fruit=Orange');
});

it('() : should encode spaces as +\'s', function() {
expect($('form#spaces').serialize()).to.equal('fruit=Blood+orange');
});

});

});
3 changes: 2 additions & 1 deletion test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ exports.forms = [
'<form id="select"><select name="fruit"><option value="Apple">Apple</option><option value="Orange" selected>Orange</option></select></form>',
'<form id="unnamed"><input type="text" name="fruit" value="Apple" /><input type="text" value="Carrot" /></form>',
'<form id="multiple"><select name="fruit" multiple><option value="Apple" selected>Apple</option><option value="Orange" selected>Orange</option><option value="Carrot">Carrot</option></select></form>',
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>'
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>',
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>'
].join('');

0 comments on commit e65ad72

Please sign in to comment.