Skip to content

Commit

Permalink
Merge pull request #203 from github/ensure-invalid-dates-fallback-to-…
Browse files Browse the repository at this point in the history
…lightdom-textcontent

ensure invalid dates fallback to lightdom textcontent
  • Loading branch information
keithamus authored Nov 15, 2022
2 parents 1be3d31 + c9ae2a7 commit 30138d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
newText = this.getFormattedDate(now) || ''
if (newText) {
this.#renderRoot.textContent = newText
} else if (this.shadowRoot === this.#renderRoot && this.textContent) {
// Ensure invalid dates fall back to lightDOM text content
this.#renderRoot.textContent = this.textContent
}

if (newText !== oldText || newTitle !== oldTitle) {
Expand Down
7 changes: 7 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ suite('relative-time', function () {
assert.equal(counter, 1)
})

test('shadowDOM reflects textContent with invalid date', () => {
const el = document.createElement('relative-time')
el.textContent = 'A date string'
el.setAttribute('datetime', 'Invalid')
if (el.shadowRoot) assert.equal(el.shadowRoot.textContent, el.textContent)
})

test('updates the time automatically when it is a few seconds ago', async function () {
// eslint-disable-next-line @typescript-eslint/no-invalid-this
this.timeout(3000)
Expand Down

0 comments on commit 30138d7

Please sign in to comment.