Skip to content

Commit

Permalink
Merge pull request hotwired#1225 from hcdeng/bugfix/enable-turbo-stre…
Browse files Browse the repository at this point in the history
…am-morph-on-text

Bugfix/enable turbo stream morph on text
  • Loading branch information
jorgemanrubia authored Mar 28, 2024
2 parents 600203e + 9c2bd18 commit 9fb05e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/core/streams/actions/morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions src/tests/unit/stream_element_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<div id="hello">Hello Turbo Morphed</div>`)
const element = createStreamElement("morph", "hello", templateElement)

assert.equal(subject.find("div#hello")?.textContent, "Hello Turbo")

subject.append(element)
await nextAnimationFrame()

assert.ok(subject.find("div#hello"))
assert.equal(subject.find("div#hello")?.textContent, "Hello Turbo Morphed")
})

test("action=morph children-only", async () => {
const templateElement = createTemplateElement(`<h1 id="hello-child-element">Hello Turbo Morphed</h1>`)
const element = createStreamElement("morph", "hello", templateElement)
Expand Down

0 comments on commit 9fb05e3

Please sign in to comment.