diff --git a/src/core/streams/actions/morph.js b/src/core/streams/actions/morph.js index d177bfd01..42e655c95 100644 --- a/src/core/streams/actions/morph.js +++ b/src/core/streams/actions/morph.js @@ -26,17 +26,19 @@ function beforeNodeRemoved(node) { } function beforeNodeMorphed(target, newElement) { - if (target instanceof HTMLElement && !target.hasAttribute("data-turbo-permanent")) { - const event = dispatch("turbo:before-morph-element", { - cancelable: true, - target, - detail: { - newElement - } - }) - return !event.defaultPrevented + if (target instanceof HTMLElement) { + if (!target.hasAttribute("data-turbo-permanent")) { + const event = dispatch("turbo:before-morph-element", { + cancelable: true, + target, + detail: { + newElement + } + }) + return !event.defaultPrevented + } + return false } - return false } function beforeAttributeUpdated(attributeName, target, mutationType) { diff --git a/src/tests/unit/stream_element_tests.js b/src/tests/unit/stream_element_tests.js index 1e3b99f92..0d3e04b61 100644 --- a/src/tests/unit/stream_element_tests.js +++ b/src/tests/unit/stream_element_tests.js @@ -210,6 +210,19 @@ test("action=morph", async () => { assert.equal(subject.find("h1#hello")?.textContent, "Hello Turbo Morphed") }) +test("action=morph with text content change", async () => { + const templateElement = createTemplateElement(`