Skip to content

Commit 317ed7a

Browse files
Change the behavior of Form.serialize to match the earlier changes to Hash#toQueryString.
Newlines are normalized and spaces are converted to `+` instead of `%20`. One day I'll centralize this logic so we don't have to do this in two places.
1 parent 020eab2 commit 317ed7a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/prototype/dom/form.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ var Form = {
120120
} else {
121121
initial = '';
122122
accumulator = function(result, key, value) {
123-
return result + (result ? '&' : '') + encodeURIComponent(key) + '=' + encodeURIComponent(value);
123+
// Normalize newlines as \r\n because the HTML spec says newlines should
124+
// be encoded as CRLFs.
125+
value = value.gsub(/(\r)?\n/, '\r\n');
126+
value = encodeURIComponent(value);
127+
// Likewise, according to the spec, spaces should be '+' rather than
128+
// '%20'.
129+
value = value.gsub(/%20/, '+');
130+
return result + (result ? '&' : '') + encodeURIComponent(key) + '=' + value;
124131
}
125132
}
126133

test/unit/form_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ new Test.Unit.Runner({
257257
};
258258

259259
// return params
260-
this.assertHashEqual(expected, Form.serialize('various', true));
260+
this.assertHashEqual(expected, Form.serialize('various', true), "test the whole form (as a hash)");
261261
// return string
262262
this.assertEnumEqual(Object.toQueryString(expected).split('&').sort(),
263-
Form.serialize('various').split('&').sort());
263+
Form.serialize('various').split('&').sort(), "test the whole form (as a string)");
264264
this.assertEqual('string', typeof $('form').serialize({ hash:false }));
265265

266266
// Checks that disabled element is not included in serialized form.

0 commit comments

Comments
 (0)