Re: form serialization (Code Worth Recommending Project)



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('&');
};

}

.



Relevant Pages

  • Re: C# vs VB.Net
    ... In VB.NET you have to ~remember to put in "namespace ... Also, in VB 6, you could have the IDE ... Status unknown - I haven't used the serialize features of dotNET. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Serializing inherited objects
    ... The new class adds some features ... not get to automatically serialize the data from my parent class. ... StreamingContext context) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross-Browser onmousedown JavaScript
    ... It would only make sense to implement the W3C events model if there is a demand for currently unused event features; and if this were happening, nothing would prevent Microsoft from extending the current model, rather than implementing the W3C's model. ... Given this situation, two solutions are available: either change/correct the scripts, or change/evolve the browser's model. ... The choice of changing the model looks like the less painful; after all, scripts do not need to updated, and the browser ends up with more capabilities, right? ...
    (comp.lang.javascript)
  • Re: A more rational approach to Browsers - Microsoft please read this
    ... Setting security in IE is a lot closer to ALL or NONE than it should be. ... The mechanism does not allow the degree of control which the Internet ... Every new feature has the potential to interact with existing features. ... > We do need a secure browser, but that can be done with one browser, and is ...
    (microsoft.public.security)
  • Re: langua= vs type=
    ... It is certainly true that language features that have been introduced ... encountering a browser that does not support the features of JavaScript ... standard to implement as the basis for a client-side scripting language. ...
    (comp.lang.javascript)