Skip to content

Commit 24414bf

Browse files
npupJorgen Rydenius
authored andcommitted
Add test and changes for serializing forms with multiple select to a string to work again.
Signed-off-by: Jorgen Rydenius <[email protected]>
1 parent addd725 commit 24414bf

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/prototype/dom/form.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,21 @@ var Form = {
119119
};
120120
} else {
121121
initial = '';
122-
accumulator = function(result, key, 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;
131-
}
122+
accumulator = function(result, key, values) {
123+
if (!Object.isArray(values)) {values = [values];}
124+
if (!values.length) {return result;}
125+
var encodedKey = encodeURIComponent(key);
126+
return result + (result ? "&" : "") + values.map(function (value) {
127+
// Normalize newlines as \r\n because the HTML spec says newlines should
128+
// be encoded as CRLFs.
129+
value = value.gsub(/(\r)?\n/, '\r\n');
130+
value = encodeURIComponent(value);
131+
// Likewise, according to the spec, spaces should be '+' rather than
132+
// '%20'.
133+
value = value.gsub(/%20/, '+');
134+
return encodedKey + "=" + value;
135+
}).join("&");
136+
};
132137
}
133138

134139
return elements.inject(initial, function(result, element) {

test/unit/fixtures/form.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,21 @@
125125
<input type="text" name="length" value="foo" />
126126
<input type="text" name="bar" value="baz" />
127127
</form>
128+
129+
<form id="form_with_multiple_select">
130+
<input type="text" name="peewee" value="herman" />
131+
<select name="colors" multiple>
132+
<option value="blue" selected>blue</option>
133+
<option value="red">red</option>
134+
<option value="green">green</option>
135+
<option value="yellow" selected>yellow</option>
136+
<option value="not grey" selected>grey</option>
137+
</select>
138+
<select name="number">
139+
<option value="0">0</option>
140+
<option value="1">1</option>
141+
<option value="2" selected>2</option>
142+
<option value="3">3</option>
143+
</select>
144+
<input type="submit" />
145+
</form>

test/unit/form_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ new Test.Unit.Runner({
325325
this.assert(!select.anInputMethod);
326326
this.assertEqual('select', select.aSelectMethod());
327327
},
328+
329+
testFormSerializeMultipleSelectToQueryString: function () {
330+
var form = $("form_with_multiple_select");
331+
this.assertEqual("peewee=herman&colors=blue&colors=yellow&colors=not+grey&number=2", form.serialize(false));
332+
},
328333

329334
testFormRequest: function() {
330335
var request = $("form").request();

0 commit comments

Comments
 (0)