Skip to content

Commit 31db333

Browse files
committed
[fix] Image support for SVG base64 data
The previous fix to support inline SVG data in utf-8 format broke images that were rendering base64 SVGs.
1 parent 9fe089c commit 31db333

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

packages/react-native-web/src/exports/Image/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const resolveAssetDimensions = source => {
5656
}
5757
};
5858

59-
const svgDataUriPattern = /^data:image\/svg\+xml;/;
59+
const svgDataUriPattern = /^(data:image\/svg\+xml;utf8,)(.*)/;
6060
const resolveAssetSource = source => {
6161
let uri;
6262
if (typeof source === 'number') {
@@ -71,12 +71,12 @@ const resolveAssetSource = source => {
7171
uri = source || '';
7272
}
7373

74-
// SVG data may contain characters (e.g., #, ") that need to be escaped
75-
if (svgDataUriPattern.test(uri)) {
76-
const parts = uri.split('<svg');
77-
const [prefix, ...svgFragment] = parts;
78-
const svg = encodeURIComponent(`<svg${svgFragment.join('<svg')}`);
79-
return `${prefix}${svg}`;
74+
const match = uri.match(svgDataUriPattern);
75+
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
76+
if (match) {
77+
const [, prefix, svg] = match;
78+
const encodedSvg = encodeURIComponent(svg);
79+
return `${prefix}${encodedSvg}`;
8080
}
8181

8282
return uri;

website/storybook/1-components/Image/examples/PropSource.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ const ImageSourceExample = () => (
1717
<Image source={sources.animatedGif} style={styles.image} />
1818
</View>
1919
<View style={styles.column}>
20-
<Text style={styles.text}>Data PNG</Text>
21-
<Image source={sources.dataPng} style={styles.image} />
20+
<Text style={styles.text}>PNG (base64)</Text>
21+
<Image source={sources.dataBase64Png} style={styles.image} />
2222
</View>
2323
<View style={styles.column}>
24-
<Text style={styles.text}>Data SVG</Text>
24+
<Text style={styles.text}>SVG (base64)</Text>
25+
<Image source={sources.dataBase64Svg} style={styles.image} />
26+
</View>
27+
<View style={styles.column}>
28+
<Text style={styles.text}>SVG (inline data)</Text>
2529
<Image source={sources.dataSvg} style={styles.image} />
2630
</View>
2731
</View>

website/storybook/1-components/Image/sources/index.js

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)