diff --git a/__tests__/attributes-to-props/index.test.ts b/__tests__/attributes-to-props/index.test.ts index 47f4b30e..e1761819 100644 --- a/__tests__/attributes-to-props/index.test.ts +++ b/__tests__/attributes-to-props/index.test.ts @@ -280,6 +280,15 @@ describe('attributesToProps with SVG attribute', () => { describe('attributesToProps with style attribute', () => { const propsEmptyStyle = { style: {} }; + it.each(['style', 'STYLE', 'StYlE'])( + 'converts %s to a style object', + (attributeName) => { + expect( + attributesToProps({ [attributeName]: 'color: red; font-size: 12px' }), + ).toEqual({ style: { color: 'red', fontSize: '12px' } }); + }, + ); + it.each([undefined, null] as unknown as string[])( 'does not parse invalid value: %s', (style) => { diff --git a/__tests__/index.test.tsx b/__tests__/index.test.tsx index e76b19b0..7934bbbe 100644 --- a/__tests__/index.test.tsx +++ b/__tests__/index.test.tsx @@ -236,7 +236,17 @@ describe('htmlparser2 option', () => { }); }); -describe('invalid styles', () => { +describe('styles', () => { + it.each(['style', 'STYLE', 'StYlE'])( + 'renders an element with a %s attribute', + (attributeName) => { + const html = `

Hello

`; + expect(render(parse(html) as React.JSX.Element)).toBe( + '

Hello

', + ); + }, + ); + it('copes with invalid styles', () => { const html = '

X

'; const reactElement = parse(html) as React.JSX.Element; diff --git a/src/attributes-to-props.ts b/src/attributes-to-props.ts index 5170491d..685751bc 100644 --- a/src/attributes-to-props.ts +++ b/src/attributes-to-props.ts @@ -84,6 +84,11 @@ export default function attributesToProps( props[propName] = attributeValue; + // transform inline style to object after normalizing the attribute name + if (propName === 'style') { + setStyleProp(attributeValue, props); + } + switch (propertyInfo?.type) { case BOOLEAN: props[propName] = true; @@ -103,9 +108,6 @@ export default function attributesToProps( } } - // transform inline style to object - setStyleProp(attributes.style, props); - return props; }