Skip to content

Commit

Permalink
[fix] Image support for SVG base64 data
Browse files Browse the repository at this point in the history
The previous fix to support inline SVG data in utf-8 format broke images
that were rendering base64 SVGs.
  • Loading branch information
necolas committed Feb 17, 2018
1 parent 9fe089c commit 31db333
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
14 changes: 7 additions & 7 deletions packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const resolveAssetDimensions = source => {
}
};

const svgDataUriPattern = /^data:image\/svg\+xml;/;
const svgDataUriPattern = /^(data:image\/svg\+xml;utf8,)(.*)/;
const resolveAssetSource = source => {
let uri;
if (typeof source === 'number') {
Expand All @@ -71,12 +71,12 @@ const resolveAssetSource = source => {
uri = source || '';
}

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

return uri;
Expand Down
10 changes: 7 additions & 3 deletions website/storybook/1-components/Image/examples/PropSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ const ImageSourceExample = () => (
<Image source={sources.animatedGif} style={styles.image} />
</View>
<View style={styles.column}>
<Text style={styles.text}>Data PNG</Text>
<Image source={sources.dataPng} style={styles.image} />
<Text style={styles.text}>PNG (base64)</Text>
<Image source={sources.dataBase64Png} style={styles.image} />
</View>
<View style={styles.column}>
<Text style={styles.text}>Data SVG</Text>
<Text style={styles.text}>SVG (base64)</Text>
<Image source={sources.dataBase64Svg} style={styles.image} />
</View>
<View style={styles.column}>
<Text style={styles.text}>SVG (inline data)</Text>
<Image source={sources.dataSvg} style={styles.image} />
</View>
</View>
Expand Down
9 changes: 6 additions & 3 deletions website/storybook/1-components/Image/sources/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31db333

Please sign in to comment.