Skip to content

fix: decode base64 SVG data URIs as UTF-8 - #3093

Open
RaphaelFakhri wants to merge 1 commit into
software-mansion:mainfrom
RaphaelFakhri:fix/base64-data-uri-utf8
Open

RaphaelFakhri wants to merge 1 commit into
software-mansion:mainfrom
RaphaelFakhri:fix/base64-data-uri-utf8

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Summary

SvgUri decodes data:image/svg+xml;base64,... URIs with atob, which returns one character per byte. Since #2808 (15.15.3), any SVG with non-ASCII text in a base64 data URI renders garbled: Café becomes Café. Before #2808, Buffer.from(content, 'base64').toString('utf-8') decoded the bytes as UTF-8.

This change keeps atob (no buffer dependency) and decodes its output as UTF-8 by percent-encoding each byte and passing the result to decodeURIComponent. If the bytes are not valid UTF-8, it returns the atob output unchanged, which matches the current behavior.

It only affects fetchText in src/utils/fetchData.ts, used by SvgUri, SvgCssUri, and SvgFromUri.

Test Plan

Added __tests__/fetchData.test.ts, which decodes an ASCII and a UTF-8 (Café © 日本 😀) base64 data URI.

npx jest __tests__/fetchData.test.ts --globalSetup <no-op> --globalTeardown <no-op>
  • Without the fix: 1 failed, 1 passed. The UTF-8 case receives Café © æ¥æ¬ ð.
  • With the fix: 2 passed.

yarn lint and yarn tsc report nothing new.

What's required for testing (prerequisites)?

An app that renders SvgUri with a base64 data URI whose SVG contains non-ASCII text.

What are the steps to reproduce (after prerequisites)?

  1. Render <SvgUri uri="data:image/svg+xml;base64,..." /> where the SVG contains <text>Café</text>.
  2. Without the fix, the text reads Café. With the fix, it reads Café.

Compatibility

OS Implemented
iOS ✅
MacOS ✅
Android ✅
Web ✅

Checklist

  • I have tested this on a device and a simulator
  • I added documentation in README.md
  • I updated the typed files (typescript)
  • I added a test for the API in the __tests__ folder

Fixes #3092

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SvgUri garbles non-ASCII text in base64 data URIs since 15.15.3

1 participant