Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added deserialization of Date attributes for custom elements #122

Merged
merged 2 commits into from
May 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion platform
11 changes: 8 additions & 3 deletions src/attrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -87,15 +87,15 @@

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
var properties = Object.keys(this[published$]);
// search for a matchable property
return properties[properties.map(lowerCase).indexOf(name.toLowerCase())];
};

function deserializeValue(inValue, inDefaultValue) {
var inferredType = typeof inDefaultValue;
if (inferredType === 'string') {
Expand All @@ -109,6 +109,11 @@
case 'false':
return false;
}

if (inDefaultValue instanceof Date) {
return new Date(Date.parse(inValue) || Date.now());
}

var float = parseFloat(inValue);
return (String(float) === inValue) ? float : inValue;
}
Expand Down
21 changes: 21 additions & 0 deletions test/html/take-attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@
<x-zot id="zot4" bool="squid"></x-zot>
<x-zot id="zot5" bool="0"></x-zot>

<element name="x-date" attributes="value">
<script>
Toolkit.register(this, {
value: new Date()
});
</script>
</element>

<x-date id="date1" value="2014/12/25"></x-date>
<x-date id="date2" value="December 25, 2014"></x-date>
<x-date id="date3" value="2014/12/25 11:45"></x-date>
<x-date id="date4" value="2014/12/25 11:45:30"></x-date>
<x-date id="date5" value="2014/12/25 11:45:30:33"></x-date>

<script>
var assert = chai.assert;
document.addEventListener('WebComponentsReady', function() {
Expand Down Expand Up @@ -97,6 +111,13 @@
//
assert.equal(document.querySelector("#zot0").num, 84);
assert.equal(document.querySelector("#zot0").str, "don't panic");
//
// Date deserialization tests
assert.equal(String(document.querySelector("#date1").value), String(new Date(2014, 11, 25)));
assert.equal(String(document.querySelector("#date2").value), String(new Date(2014, 11, 25)));
assert.equal(String(document.querySelector("#date3").value), String(new Date(2014, 11, 25, 11, 45)));
assert.equal(String(document.querySelector("#date4").value), String(new Date(2014, 11, 25, 11, 45, 30)));
assert.equal(document.querySelector("#date5").value.getMilliseconds(), new Date(2014, 11, 25, 11, 45, 30, 33).getMilliseconds());
//
done();
});
Expand Down
9 changes: 5 additions & 4 deletions toolkit.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion toolkit.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions toolkit.native.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion toolkit.native.min.js.map

Large diffs are not rendered by default.