Skip to content

Commit dcf479f

Browse files
authored
Merge pull request #215 from github/avoid-updating-during-update-call
avoid updating during update call
2 parents c68fbe2 + 7f73eab commit dcf479f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/relative-time-element.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const dateObserver = new (class {
7979

8080
export default class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFormatOptions {
8181
#customTitle = false
82+
#updating = false
8283

8384
get #lang() {
8485
return this.closest('[lang]')?.getAttribute('lang') ?? 'default'
@@ -336,22 +337,24 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
336337
// Internal: Refresh the time element's formatted date when an attribute changes.
337338
attributeChangedCallback(attrName: string, oldValue: unknown, newValue: unknown): void {
338339
if (oldValue === newValue) return
339-
if (attrName === 'title') this.#customTitle = true
340-
if (!this.#customTitle) this.update()
340+
if (attrName === 'title') {
341+
this.#customTitle = newValue !== null && this.getFormattedTitle() !== newValue
342+
}
343+
if (!this.#updating && !(attrName === 'title' && this.#customTitle)) {
344+
this.update()
345+
}
341346
}
342347

343348
update() {
349+
this.#updating = true
344350
const oldText: string = this.#renderRoot.textContent || ''
345351
const oldTitle: string = this.getAttribute('title') || ''
346352
let newTitle: string = oldTitle
347353
let newText: string = oldText
348354
const now = new Date()
349355
if (!this.#customTitle) {
350356
newTitle = this.getFormattedTitle() || ''
351-
if (newTitle) {
352-
this.setAttribute('title', newTitle)
353-
this.#customTitle = false
354-
}
357+
if (newTitle) this.setAttribute('title', newTitle)
355358
}
356359

357360
newText = this.getFormattedDate(now) || ''
@@ -374,5 +377,6 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
374377
} else {
375378
dateObserver.unobserve(this)
376379
}
380+
this.#updating = false
377381
}
378382
}

test/relative-time.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ suite('relative-time', function () {
5656
assert.equal(counter, 1)
5757
el.setAttribute('title', 'another custom')
5858
assert.equal(counter, 1)
59+
el.removeAttribute('title')
60+
assert.equal(counter, 2)
61+
})
62+
63+
test('sets title back to default if removed', () => {
64+
const el = document.createElement('relative-time')
65+
el.setAttribute('datetime', new Date().toISOString())
66+
assert.ok(el.getAttribute('title'))
67+
const text = el.getAttribute('title')
68+
el.setAttribute('title', 'custom')
69+
assert.equal(el.getAttribute('title'), 'custom')
70+
el.removeAttribute('title')
71+
assert.equal(el.getAttribute('title'), text)
5972
})
6073

6174
test('shadowDOM reflects textContent with invalid date', () => {

0 commit comments

Comments
 (0)