Skip to content

Commit 2fbc2f8

Browse files
committed
Improve claim_text
Split an existing text node if we match the prefix
1 parent a16bb39 commit 2fbc2f8

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/runtime/internal/dom.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ function init_hydrate(target: NodeEx) {
4141
let children: ArrayLike<NodeEx2> = target.childNodes as NodeListOf<NodeEx2>;
4242

4343
// If target is head, there may be children without claim_order
44-
if (target.nodeName.toLowerCase() == "head") {
44+
if (target.nodeName.toLowerCase() === 'head') {
4545
const myChildren = [];
4646
for (let i = 0; i < children.length; i++) {
4747
const node = children[i];
4848
if (node.claim_order !== undefined) {
49-
myChildren.push(node)
49+
myChildren.push(node);
5050
}
5151
}
5252
children = myChildren;
@@ -332,7 +332,7 @@ function init_claim_info(nodes: ChildNodeArray) {
332332
}
333333
}
334334

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

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

344344
if (predicate(node)) {
345-
processNode(node);
345+
const replacement = processNode(node);
346346

347-
nodes.splice(i, 1);
347+
if (replacement === undefined) {
348+
nodes.splice(i, 1);
349+
} else {
350+
nodes[i] = replacement;
351+
}
348352
if (!dontUpdateLastIndex) {
349353
nodes.claim_info.last_index = i;
350354
}
@@ -359,12 +363,16 @@ function claim_node<R extends ChildNodeEx>(nodes: ChildNodeArray, predicate: (no
359363
const node = nodes[i];
360364

361365
if (predicate(node)) {
362-
processNode(node);
366+
const replacement = processNode(node);
363367

364-
nodes.splice(i, 1);
368+
if (replacement === undefined) {
369+
nodes.splice(i, 1);
370+
} else {
371+
nodes[i] = replacement;
372+
}
365373
if (!dontUpdateLastIndex) {
366374
nodes.claim_info.last_index = i;
367-
} else {
375+
} else if (replacement === undefined) {
368376
// Since we spliced before the last_index, we decrease it
369377
nodes.claim_info.last_index--;
370378
}
@@ -394,6 +402,7 @@ export function claim_element(nodes: ChildNodeArray, name: string, attributes: {
394402
}
395403
}
396404
remove.forEach(v => node.removeAttribute(v));
405+
return undefined;
397406
},
398407
() => svg ? svg_element(name as keyof SVGElementTagNameMap) : element(name as keyof HTMLElementTagNameMap)
399408
);
@@ -404,7 +413,14 @@ export function claim_text(nodes: ChildNodeArray, data) {
404413
nodes,
405414
(node: ChildNode): node is Text => node.nodeType === 3,
406415
(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+
}
408424
},
409425
() => text(data),
410426
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)