feat: add support base64 uri type in SvgUri component (#2444)

# Summary
Feature #2142 

Add support for data: image/svg+XML; Base64 format.

## Test Plan
TestExample app -> src -> Test2142

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |          |
| MacOS   |          |
| Android |          |
| Web     |          |

---------

Co-authored-by: Jakub Grzywacz <jakub.grzywacz@swmansion.com>
This commit is contained in:
Bohdan Artiukhov
2024-09-24 10:23:14 +02:00
committed by GitHub
parent 712201a19e
commit 14a131c5d8
3 changed files with 35 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
import { Platform } from 'react-native';
import { Buffer } from 'buffer';
export async function fetchText(uri?: string): Promise<string | null> {
if (!uri) {
@@ -6,11 +7,22 @@ export async function fetchText(uri?: string): Promise<string | null> {
}
if (uri.startsWith('data:image/svg+xml;utf8') && Platform.OS === 'android') {
return dataUriToXml(uri);
} else if (uri.startsWith('data:image/svg+xml;base64')) {
return decodeBase64Image(uri);
} else {
return fetchUriData(uri);
}
}
const decodeBase64Image = (uri: string) => {
const decoded = decodeURIComponent(uri);
const splitContent = decoded.split(';')[1].split(',');
const dataType = splitContent[0] as BufferEncoding;
const content = splitContent.slice(1).join(',');
return Buffer.from(content, dataType).toString('utf-8');
};
function dataUriToXml(uri: string): string | null {
try {
// decode and remove data:image/svg+xml;utf8, prefix