Skip to content

Commit

Permalink
morphOuterHTML array return similification
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelWest22 committed Jan 17, 2025
1 parent c7bc079 commit a5696e9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,28 @@ var Idiomorph = (function () {
function morphOuterHTML(ctx, oldNode, newNode) {
const oldParent = normalizeParent(oldNode);

// basis for calulating which nodes were morphed
// since there may be unmorphed sibling nodes
let childNodes = Array.from(oldParent.childNodes);
const index = childNodes.indexOf(oldNode);
// how many elements are to the right of the oldNode
const rightMargin = childNodes.length - (index + 1);
// store start and end points so we can find inbetween nodes to return later
// as we can avoid returning before or after siblings
const beforeStartPoint = oldNode.previousSibling;
const endPoint = oldNode.nextSibling;

morphChildren(
ctx,
oldParent,
newNode,
// these two optional params are the secret sauce
oldNode, // start point for iteration
oldNode.nextSibling, // end point for iteration
endPoint, // end point for iteration
);

// return just the morphed nodes
childNodes = Array.from(oldParent.childNodes);
return childNodes.slice(index, childNodes.length - rightMargin);
const nodes = []
// return array from the first node added to before the last node
let cursor = beforeStartPoint ? beforeStartPoint.nextSibling : oldParent.firstChild;
while (cursor && cursor != endPoint) {
nodes.push(cursor);
cursor = cursor.nextSibling;
}
return nodes;
}

/**
Expand Down

0 comments on commit a5696e9

Please sign in to comment.