Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/hast-util-to-babel-ast/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ describe('hast-util-to-babel-ast', () => {
const code = `<svg><path style="--index: 1; font-size: 24px;"></path><path style="--index: 2"></path></svg>`
expect(transform(code)).toMatchInlineSnapshot(`
"<svg><path style={{
"--index": 1,
"--index": "1",
fontSize: 24
}} /><path style={{
"--index": 2
"--index": "2"
}} /></svg>;"
`)
})

it('preserves units in CSS custom property values', () => {
const code = `<svg style="--size: 20px; --spacing: 1.5rem; --width: 100%; --gradient: linear-gradient(red, blue);"></svg>`
const output = transform(code)
expect(output).toContain(`"--size": "20px"`)
expect(output).toContain(`"--spacing": "1.5rem"`)
expect(output).toContain(`"--width": "100%"`)
expect(output).toContain(`"--gradient": "linear-gradient(red, blue)"`)
})
})
5 changes: 3 additions & 2 deletions packages/hast-util-to-babel-ast/src/stringToObjectStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const formatKey = (key: string) => {
/**
* Format style value into JSX style object value.
*/
const formatValue = (value: string) => {
const formatValue = (value: string, key: string) => {
if (VAR_REGEX.test(key)) return t.stringLiteral(value)
if (isNumeric(value)) return t.numericLiteral(Number(value))
if (isConvertiblePixelValue(value))
return t.numericLiteral(Number(trimEnd(value, 'px')))
Expand All @@ -53,7 +54,7 @@ export const stringToObjectStyle = (rawStyle: string): t.ObjectExpression => {
const value = style.substr(firstColon + 1).trim()
const key = style.substr(0, firstColon)
if (key !== '') {
const property = t.objectProperty(formatKey(key), formatValue(value))
const property = t.objectProperty(formatKey(key), formatValue(value, key))
properties.push(property)
}
}
Expand Down