Skip to content

Commit 7c209cb

Browse files
refactor(utilities): create setStyleProp helper from styleToJS
Add tests
1 parent 774c38e commit 7c209cb

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

lib/attributes-to-props.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
var reactProperty = require('react-property');
2-
var styleToJS = require('style-to-js').default;
32
var utilities = require('./utilities');
43

4+
var setStyleProp = utilities.setStyleProp;
5+
56
var htmlProperties = reactProperty.html;
67
var svgProperties = reactProperty.svg;
78
var isCustomAttribute = reactProperty.isCustomAttribute;
89

910
var hasOwnProperty = Object.prototype.hasOwnProperty;
1011

11-
var styleToJSOptions = { reactCompat: true };
12-
1312
/**
1413
* Converts HTML/SVG DOM attributes to React props.
1514
*
16-
* @param {Object} [attributes={}] - The HTML/SVG DOM attributes.
17-
* @return {Object} - The React props.
15+
* @param {object} [attributes={}] - HTML/SVG DOM attributes.
16+
* @return {object} - React props.
1817
*/
1918
function attributesToProps(attributes) {
2019
attributes = attributes || {};
@@ -59,10 +58,8 @@ function attributesToProps(attributes) {
5958
}
6059
}
6160

62-
// convert inline style to object
63-
if (attributes.style != null) {
64-
props.style = styleToJS(attributes.style, styleToJSOptions);
65-
}
61+
// transform inline style to object
62+
setStyleProp(attributes.style, props);
6663

6764
return props;
6865
}

lib/utilities.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var React = require('react');
2+
var styleToJS = require('style-to-js').default;
23

34
/**
45
* Swap key with value in an object.
@@ -70,14 +71,30 @@ function isCustomComponent(tagName, props) {
7071
}
7172
}
7273

74+
var styleToJSOptions = { reactCompat: true };
75+
76+
/**
77+
* Sets style prop.
78+
*
79+
* @param {null|undefined|string} style
80+
* @param {object} props
81+
*/
82+
function setStyleProp(style, props) {
83+
if (style === null || style === undefined) {
84+
return;
85+
}
86+
props.style = styleToJS(style, styleToJSOptions);
87+
}
88+
7389
/**
74-
* @constant {Boolean}
90+
* @constant {boolean}
7591
* @see {@link https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html}
7692
*/
7793
var PRESERVE_CUSTOM_ATTRIBUTES = React.version.split('.')[0] >= 16;
7894

7995
module.exports = {
8096
PRESERVE_CUSTOM_ATTRIBUTES: PRESERVE_CUSTOM_ATTRIBUTES,
8197
invertObject: invertObject,
82-
isCustomComponent: isCustomComponent
98+
isCustomComponent: isCustomComponent,
99+
setStyleProp: setStyleProp
83100
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`setStyleProp sets props.style 1`] = `
4+
Object {
5+
"style": Object {
6+
"WebkitUserSelect": "none",
7+
"backgroundColor": "#bada55",
8+
"backgroundImage": "linear-gradient(to bottom, rgba(255,255,0,0.5), rgba(0,0,255,0.5)),
9+
url('https://mdn.mozillademos.org/files/7693/catfront.png')",
10+
"color": "red",
11+
"lineHeight": "1",
12+
},
13+
"width": 100,
14+
}
15+
`;

test/utilities.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const React = require('react');
22
const {
33
PRESERVE_CUSTOM_ATTRIBUTES,
44
invertObject,
5-
isCustomComponent
5+
isCustomComponent,
6+
setStyleProp
67
} = require('../lib/utilities');
78

89
describe('invertObject', () => {
@@ -91,3 +92,29 @@ describe('PRESERVE_CUSTOM_ATTRIBUTES', () => {
9192
expect(PRESERVE_CUSTOM_ATTRIBUTES).toBe(isReactGreaterThan15);
9293
});
9394
});
95+
96+
describe('setStyleProp', () => {
97+
it.each([undefined, null])(
98+
'does not set props.style when style=%p',
99+
style => {
100+
const props = {};
101+
expect(setStyleProp(style, props)).toBe(undefined);
102+
expect(props).toEqual({});
103+
}
104+
);
105+
106+
it('sets props.style', () => {
107+
const style = `
108+
color: red;
109+
background-color: #bada55;
110+
-webkit-user-select: none;
111+
line-height: 1;
112+
background-image:
113+
linear-gradient(to bottom, rgba(255,255,0,0.5), rgba(0,0,255,0.5)),
114+
url('https://mdn.mozillademos.org/files/7693/catfront.png');
115+
`;
116+
const props = { style: { foo: 'bar' }, width: 100 };
117+
expect(setStyleProp(style, props)).toBe(undefined);
118+
expect(props).toMatchSnapshot();
119+
});
120+
});

0 commit comments

Comments
 (0)