Re: form serialization (Code Worth Recommending Project)
- From: David Mark <dmark.cinsoft@xxxxxxxxx>
- Date: Thu, 6 Dec 2007 15:10:41 -0800 (PST)
On Dec 6, 5:32 pm, Peter Michaux <petermich...@xxxxxxxxx> wrote:
Below is the more complex form serialization implementation in a
branch of the repository. This one even contains an attempt at
documentation although it isn't so great.
I'd like to know other opinions of assuming the existance of
Array.protoype.join. It is very easy to test for it and the test can
even be short as adding "&&[].join".
I'm not assuming Array.prototype.join because some other new features
are required for this function to even work. That would be object
inference.
I'm assuming Array.prototype.join because it was part of the
ECMAScript 1 spec and that is from a long time ago. I have never
tested for Array.prototype.join anywhere but my JavaScript code is
mostly for behind login.
I think it is safe to skip testing for anything introduced in
JavaScript 1.1.
The Code Worth Recommending project is about upholding high standards
so a check for Array.prototype.join wouldn't cause me concern but
there are other things being assumed in this function that are equally
old: for example, forms have an "elements" object property.
If Array.prototype.join is not tested then that sets a precedent for
the whole project. I was thinking the following feature testing
principle might be a good one
===========
Test for the existence of all features that were introduced after NN4
and IE4. (i.e. features that were not in both NN4 and IE4)
For features that were in NN4 and IE4, only test for their existence
if there is a newer known browser that does not have the feature.
Test that a feature works properly if there has been a known buggy
implementation of that feature or implementations with various
abilities (e.g. String.prototype.replace) in NN4, IE4 or newer
browsers.
Makes sense to me.
===========
Peter
/**
* @object serializeFormUrlencoded [function]
* All elements of the form of type
* * select-one
* * select-multiple
* * radio
* * checkbox
* * text
* * password
* * hidden
* * textarea
* with a non-empty name property and are not disabled
* will be serialized.
*
* Serialize form data in a format similar or identical to the
* <a href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/
forms.html#h-17.13.4.1">
* application/x-www-form-urlencoded</a> standardized format.
* The handling of whitespace is determined by which
* urlencode function is used with this function.
*
* @param f [form]
* The form element to be serialized.
*
* @returns [string]
* The serialized form.
*
* @dependencies
* r - user - urlencode
* r - user - getOptionValue
* a - ES1 - Array.prototype.join
* a - DOM1 - HTMLFormElement.elements
* a - DOM1 - HTMLFormElement.elements.length
* a - DOM1 - HTMLFormElement.elements[i].name
* a - DOM1 - HTMLFormElement.elements[i].type
* a - DOM1 - HTMLFormElement.elements[i].value
* a - DOM1 - HTMLFormElement.elements[i].disabled
* a - DOM1 - HTMLSelectElement.options
* a - DOM1 - HTMLSelectElement.selectedIndex
* a - DOM1 - HTMLOptionElement.selected
* a - DOM1 - HTMLInputElement.checked
*/
// test for required features
if (typeof urlencode != 'undefined' &&
typeof getOptionValue != 'undefined') {
var serializeFormUrlencoded = function(f) {
var e, // form element
n, // form element's name
o, // option element
es = f.elements,
c = []; // the serialization data parts
function add(n, v) {
c[c.length] = urlencode(n) + "=" + urlencode(v);
}
for (var i=0, ilen=es.length; i<ilen; i++) {
e = es[i];
n = e.name;
if (n && !e.disabled) {
switch (e.type) {
// The 'select-one' case could reuse 'select-multiple' case
// The 'select-one' case code is an optimization for
// serialization processing time.
case 'select-one':
if (e.selectedIndex >= 0) {
I think != -1 is clearer as no other negative values are possible.
add(n, getOptionValue(e.options[e.selectedIndex]));
}
break;
case 'select-multiple':
for (var j=0, jlen=e.options.length; j<jlen; j++) {
o = e.options[j];
if (o.selected) {
add(n, getOptionValue(o));
}
}
break;
case 'checkbox':
case 'radio':
if (e.checked) {
add(n, e.value);
}
For the 'checkbox' type, if no value is defined, the default is "on."
break;
case 'text':
case 'password':
case 'hidden':
case 'textarea':
add(n, e.value);
} // switch
You might want to put this code in its own function as it is sometimes
useful to query the value of just one form element by name. Granted,
it would be less efficient for this function and would raise the issue
I brought up before about multiple elements with the same name.
}
}
return c.join('&');
};
}
.
- References:
- Re: form serialization (Code Worth Recommending Project)
- From: Peter Michaux
- Re: form serialization (Code Worth Recommending Project)
- Prev by Date: Re: please help me
- Next by Date: Re: form serialization (Code Worth Recommending Project)
- Previous by thread: Re: form serialization (Code Worth Recommending Project)
- Next by thread: Re: form serialization (Code Worth Recommending Project)
- Index(es):
Relevant Pages
|