Skip to content

Commit e525f78

Browse files
committed
Handle potential errors from atob() or btoa()
1 parent d3e722f commit e525f78

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

app/components/primer/open_project/avatar_fallback.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export class AvatarFallbackElement extends HTMLElement {
66
@attr altText = ''
77

88
connectedCallback() {
9+
// If either uniqueId or altText is missing, skip color customization so the SVG
10+
// keeps its default gray fill defined in the source and no color override is applied.
911
if (!this.uniqueId || !this.altText) return
1012

1113
const img = this.querySelector('img[src^="data:image/svg+xml"]')
@@ -34,10 +36,14 @@ export class AvatarFallbackElement extends HTMLElement {
3436
private updateSvgColor(img: HTMLImageElement, color: string) {
3537
const dataUri = img.src
3638
const base64 = dataUri.replace('data:image/svg+xml;base64,', '')
37-
const svg = atob(base64)
3839

39-
const updatedSvg = svg.replace(/fill="hsl\([^"]+\)"/, `fill="${color}"`)
40-
41-
img.src = `data:image/svg+xml;base64,${btoa(updatedSvg)}`
40+
try {
41+
const svg = atob(base64)
42+
const updatedSvg = svg.replace(/fill="hsl\([^"]+\)"/, `fill="${color}"`)
43+
img.src = `data:image/svg+xml;base64,${btoa(updatedSvg)}`
44+
} catch (_error) {
45+
// If the SVG data is malformed or not valid base64, skip updating the color
46+
// to avoid breaking the component.
47+
}
4248
}
4349
}

0 commit comments

Comments
 (0)