Skip to content

fix: wrap Intl.<>() calls in try/catch #297

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 6 commits into from
Dec 10, 2024
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
11 changes: 6 additions & 5 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ export class RelativeTimeElement extends HTMLElement implements Intl.DateTimeFor
#updating: false | Promise<void> = false

get #lang() {
return (
this.closest('[lang]')?.getAttribute('lang') ||
this.ownerDocument.documentElement.getAttribute('lang') ||
'default'
)
const lang = this.closest('[lang]')?.getAttribute('lang') || this.ownerDocument.documentElement.getAttribute('lang')
try {
return new Intl.Locale(lang ?? '').toString()
} catch {
return 'default'
}
}

#renderRoot: Node = this.shadowRoot ? this.shadowRoot : this.attachShadow ? this.attachShadow({mode: 'open'}) : this
Expand Down
11 changes: 11 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,17 @@ suite('relative-time', function () {
})
}

test('renders correctly when given an invalid lang', async () => {
const now = new Date().toISOString()

const element = document.createElement('relative-time')
element.setAttribute('datetime', now)
element.setAttribute('lang', 'does-not-exist')

await Promise.resolve()
assert.equal(element.shadowRoot.textContent, 'now')
})

suite('[tense=past]', function () {
test('always uses relative dates', async () => {
freezeTime(new Date(2033, 1, 1))
Expand Down
Loading