Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .serialize() support. Fixes #69 #827

Merged
merged 1 commit into from
May 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('');