From eb796d04284bba5133b48bb54c372f495b725ea9 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Mon, 23 Dec 2024 14:51:22 -0600 Subject: [PATCH 1/2] failing test for returning false to beforeNodeMorphed during pantry restore. --- test/two-pass.js | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/test/two-pass.js b/test/two-pass.js index aff806a..f702a4e 100644 --- a/test/two-pass.js +++ b/test/two-pass.js @@ -1,7 +1,5 @@ describe("Two-pass option for retaining more state", function () { - beforeEach(function () { - clearWorkArea(); - }); + setup(); it("fails to preserve all non-attribute element state with single-pass option", function () { getWorkArea().append( @@ -428,4 +426,45 @@ describe("Two-pass option for retaining more state", function () { ], ]); }); + + it("beforeNodeMorphed hook also applies to nodes restored from the pantry", function () { + getWorkArea().append( + make(` +
+

First paragraph

+

Second paragraph

+
+ `), + ); + document.getElementById("first").innerHTML = "First paragraph EDITED"; + document.getElementById("second").innerHTML = "Second paragraph EDITED"; + + let finalSrc = ` +
+

Second paragraph

+

First paragraph

+
+ `; + + Idiomorph.morph(getWorkArea(), finalSrc, { + morphStyle: "innerHTML", + twoPass: true, + callbacks: { + // basic implementation of a preserve-me attr + beforeNodePantried(node) { + if (node.parentNode?.dataset?.preserveMe) return false; + }, + beforeNodeMorphed(oldNode, newContent) { + if (oldNode.dataset?.preserveMe) return false; + }, + }, + }); + + getWorkArea().innerHTML.should.equal(` +
+

Second paragraph EDITED

+

First paragraph EDITED

+
+ `); + }); }); From 1341e04af4630fb545b80c411eb2d56859a288d3 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Mon, 23 Dec 2024 14:56:17 -0600 Subject: [PATCH 2/2] we can just reuse morphNodeTo instead of this half-working mess. --- src/idiomorph.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/idiomorph.js b/src/idiomorph.js index b2e42a9..f4c8ff9 100644 --- a/src/idiomorph.js +++ b/src/idiomorph.js @@ -1249,22 +1249,10 @@ var Idiomorph = (function () { if (matchElement.parentElement?.moveBefore) { // @ts-ignore - use proposed moveBefore feature matchElement.parentElement.moveBefore(element, matchElement); - while (matchElement.hasChildNodes()) { - // @ts-ignore - use proposed moveBefore feature - element.moveBefore(matchElement.firstChild, null); - } } else { matchElement.before(element); - while (matchElement.firstChild) { - element.insertBefore(matchElement.firstChild, null); - } - } - if ( - ctx.callbacks.beforeNodeMorphed(element, matchElement) !== false - ) { - syncNodeFrom(matchElement, element, ctx); - ctx.callbacks.afterNodeMorphed(element, matchElement); } + morphOldNodeTo(element, matchElement, ctx); matchElement.remove(); } });