Skip to content

Commit

Permalink
tons of misc fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
botandrose-machine committed Dec 13, 2024
1 parent a8a45e7 commit d6bc2d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ var Idiomorph = (function () {

// innerHTML, so we are only updating the children
morphChildren(normalizedNewContent, oldNode, ctx);
if (ctx.twoPass) {
if (ctx.pantry) {
restoreFromPantry(oldNode, ctx);
}
return Array.from(oldNode.children);
Expand Down Expand Up @@ -1075,16 +1075,14 @@ var Idiomorph = (function () {
removeIdsFromConsideration(ctx, tempNode)
if (ctx.callbacks.beforeNodeRemoved(tempNode) === false) return;
if (ctx.pantry) {
moveToPantry(tempNode.pantry, ctx);
moveToPantry(tempNode, ctx);
} else {
tempNode.parentNode?.removeChild(tempNode);
}
ctx.callbacks.afterNodeRemoved(tempNode);
}

function moveToPantry(node, ctx) {
if (!node) return;

// If the node is a leaf (no children), process it, and then we're done
if (!node.children || node.children.length === 0) {
if (node.id) {
Expand All @@ -1099,19 +1097,20 @@ var Idiomorph = (function () {

// After processing children, process the current node
if (node.id) {
node.innerHTML = '';
ctx.pantry.appendChild(node);
}
}
}

function restoreFromPantry(root, ctx) {
Array.from(ctx.pantry.children).forEach(element => {
const matchElement = root.findElementById(element.id);
const matchElement = root.querySelector(`#${element.id}`);
if (matchElement) {
matchElement.before(element);
element.replaceChildren(matchElement.childNodes);
syncNodeFrom(matchElement, element, ctx);
matchElement.remove();
syncNodeFrom(newContent, oldNode, ctx);
}
});
ctx.pantry.remove();
Expand Down
8 changes: 4 additions & 4 deletions test/two-pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe("Two-pass option for retaining more state", function(){
`;
Idiomorph.morph(getWorkArea(), finalSrc, {morphStyle:'innerHTML'});

document.getElementById("first").indeterminate.should.be.false
document.getElementById("second").indeterminate.should.be.true
const states = Array.from(getWorkArea().querySelectorAll("input")).map(e => e.indeterminate);
states.should.eql([true, false]);
});

it('preserves all non-attribute element state with two-pass option', function()
Expand All @@ -46,7 +46,7 @@ describe("Two-pass option for retaining more state", function(){
`;
Idiomorph.morph(getWorkArea(), finalSrc, {morphStyle:'innerHTML',twoPass:true});

document.getElementById("first").indeterminate.should.be.true
document.getElementById("second").indeterminate.should.be.true
const states = Array.from(getWorkArea().querySelectorAll("input")).map(e => e.indeterminate);
states.should.eql([true, true]);
});
});

0 comments on commit d6bc2d3

Please sign in to comment.