Skip to content

Commit

Permalink
Merge pull request github#33 from github/no-datetime
Browse files Browse the repository at this point in the history
Ignore malformed dates
  • Loading branch information
dgraham committed Jun 30, 2014
2 parents 7a0e3a0 + 8b6c1fc commit 313ba80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ test('switches to dates after 30 days', function() {
ok(time.textContent.match(/on \w\w\w \d{1,2}/));
});

test('ignores malformed dates', function() {
var time = document.createElement('time', 'relative-time');
time.textContent = 'Jun 30';
time.setAttribute('datetime', 'bogus');
equal(time.textContent, 'Jun 30');
});

test('ignores blank dates', function() {
var time = document.createElement('time', 'relative-time');
time.textContent = 'Jun 30';
time.setAttribute('datetime', '');
equal(time.textContent, 'Jun 30');
});

test('ignores removed dates', function() {
var time = document.createElement('time', 'relative-time');
var now = new Date().toISOString();

time.setAttribute('datetime', now);
equal(time.textContent, 'just now');

time.removeAttribute('datetime');
equal(time.textContent, 'just now');
});

test('sets relative contents when parsed element is upgraded', function() {
var now = new Date().toISOString();
var root = document.createElement('div');
Expand Down
3 changes: 2 additions & 1 deletion time-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@
// Returns nothing.
ExtendedTimePrototype.attributeChangedCallback = function(attrName, oldValue, newValue) {
if (attrName === 'datetime') {
this._date = new Date(Date.parse(newValue));
var millis = Date.parse(newValue);
this._date = isNaN(millis) ? null : new Date(millis);
}

var title = this.getFormattedTitle();
Expand Down

0 comments on commit 313ba80

Please sign in to comment.