Skip to content

Commit

Permalink
Merge pull request #129 from github/title
Browse files Browse the repository at this point in the history
Update title attribute when datetime attribute is changed
  • Loading branch information
muan authored Nov 13, 2019
2 parents e0db5e6 + 1131a1a commit dc73c5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/extended-time-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class ExtendedTimeElement extends HTMLElement {

// Internal: Refresh the time element's formatted date when an attribute changes.
attributeChangedCallback(attrName: string, oldValue: string, newValue: string) {
const oldTitle = this.getFormattedTitle()
if (attrName === 'datetime') {
const millis = Date.parse(newValue)
if (isNaN(millis)) {
Expand All @@ -31,8 +32,10 @@ export default class ExtendedTimeElement extends HTMLElement {
datetimes.set(this, new Date(millis))
}
}

const title = this.getFormattedTitle()
if (title && !this.hasAttribute('title')) {
const currentTitle = this.getAttribute('title')
if (title && (!currentTitle || currentTitle === oldTitle)) {
this.setAttribute('title', title)
}

Expand Down
11 changes: 11 additions & 0 deletions test/title-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ suite('title-format', function() {
assert.match(time.getAttribute('title'), /\d{4}/)
})

test('update the title attribute after a datetime value change', function() {
const time = document.createElement('local-time')
time.setAttribute('datetime', '1970-05-01T00:00:00.000Z')
assert.match(time.getAttribute('title'), /1970/)
time.setAttribute('datetime', '1979-05-01T00:00:00.000Z')
assert.match(time.getAttribute('title'), /1979/)
time.setAttribute('title', 'custom title')
time.setAttribute('datetime', '1989-05-01T00:00:00.000Z')
assert.match(time.getAttribute('title'), /custom title/)
})

test('set the title attribute when parsed element is upgraded', function() {
const root = document.createElement('div')
root.innerHTML = '<local-time datetime="1970-01-01T00:00:00.000Z"></local-time>'
Expand Down

0 comments on commit dc73c5c

Please sign in to comment.