mirror of
https://github.com/zoriya/react-native-svg.git
synced 2025-12-06 07:06:11 +00:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user