Skip to content

Commit 30138d7

Browse files
authored
Merge pull request #203 from github/ensure-invalid-dates-fallback-to-lightdom-textcontent
ensure invalid dates fallback to lightdom textcontent
2 parents 1be3d31 + c9ae2a7 commit 30138d7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/relative-time-element.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ export default class RelativeTimeElement extends HTMLElement implements Intl.Dat
331331
newText = this.getFormattedDate(now) || ''
332332
if (newText) {
333333
this.#renderRoot.textContent = newText
334+
} else if (this.shadowRoot === this.#renderRoot && this.textContent) {
335+
// Ensure invalid dates fall back to lightDOM text content
336+
this.#renderRoot.textContent = this.textContent
334337
}
335338

336339
if (newText !== oldText || newTitle !== oldTitle) {

test/relative-time.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ suite('relative-time', function () {
5858
assert.equal(counter, 1)
5959
})
6060

61+
test('shadowDOM reflects textContent with invalid date', () => {
62+
const el = document.createElement('relative-time')
63+
el.textContent = 'A date string'
64+
el.setAttribute('datetime', 'Invalid')
65+
if (el.shadowRoot) assert.equal(el.shadowRoot.textContent, el.textContent)
66+
})
67+
6168
test('updates the time automatically when it is a few seconds ago', async function () {
6269
// eslint-disable-next-line @typescript-eslint/no-invalid-this
6370
this.timeout(3000)

0 commit comments

Comments
 (0)