Skip to content

Commit

Permalink
fix: should respect stringify number for line-height (vercel#543)
Browse files Browse the repository at this point in the history
Close vercel#535

Co-authored-by: Shu Ding <g@shud.in>
  • Loading branch information
2 people authored and sahithyandev committed Apr 26, 2024
1 parent cde7d7b commit 23199e1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/handler/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ function handleFallbackColor(
}

function purify(name: string, value?: string | number) {
if (typeof value === 'number') {
if (!optOutPx.has(name)) return value + 'px'
if (keepNumber.has(name)) return value
return String(value)
}
return value
const num = Number(value)
if (isNaN(num)) return value
if (!optOutPx.has(name)) return num + 'px'
if (keepNumber.has(name)) return num
return String(value)
}

function handleSpecialCase(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/line-height.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { it, describe, expect } from 'vitest'

import { initFonts, toImage } from './utils.js'
import satori from '../src/index.js'

describe('line-height', () => {
let fonts
initFonts((f) => (fonts = f))
it('should work correctly', async () => {
const svgs = await Promise.all(
[1, '1'].map((lineHeight) =>
satori(
<div
style={{
margin: 0,
padding: 0,
fontSize: '30px',
width: '100px',
height: '100px',
background: 'white',
lineHeight,
}}
>
Hello I am some text that is here.
</div>,
{ width: 100, height: 100, fonts, embedFont: true }
)
)
)

svgs.forEach((svg) => {
expect(toImage(svg, 100)).toMatchImageSnapshot()
})
})
})

0 comments on commit 23199e1

Please sign in to comment.