Skip to content

Commit c5281c9

Browse files
committed
Inline dangerousStyleValue
1 parent 4ad6e2d commit c5281c9

File tree

3 files changed

+68
-79
lines changed

3 files changed

+68
-79
lines changed

packages/react-dom-bindings/src/client/CSSPropertyOperations.js

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
import {shorthandToLonghand} from './CSSShorthandProperty';
99

10-
import dangerousStyleValue from './dangerousStyleValue';
1110
import hyphenateStyleName from '../shared/hyphenateStyleName';
1211
import warnValidStyle from '../shared/warnValidStyle';
12+
import {isUnitlessNumber} from '../shared/CSSProperty';
13+
import {checkCSSPropertyStringCoercion} from 'shared/CheckStringCoercion';
1314

1415
/**
1516
* Operations for dealing with CSS properties.
@@ -29,19 +30,36 @@ export function createDangerousStringForStyles(styles) {
2930
if (!styles.hasOwnProperty(styleName)) {
3031
continue;
3132
}
32-
const styleValue = styles[styleName];
33-
if (styleValue != null) {
33+
const value = styles[styleName];
34+
if (value != null && typeof value !== 'boolean' && value !== '') {
3435
const isCustomProperty = styleName.indexOf('--') === 0;
35-
serialized +=
36-
delimiter +
37-
(isCustomProperty ? styleName : hyphenateStyleName(styleName)) +
38-
':';
39-
serialized += dangerousStyleValue(
40-
styleName,
41-
styleValue,
42-
isCustomProperty,
43-
);
44-
36+
if (isCustomProperty) {
37+
if (__DEV__) {
38+
checkCSSPropertyStringCoercion(value, styleName);
39+
}
40+
serialized += delimiter + styleName + ':' + ('' + value).trim();
41+
} else {
42+
if (
43+
typeof value === 'number' &&
44+
value !== 0 &&
45+
!(
46+
isUnitlessNumber.hasOwnProperty(styleName) &&
47+
isUnitlessNumber[styleName]
48+
)
49+
) {
50+
serialized +=
51+
delimiter + hyphenateStyleName(styleName) + ':' + value + 'px';
52+
} else {
53+
if (__DEV__) {
54+
checkCSSPropertyStringCoercion(value, styleName);
55+
}
56+
serialized +=
57+
delimiter +
58+
hyphenateStyleName(styleName) +
59+
':' +
60+
('' + value).trim();
61+
}
62+
}
4563
delimiter = ';';
4664
}
4765
}
@@ -58,28 +76,46 @@ export function createDangerousStringForStyles(styles) {
5876
*/
5977
export function setValueForStyles(node, styles) {
6078
const style = node.style;
61-
for (let styleName in styles) {
79+
for (const styleName in styles) {
6280
if (!styles.hasOwnProperty(styleName)) {
6381
continue;
6482
}
83+
const value = styles[styleName];
6584
const isCustomProperty = styleName.indexOf('--') === 0;
6685
if (__DEV__) {
6786
if (!isCustomProperty) {
68-
warnValidStyle(styleName, styles[styleName]);
87+
warnValidStyle(styleName, value);
6988
}
7089
}
71-
const styleValue = dangerousStyleValue(
72-
styleName,
73-
styles[styleName],
74-
isCustomProperty,
75-
);
76-
if (styleName === 'float') {
77-
styleName = 'cssFloat';
78-
}
79-
if (isCustomProperty) {
80-
style.setProperty(styleName, styleValue);
90+
91+
if (value == null || typeof value === 'boolean' || value === '') {
92+
if (isCustomProperty) {
93+
style.setProperty(styleName, '');
94+
} else if (styleName === 'float') {
95+
style.cssFloat = '';
96+
} else {
97+
style[styleName] = '';
98+
}
99+
} else if (isCustomProperty) {
100+
style.setProperty(styleName, value);
101+
} else if (
102+
typeof value === 'number' &&
103+
value !== 0 &&
104+
!(
105+
isUnitlessNumber.hasOwnProperty(styleName) &&
106+
isUnitlessNumber[styleName]
107+
)
108+
) {
109+
style[styleName] = value + 'px'; // Presumes implicit 'px' suffix for unitless numbers
81110
} else {
82-
style[styleName] = styleValue;
111+
if (styleName === 'float') {
112+
style.cssFloat = value;
113+
} else {
114+
if (__DEV__) {
115+
checkCSSPropertyStringCoercion(value, styleName);
116+
}
117+
style[styleName] = ('' + value).trim();
118+
}
83119
}
84120
}
85121
}

packages/react-dom-bindings/src/client/ReactDOMHostConfig.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import {
7070
DOCUMENT_TYPE_NODE,
7171
DOCUMENT_FRAGMENT_NODE,
7272
} from './HTMLNodeType';
73-
import dangerousStyleValue from './dangerousStyleValue';
7473

7574
import {retryIfBlockedOn} from '../events/ReactDOMEventReplaying';
7675

@@ -750,7 +749,12 @@ export function unhideInstance(instance: Instance, props: Props): void {
750749
styleProp.hasOwnProperty('display')
751750
? styleProp.display
752751
: null;
753-
instance.style.display = dangerousStyleValue('display', display);
752+
instance.style.display =
753+
display == null || typeof display === 'boolean'
754+
? ''
755+
: // The value would've errored already if it wasn't safe.
756+
// eslint-disable-next-line react-internal/safe-string-coercion
757+
('' + display).trim();
754758
}
755759

756760
export function unhideTextInstance(

packages/react-dom-bindings/src/client/dangerousStyleValue.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)