From: Matěj Cepl on
Dne 18.12.2009 17:52, Thomas 'PointedEars' Lahn napsal(a):
> And here is a better (but still improvable), production code from another
> project that I have not backported yet:

I will gladly improve it, if I can ... what's the license? You mentioned
in the previous post GPLv3, but I would much prefer (for many reasons)
MIT/X11. Can I, please, or do I have to rewrite it from scratch?

Thanks a lot for the ideas anyway,

Matěj

--
http://www.ceplovi.cz/matej/, Jabber: mcepl<at>ceplovi.cz
GPG Finger: 89EF 4BC6 288A BF43 1BAB 25C3 E09F EF25 D964 84AC

Our lives are spectacles of powerlessness.
-- Richard Rohr
From: Thomas 'PointedEars' Lahn on
Matěj Cepl wrote:

> Thomas 'PointedEars' Lahn napsal(a):
>> And here is a better (but still improvable), production code from another
>> project that I have not backported yet:
>
> I will gladly improve it, if I can ... what's the license?

This will be probably be distributed under the GPLv3, too, as it will
replace what I had in mind for the next release for the most part.

> You mentioned in the previous post GPLv3, but I would much prefer (for
> many reasons) MIT/X11. Can I, please, or do I have to rewrite it from
> scratch?

AFAIK, the MIT License without modifications is fully GPL-compatible. AIUI,
that means code under the former license can be combined with a program
licensed under the GPL without conflict, but not vice-versa. So, IIUC, if
you want to use this code with an MIT-licensed product you will need to
rewrite it from scratch.

> Thanks a lot for the ideas anyway,

