Skip to content

Commit 509486e

Browse files
committed
fix(utilities): skip corrupted style attributes
Fixes #256
1 parent 48a46de commit 509486e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/utilities.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ function setStyleProp(style, props) {
8383
if (style === null || style === undefined) {
8484
return;
8585
}
86-
props.style = styleToJS(style, styleToJSOptions);
86+
try {
87+
props.style = styleToJS(style, styleToJSOptions);
88+
} catch (err) {
89+
props.style = {};
90+
}
8791
}
8892

8993
/**

test/index.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,14 @@ describe('trim option', () => {
153153
);
154154
});
155155
});
156+
157+
describe('corrupted styles', () => {
158+
it('copes with corrupted styles', () => {
159+
const html = '<p style="font - size: 1em">X</p>';
160+
const options = {};
161+
const reactElement = parse(html, options);
162+
expect(render(reactElement)).toBe(
163+
'<p>X</p>'
164+
);
165+
});
166+
})

test/utilities.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,11 @@ describe('setStyleProp', () => {
117117
expect(setStyleProp(style, props)).toBe(undefined);
118118
expect(props).toMatchSnapshot();
119119
});
120+
121+
it('does not set props.style when style attribute corrupt', () => {
122+
const style = `font - size: 1em`;
123+
const props = {};
124+
expect(setStyleProp(style, props)).toBe(undefined);
125+
expect(props).toEqual({style: {}});
126+
});
120127
});

0 commit comments

Comments
 (0)