Skip to content

avoid updating during update call #215

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

Merged
merged 1 commit into from
Nov 25, 2022
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
16 changes: 10 additions & 6 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const dateObserver = new (class {

export default class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFormatOptions {
#customTitle = false
#updating = false

get #lang() {
return this.closest('[lang]')?.getAttribute('lang') ?? 'default'
Expand Down Expand Up @@ -336,22 +337,24 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
// Internal: Refresh the time element's formatted date when an attribute changes.
attributeChangedCallback(attrName: string, oldValue: unknown, newValue: unknown): void {
if (oldValue === newValue) return
if (attrName === 'title') this.#customTitle = true
if (!this.#customTitle) this.update()
if (attrName === 'title') {
this.#customTitle = newValue !== null && this.getFormattedTitle() !== newValue
}
if (!this.#updating && !(attrName === 'title' && this.#customTitle)) {
this.update()
}
}

update() {
this.#updating = true
const oldText: string = this.#renderRoot.textContent || ''
const oldTitle: string = this.getAttribute('title') || ''
let newTitle: string = oldTitle
let newText: string = oldText
const now = new Date()
if (!this.#customTitle) {
newTitle = this.getFormattedTitle() || ''
if (newTitle) {
this.setAttribute('title', newTitle)
this.#customTitle = false
}
if (newTitle) this.setAttribute('title', newTitle)
}

newText = this.getFormattedDate(now) || ''
Expand All @@ -374,5 +377,6 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
} else {
dateObserver.unobserve(this)
}
this.#updating = false
}
}
13 changes: 13 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ suite('relative-time', function () {
assert.equal(counter, 1)
el.setAttribute('title', 'another custom')
assert.equal(counter, 1)
el.removeAttribute('title')
assert.equal(counter, 2)
})

test('sets title back to default if removed', () => {
const el = document.createElement('relative-time')
el.setAttribute('datetime', new Date().toISOString())
assert.ok(el.getAttribute('title'))
const text = el.getAttribute('title')
el.setAttribute('title', 'custom')
assert.equal(el.getAttribute('title'), 'custom')
el.removeAttribute('title')
assert.equal(el.getAttribute('title'), text)
})

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