Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand Down
45 changes: 42 additions & 3 deletions test/two-pass.js
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(`
<div>
<p data-preserve-me="true" id="first">First paragraph</p>
<p data-preserve-me="true" id="second">Second paragraph</p>
</div>
`),
);
document.getElementById("first").innerHTML = "First paragraph EDITED";
document.getElementById("second").innerHTML = "Second paragraph EDITED";

let finalSrc = `
<div>
<p data-preserve-me="true" id="second">Second paragraph</p>
<p data-preserve-me="true" id="first">First paragraph</p>
</div>
`;

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(`
<div>
<p data-preserve-me="true" id="second">Second paragraph EDITED</p>
<p data-preserve-me="true" id="first">First paragraph EDITED</p>
</div>
`);
});
});