1
+ import prettyFormat from 'pretty-format' ;
1
2
2
3
const SHALLOW = { shallow : true } ;
3
4
@@ -53,7 +54,7 @@ let memoize = (fn, mem={}) => v => mem[v] || (mem[v] = fn(v));
53
54
54
55
let indent = ( s , char ) => String ( s ) . replace ( / ( \n + ) / g, '$1' + ( char || '\t' ) ) ;
55
56
56
- let isLargeString = s => ( String ( s ) . length > 40 || String ( s ) . indexOf ( '\n' ) !== - 1 || String ( s ) . indexOf ( '<' ) !== - 1 ) ;
57
+ let isLargeString = ( s , length , ignoreLines ) => ( String ( s ) . length > ( length || 40 ) || ( ! ignoreLines && String ( s ) . indexOf ( '\n' ) !== - 1 ) || String ( s ) . indexOf ( '<' ) !== - 1 ) ;
57
58
58
59
function styleObjToCss ( s ) {
59
60
let str = '' ;
@@ -89,6 +90,27 @@ function getNodeProps(vnode) {
89
90
return props ;
90
91
}
91
92
93
+ // we have to patch in Array support, Possible issue in npm.im/pretty-format
94
+ let preactPlugin = {
95
+ test ( object ) {
96
+ if ( Array . isArray ( object ) ) {
97
+ return preactPlugin . test ( object [ 0 ] ) ;
98
+ }
99
+ // return object && Object.prototype.toString.call(object)==='[object VNode]';
100
+ return object && typeof object === 'object' && 'nodeName' in object && 'attributes' in object && 'children' in object ;
101
+ } ,
102
+ print ( val , print , indent ) {
103
+ if ( Array . isArray ( val ) ) {
104
+ return 'Array [\n ' + val . map ( v => preactPlugin . test ( v ) ? renderToString ( v , preactPlugin . context , preactPlugin . opts , true ) : v ) . join ( ',\n ' ) + '\n]' ;
105
+ }
106
+ return renderToString ( val , preactPlugin . context , preactPlugin . opts , true ) ;
107
+ }
108
+ } ;
109
+
110
+ let prettyFormatOpts = {
111
+ plugins : [ preactPlugin ]
112
+ } ;
113
+
92
114
/** Render Preact JSX + Components to an HTML string.
93
115
* @name render
94
116
* @function
@@ -165,8 +187,7 @@ export default function renderToString(vnode, context, opts, inner) {
165
187
}
166
188
167
189
// render JSX to HTML
168
- let s = `<${ nodeName } ` ,
169
- html ;
190
+ let s = '' , html ;
170
191
171
192
if ( attributes ) {
172
193
let attrs = objectKeys ( attributes ) ;
@@ -179,13 +200,31 @@ export default function renderToString(vnode, context, opts, inner) {
179
200
v = attributes [ name ] ;
180
201
if ( name === 'children' ) continue ;
181
202
if ( ! ( opts && opts . allAttributes ) && ( name === 'key' || name === 'ref' ) ) continue ;
203
+
204
+ if ( opts . jsx ) {
205
+ if ( typeof v !== 'string' ) {
206
+ preactPlugin . context = context ;
207
+ preactPlugin . opts = opts ;
208
+ v = prettyFormat ( v , prettyFormatOpts ) ;
209
+ if ( ~ v . indexOf ( '\n' ) ) {
210
+ v = `${ indent ( '\n' + v , indentChar ) } \n` ;
211
+ }
212
+ s += indent ( `\n${ name } ={${ v } }` , indentChar ) ;
213
+ }
214
+ else {
215
+ s += `\n${ indentChar } ${ name } ="${ encodeEntities ( v ) } "` ;
216
+ }
217
+ continue ;
218
+ }
219
+
182
220
if ( name === 'className' ) {
183
221
if ( attributes [ 'class' ] ) continue ;
184
222
name = 'class' ;
185
223
}
186
224
if ( name === 'style' && v && typeof v === 'object' ) {
187
225
v = styleObjToCss ( v ) ;
188
226
}
227
+
189
228
if ( name === 'dangerouslySetInnerHTML' ) {
190
229
html = v && v . __html ;
191
230
}
@@ -203,7 +242,25 @@ export default function renderToString(vnode, context, opts, inner) {
203
242
}
204
243
}
205
244
206
- s += '>' ;
245
+ // account for >1 multiline attribute
246
+ let sub = s . replace ( / ^ \n \s * / , ' ' ) ;
247
+ if ( sub !== s && ! ~ sub . indexOf ( '\n' ) ) s = sub ;
248
+ else if ( ~ s . indexOf ( '\n' ) ) s += '\n' ;
249
+ // s += '>';
250
+
251
+ s = `<${ nodeName } ${ s } >` ;
252
+
253
+ // if (opts && opts.jsx) {
254
+ // if (isLargeString(s, 80, true) || s.split('\n').length>3) {
255
+ // s = `<${nodeName}${s}\n>`;
256
+ // }
257
+ // else {
258
+ // s = `<${nodeName}${s}\n>`;
259
+ // }
260
+ // }
261
+ // else {
262
+ // s = `<${nodeName}${s}>`;
263
+ // }
207
264
208
265
if ( html ) {
209
266
// if multiline, indent.
@@ -216,7 +273,7 @@ export default function renderToString(vnode, context, opts, inner) {
216
273
let len = children && children . length ;
217
274
if ( len ) {
218
275
let pieces = [ ] ,
219
- hasLarge = false ;
276
+ hasLarge = ~ s . indexOf ( '\n' ) ;
220
277
for ( let i = 0 ; i < len ; i ++ ) {
221
278
let child = children [ i ] ;
222
279
if ( ! falsey ( child ) ) {
0 commit comments