From 0b4e142d8aea0383b3fcf30af925c982b7b175c0 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Mon, 23 Mar 2009 00:15:34 -0400 Subject: [PATCH] `Form.serialize` now works safely with forms that have "length"-named elements. [#77 state:resolved] --- CHANGELOG | 2 ++ src/dom/form.js | 21 ++++++++++++++------- test/unit/form_test.js | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c9ba5a9..bd00446 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/src/dom/form.js b/src/dom/form.js index e398374..274969a 100644 --- a/src/dom/form.js +++ b/src/dom/form.js @@ -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; + }) }, /** diff --git a/test/unit/form_test.js b/test/unit/form_test.js index 502691e..f80044f 100644 --- a/test/unit/form_test.js +++ b/test/unit/form_test.js @@ -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)); } }); \ No newline at end of file