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(); } }); 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

+
+ `); + }); });