From 0ea950819a22e65599c7ad9e19a27981a2f39ee7 Mon Sep 17 00:00:00 2001 From: Brandon Satrom Date: Tue, 7 May 2013 20:46:19 -0500 Subject: [PATCH] added deserialization of Date attributes for custom elements --- src/attrs.js | 31 ++++++++++++++++++++++++++++--- test/html/take-attributes.html | 25 +++++++++++++++++++++++++ toolkit.min.js | 9 +++++---- toolkit.min.js.map | 2 +- toolkit.native.min.js | 7 ++++--- toolkit.native.min.js.map | 2 +- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/attrs.js b/src/attrs.js index 635cbee9d5..1eea618374 100644 --- a/src/attrs.js +++ b/src/attrs.js @@ -62,7 +62,7 @@ function takeAttributes() { // for each attribute forEach(this.attributes, function(a) { - // try to match this attribute to a property (attributess are + // try to match this attribute to a property (attributes are // all lower-case, so this is case-insensitive search) var name = propertyForAttribute.call(this, a.name); if (name) { @@ -87,7 +87,7 @@ var lowerCase = String.prototype.toLowerCase.call.bind( String.prototype.toLowerCase); - + // return the published property matching name, or undefined function propertyForAttribute(name) { // matchable properties must be published @@ -95,7 +95,16 @@ // search for a matchable property return properties[properties.map(lowerCase).indexOf(name.toLowerCase())]; }; - + + function createDate(year, month, day, hour, minute, second, millisecond) { + // hour through ms are optional and undefined if not provided. Passing + // undefined into the Date constructor results in an invalid date. + // Null, on the other hand, is valid. + return new Date(year, month-1, day, + hour ? hour : null, minute ? minute : null, + second ? second : null, millisecond ? millisecond : null); + }; + function deserializeValue(inValue, inDefaultValue) { var inferredType = typeof inDefaultValue; if (inferredType === 'string') { @@ -109,6 +118,22 @@ case 'false': return false; } + + // If the default attribute value is a Date, provide a Date object + // for the specified value. + if (inDefaultValue instanceof Date) { + // Strip the date string into its component parts and pass each into + // the Date constructor. + var dateParts = inValue.match(/(\d+)/g); + + // length < 3 means a text format was used (ex. March 20, 2010) + if (dateParts.length >= 3) { + return createDate.apply(null, dateParts); + } else { + return new Date(inValue); + } + } + var float = parseFloat(inValue); return (String(float) === inValue) ? float : inValue; } diff --git a/test/html/take-attributes.html b/test/html/take-attributes.html index 3e8ada465f..3a30b02544 100644 --- a/test/html/take-attributes.html +++ b/test/html/take-attributes.html @@ -59,6 +59,22 @@ + + + + + + + + + + + +