Permalink
Browse files

Merge branch 'jafl-y-io-serialize' into dev-master

  • Loading branch information...
2 parents 5785823 + 870e65d commit 3c57b55abd0c95af317b64ddde98c4a12d31ea03 @ericf ericf committed Jan 2, 2013

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -10,16 +10,41 @@ YUI.add('io-form', function (Y, NAME) {
var eUC = encodeURIComponent;
+/**
+ * Method to enumerate through an HTML form's elements collection
+ * and return a string comprised of key-value pairs.
+ *
+ * @method serialize
+ * @static
+ * @param {Node|String} form YUI form node or HTML form id
+ * @param {Object} [options] Configuration options.
+ * @param {Boolean} [options.useDisabled=false] Whether to include disabled fields.
+ * @param {Object|String} [options.extra] Extra values to include. May be a query string or an object with key/value pairs.
+ * @return {String}
+ */
+Y.IO.stringify = function(form, options) {
+ options = options || {};
+
+ var s = Y.IO.prototype._serialize({
+ id: form,
+ useDisabled: options.useDisabled
+ },
+ options.extra && typeof options.extra === 'object' ? Y.QueryString.stringify(options.extra) : options.extra);
+
+ return s;
+};
+
Y.mix(Y.IO.prototype, {
/**
* Method to enumerate through an HTML form's elements collection
* and return a string comprised of key-value pairs.
*
* @method _serialize
* @private
- * @static
- * @param {Object} c - YUI form node or HTML form id.
- * @param {String} s - Key-value data defined in the configuration object.
+ * @param {Object} c
+ * @param {String|Element} c.id YUI form node or HTML form id
+ * @param {Boolean} c.useDisabled `true` to include disabled fields
+ * @param {String} s Key-value data defined in the configuration object.
* @return {String}
*/
_serialize: function(c, s) {
@@ -29,12 +54,16 @@ Y.mix(Y.IO.prototype, {
id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
e, f, n, v, d, i, il, j, jl, o;
- if (!id) {
- id = Y.guid('io:');
- c.id.setAttribute('id', id);
- }
+ if (!id) {
+ id = Y.guid('io:');
+ c.id.setAttribute('id', id);
+ }
- f = Y.config.doc.getElementById(id);
+ f = Y.config.doc.getElementById(id);
+
+ if (!f || !f.elements) {
+ return s || '';
+ }
// Iterate over the form elements collection to construct the
// label-value pairs.
@@ -87,8 +116,13 @@ Y.mix(Y.IO.prototype, {
}
}
}
+
+ if (s) {
+ data[item++] = s;
+ }
+
Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io');
- return s ? data.join('&') + "&" + s : data.join('&');
+ return data.join('&');
}
}, true);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -10,16 +10,41 @@ YUI.add('io-form', function (Y, NAME) {
var eUC = encodeURIComponent;
+/**
+ * Method to enumerate through an HTML form's elements collection
+ * and return a string comprised of key-value pairs.
+ *
+ * @method serialize
+ * @static
+ * @param {Node|String} form YUI form node or HTML form id
+ * @param {Object} [options] Configuration options.
+ * @param {Boolean} [options.useDisabled=false] Whether to include disabled fields.
+ * @param {Object|String} [options.extra] Extra values to include. May be a query string or an object with key/value pairs.
+ * @return {String}
+ */
+Y.IO.stringify = function(form, options) {
+ options = options || {};
+
+ var s = Y.IO.prototype._serialize({
+ id: form,
+ useDisabled: options.useDisabled
+ },
+ options.extra && typeof options.extra === 'object' ? Y.QueryString.stringify(options.extra) : options.extra);
+
+ return s;
+};
+
Y.mix(Y.IO.prototype, {
/**
* Method to enumerate through an HTML form's elements collection
* and return a string comprised of key-value pairs.
*
* @method _serialize
* @private
- * @static
- * @param {Object} c - YUI form node or HTML form id.
- * @param {String} s - Key-value data defined in the configuration object.
+ * @param {Object} c
+ * @param {String|Element} c.id YUI form node or HTML form id
+ * @param {Boolean} c.useDisabled `true` to include disabled fields
+ * @param {String} s Key-value data defined in the configuration object.
* @return {String}
*/
_serialize: function(c, s) {
@@ -29,12 +54,16 @@ Y.mix(Y.IO.prototype, {
id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
e, f, n, v, d, i, il, j, jl, o;
- if (!id) {
- id = Y.guid('io:');
- c.id.setAttribute('id', id);
- }
+ if (!id) {
+ id = Y.guid('io:');
+ c.id.setAttribute('id', id);
+ }
- f = Y.config.doc.getElementById(id);
+ f = Y.config.doc.getElementById(id);
+
+ if (!f || !f.elements) {
+ return s || '';
+ }
// Iterate over the form elements collection to construct the
// label-value pairs.
@@ -87,7 +116,12 @@ Y.mix(Y.IO.prototype, {
}
}
}
- return s ? data.join('&') + "&" + s : data.join('&');
+
+ if (s) {
+ data[item++] = s;
+ }
+
+ return data.join('&');
}
}, true);
View
@@ -8,16 +8,41 @@
var eUC = encodeURIComponent;
+/**
+ * Method to enumerate through an HTML form's elements collection
+ * and return a string comprised of key-value pairs.
+ *
+ * @method serialize
+ * @static
+ * @param {Node|String} form YUI form node or HTML form id
+ * @param {Object} [options] Configuration options.
+ * @param {Boolean} [options.useDisabled=false] Whether to include disabled fields.
+ * @param {Object|String} [options.extra] Extra values to include. May be a query string or an object with key/value pairs.
+ * @return {String}
+ */
+Y.IO.stringify = function(form, options) {
+ options = options || {};
+
+ var s = Y.IO.prototype._serialize({
+ id: form,
+ useDisabled: options.useDisabled
+ },
+ options.extra && typeof options.extra === 'object' ? Y.QueryString.stringify(options.extra) : options.extra);
+
+ return s;
+};
+
Y.mix(Y.IO.prototype, {
/**
* Method to enumerate through an HTML form's elements collection
* and return a string comprised of key-value pairs.
*
* @method _serialize
* @private
- * @static
- * @param {Object} c - YUI form node or HTML form id.
- * @param {String} s - Key-value data defined in the configuration object.
+ * @param {Object} c
+ * @param {String|Element} c.id YUI form node or HTML form id
+ * @param {Boolean} c.useDisabled `true` to include disabled fields
+ * @param {String} s Key-value data defined in the configuration object.
* @return {String}
*/
_serialize: function(c, s) {
@@ -27,12 +52,16 @@ Y.mix(Y.IO.prototype, {
id = (typeof c.id === 'string') ? c.id : c.id.getAttribute('id'),
e, f, n, v, d, i, il, j, jl, o;
- if (!id) {
- id = Y.guid('io:');
- c.id.setAttribute('id', id);
- }
+ if (!id) {
+ id = Y.guid('io:');
+ c.id.setAttribute('id', id);
+ }
- f = Y.config.doc.getElementById(id);
+ f = Y.config.doc.getElementById(id);
+
+ if (!f || !f.elements) {
+ return s || '';
+ }
// Iterate over the form elements collection to construct the
// label-value pairs.
@@ -85,7 +114,12 @@ Y.mix(Y.IO.prototype, {
}
}
}
+
+ if (s) {
+ data[item++] = s;
+ }
+
Y.log('HTML form serialized. The value is: ' + data.join('&'), 'info', 'io');
- return s ? data.join('&') + "&" + s : data.join('&');
+ return data.join('&');
}
}, true);
@@ -7,6 +7,21 @@
<div id="log"></div>
+<form id="test-form" action="" method="POST">
+ <input name="name" value="eric" type="text" />
+ <input name="age" value="27" type="text" />
+ <input name="username" value="ericf" type="text" disabled="disabled" />
+ <input name="secret" value="p@55w0rd" type="hidden" />
+ <input name="save" value="Save" type="submit" />
+</form>
+
+<form id="disabled-form" action="" method="POST">
+ <input name="username" value="ericf" type="text" disabled="disabled" />
+</form>
+
+<form id="empty-form" action="" method="POST">
+</form>
+
<script type="text/javascript" src="../../../../build/yui/yui.js"></script>
<script type="text/javascript" src="modules.js"></script>
<script type="text/javascript" src="urls.js"></script>
@@ -0,0 +1,61 @@
+YUI.add('serialize-tests', function (Y) {
+
+ var suite = new Y.Test.Suite('IO Serialize Tests'),
+ form = Y.one('#test-form').remove();
+
+ suite.add(new Y.Test.Case({
+ name: 'Form Serialization',
+
+ setUp: function () {
+ this.form = form.cloneNode(true).appendTo('body');
+ },
+
+ tearDown: function () {
+ this.form.remove();
+ delete this.form;
+ },
+
+ 'stringify should serialize the form': function () {
+ var postBody = Y.IO.stringify(this.form);
+ Y.Assert.areSame('name=eric&age=27&secret=p%4055w0rd&save=Save', postBody);
+ },
+
+ 'stringify should serialize the form by id': function () {
+ var postBody = Y.IO.stringify('test-form');
+ Y.Assert.areSame('name=eric&age=27&secret=p%4055w0rd&save=Save', postBody);
+ },
+
+ 'stringify with `useDisabled` should serialize the form including disabled fields': function () {
+ var postBody = Y.IO.stringify(this.form, {useDisabled: true});
+ Y.Assert.areSame('name=eric&age=27&username=ericf&secret=p%4055w0rd&save=Save', postBody);
+ },
+
+ 'stringify should serialize the form plus string data': function () {
+ var postBody = Y.IO.stringify(this.form, {extra: 'foo=bar'});
+ Y.Assert.areSame('name=eric&age=27&secret=p%4055w0rd&save=Save&foo=bar', postBody);
+ },
+
+ 'stringify should serialize the form plus object data': function () {
+ var postBody = Y.IO.stringify(this.form, {extra: {foo:'bar'}});
+ Y.Assert.areSame('name=eric&age=27&secret=p%4055w0rd&save=Save&foo=bar', postBody);
+ },
+
+ 'stringify should serialize empty form': function () {
+ var postBody = Y.IO.stringify('empty-form');
+ Y.Assert.areSame('', postBody);
+ },
+
+ 'stringify should serialize empty form plus data': function () {
+ var postBody = Y.IO.stringify('empty-form', {extra: 'foo=bar'});
+ Y.Assert.areSame('foo=bar', postBody);
+ },
+
+ 'stringify should serialize disabled form': function () {
+ var postBody = Y.IO.stringify('disabled-form');
+ Y.Assert.areSame('', postBody);
+ }
+ }));
+
+ Y.Test.Runner.add(suite);
+
+});
@@ -34,6 +34,10 @@ var testModules = {
"transport-tests": {
"fullpath": "./js/transport-tests.js",
"requires": ["io-xdr", "test"]
+ },
+ "serialize-tests": {
+ "fullpath": "./js/serialize-tests.js",
+ "requires": ["io-form", "test"]
}
};

0 comments on commit 3c57b55

Please sign in to comment.