1
- import prettyFormat from 'pretty-format ' ;
1
+ import { objectKeys , encodeEntities , falsey , memoize , indent , isLargeString , styleObjToCss , assign , getNodeProps } from './util ' ;
2
2
3
3
const SHALLOW = { shallow : true } ;
4
4
5
- const ESC = {
6
- '<' : '<' ,
7
- '>' : '>' ,
8
- '"' : '"' ,
9
- '&' : '&'
10
- } ;
5
+ // components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
6
+ const UNNAMED = [ ] ;
11
7
12
8
const EMPTY = { } ;
13
9
@@ -28,81 +24,6 @@ const VOID_ELEMENTS = [
28
24
'wbr'
29
25
] ;
30
26
31
- // DOM properties that should NOT have "px" added when numeric
32
- export const NON_DIMENSION_PROPS = {
33
- boxFlex :1 , boxFlexGroup :1 , columnCount :1 , fillOpacity :1 , flex :1 , flexGrow :1 ,
34
- flexPositive :1 , flexShrink :1 , flexNegative :1 , fontWeight :1 , lineClamp :1 , lineHeight :1 ,
35
- opacity :1 , order :1 , orphans :1 , strokeOpacity :1 , widows :1 , zIndex :1 , zoom :1
36
- } ;
37
-
38
- // components without names, kept as a hash for later comparison to return consistent UnnamedComponentXX names.
39
- const UNNAMED = [ ] ;
40
-
41
- const objectKeys = Object . keys || ( obj => {
42
- let keys = [ ] ;
43
- for ( let i in obj ) if ( obj . hasOwnProperty ( i ) ) keys . push ( i ) ;
44
- return keys ;
45
- } ) ;
46
-
47
- let encodeEntities = s => String ( s ) . replace ( / [ < > " & ] / g, escapeChar ) ;
48
-
49
- let escapeChar = a => ESC [ a ] || a ;
50
-
51
- let falsey = v => v == null || v === false ;
52
-
53
- let memoize = ( fn , mem = { } ) => v => mem [ v ] || ( mem [ v ] = fn ( v ) ) ;
54
-
55
- let indent = ( s , char ) => String ( s ) . replace ( / ( \n + ) / g, '$1' + ( char || '\t' ) ) ;
56
-
57
- let isLargeString = ( s , length , ignoreLines ) => ( String ( s ) . length > ( length || 40 ) || ( ! ignoreLines && String ( s ) . indexOf ( '\n' ) !== - 1 ) || String ( s ) . indexOf ( '<' ) !== - 1 ) ;
58
-
59
- function styleObjToCss ( s ) {
60
- let str = '' ;
61
- for ( let prop in s ) {
62
- let val = s [ prop ] ;
63
- if ( val != null ) {
64
- if ( str ) str += ' ' ;
65
- str += jsToCss ( prop ) ;
66
- str += ': ' ;
67
- str += val ;
68
- if ( typeof val === 'number' && ! NON_DIMENSION_PROPS [ prop ] ) {
69
- str += 'px' ;
70
- }
71
- str += ';' ;
72
- }
73
- }
74
- return str ;
75
- }
76
-
77
- // Convert a JavaScript camel-case CSS property name to a CSS property name
78
- let jsToCss = memoize ( s => s . replace ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) ) ;
79
-
80
- function assign ( obj , props ) {
81
- for ( let i in props ) obj [ i ] = props [ i ] ;
82
- return obj ;
83
- }
84
-
85
- function getNodeProps ( vnode ) {
86
- let defaultProps = vnode . nodeName . defaultProps ,
87
- props = assign ( { } , defaultProps || vnode . attributes ) ;
88
- if ( defaultProps ) assign ( props , vnode . attributes ) ;
89
- if ( vnode . children ) props . children = vnode . children ;
90
- return props ;
91
- }
92
-
93
- // we have to patch in Array support, Possible issue in npm.im/pretty-format
94
- let preactPlugin = {
95
- test ( object ) {
96
- return object && typeof object === 'object' && 'nodeName' in object && 'attributes' in object && 'children' in object && ! ( 'nodeType' in object ) ;
97
- } ,
98
- print ( val , print , indent ) {
99
- return renderToString ( val , preactPlugin . context , preactPlugin . opts , true ) ;
100
- }
101
- } ;
102
-
103
- let prettyFormatOpts = {
104
- plugins : [ preactPlugin ]
105
- } ;
106
27
107
28
/** Render Preact JSX + Components to an HTML string.
108
29
* @name render
@@ -186,19 +107,9 @@ export default function renderToString(vnode, context, opts, inner) {
186
107
if ( name === 'children' ) continue ;
187
108
if ( ! ( opts && opts . allAttributes ) && ( name === 'key' || name === 'ref' ) ) continue ;
188
109
189
- if ( opts . jsx ) {
190
- if ( typeof v !== 'string' ) {
191
- preactPlugin . context = context ;
192
- preactPlugin . opts = opts ;
193
- v = prettyFormat ( v , prettyFormatOpts ) ;
194
- if ( ~ v . indexOf ( '\n' ) ) {
195
- v = `${ indent ( '\n' + v , indentChar ) } \n` ;
196
- }
197
- s += indent ( `\n${ name } ={${ v } }` , indentChar ) ;
198
- }
199
- else {
200
- s += `\n${ indentChar } ${ name } ="${ encodeEntities ( v ) } "` ;
201
- }
110
+ let hooked = opts . attributeHook && opts . attributeHook ( name , v , context , opts ) ;
111
+ if ( hooked ) {
112
+ s += hooked ;
202
113
continue ;
203
114
}
204
115
0 commit comments