7
7
8
8
import { shorthandToLonghand } from './CSSShorthandProperty' ;
9
9
10
- import dangerousStyleValue from './dangerousStyleValue' ;
11
10
import hyphenateStyleName from '../shared/hyphenateStyleName' ;
12
11
import warnValidStyle from '../shared/warnValidStyle' ;
12
+ import { isUnitlessNumber } from '../shared/CSSProperty' ;
13
+ import { checkCSSPropertyStringCoercion } from 'shared/CheckStringCoercion' ;
13
14
14
15
/**
15
16
* Operations for dealing with CSS properties.
@@ -29,19 +30,36 @@ export function createDangerousStringForStyles(styles) {
29
30
if ( ! styles . hasOwnProperty ( styleName ) ) {
30
31
continue ;
31
32
}
32
- const styleValue = styles [ styleName ] ;
33
- if ( styleValue != null ) {
33
+ const value = styles [ styleName ] ;
34
+ if ( value != null && typeof value !== 'boolean' && value !== '' ) {
34
35
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
+ }
45
63
delimiter = ';' ;
46
64
}
47
65
}
@@ -58,28 +76,46 @@ export function createDangerousStringForStyles(styles) {
58
76
*/
59
77
export function setValueForStyles ( node , styles ) {
60
78
const style = node . style ;
61
- for ( let styleName in styles ) {
79
+ for ( const styleName in styles ) {
62
80
if ( ! styles . hasOwnProperty ( styleName ) ) {
63
81
continue ;
64
82
}
83
+ const value = styles [ styleName ] ;
65
84
const isCustomProperty = styleName . indexOf ( '--' ) === 0 ;
66
85
if ( __DEV__ ) {
67
86
if ( ! isCustomProperty ) {
68
- warnValidStyle ( styleName , styles [ styleName ] ) ;
87
+ warnValidStyle ( styleName , value ) ;
69
88
}
70
89
}
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
81
110
} 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
+ }
83
119
}
84
120
}
85
121
}
0 commit comments