Skip to content

Commit e65ad72

Browse files
twolfsonfb55
authored andcommittedMay 8, 2016
Added .serialize() support. Fixes #69 (#827)
1 parent df39f33 commit e65ad72

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
 

‎lib/api/forms.js

+14
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@
22
// https://github.com/jquery/jquery/blob/2.1.3/src/serialize.js
33
var _ = require('lodash'),
44
submittableSelector = 'input,select,textarea,keygen',
5+
r20 = /%20/g,
56
rCRLF = /\r?\n/g;
67

8+
exports.serialize = function() {
9+
// Convert form elements into name/value objects
10+
var arr = this.serializeArray();
11+
12+
// Serialize each element into a key/value string
13+
var retArr = _.map(arr, function(data) {
14+
return encodeURIComponent(data.name) + '=' + encodeURIComponent(data.value);
15+
});
16+
17+
// Return the resulting serialization
18+
return retArr.join('&').replace(r20, '+');
19+
};
20+
721
exports.serializeArray = function() {
822
// Resolve all form elements from either forms or collections of form elements
923
var Cheerio = this.constructor;

‎test/api/forms.js

+24
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,28 @@ describe('$(...)', function() {
128128

129129
});
130130

131+
describe('.serialize', function() {
132+
133+
it('() : should get form controls', function() {
134+
expect($('form#simple').serialize()).to.equal('fruit=Apple');
135+
});
136+
137+
it('() : should get nested form controls', function() {
138+
expect($('form#nested').serialize()).to.equal('fruit=Apple&vegetable=Carrot');
139+
});
140+
141+
it('() : should not get disabled form controls', function() {
142+
expect($('form#disabled').serialize()).to.equal('');
143+
});
144+
145+
it('() : should get multiple selected options', function() {
146+
expect($('form#multiple').serialize()).to.equal('fruit=Apple&fruit=Orange');
147+
});
148+
149+
it('() : should encode spaces as +\'s', function() {
150+
expect($('form#spaces').serialize()).to.equal('fruit=Blood+orange');
151+
});
152+
153+
});
154+
131155
});

‎test/fixtures.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ exports.forms = [
6767
'<form id="select"><select name="fruit"><option value="Apple">Apple</option><option value="Orange" selected>Orange</option></select></form>',
6868
'<form id="unnamed"><input type="text" name="fruit" value="Apple" /><input type="text" value="Carrot" /></form>',
6969
'<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>',
70-
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>'
70+
'<form id="textarea"><textarea name="fruits">Apple\nOrange</textarea></form>',
71+
'<form id="spaces"><input type="text" name="fruit" value="Blood orange" /></form>'
7172
].join('');

0 commit comments

Comments
 (0)
Please sign in to comment.