Skip to content

Commit 83efbce

Browse files
fix(dom-to-react): transform style to object for Web Components
Fixes #188
1 parent 2afcf15 commit 83efbce

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/dom-to-react.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var React = require('react');
22
var attributesToProps = require('./attributes-to-props');
33
var utilities = require('./utilities');
44

5+
var setStyleProp = utilities.setStyleProp;
6+
57
/**
68
* Converts DOM nodes to React elements.
79
*
@@ -62,8 +64,10 @@ function domToReact(nodes, options) {
6264
}
6365

6466
props = node.attribs;
65-
if (!skipAttributesToProps(node)) {
66-
props = attributesToProps(node.attribs);
67+
if (skipAttributesToProps(node)) {
68+
setStyleProp(props.style, props);
69+
} else if (props) {
70+
props = attributesToProps(props);
6771
}
6872

6973
children = null;
@@ -110,7 +114,7 @@ function domToReact(nodes, options) {
110114

111115
/**
112116
* Determines whether DOM element attributes should be transformed to props.
113-
* Web Components (custom elements) should not have their attributes transformed.
117+
* Web Components should not have their attributes transformed except for `style`.
114118
*
115119
* @param {DomElement} node
116120
* @return {boolean}

test/__snapshots__/dom-to-react.test.js.snap

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ exports[`domToReact converts custom element with attributes 1`] = `
1919
<custom-element
2020
class="myClass"
2121
custom-attribute="value"
22-
style="-o-transition: all .5s; line-height: 1;"
22+
style={
23+
Object {
24+
"OTransition": "all .5s",
25+
"lineHeight": "1",
26+
}
27+
}
2328
/>
2429
`;
2530
@@ -87,6 +92,11 @@ exports[`domToReact when React >=16 preserves unknown attributes 1`] = `
8792
<custom-element
8893
class="myClass"
8994
custom-attribute="value"
90-
style="-o-transition: all .5s; line-height: 1;"
95+
style={
96+
Object {
97+
"OTransition": "all .5s",
98+
"lineHeight": "1",
99+
}
100+
}
91101
/>
92102
`;

0 commit comments

Comments
 (0)