@@ -27,6 +27,13 @@ const VOID_ELEMENTS = [
27
27
'wbr'
28
28
] ;
29
29
30
+ // DOM properties that should NOT have "px" added when numeric
31
+ export const NON_DIMENSION_PROPS = {
32
+ boxFlex :1 , boxFlexGroup :1 , columnCount :1 , fillOpacity :1 , flex :1 , flexGrow :1 ,
33
+ flexPositive :1 , flexShrink :1 , flexNegative :1 , fontWeight :1 , lineClamp :1 , lineHeight :1 ,
34
+ opacity :1 , order :1 , orphans :1 , strokeOpacity :1 , widows :1 , zIndex :1 , zoom :1
35
+ } ;
36
+
30
37
// components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
31
38
const UNNAMED = [ ] ;
32
39
@@ -42,10 +49,33 @@ let escapeChar = a => ESC[a] || a;
42
49
43
50
let falsey = v => v == null || v === false ;
44
51
52
+ let memoize = ( fn , mem = { } ) => v => mem [ v ] || ( mem [ v ] = fn ( v ) ) ;
53
+
45
54
let indent = ( s , char ) => String ( s ) . replace ( / ( \n + ) / g, '$1' + ( char || '\t' ) ) ;
46
55
47
56
let isLargeString = s => ( String ( s ) . length > 40 || String ( s ) . indexOf ( '\n' ) !== - 1 || String ( s ) . indexOf ( '<' ) !== - 1 ) ;
48
57
58
+ function styleObjToCss ( s ) {
59
+ let str = '' ;
60
+ for ( let prop in s ) {
61
+ let val = s [ prop ] ;
62
+ if ( val != null ) {
63
+ if ( str ) str += ' ' ;
64
+ str += jsToCss ( prop ) ;
65
+ str += ': ' ;
66
+ str += val ;
67
+ if ( typeof val === 'number' && ! NON_DIMENSION_PROPS [ prop ] ) {
68
+ str += 'px' ;
69
+ }
70
+ str += ';' ;
71
+ }
72
+ }
73
+ return str ;
74
+ }
75
+
76
+ // Convert a JavaScript camel-case CSS property name to a CSS property name
77
+ let jsToCss = memoize ( s => s . replace ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) ) ;
78
+
49
79
function assign ( obj , props ) {
50
80
for ( let i in props ) obj [ i ] = props [ i ] ;
51
81
return obj ;
@@ -152,6 +182,9 @@ export default function renderToString(vnode, context, opts, inner) {
152
182
if ( attributes [ 'class' ] ) continue ;
153
183
name = 'class' ;
154
184
}
185
+ if ( name === 'style' && v && typeof v === 'object' ) {
186
+ v = styleObjToCss ( v ) ;
187
+ }
155
188
if ( name === 'dangerouslySetInnerHTML' ) {
156
189
html = v && v . __html ;
157
190
}
0 commit comments