MCPcopy
hub / github.com/rrweb-io/rrweb / diffChildren

Function diffChildren

packages/rrdom/src/diff.ts:373–532  ·  view source on GitHub ↗
(
  oldTree: Node,
  newTree: IRRNode,
  replayer: ReplayerHandler,
  rrnodeMirror: Mirror,
)

Source from the content-addressed store, hash-verified

371}
372
373function diffChildren(
374 oldTree: Node,
375 newTree: IRRNode,
376 replayer: ReplayerHandler,
377 rrnodeMirror: Mirror,
378) {
379 const oldChildren: (Node | undefined)[] = Array.from(oldTree.childNodes);
380 const newChildren = newTree.childNodes;
381 if (oldChildren.length === 0 && newChildren.length === 0) return;
382 let oldStartIndex = 0,
383 oldEndIndex = oldChildren.length - 1,
384 newStartIndex = 0,
385 newEndIndex = newChildren.length - 1;
386 let oldStartNode = oldChildren[oldStartIndex],
387 oldEndNode = oldChildren[oldEndIndex],
388 newStartNode = newChildren[newStartIndex],
389 newEndNode = newChildren[newEndIndex];
390 let oldIdToIndex: Record<number, number> | undefined = undefined,
391 indexInOld: number | undefined = undefined;
392 while (oldStartIndex <= oldEndIndex && newStartIndex <= newEndIndex) {
393 if (oldStartNode === undefined) {
394 oldStartNode = oldChildren[++oldStartIndex];
395 } else if (oldEndNode === undefined) {
396 oldEndNode = oldChildren[--oldEndIndex];
397 } else if (
398 // same first node?
399 nodeMatching(oldStartNode, newStartNode, replayer.mirror, rrnodeMirror)
400 ) {
401 oldStartNode = oldChildren[++oldStartIndex];
402 newStartNode = newChildren[++newStartIndex];
403 } else if (
404 // same last node?
405 nodeMatching(oldEndNode, newEndNode, replayer.mirror, rrnodeMirror)
406 ) {
407 oldEndNode = oldChildren[--oldEndIndex];
408 newEndNode = newChildren[--newEndIndex];
409 } else if (
410 // is the first old node the same as the last new node?
411 nodeMatching(oldStartNode, newEndNode, replayer.mirror, rrnodeMirror)
412 ) {
413 try {
414 oldTree.insertBefore(oldStartNode, oldEndNode.nextSibling);
415 } catch (e) {
416 console.warn(e);
417 }
418 oldStartNode = oldChildren[++oldStartIndex];
419 newEndNode = newChildren[--newEndIndex];
420 } else if (
421 // is the last old node the same as the first new node?
422 nodeMatching(oldEndNode, newStartNode, replayer.mirror, rrnodeMirror)
423 ) {
424 try {
425 oldTree.insertBefore(oldEndNode, oldStartNode);
426 } catch (e) {
427 console.warn(e);
428 }
429 oldEndNode = oldChildren[--oldEndIndex];
430 newStartNode = newChildren[++newStartIndex];

Callers 2

diffFunction · 0.85

Calls 10

nodeMatchingFunction · 0.85
createOrGetNodeFunction · 0.85
diffFunction · 0.85
warnMethod · 0.80
insertBeforeMethod · 0.65
hasNodeMethod · 0.65
getIdMethod · 0.65
removeChildMethod · 0.65
removeNodeFromMapMethod · 0.65
getNodeMethod · 0.65

Tested by

no test coverage detected