4
4
UNSAFE_NAME ,
5
5
NAMESPACE_REPLACE_REGEX ,
6
6
HTML_LOWER_CASE ,
7
- SVG_CAMEL_CASE
7
+ SVG_CAMEL_CASE ,
8
+ createComponent
8
9
} from './lib/util.js' ;
9
10
import { options , h , Fragment } from 'preact' ;
10
11
import {
@@ -22,6 +23,7 @@ import {
22
23
CATCH_ERROR
23
24
} from './lib/constants.js' ;
24
25
26
+ const EMPTY_OBJ = { } ;
25
27
const EMPTY_ARR = [ ] ;
26
28
const isArray = Array . isArray ;
27
29
const assign = Object . assign ;
@@ -147,13 +149,6 @@ export async function renderToStringAsync(vnode, context) {
147
149
}
148
150
}
149
151
150
- // Installed as setState/forceUpdate for function components
151
- function markAsDirty ( ) {
152
- this . __d = true ;
153
- }
154
-
155
- const EMPTY_OBJ = { } ;
156
-
157
152
/**
158
153
* @param {VNode } vnode
159
154
* @param {Record<string, unknown> } context
@@ -238,9 +233,9 @@ function _renderToString(
238
233
239
234
let vnodeType = typeof vnode ;
240
235
// Text VNodes: escape as HTML
241
- if ( vnodeType !== 'object' ) {
242
- if ( vnodeType === 'function' ) return EMPTY_STR ;
243
- return vnodeType === 'string' ? encodeEntities ( vnode ) : vnode + EMPTY_STR ;
236
+ if ( vnodeType != 'object' ) {
237
+ if ( vnodeType == 'function' ) return EMPTY_STR ;
238
+ return vnodeType == 'string' ? encodeEntities ( vnode ) : vnode + EMPTY_STR ;
244
239
}
245
240
246
241
// Recurse into children / Arrays
@@ -250,7 +245,7 @@ function _renderToString(
250
245
parent [ CHILDREN ] = vnode ;
251
246
for ( let i = 0 ; i < vnode . length ; i ++ ) {
252
247
let child = vnode [ i ] ;
253
- if ( child == null || typeof child === 'boolean' ) continue ;
248
+ if ( child == null || typeof child == 'boolean' ) continue ;
254
249
255
250
const childRender = _renderToString (
256
251
child ,
@@ -262,10 +257,12 @@ function _renderToString(
262
257
renderer
263
258
) ;
264
259
265
- if ( typeof childRender === 'string' ) {
260
+ if ( typeof childRender == 'string' ) {
266
261
rendered = rendered + childRender ;
267
262
} else {
268
- renderArray = renderArray || [ ] ;
263
+ if ( ! renderArray ) {
264
+ renderArray = [ ] ;
265
+ }
269
266
270
267
if ( rendered ) renderArray . push ( rendered ) ;
271
268
@@ -294,14 +291,14 @@ function _renderToString(
294
291
if ( beforeDiff ) beforeDiff ( vnode ) ;
295
292
296
293
let type = vnode . type ,
297
- props = vnode . props ,
298
- cctx = context ,
299
- contextType ,
300
- rendered ,
301
- component ;
294
+ props = vnode . props ;
302
295
303
296
// Invoke rendering on Components
304
- if ( typeof type === 'function' ) {
297
+ if ( typeof type == 'function' ) {
298
+ let cctx = context ,
299
+ contextType ,
300
+ rendered ,
301
+ component ;
305
302
if ( type === Fragment ) {
306
303
// Serialized precompiled JSX.
307
304
if ( 'tpl' in props ) {
@@ -315,7 +312,7 @@ function _renderToString(
315
312
316
313
// Check if we're dealing with a vnode or an array of nodes
317
314
if (
318
- typeof value === 'object' &&
315
+ typeof value == 'object' &&
319
316
( value . constructor === undefined || isArray ( value ) )
320
317
) {
321
318
out =
@@ -340,9 +337,7 @@ function _renderToString(
340
337
} else if ( 'UNSTABLE_comment' in props ) {
341
338
// Fragments are the least used components of core that's why
342
339
// branching here for comments has the least effect on perf.
343
- return (
344
- '<!--' + encodeEntities ( props . UNSTABLE_comment || EMPTY_STR ) + '-->'
345
- ) ;
340
+ return '<!--' + encodeEntities ( props . UNSTABLE_comment ) + '-->' ;
346
341
}
347
342
348
343
rendered = props . children ;
@@ -354,22 +349,15 @@ function _renderToString(
354
349
}
355
350
356
351
let isClassComponent =
357
- type . prototype && typeof type . prototype . render === 'function' ;
352
+ type . prototype && typeof type . prototype . render == 'function' ;
358
353
if ( isClassComponent ) {
359
354
rendered = /**#__NOINLINE__**/ renderClassComponent ( vnode , cctx ) ;
360
355
component = vnode [ COMPONENT ] ;
361
356
} else {
362
- vnode [ COMPONENT ] = component = {
363
- __v : vnode ,
364
- props,
365
- context : cctx ,
366
- // silently drop state updates
367
- setState : markAsDirty ,
368
- forceUpdate : markAsDirty ,
369
- __d : true ,
370
- // hooks
371
- __h : [ ]
372
- } ;
357
+ vnode [ COMPONENT ] = component = /**#__NOINLINE__**/ createComponent (
358
+ vnode ,
359
+ cctx
360
+ ) ;
373
361
374
362
// If a hook invokes setState() to invalidate the component during rendering,
375
363
// re-render it up to 25 times to allow "settling" of memoized states.
@@ -402,7 +390,7 @@ function _renderToString(
402
390
rendered != null &&
403
391
rendered . type === Fragment &&
404
392
rendered . key == null &&
405
- ! ( 'tpl' in rendered . props ) ;
393
+ rendered . props . tpl == null ;
406
394
rendered = isTopLevelFragment ? rendered . props . children : rendered ;
407
395
408
396
try {
@@ -416,8 +404,6 @@ function _renderToString(
416
404
renderer
417
405
) ;
418
406
} catch ( err ) {
419
- let str = EMPTY_STR ;
420
-
421
407
if ( type . getDerivedStateFromError ) {
422
408
component [ NEXT_STATE ] = type . getDerivedStateFromError ( err ) ;
423
409
}
@@ -438,10 +424,10 @@ function _renderToString(
438
424
rendered != null &&
439
425
rendered . type === Fragment &&
440
426
rendered . key == null &&
441
- ! ( 'tpl' in rendered . props ) ;
427
+ rendered . props . tpl == null ;
442
428
rendered = isTopLevelFragment ? rendered . props . children : rendered ;
443
429
444
- str = _renderToString (
430
+ return _renderToString (
445
431
rendered ,
446
432
context ,
447
433
isSvgMode ,
@@ -452,7 +438,7 @@ function _renderToString(
452
438
) ;
453
439
}
454
440
455
- return str ;
441
+ return EMPTY_STR ;
456
442
} finally {
457
443
if ( afterDiff ) afterDiff ( vnode ) ;
458
444
vnode [ PARENT ] = null ;
@@ -468,7 +454,7 @@ function _renderToString(
468
454
rendered != null &&
469
455
rendered . type === Fragment &&
470
456
rendered . key == null &&
471
- ! ( 'tpl' in rendered . props ) ;
457
+ rendered . props . tpl == null ;
472
458
rendered = isTopLevelFragment ? rendered . props . children : rendered ;
473
459
474
460
try {
@@ -513,7 +499,7 @@ function _renderToString(
513
499
514
500
if ( ! asyncMode ) throw error ;
515
501
516
- if ( ! error || typeof error . then !== 'function' ) throw error ;
502
+ if ( ! error || typeof error . then != 'function' ) throw error ;
517
503
518
504
const renderNestedChildren = ( ) => {
519
505
try {
@@ -527,7 +513,7 @@ function _renderToString(
527
513
renderer
528
514
) ;
529
515
} catch ( e ) {
530
- if ( ! e || typeof e . then !== 'function' ) throw e ;
516
+ if ( ! e || typeof e . then != 'function' ) throw e ;
531
517
532
518
return e . then (
533
519
( ) =>
@@ -557,7 +543,7 @@ function _renderToString(
557
543
for ( let name in props ) {
558
544
let v = props [ name ] ;
559
545
560
- if ( typeof v === 'function' ) continue ;
546
+ if ( typeof v == 'function' ) continue ;
561
547
562
548
switch ( name ) {
563
549
case 'children' :
@@ -663,7 +649,7 @@ function _renderToString(
663
649
' ' +
664
650
name +
665
651
'="' +
666
- ( typeof v === 'string' ? encodeEntities ( v ) : v + EMPTY_STR ) +
652
+ ( typeof v == 'string' ? encodeEntities ( v ) : v + EMPTY_STR ) +
667
653
'"' ;
668
654
}
669
655
}
@@ -711,7 +697,7 @@ function _renderToString(
711
697
const startTag = s + '>' ;
712
698
713
699
if ( isArray ( html ) ) return [ startTag , ...html , endTag ] ;
714
- else if ( typeof html !== 'string' ) return [ startTag , html , endTag ] ;
700
+ else if ( typeof html != 'string' ) return [ startTag , html , endTag ] ;
715
701
return startTag + html + endTag ;
716
702
}
717
703
0 commit comments