Skip to content

Commit 9fb05e3

Browse files
Merge pull request hotwired#1225 from hcdeng/bugfix/enable-turbo-stream-morph-on-text
Bugfix/enable turbo stream morph on text
2 parents 600203e + 9c2bd18 commit 9fb05e3

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/core/streams/actions/morph.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ function beforeNodeRemoved(node) {
2626
}
2727

2828
function beforeNodeMorphed(target, newElement) {
29-
if (target instanceof HTMLElement && !target.hasAttribute("data-turbo-permanent")) {
30-
const event = dispatch("turbo:before-morph-element", {
31-
cancelable: true,
32-
target,
33-
detail: {
34-
newElement
35-
}
36-
})
37-
return !event.defaultPrevented
29+
if (target instanceof HTMLElement) {
30+
if (!target.hasAttribute("data-turbo-permanent")) {
31+
const event = dispatch("turbo:before-morph-element", {
32+
cancelable: true,
33+
target,
34+
detail: {
35+
newElement
36+
}
37+
})
38+
return !event.defaultPrevented
39+
}
40+
return false
3841
}
39-
return false
4042
}
4143

4244
function beforeAttributeUpdated(attributeName, target, mutationType) {

src/tests/unit/stream_element_tests.js

+13
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ test("action=morph", async () => {
210210
assert.equal(subject.find("h1#hello")?.textContent, "Hello Turbo Morphed")
211211
})
212212

213+
test("action=morph with text content change", async () => {
214+
const templateElement = createTemplateElement(`<div id="hello">Hello Turbo Morphed</div>`)
215+
const element = createStreamElement("morph", "hello", templateElement)
216+
217+
assert.equal(subject.find("div#hello")?.textContent, "Hello Turbo")
218+
219+
subject.append(element)
220+
await nextAnimationFrame()
221+
222+
assert.ok(subject.find("div#hello"))
223+
assert.equal(subject.find("div#hello")?.textContent, "Hello Turbo Morphed")
224+
})
225+
213226
test("action=morph children-only", async () => {
214227
const templateElement = createTemplateElement(`<h1 id="hello-child-element">Hello Turbo Morphed</h1>`)
215228
const element = createStreamElement("morph", "hello", templateElement)

0 commit comments

Comments
 (0)