Skip to content
Merged
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
9 changes: 9 additions & 0 deletions __tests__/attributes-to-props/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 11 additions & 1 deletion __tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<p ${attributeName}="color: red; font-size: 12px">Hello</p>`;
expect(render(parse(html) as React.JSX.Element)).toBe(
'<p style="color:red;font-size:12px">Hello</p>',
);
},
);

it('copes with invalid styles', () => {
const html = '<p style="font - size: 1em">X</p>';
const reactElement = parse(html) as React.JSX.Element;
Expand Down
8 changes: 5 additions & 3 deletions src/attributes-to-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -103,9 +108,6 @@ export default function attributesToProps(
}
}

// transform inline style to object
setStyleProp(attributes.style, props);

return props;
}

Expand Down
Loading