You are welcome.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: David Mark on
On Dec 18, 11:52 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> Thomas 'PointedEars' Lahn wrote:
> > Matěj Cepl wrote:
> >> Dne 16.12.2009 22:55, Thomas 'PointedEars' Lahn napsal(a):
> >>> You can store the form data in a cookie or Storage, and restore the form
> >>> data from it when applicable.  You could make an non-modifying XHR
> >>> request to your server and handle it synchronously to see if you are
> >>> online.  You need to assume you are online if XHR is not supported.
>
> >> Hmm, sounds interesting. I have in meantime dived a little bit more into
> >> forms submission technology and I think I am getting some ideas now. It
> >> will be fun to put it all together but it sounds doable. If I have
> >> enough time, that is :(.
>
> > Following the beta of a form serialization method that I am going to
> > distribute as free software (under the GNU GPL v3) as part of
> > httprequest.js, which should be enough to get you started:
>
> > [...]
>
> And here is a better (but still improvable), production code from another
> project that I have not backported yet:
>
> /* ------------------------------------------------------------------ */
>
> /**
>  * @param f : Form|HTMLFormElement
>  * @return string
>  */
> function serializeForm(f)
> {
>   var gotSubmit = false;
>
>   /**
>    * @param o
>    */
>   function serializeControl(o)
>   {
>     /* HTML 4.01: Controls that are disabled cannot be successful. */
>     if (!o.disabled)
>     {
>       /*
>        * If a form contains more than one submit button,
>        * only the activated submit button is successful.
>        * (here: the first one)
>        */
>       var isSubmit = /(^|\s)(submit|image)(\s|$)/i.test(o.type);
>       if (!gotSubmit || !isSubmit)
>       {
>         if (isSubmit) gotSubmit = true;
>
>         /*
>          * For menus, the control name is provided by a SELECT element
>          * and values are provided by OPTION elements. Only selected
>          * options may be successful. When no options are selected,
>          * the control is not successful and neither the name nor any
>          * values are submitted to the server when the form is submitted.
>          */
>         var m = /(^|\s)(select(-one)?|undefined)(\s|$)/i.exec(o.type);
>         if (m)
>         {
>           /* select-one */
>           if (m[3])
>           {
>             if (o.selectedIndex > -1)
>             {
>               items.add(o.name, o.options[o.selectedIndex].value);

This is a problem. You need to know if the value attribute is
present. Depends on the context, of course.


>             }
>           }
>
>           /* select */
>           else if (m[2])
>           {
>             for (var i = 0, opts = o.options, len = opts && opts.length;
>                  i < len; i++)
>             {
>               var opt = opts[i];
>               if (opt.selected)
>               {
>                 items.add(o.name, opt.value);

Same here.

>               }
>             }
>           }
>         }
>
>         /*
>          * All "on" checkboxes may be successful..
>          * For radio buttons that share the same value of the
>          * name attribute, only the "on" radio button may be successful.
>          */
>         else if (!/(^|\s)file|reset(\s|$)/i.test(o.type)
>                  && !(/(^|\s)object(\s|$)/i.test(o.tagName) && o.declare)
>                  && !/(^|\s)(checkbox|radio)(\s|$)/i.test(o.type)
>                  || o.checked)
>         {
>           items.add(o.name, o.value);
>         }
>       }
>     }
>   }
>
>   var es = getFeature(f, "elements");
>   if (es)
>   {
>     var items = [];
>
>     items.add = function(sName, sValue) {
>       var s = esc(sName) + "=" + esc(sValue);
>       this.push(s);
>     };
>
>     if (!isMethod(items, "push"))

Is this the host method test function?

>     {
>       items.push = function() {
>         for (var i = 0, len = arguments.length; i < len; i++)
>         {
>           items[items.length] = arguments[i];
>         }
>       };
>     }
>
>     for (var i = 0, len = es.length; i < len; i++)
>     {
>       var e = es[i];
>
>       /*
>        * Elements with the same name create a NodeList object,
>        * however options of select objects are also indexable in Gecko.
>        */
>       if (typeof e[0] != "undefined" && typeof e.options == "undefined")

I don't care for that.

>       {
>         for (var j = 0, len2 = e.length; j < len2; j++)
>         {
>           serializeControl(e[j]);
>         }
>       }
>       else
>       {
>         serializeControl(e);
>       }
>     }
>
>     return items.join("&");
>   }
>
>   return "";
>
> }
>
> /* ------------------------------------------------------------------ */
>
> esc(), getFeature() and isMethod() are user-defined, of course, but you
> probably get the idea..
>

Mostly.
From: Thomas 'PointedEars' Lahn on
David Mark wrote:

> Thomas 'PointedEars' Lahn wrote:
>> items.add(o.name, o.options[o.selectedIndex].value);
>
> This is a problem. You need to know if the value attribute is
> present. Depends on the context, of course.

You are mistaken. Keep in mind that this is supposed to serialize the form
as if it was submitted as usual.

>> [...]
>> for (var i = 0, opts = o.options, len = opts && opts.length;
>> i < len; i++)
>> {
>> var opt = opts[i];
>> if (opt.selected)
>> {
>> items.add(o.name, opt.value);
>
> Same here.

No.

>> if (!isMethod(items, "push"))
>
> Is this the host method test function?

It is a general one. I make no distinction there.

>> /*
>> * Elements with the same name create a NodeList object,
>> * however options of select objects are also indexable in Gecko.
>> */
>> if (typeof e[0] != "undefined" && typeof e.options == "undefined")
>
> I don't care for that.

You should. If the second `typeof' test would be omitted, execution would
enter the loop with a SELECT element in a Gecko-based UA.

>> {
>> for (var j = 0, len2 = e.length; j < len2; j++)
>> {
>> serializeControl(e[j]);
>> }
>> }
>> else
>> {
>> serializeControl(e);
>> }
>> [...]

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
From: David Mark on
On Dec 18, 4:02 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> David Mark wrote:
> > Thomas 'PointedEars' Lahn wrote:
> >> items.add(o.name, o.options[o.selectedIndex].value);
>
> > This is a problem.  You need to know if the value attribute is
> > present.  Depends on the context, of course.
>
> You are mistaken.  Keep in mind that this is supposed to serialize the form
> as if it was submitted as usual.

I know what it is supposed to do and you are mistaken. What do you
make of these?

<option></option>
<option>test</option>
<option value=""></option>
<option value="">test</option>

>
> >> [...]
> >> for (var i = 0, opts = o.options, len = opts && opts.length;
> >> i < len; i++)
> >> {
> >> var opt = opts[i];
> >> if (opt.selected)
> >> {
> >> items.add(o.name, opt.value);
>
> > Same here.
>
> No.

Yes. :)

>
> >> if (!isMethod(items, "push"))
>
> > Is this the host method test function?
>
> It is a general one.  I make no distinction there.
>
> >> /*
> >> * Elements with the same name create a NodeList object,
> >> * however options of select objects are also indexable in Gecko.
> >> */
> >> if (typeof e[0] != "undefined" && typeof e.options == "undefined")
>
> > I don't care for that.
>
> You should.  If the second `typeof' test would be omitted, execution would
> enter the loop with a SELECT element in a Gecko-based UA.

I understand why, but don't like the way you did it.