8000 Merge pull request #297 from github/francinelucca/fix/locale-error · github/relative-time-element@a80c1bf · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a80c1bf

Browse files
Merge pull request #297 from github/francinelucca/fix/locale-error
fix: wrap Intl.<>() calls in try/catch
2 parents bc1b8b7 + b6dbae3 commit a80c1bf

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/relative-time-element.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
8282
#updating: false | Promise<void> = false
8383

8484
get #lang() {
85-
return (
86-
this.closest('[lang]')?.getAttribute('lang') ||
87-
this.ownerDocument.documentElement.getAttribute('lang') ||
88-
'default'
89-
)
85+
const lang = this.closest('[lang]')?.getAttribute('lang') || this.ownerDocument.documentElement.getAttribute('lang')
86+
try {
87+
return new Intl.Locale(lang ?? '').toString()
88+
} catch {
89+
return 'default'
90+
}
9091
}
9192

9293
#renderRoot: Node = this.shadowRoot ? this.shadowRoot : this.attachShadow ? this.attachShadow({mode: 'open'}) : this

test/relative-time.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ suite('relative-time', function () {
434434
})
435435
}
436436

437+
test('renders correctly when given an invalid lang', async () => {
438+
const now = new Date().toISOString()
439+
440+
const element = document.createElement('relative-time')
441+
element.setAttribute('datetime', now)
442+
element.setAttribute('lang', 'does-not-exist')
443+
444+
await Promise.resolve()
445+
assert.equal(element.shadowRoot.textContent, 'now')
446+
})
447+
437448
suite('[tense=past]', function () {
438449
test('always uses relative dates', async () => {
439450
freezeTime(new Date(2033, 1, 1))

0 commit comments

Comments
 (0)
0