@@ -41,12 +41,12 @@ function init_hydrate(target: NodeEx) {
41
41
let children : ArrayLike < NodeEx2 > = target . childNodes as NodeListOf < NodeEx2 > ;
42
42
43
43
// If target is head, there may be children without claim_order
44
- if ( target . nodeName . toLowerCase ( ) == " head" ) {
44
+ if ( target . nodeName . toLowerCase ( ) === ' head' ) {
45
45
const myChildren = [ ] ;
46
46
for ( let i = 0 ; i < children . length ; i ++ ) {
47
47
const node = children [ i ] ;
48
48
if ( node . claim_order !== undefined ) {
49
- myChildren . push ( node )
49
+ myChildren . push ( node ) ;
50
50
}
51
51
}
52
52
children = myChildren ;
@@ -332,7 +332,7 @@ function init_claim_info(nodes: ChildNodeArray) {
332
332
}
333
333
}
334
334
335
- function claim_node < R extends ChildNodeEx > ( nodes : ChildNodeArray , predicate : ( node : ChildNodeEx ) => node is R , processNode : ( node : ChildNodeEx ) => void , createNode : ( ) => R , dontUpdateLastIndex : boolean = false ) {
335
+ function claim_node < R extends ChildNodeEx > ( nodes : ChildNodeArray , predicate : ( node : ChildNodeEx ) => node is R , processNode : ( node : ChildNodeEx ) => ChildNodeEx | undefined , createNode : ( ) => R , dontUpdateLastIndex : boolean = false ) {
336
336
// Try to find nodes in an order such that we lengthen the longest increasing subsequence
337
337
init_claim_info ( nodes ) ;
338
338
@@ -342,9 +342,13 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
342
342
const node = nodes [ i ] ;
343
343
344
344
if ( predicate ( node ) ) {
345
- processNode ( node ) ;
345
+ const replacement = processNode ( node ) ;
346
346
347
- nodes . splice ( i , 1 ) ;
347
+ if ( replacement === undefined ) {
348
+ nodes . splice ( i , 1 ) ;
349
+ } else {
350
+ nodes [ i ] = replacement ;
351
+ }
348
352
if ( ! dontUpdateLastIndex ) {
349
353
nodes . claim_info . last_index = i ;
350
354
}
@@ -359,12 +363,16 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
359
363
const node = nodes [ i ] ;
360
364
361
365
if ( predicate ( node ) ) {
362
- processNode ( node ) ;
366
+ const replacement = processNode ( node ) ;
363
367
364
- nodes . splice ( i , 1 ) ;
368
+ if ( replacement === undefined ) {
369
+ nodes . splice ( i , 1 ) ;
370
+ } else {
371
+ nodes [ i ] = replacement ;
372
+ }
365
373
if ( ! dontUpdateLastIndex ) {
366
374
nodes . claim_info . last_index = i ;
367
- } else {
375
+ } else if ( replacement === undefined ) {
368
376
// Since we spliced before the last_index, we decrease it
369
377
nodes . claim_info . last_index -- ;
370
378
}
@@ -394,6 +402,7 @@ export function claim_element(nodes: ChildNodeArray, name: string, attributes: {
394
402
}
395
403
}
396
404
remove . forEach ( v => node . removeAttribute ( v ) ) ;
405
+ return undefined ;
397
406
} ,
398
407
( ) => svg ? svg_element ( name as keyof SVGElementTagNameMap ) : element ( name as keyof HTMLElementTagNameMap )
399
408
) ;
@@ -404,7 +413,14 @@ export function claim_text(nodes: ChildNodeArray, data) {
404
413
nodes ,
405
414
( node : ChildNode ) : node is Text => node . nodeType === 3 ,
406
415
( node : Text ) => {
407
- node . data = '' + data ;
416
+ const dataStr = '' + data ;
417
+ if ( node . data . startsWith ( dataStr ) ) {
418
+ if ( node . data . length !== dataStr . length ) {
419
+ return node . splitText ( dataStr . length ) ;
420
+ }
421
+ } else {
422
+ node . data = dataStr ;
423
+ }
408
424
} ,
409
425
( ) => text ( data ) ,
410
426
true // Text nodes should not update last index since it is likely not worth it to eliminate an increasing subsequence of actual elements
0 commit comments