Skip to content

Commit 73c5145

Browse files
committed
Improve claim_text
Split an existing text node if we match the prefix
1 parent 157ab2f commit 73c5145

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/runtime/internal/dom.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ function init_claim_info(nodes: ChildNodeArray) {
325325
}
326326
}
327327

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) {
329329
// Try to find nodes in an order such that we lengthen the longest increasing subsequence
330330
init_claim_info(nodes);
331331

@@ -335,9 +335,13 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
335335
const node = nodes[i];
336336

337337
if (predicate(node)) {
338-
processNode(node);
338+
const replacement = processNode(node);
339339

340-
nodes.splice(i, 1);
340+
if (replacement === undefined) {
341+
nodes.splice(i, 1);
342+
} else {
343+
nodes[i] = replacement;
344+
}
341345
if (!dontUpdateLastIndex) {
342346
nodes.claim_info.last_index = i;
343347
}
@@ -352,12 +356,16 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
352356
const node = nodes[i];
353357

354358
if (predicate(node)) {
355-
processNode(node);
359+
const replacement = processNode(node);
356360

357-
nodes.splice(i, 1);
361+
if (replacement === undefined) {
362+
nodes.splice(i, 1);
363+
} else {
364+
nodes[i] = replacement;
365+
}
358366
if (!dontUpdateLastIndex) {
359367
nodes.claim_info.last_index = i;
360-
} else {
368+
} else if (replacement === undefined) {
361369
// Since we spliced before the last_index, we decrease it
362370
nodes.claim_info.last_index--;
363371
}
@@ -387,6 +395,7 @@ export function claim_element(nodes: ChildNodeArray, name: string, attributes: {
387395
}
388396
}
389397
remove.forEach(v => node.removeAttribute(v));
398+
return undefined;
390399
},
391400
() => svg ? svg_element(name as keyof SVGElementTagNameMap) : element(name as keyof HTMLElementTagNameMap)
392401
);
@@ -397,7 +406,14 @@ export function claim_text(nodes: ChildNodeArray, data) {
397406
nodes,
398407
(node: ChildNode): node is Text => node.nodeType === 3,
399408
(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+
}
401417
},
402418
() => text(data),
403419
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

Comments
 (0)