@@ -325,7 +325,7 @@ function init_claim_info(nodes: ChildNodeArray) {
325
325
}
326
326
}
327
327
328
- function claim_node < R extends ChildNodeEx > ( nodes : ChildNodeArray , predicate : ( node : ChildNodeEx ) => node is R , processNode : ( node : ChildNodeEx ) => void , createNode : ( ) => R , dontUpdateLastIndex : boolean = false ) {
328
+ function claim_node < R extends ChildNodeEx > ( nodes : ChildNodeArray , predicate : ( node : ChildNodeEx ) => node is R , processNode : ( node : ChildNodeEx ) => ChildNodeEx | undefined , createNode : ( ) => R , dontUpdateLastIndex : boolean = false ) {
329
329
// Try to find nodes in an order such that we lengthen the longest increasing subsequence
330
330
init_claim_info ( nodes ) ;
331
331
@@ -335,9 +335,13 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
335
335
const node = nodes [ i ] ;
336
336
337
337
if ( predicate ( node ) ) {
338
- processNode ( node ) ;
338
+ const replacement = processNode ( node ) ;
339
339
340
- nodes . splice ( i , 1 ) ;
340
+ if ( replacement === undefined ) {
341
+ nodes . splice ( i , 1 ) ;
342
+ } else {
343
+ nodes [ i ] = replacement ;
344
+ }
341
345
if ( ! dontUpdateLastIndex ) {
342
346
nodes . claim_info . last_index = i ;
343
347
}
@@ -352,12 +356,16 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
352
356
const node = nodes [ i ] ;
353
357
354
358
if ( predicate ( node ) ) {
355
- processNode ( node ) ;
359
+ const replacement = processNode ( node ) ;
356
360
357
- nodes . splice ( i , 1 ) ;
361
+ if ( replacement === undefined ) {
362
+ nodes . splice ( i , 1 ) ;
363
+ } else {
364
+ nodes [ i ] = replacement ;
365
+ }
358
366
if ( ! dontUpdateLastIndex ) {
359
367
nodes . claim_info . last_index = i ;
360
- } else {
368
+ } else if ( replacement === undefined ) {
361
369
// Since we spliced before the last_index, we decrease it
362
370
nodes . claim_info . last_index -- ;
363
371
}
@@ -387,6 +395,7 @@ export function claim_element(nodes: ChildNodeArray, name: string, attributes: {
387
395
}
388
396
}
389
397
remove . forEach ( v => node . removeAttribute ( v ) ) ;
398
+ return undefined ;
390
399
} ,
391
400
( ) => svg ? svg_element ( name as keyof SVGElementTagNameMap ) : element ( name as keyof HTMLElementTagNameMap )
392
401
) ;
@@ -397,7 +406,14 @@ export function claim_text(nodes: ChildNodeArray, data) {
397
406
nodes ,
398
407
( node : ChildNode ) : node is Text => node . nodeType === 3 ,
399
408
( node : Text ) => {
400
- node . data = '' + data ;
409
+ const dataStr = '' + data ;
410
+ if ( node . data . startsWith ( dataStr ) ) {
411
+ if ( node . data . length !== dataStr . length ) {
412
+ return node . splitText ( dataStr . length ) ;
413
+ }
414
+ } else {
415
+ node . data = dataStr ;
416
+ }
401
417
} ,
402
418
( ) => text ( data ) ,
403
419
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