Skip to content

Commit 39b2970

Browse files
TrySoundgaearon
authored andcommitted
Prevents adding units to css custom properties (#9966)
* Prevents adding units to css custom properties * Fix code style * Optimize custom property checking * Prevents adding units to css custom properties in markup creation * Update passing tests * Fix argument name and reuse check in DEV
1 parent ae94ea7 commit 39b2970

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

src/renderers/dom/shared/CSSPropertyOperations.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ if (__DEV__) {
128128
* @param {ReactDOMComponent} component
129129
*/
130130
var warnValidStyle = function(name, value, component) {
131-
// Don't warn for CSS variables
132-
if (name.indexOf('--') === 0) {
133-
return;
134-
}
135131
var owner;
136132
if (component) {
137133
owner = component._currentElement._owner;
@@ -173,14 +169,22 @@ var CSSPropertyOperations = {
173169
if (!styles.hasOwnProperty(styleName)) {
174170
continue;
175171
}
172+
var isCustomProperty = styleName.indexOf('--') === 0;
176173
var styleValue = styles[styleName];
177174
if (__DEV__) {
178-
warnValidStyle(styleName, styleValue, component);
175+
if (!isCustomProperty) {
176+
warnValidStyle(styleName, styleValue, component);
177+
}
179178
}
180179
if (styleValue != null) {
181180
serialized += processStyleName(styleName) + ':';
182181
serialized +=
183-
dangerousStyleValue(styleName, styleValue, component) + ';';
182+
dangerousStyleValue(
183+
styleName,
184+
styleValue,
185+
component,
186+
isCustomProperty,
187+
) + ';';
184188
}
185189
}
186190
return serialized || null;
@@ -208,18 +212,22 @@ var CSSPropertyOperations = {
208212
if (!styles.hasOwnProperty(styleName)) {
209213
continue;
210214
}
215+
var isCustomProperty = styleName.indexOf('--') === 0;
211216
if (__DEV__) {
212-
warnValidStyle(styleName, styles[styleName], component);
217+
if (!isCustomProperty) {
218+
warnValidStyle(styleName, styles[styleName], component);
219+
}
213220
}
214221
var styleValue = dangerousStyleValue(
215222
styleName,
216223
styles[styleName],
217224
component,
225+
isCustomProperty,
218226
);
219227
if (styleName === 'float' || styleName === 'cssFloat') {
220228
styleName = styleFloatAccessor;
221229
}
222-
if (styleName.indexOf('--') === 0) {
230+
if (isCustomProperty) {
223231
style.setProperty(styleName, styleValue);
224232
} else if (styleValue) {
225233
style[styleName] = styleValue;

src/renderers/dom/shared/__tests__/CSSPropertyOperations-test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ describe('CSSPropertyOperations', () => {
101101
).toBe('-ms-transition:none;-moz-transition:none;');
102102
});
103103

104+
it('should create markup with unitless css custom property', () => {
105+
expect(
106+
CSSPropertyOperations.createMarkupForStyles({
107+
'--foo': 5,
108+
}),
109+
).toBe('--foo:5');
110+
});
111+
104112
it('should set style attribute when styles exist', () => {
105113
var styles = {
106114
backgroundColor: '#000',
@@ -254,7 +262,7 @@ describe('CSSPropertyOperations', () => {
254262
);
255263
});
256264

257-
it('should not warn when setting CSS variables', () => {
265+
it('should not warn when setting CSS custom properties', () => {
258266
class Comp extends React.Component {
259267
render() {
260268
return <div style={{'--foo-primary': 'red', backgroundColor: 'red'}} />;
@@ -267,4 +275,17 @@ describe('CSSPropertyOperations', () => {
267275

268276
expect(console.error.calls.count()).toBe(0);
269277
});
278+
279+
it('should not add units to CSS custom properties', () => {
280+
class Comp extends React.Component {
281+
render() {
282+
return <div style={{'--foo': 5}} />;
283+
}
284+
}
285+
286+
var root = document.createElement('div');
287+
ReactDOM.render(<Comp />, root);
288+
289+
expect(root.children[0].style.Foo).toEqual('5');
290+
});
270291
});

src/renderers/dom/shared/dangerousStyleValue.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var styleWarnings = {};
2727
* @param {ReactDOMComponent} component
2828
* @return {string} Normalized style value with dimensions applied.
2929
*/
30-
function dangerousStyleValue(name, value, component) {
30+
function dangerousStyleValue(name, value, component, isCustomProperty) {
3131
// Note that we've removed escapeTextForBrowser() calls here since the
3232
// whole string will be escaped when the attribute is injected into
3333
// the markup. If you provide unsafe user data here they can inject
@@ -44,12 +44,14 @@ function dangerousStyleValue(name, value, component) {
4444
}
4545

4646
var isNonNumeric = isNaN(value);
47-
if (
48-
isNonNumeric ||
49-
value === 0 ||
50-
(isUnitlessNumber.hasOwnProperty(name) && isUnitlessNumber[name])
51-
) {
52-
return '' + value; // cast to string
47+
if (!isCustomProperty) {
48+
if (
49+
isNonNumeric ||
50+
value === 0 ||
51+
(isUnitlessNumber.hasOwnProperty(name) && isUnitlessNumber[name])
52+
) {
53+
return '' + value; // cast to string
54+
}
5355
}
5456

5557
if (typeof value === 'string') {

0 commit comments

Comments
 (0)