Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where Idiomorph sometimes ignores data-turbo-permanent #1321

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"chai": "~4.3.4",
"eslint": "^8.13.0",
"express": "^4.18.2",
"idiomorph": "https://github.com/bigskysoftware/idiomorph.git",
"idiomorph": "https://github.com/botandrose/idiomorph.git#single-pass-build",
"multer": "^1.4.2",
"rollup": "^2.35.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/core/morphing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function morphElements(currentElement, newElement, { callbacks, ...option
}

export function morphChildren(currentElement, newElement) {
morphElements(currentElement, newElement.children, {
morphElements(currentElement, newElement.childNodes, {
morphStyle: "innerHTML"
})
}
Expand Down
36 changes: 36 additions & 0 deletions src/tests/fixtures/permanent_children.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="turbo-refresh-method" content="morph">
<meta name="turbo-refresh-scroll" content="preserve">

<title>Turbo</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
</head>
<body>
<section>
<h1>Permanent children</h1>
</section>

<ul>
<li id="first-li">
<input id="first-checkbox" type="checkbox" data-turbo-permanent>
<label for="first-checkbox">First checkbox</label>
</li>
<li id="second-li">
<input id="second-checkbox" type="checkbox" data-turbo-permanent>
<label for="second-checkbox">Second checkbox</label>
</li>
</ul>

<form id="form" action="/__turbo/refresh" method="post" class="redirect">
<input type="hidden" name="path" value="/src/tests/fixtures/permanent_children.html">
<input type="hidden" name="sleep" value="50">
<input id="form-submit" type="submit" value="form[method=post]">
</form>

</body>
</html>

22 changes: 22 additions & 0 deletions src/tests/functional/page_refresh_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,28 @@ test("it preserves data-turbo-permanent elements that don't match when their ids
await expect(page.locator("#preserve-me")).toHaveText("Preserve me, I have a family!")
})

test("it preserves data-turbo-permanent children", async ({ page }) => {
await page.goto("/src/tests/fixtures/permanent_children.html")

await page.evaluate(() => {
// simulate result of client-side drag-and-drop reordering
document.getElementById("first-li").before(document.getElementById("second-li"))

// set state of data-turbo-permanent checkbox
document.getElementById("second-checkbox").checked = true
})

// morph page back to original li ordering
await page.click("#form-submit")
await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })

// data-turbo-permanent checkbox should still be checked
assert.ok(
await hasSelector(page, "#second-checkbox:checked"),
"retains state of data-turbo-permanent child"
)
})

test("renders unprocessable entity responses with morphing", async ({ page }) => {
await page.goto("/src/tests/fixtures/page_refresh.html")

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1932,9 +1932,9 @@ [email protected]:
dependencies:
safer-buffer ">= 2.1.2 < 3"

"idiomorph@https://github.com/bigskysoftware/idiomorph.git":
version "0.3.0"
resolved "https://github.com/bigskysoftware/idiomorph.git#b5115add9f7ab04c04af0624385540dff04e0735"
"idiomorph@https://github.com/botandrose/idiomorph.git#single-pass-build":
version "0.4.0"
resolved "https://github.com/botandrose/idiomorph.git#7c4904418ddba27e991318242423cb22f1f1a9b2"

ieee754@^1.1.13:
version "1.2.1"
Expand Down
Loading