Skip to content

Commit 1ed58a1

Browse files
Merge pull request #257 from dogriffiths/master
fix(utilities): skip invalid style attributes
2 parents b0c1ca0 + 49e00cf commit 1ed58a1

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/utilities.js

Lines changed: 5 additions & 1 deletion
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,12 @@ describe('trim option', () => {
153153
);
154154
});
155155
});
156+
157+
describe('invalid styles', () => {
158+
it('copes with invalid 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('<p>X</p>');
163+
});
164+
});

test/utilities.test.js

Lines changed: 7 additions & 0 deletions
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)