MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / _morphChildren

Function _morphChildren

www/js/_hyperscript.esm.js:3561–3622  ·  view source on GitHub ↗
(ctx, oldParent, newParent, insertionPoint = null, endPoint = null)

Source from the content-addressed store, hash-verified

3559 }
3560};
3561function _morphChildren(ctx, oldParent, newParent, insertionPoint = null, endPoint = null) {
3562 if (oldParent instanceof HTMLTemplateElement && newParent instanceof HTMLTemplateElement) {
3563 oldParent = oldParent.content;
3564 newParent = newParent.content;
3565 }
3566 insertionPoint ||= oldParent.firstChild;
3567 let newChild = newParent.firstChild;
3568 while (newChild) {
3569 let matchedNode;
3570 if (insertionPoint && insertionPoint !== endPoint) {
3571 matchedNode = _findBestMatch(ctx, newChild, insertionPoint, endPoint);
3572 if (matchedNode && matchedNode !== insertionPoint) {
3573 let cursor = insertionPoint;
3574 while (cursor && cursor !== matchedNode) {
3575 let tempNode = cursor;
3576 cursor = cursor.nextSibling;
3577 if (tempNode instanceof Element && (ctx.idMap.has(tempNode) || _matchesUpcomingSibling(ctx, tempNode, newChild))) {
3578 _moveBefore(oldParent, tempNode, endPoint);
3579 } else {
3580 _removeNode(ctx, tempNode);
3581 }
3582 }
3583 }
3584 }
3585 if (!matchedNode && newChild instanceof Element && ctx.persistentIds.has(newChild.id)) {
3586 let escapedId = CSS.escape(newChild.id);
3587 matchedNode = ctx.target.id === newChild.id && ctx.target || ctx.target.querySelector('[id="' + escapedId + '"]') || ctx.pantry.querySelector('[id="' + escapedId + '"]');
3588 let element = matchedNode;
3589 while (element = element.parentNode) {
3590 let idSet = ctx.idMap.get(element);
3591 if (idSet) {
3592 idSet.delete(matchedNode.id);
3593 if (!idSet.size) ctx.idMap.delete(element);
3594 }
3595 }
3596 _moveBefore(oldParent, matchedNode, insertionPoint);
3597 }
3598 if (matchedNode) {
3599 _morphNode(matchedNode, newChild, ctx);
3600 insertionPoint = matchedNode.nextSibling;
3601 newChild = newChild.nextSibling;
3602 continue;
3603 }
3604 let nextNewChild = newChild.nextSibling;
3605 if (ctx.idMap.has(newChild)) {
3606 let placeholder = document.createElement(newChild.tagName);
3607 oldParent.insertBefore(placeholder, insertionPoint);
3608 _morphNode(placeholder, newChild, ctx);
3609 insertionPoint = placeholder.nextSibling;
3610 } else {
3611 oldParent.insertBefore(newChild, insertionPoint);
3612 ctx.callbacks.afterNodeAdded?.(newChild);
3613 insertionPoint = newChild.nextSibling;
3614 }
3615 newChild = nextNewChild;
3616 }
3617 while (insertionPoint && insertionPoint !== endPoint) {
3618 let tempNode = insertionPoint;

Callers 2

morphMethod · 0.70
_morphNodeFunction · 0.70

Calls 7

_findBestMatchFunction · 0.70
_matchesUpcomingSiblingFunction · 0.70
_moveBeforeFunction · 0.70
_removeNodeFunction · 0.70
_morphNodeFunction · 0.70
getMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected