7
7
XLINK ,
8
8
VOID_ELEMENTS
9
9
} from './util' ;
10
- import { options , Fragment } from 'preact' ;
10
+ import { options , h , Fragment } from 'preact' ;
11
11
import { _renderToStringPretty } from './pretty' ;
12
12
import {
13
13
COMMIT ,
@@ -16,9 +16,11 @@ import {
16
16
DIFFED ,
17
17
DIRTY ,
18
18
NEXT_STATE ,
19
+ PARENT ,
19
20
RENDER ,
20
21
SKIP_EFFECTS ,
21
- VNODE
22
+ VNODE ,
23
+ CHILDREN
22
24
} from './constants' ;
23
25
24
26
/** @typedef {import('preact').VNode } VNode */
@@ -59,6 +61,9 @@ function renderToString(vnode, context, opts) {
59
61
const previousSkipEffects = options [ SKIP_EFFECTS ] ;
60
62
options [ SKIP_EFFECTS ] = true ;
61
63
64
+ const parent = h ( Fragment , null ) ;
65
+ parent [ CHILDREN ] = [ vnode ] ;
66
+
62
67
let res ;
63
68
if (
64
69
opts &&
@@ -72,7 +77,7 @@ function renderToString(vnode, context, opts) {
72
77
) {
73
78
res = _renderToStringPretty ( vnode , context , opts ) ;
74
79
} else {
75
- res = _renderToString ( vnode , context , false , undefined ) ;
80
+ res = _renderToString ( vnode , context , false , undefined , parent ) ;
76
81
}
77
82
78
83
// options._commit, we don't schedule any effects in this library right now,
@@ -181,7 +186,7 @@ const isArray = Array.isArray;
181
186
const assign = Object . assign ;
182
187
183
188
/** The default export is an alias of `render()`. */
184
- function _renderToString ( vnode , context , isSvgMode , selectValue ) {
189
+ function _renderToString ( vnode , context , isSvgMode , selectValue , parent ) {
185
190
// Ignore non-rendered VNodes/values
186
191
if ( vnode == null || vnode === true || vnode === false || vnode === '' ) {
187
192
return '' ;
@@ -195,13 +200,16 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
195
200
// Recurse into children / Arrays
196
201
if ( isArray ( vnode ) ) {
197
202
let rendered = '' ;
203
+ parent [ CHILDREN ] = vnode ;
198
204
for ( let i = 0 ; i < vnode . length ; i ++ ) {
199
205
rendered =
200
- rendered + _renderToString ( vnode [ i ] , context , isSvgMode , selectValue ) ;
206
+ rendered +
207
+ _renderToString ( vnode [ i ] , context , isSvgMode , selectValue , parent ) ;
201
208
}
202
209
return rendered ;
203
210
}
204
211
212
+ vnode [ PARENT ] = parent ;
205
213
if ( options [ DIFF ] ) options [ DIFF ] ( vnode ) ;
206
214
207
215
let type = vnode . type ,
@@ -210,30 +218,32 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
210
218
// Invoke rendering on Components
211
219
const isComponent = typeof type === 'function' ;
212
220
if ( isComponent ) {
213
- if ( type === Fragment ) {
214
- return _renderToString (
215
- vnode . props . children ,
216
- context ,
217
- isSvgMode ,
218
- selectValue
219
- ) ;
220
- }
221
-
222
221
let rendered ;
223
- if ( type . prototype && typeof type . prototype . render === 'function' ) {
224
- rendered = renderClassComponent ( vnode , context ) ;
222
+ if ( type === Fragment ) {
223
+ rendered = props . children ;
225
224
} else {
226
- rendered = renderFunctionComponent ( vnode , context ) ;
227
- }
225
+ if ( type . prototype && typeof type . prototype . render === 'function' ) {
226
+ rendered = renderClassComponent ( vnode , context ) ;
227
+ } else {
228
+ rendered = renderFunctionComponent ( vnode , context ) ;
229
+ }
228
230
229
- let component = vnode [ COMPONENT ] ;
230
- if ( component . getChildContext ) {
231
- context = assign ( { } , context , component . getChildContext ( ) ) ;
231
+ let component = vnode [ COMPONENT ] ;
232
+ if ( component . getChildContext ) {
233
+ context = assign ( { } , context , component . getChildContext ( ) ) ;
234
+ }
232
235
}
233
236
234
237
// Recurse into children before invoking the after-diff hook
235
- const str = _renderToString ( rendered , context , isSvgMode , selectValue ) ;
238
+ const str = _renderToString (
239
+ rendered ,
240
+ context ,
241
+ isSvgMode ,
242
+ selectValue ,
243
+ vnode
244
+ ) ;
236
245
if ( options [ DIFFED ] ) options [ DIFFED ] ( vnode ) ;
246
+ vnode [ PARENT ] = undefined ;
237
247
return str ;
238
248
}
239
249
@@ -314,13 +324,19 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
314
324
pieces = pieces + encodeEntities ( children ) ;
315
325
hasChildren = true ;
316
326
} else if ( isArray ( children ) ) {
327
+ vnode [ CHILDREN ] = children ;
317
328
for ( let i = 0 ; i < children . length ; i ++ ) {
318
329
let child = children [ i ] ;
319
-
320
330
if ( child != null && child !== false ) {
321
331
let childSvgMode =
322
332
type === 'svg' || ( type !== 'foreignObject' && isSvgMode ) ;
323
- let ret = _renderToString ( child , context , childSvgMode , selectValue ) ;
333
+ let ret = _renderToString (
334
+ child ,
335
+ context ,
336
+ childSvgMode ,
337
+ selectValue ,
338
+ vnode
339
+ ) ;
324
340
325
341
// Skip if we received an empty string
326
342
if ( ret ) {
@@ -330,9 +346,16 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
330
346
}
331
347
}
332
348
} else if ( children != null && children !== false && children !== true ) {
349
+ vnode [ CHILDREN ] = [ children ] ;
333
350
let childSvgMode =
334
351
type === 'svg' || ( type !== 'foreignObject' && isSvgMode ) ;
335
- let ret = _renderToString ( children , context , childSvgMode , selectValue ) ;
352
+ let ret = _renderToString (
353
+ children ,
354
+ context ,
355
+ childSvgMode ,
356
+ selectValue ,
357
+ vnode
358
+ ) ;
336
359
337
360
// Skip if we received an empty string
338
361
if ( ret ) {
@@ -342,6 +365,7 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
342
365
}
343
366
344
367
if ( options [ DIFFED ] ) options [ DIFFED ] ( vnode ) ;
368
+ vnode [ PARENT ] = undefined ;
345
369
346
370
if ( hasChildren ) {
347
371
s = s + pieces ;
0 commit comments