@@ -11,7 +11,6 @@ import hyphenateStyleName from '../shared/hyphenateStyleName';
11
11
import warnValidStyle from '../shared/warnValidStyle' ;
12
12
import isUnitlessNumber from '../shared/isUnitlessNumber' ;
13
13
import { checkCSSPropertyStringCoercion } from 'shared/CheckStringCoercion' ;
14
- import { diffInCommitPhase } from 'shared/ReactFeatureFlags' ;
15
14
16
15
/**
17
16
* Operations for dealing with CSS properties.
@@ -65,50 +64,14 @@ export function createDangerousStringForStyles(styles) {
65
64
}
66
65
}
67
66
68
- function setValueForStyle ( style , styleName , value ) {
69
- const isCustomProperty = styleName . indexOf ( '--' ) === 0 ;
70
- if ( __DEV__ ) {
71
- if ( ! isCustomProperty ) {
72
- warnValidStyle ( styleName , value ) ;
73
- }
74
- }
75
-
76
- if ( value == null || typeof value === 'boolean' || value === '' ) {
77
- if ( isCustomProperty ) {
78
- style . setProperty ( styleName , '' ) ;
79
- } else if ( styleName === 'float' ) {
80
- style . cssFloat = '' ;
81
- } else {
82
- style [ styleName ] = '' ;
83
- }
84
- } else if ( isCustomProperty ) {
85
- style . setProperty ( styleName , value ) ;
86
- } else if (
87
- typeof value === 'number' &&
88
- value !== 0 &&
89
- ! isUnitlessNumber ( styleName )
90
- ) {
91
- style [ styleName ] = value + 'px' ; // Presumes implicit 'px' suffix for unitless numbers
92
- } else {
93
- if ( styleName === 'float' ) {
94
- style . cssFloat = value ;
95
- } else {
96
- if ( __DEV__ ) {
97
- checkCSSPropertyStringCoercion ( value , styleName ) ;
98
- }
99
- style [ styleName ] = ( '' + value ) . trim ( ) ;
100
- }
101
- }
102
- }
103
-
104
67
/**
105
68
* Sets the value for multiple styles on a node. If a value is specified as
106
69
* '' (empty string), the corresponding style property will be unset.
107
70
*
108
71
* @param {DOMElement } node
109
72
* @param {object } styles
110
73
*/
111
- export function setValueForStyles ( node , styles , prevStyles ) {
74
+ export function setValueForStyles ( node , styles ) {
112
75
if ( styles != null && typeof styles !== 'object' ) {
113
76
throw new Error (
114
77
'The `style` prop expects a mapping from style properties to values, ' +
@@ -125,39 +88,42 @@ export function setValueForStyles(node, styles, prevStyles) {
125
88
}
126
89
127
90
const style = node . style ;
128
-
129
- if ( diffInCommitPhase && prevStyles != null ) {
130
- if ( __DEV__ ) {
131
- validateShorthandPropertyCollisionInDev ( prevStyles , styles ) ;
91
+ for ( const styleName in styles ) {
92
+ if ( ! styles . hasOwnProperty ( styleName ) ) {
93
+ continue ;
132
94
}
133
-
134
- for ( const styleName in prevStyles ) {
135
- if (
136
- prevStyles . hasOwnProperty ( styleName ) &&
137
- ( styles == null || ! styles . hasOwnProperty ( styleName ) )
138
- ) {
139
- // Clear style
140
- const isCustomProperty = styleName . indexOf ( '--' ) === 0 ;
141
- if ( isCustomProperty ) {
142
- style . setProperty ( styleName , '' ) ;
143
- } else if ( styleName === 'float' ) {
144
- style . cssFloat = '' ;
145
- } else {
146
- style [ styleName ] = '' ;
147
- }
95
+ const value = styles [ styleName ] ;
96
+ const isCustomProperty = styleName . indexOf ( '--' ) === 0 ;
97
+ if ( __DEV__ ) {
98
+ if ( ! isCustomProperty ) {
99
+ warnValidStyle ( styleName , value ) ;
148
100
}
149
101
}
150
- for ( const styleName in styles ) {
151
- const value = styles [ styleName ] ;
152
- if ( styles . hasOwnProperty ( styleName ) && prevStyles [ styleName ] !== value ) {
153
- setValueForStyle ( style , styleName , value ) ;
102
+
103
+ if ( value == null || typeof value === 'boolean' || value === '' ) {
104
+ if ( isCustomProperty ) {
105
+ style . setProperty ( styleName , '' ) ;
106
+ } else if ( styleName === 'float' ) {
107
+ style . cssFloat = '' ;
108
+ } else {
109
+ style [ styleName ] = '' ;
154
110
}
155
- }
156
- } else {
157
- for ( const styleName in styles ) {
158
- if ( styles . hasOwnProperty ( styleName ) ) {
159
- const value = styles [ styleName ] ;
160
- setValueForStyle ( style , styleName , value ) ;
111
+ } else if ( isCustomProperty ) {
112
+ style . setProperty ( styleName , value ) ;
113
+ } else if (
114
+ typeof value === 'number' &&
115
+ value !== 0 &&
116
+ ! isUnitlessNumber ( styleName )
117
+ ) {
118
+ style [ styleName ] = value + 'px' ; // Presumes implicit 'px' suffix for unitless numbers
119
+ } else {
120
+ if ( styleName === 'float' ) {
121
+ style . cssFloat = value ;
122
+ } else {
123
+ if ( __DEV__ ) {
124
+ checkCSSPropertyStringCoercion ( value , styleName ) ;
125
+ }
126
+ style [ styleName ] = ( '' + value ) . trim ( ) ;
161
127
}
162
128
}
163
129
}
@@ -201,38 +167,15 @@ function expandShorthandMap(styles) {
201
167
* becomes .style.fontVariant = ''
202
168
*/
203
169
export function validateShorthandPropertyCollisionInDev (
204
- prevStyles ,
170
+ styleUpdates ,
205
171
nextStyles ,
206
172
) {
207
173
if ( __DEV__ ) {
208
174
if ( ! nextStyles ) {
209
175
return ;
210
176
}
211
177
212
- // Compute the diff as it would happen elsewhere.
213
- const expandedUpdates = { } ;
214
- if ( prevStyles ) {
215
- for ( const key in prevStyles ) {
216
- if ( prevStyles . hasOwnProperty ( key ) && ! nextStyles . hasOwnProperty ( key ) ) {
217
- const longhands = shorthandToLonghand [ key ] || [ key ] ;
218
- for ( let i = 0 ; i < longhands . length ; i ++ ) {
219
- expandedUpdates [ longhands [ i ] ] = key ;
220
- }
221
- }
222
- }
223
- }
224
- for ( const key in nextStyles ) {
225
- if (
226
- nextStyles . hasOwnProperty ( key ) &&
227
- ( ! prevStyles || prevStyles [ key ] !== nextStyles [ key ] )
228
- ) {
229
- const longhands = shorthandToLonghand [ key ] || [ key ] ;
230
- for ( let i = 0 ; i < longhands . length ; i ++ ) {
231
- expandedUpdates [ longhands [ i ] ] = key ;
232
- }
233
- }
234
- }
235
-
178
+ const expandedUpdates = expandShorthandMap ( styleUpdates ) ;
236
179
const expandedStyles = expandShorthandMap ( nextStyles ) ;
237
180
const warnedAbout = { } ;
238
181
for ( const key in expandedUpdates ) {
@@ -250,7 +193,7 @@ export function validateShorthandPropertyCollisionInDev(
250
193
"avoid this, don't mix shorthand and non-shorthand properties " +
251
194
'for the same value; instead, replace the shorthand with ' +
252
195
'separate values.' ,
253
- isValueEmpty ( nextStyles [ originalKey ] ) ? 'Removing' : 'Updating' ,
196
+ isValueEmpty ( styleUpdates [ originalKey ] ) ? 'Removing' : 'Updating' ,
254
197
originalKey ,
255
198
correctOriginalKey ,
256
199
) ;
0 commit comments