Skip to content

Commit

Permalink
fix(assets): use base64 when inlining SVG with foreignObject tag (vit…
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre authored Nov 5, 2023
1 parent 12f9230 commit 9e20ed6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,19 @@ export async function urlToBuiltUrl(
// Inspired by https://github.com/iconify/iconify/blob/main/packages/utils/src/svg/url.ts
function svgToDataURL(content: Buffer): string {
const stringContent = content.toString()
// If the SVG contains some text, any transformation is unsafe, and given that double quotes would then
// If the SVG contains some text or HTML, any transformation is unsafe, and given that double quotes would then
// need to be escaped, the gain to use a data URI would be ridiculous if not negative
if (stringContent.includes('<text')) {
if (
stringContent.includes('<text') ||
stringContent.includes('<foreignObject')
) {
return `data:image/svg+xml;base64,${content.toString('base64')}`
} else {
return (
'data:image/svg+xml,' +
stringContent
.trim()
.replaceAll(/>\s+</g, '><')
.replaceAll('"', "'")
.replaceAll('%', '%25')
.replaceAll('#', '%23')
Expand Down

0 comments on commit 9e20ed6

Please sign in to comment.