`Form.serialize` now works safely with forms that have "length"-named elements. [#77 state:resolved]

This commit is contained in:
Juriy Zaytsev 2009-03-23 00:15:34 -04:00
parent 71a8663370
commit 0b4e142d8a
3 changed files with 35 additions and 7 deletions

View File

@ -1,3 +1,5 @@
* `Form.serialize` now works safely with forms that have "length"-named elements. [#77 state:resolved] (Peter Adrianov, John-David Dalton, kangax)
* `Element#update` now takes care of SCRIPT elements in IE. [#573 state:resolved] (Martin, Tobie Langel, kangax)
* Remove unused local variables from `Element.extend`. Fix one of the form tests to remove `_extendedByPrototype` by setting it to `undefined` rather than `false` (`_extendedByPrototype` being `false` does not force `Element.extend` to re-extend element). (T.J. Crowder, kangax)

View File

@ -82,13 +82,20 @@ Form.Methods = {
* Returns a collection of all controls within a form.
**/
getElements: function(form) {
return $A($(form).getElementsByTagName('*')).inject([],
function(elements, child) {
if (Form.Element.Serializers[child.tagName.toLowerCase()])
elements.push(Element.extend(child));
return elements;
}
);
var elements = $(form).getElementsByTagName('*'),
element,
arr = [ ],
serializers = Form.Element.Serializers;
// `length` is not used to prevent interference with
// length-named elements shadowing `length` of a nodelist
for (var i = 0; element = elements[i]; i++) {
arr.push(element);
}
return arr.inject([], function(elements, child) {
if (serializers[child.tagName.toLowerCase()])
elements.push(Element.extend(child));
return elements;
})
},
/**

View File

@ -359,5 +359,24 @@ new Test.Unit.Runner({
'multiple select options improperly set');
input.setValue(['1', '3']);
this.assertEnumEqual(['1', '3'], input.getValue());
},
testSerializeFormTroublesomeNames: function() {
var el = new Element('form', {
action: '/'
});
var input = new Element('input', {
type: 'text',
name: 'length',
value: 'foo'
});
var input2 = new Element('input', {
type: 'text',
name: 'bar',
value: 'baz'
});
el.appendChild(input);
el.appendChild(input2);
this.assertHashEqual({ length: 'foo', bar: 'baz' }, el.serialize(true));
}
});