Skip to content

Commit dc73c5c

Browse files
authored
Merge pull request #129 from github/title
Update title attribute when datetime attribute is changed
2 parents e0db5e6 + 1131a1a commit dc73c5c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/extended-time-element.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class ExtendedTimeElement extends HTMLElement {
2323

2424
// Internal: Refresh the time element's formatted date when an attribute changes.
2525
attributeChangedCallback(attrName: string, oldValue: string, newValue: string) {
26+
const oldTitle = this.getFormattedTitle()
2627
if (attrName === 'datetime') {
2728
const millis = Date.parse(newValue)
2829
if (isNaN(millis)) {
@@ -31,8 +32,10 @@ export default class ExtendedTimeElement extends HTMLElement {
3132
datetimes.set(this, new Date(millis))
3233
}
3334
}
35+
3436
const title = this.getFormattedTitle()
35-
if (title && !this.hasAttribute('title')) {
37+
const currentTitle = this.getAttribute('title')
38+
if (title && (!currentTitle || currentTitle === oldTitle)) {
3639
this.setAttribute('title', title)
3740
}
3841

test/title-format.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ suite('title-format', function() {
2828
assert.match(time.getAttribute('title'), /\d{4}/)
2929
})
3030

31+
test('update the title attribute after a datetime value change', function() {
32+
const time = document.createElement('local-time')
33+
time.setAttribute('datetime', '1970-05-01T00:00:00.000Z')
34+
assert.match(time.getAttribute('title'), /1970/)
35+
time.setAttribute('datetime', '1979-05-01T00:00:00.000Z')
36+
assert.match(time.getAttribute('title'), /1979/)
37+
time.setAttribute('title', 'custom title')
38+
time.setAttribute('datetime', '1989-05-01T00:00:00.000Z')
39+
assert.match(time.getAttribute('title'), /custom title/)
40+
})
41+
3142
test('set the title attribute when parsed element is upgraded', function() {
3243
const root = document.createElement('div')
3344
root.innerHTML = '<local-time datetime="1970-01-01T00:00:00.000Z"></local-time>'

0 commit comments

Comments
 (0)