Skip to content
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

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

Merged
merged 2 commits into from
Sep 24, 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
1 change: 1 addition & 0 deletions apps/test-examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Test2071 from './src/Test2071';
import Test2080 from './src/Test2080';
import Test2086 from './src/Test2086';
import Test2089 from './src/Test2089';
import Test2142 from './src/Test2142';
import Test2148 from './src/Test2148';
import Test2196 from './src/Test2196';
import Test2248 from './src/Test2248';
Expand Down
22 changes: 22 additions & 0 deletions apps/test-examples/src/Test2142.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {SvgUri} from 'react-native-svg';

export default function App() {
return (
<>
<SvgUri
uri={`data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj4KICA8cmVjdCB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgZmlsbD0iYmx1ZSIgLz48L3N2Zz4=`}
/>
<SvgUri
uri={`data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIgdmlld0JveD0iMCAwIDEwMCAxMDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiAgPHRpdGxlPkNpcmNsZTwvdGl0bGU+CiAgPGNpcmNsZSBjeD0iNTAiIGN5PSI1MCIgcj0iNDIiIHN0eWxlPSJmaWxsOnJlZDsiIC8+Cjwvc3ZnPg==`}
/>
<SvgUri
uri={`data:image/svg+x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jM5NjQ5LTY1LjE4OS0xMi41NTI3NEE3Ny4zNjg1Niw3Ny4zNjg1NiwwLDAsMCwzNy41MDEsMTgwLjg3N2EzLjk5OTc1LDMuOTk5NzUsMCwwLDAsNC45OTE3LDYuMjUxLDcwLjc3OTA2LDcwLjc3OTA2LDAsMCwxLDI0LjgwMTc1LTExLjMyNzE1YzE5Ljk4NjMzLTQuOTAzMzIsMzkuNjY0NTUtMS4wMjA1MSw1OC40ODY4MiwxMS41MjczNCwxNy4wMTk1MywxMS4zNDY2OCwzMy4wODc4OSwxNC44NjUyNCw0Ni40NzM2MywxNC44NjQyNkE3OS40NDk3Miw3OS40NDk3MiwwLDAsMCwyMTguNDk5LDE4Ny4xMjMwNWEzLjk5OTc1LDMuOTk5NzUsMCwwLDAtNC45OTE3LTYuMjUxWiIvPgo8L3N2Zz4=`}
/>
</>
);
}
12 changes: 12 additions & 0 deletions src/utils/fetchData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { Platform } from 'react-native';
import { Buffer } from 'buffer';

export async function fetchText(uri?: string): Promise<string | null> {
if (!uri) {
return 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
Expand Down
Loading