1
YUI.add('io-form', function (Y, NAME) {
4
* Extends IO to enable HTML form data serialization, when specified
5
* in the transaction's configuration object.
11
var eUC = encodeURIComponent;
14
* Enumerate through an HTML form's elements collection
15
* and return a string comprised of key-value pairs.
19
* @param {Node|String} form YUI form node or HTML form id
20
* @param {Object} [options] Configuration options.
21
* @param {Boolean} [options.useDisabled=false] Whether to include disabled fields.
22
* @param {Object|String} [options.extra] Extra values to include. May be a query string or an object with key/value pairs.
25
Y.IO.stringify = function(form, options) {
26
options = options || {};
28
var s = Y.IO.prototype._serialize({
30
useDisabled: options.useDisabled
32
options.extra && typeof options.extra === 'object' ? Y.QueryString.stringify(options.extra) : options.extra);
37
Y.mix(Y.IO.prototype, {
39
* Enumerate through an HTML form's elements collection
40
* and return a string comprised of key-value pairs.
45
* @param {String|Element} c.id YUI form node or HTML form id
46
* @param {Boolean} c.useDisabled `true` to include disabled fields
47
* @param {String} s Key-value data defined in the configuration object.
50
_serialize: function(c, s) {
52
df = c.useDisabled || false,
54
id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
55
e, f, n, v, d, i, il, j, jl, o;
59
c.id.setAttribute('id', id);
62
f = Y.config.doc.getElementById(id);
64
if (!f || !f.elements) {
68
// Iterate over the form elements collection to construct the
70
for (i = 0, il = f.elements.length; i < il; ++i) {
75
if (df ? n : n && !d) {
80
// Safari, Opera, FF all default options.value from .text if
81
// value attribute not specified in markup
83
if (e.selectedIndex > -1) {
84
o = e.options[e.selectedIndex];
85
data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
88
case 'select-multiple':
89
if (e.selectedIndex > -1) {
90
for (j = e.selectedIndex, jl = e.options.length; j < jl; ++j) {
93
data[item++] = n + eUC(o.attributes.value && o.attributes.value.specified ? o.value : o.text);
101
data[item++] = n + v;
105
// stub case as XMLHttpRequest will only send the file path as a string.
107
// stub case for fieldset element which returns undefined.
109
// stub case for input type reset button.
111
// stub case for input type button elements.
115
data[item++] = n + v;
124
Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io');
125
return data.join('&');
130
}, '@VERSION@', {"requires": ["io-base", "node-base"]});