Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/handler/preprocess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReactElement, ReactNode } from 'react'
import { resolveImageData, cache } from './image.js'
import { isReactElement, parseViewBox } from '../utils.js'
import { isReactElement, parseViewBox, midline } from '../utils.js'

// Based on
// https://raw.githubusercontent.com/facebook/react/master/packages/react-dom/src/shared/possibleStandardNames.js
Expand Down Expand Up @@ -115,7 +115,8 @@ function translateSVGNodeToSVGString(

const { children, style, ...restProps } = node.props || {}
const currentColor = style?.color || inheritedColor
return `<${type}${Object.entries(restProps)

const attrs = `${Object.entries(restProps)
.map(([k, _v]) => {
if (typeof _v === 'string' && _v.toLowerCase() === 'currentcolor') {
_v = currentColor
Expand All @@ -126,7 +127,18 @@ function translateSVGNodeToSVGString(
}
return ` ${ATTRIBUTE_MAPPING[k] || k}="${_v}"`
})
.join('')}>${translateSVGNodeToSVGString(children, currentColor)}</${type}>`
.join('')}`

const styles = style
? ` style="${Object.entries(style)
.map(([k, _v]) => `${midline(k)}:${_v}`)
.join(';')}"`
: ''

return `<${type}${attrs}${styles}>${translateSVGNodeToSVGString(
children,
currentColor
)}</${type}>`
}
/**
* pre process node and resolve absolute link to img data for image element
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,10 @@ export function splitByBreakOpportunities(

return { words, requiredBreaks }
}

export const midline = (s: string) => {
return s.replaceAll(
/([A-Z])/g,
(_, letter: string) => `-${letter.toLowerCase()}`
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/svg.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,30 @@ describe('SVG', () => {
)
expect(toImage(svg, 100)).toMatchImageSnapshot()
})

// TODO wait for @resvg/resvg-js to support mask-type
it('should respect style on svg node', async () => {
const svg = await satori(
<div
style={{
height: '100%',
width: '100%',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
fontSize: 32,
fontWeight: 600,
}}
>
<svg xmlns='http://www.w3.org/2000/svg' width='30' height='30'>
<circle cx='50' cy='50' r='50' style={{ fill: 'gold' }} />
</svg>
</div>,
{ width: 100, height: 100, fonts }
)

expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})

1 comment on commit 329a987

@vercel
Copy link

@vercel vercel bot commented on 329a987 May 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.