Skip to content

Commit ac9e99f

Browse files
Merge pull request #1321 from botandrose/morph-reorder-with-data-permanent-children-redux
Fix bug where Idiomorph sometimes ignores data-turbo-permanent
2 parents 9e057f2 + 67faf4f commit ac9e99f

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"chai": "~4.3.4",
4545
"eslint": "^8.13.0",
4646
"express": "^4.18.2",
47-
"idiomorph": "https://github.com/bigskysoftware/idiomorph.git",
47+
"idiomorph": "~0.7.2",
4848
"multer": "^1.4.2",
4949
"rollup": "^2.35.1"
5050
},

src/core/morphing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Idiomorph } from "idiomorph/dist/idiomorph.esm.js"
1+
import { Idiomorph } from "idiomorph"
22
import { dispatch } from "../util"
33

44
export function morphElements(currentElement, newElement, { callbacks, ...options } = {}) {
@@ -9,7 +9,7 @@ export function morphElements(currentElement, newElement, { callbacks, ...option
99
}
1010

1111
export function morphChildren(currentElement, newElement) {
12-
morphElements(currentElement, newElement.children, {
12+
morphElements(currentElement, newElement.childNodes, {
1313
morphStyle: "innerHTML"
1414
})
1515
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="turbo-refresh-method" content="morph">
6+
<meta name="turbo-refresh-scroll" content="preserve">
7+
8+
<title>Turbo</title>
9+
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
10+
<script src="/src/tests/fixtures/test.js"></script>
11+
</head>
12+
<body>
13+
<section>
14+
<h1>Permanent children</h1>
15+
</section>
16+
17+
<ul>
18+
<li id="first-li">
19+
<input id="first-checkbox" type="checkbox" data-turbo-permanent>
20+
<label for="first-checkbox">First checkbox</label>
21+
</li>
22+
<li id="second-li">
23+
<input id="second-checkbox" type="checkbox" data-turbo-permanent>
24+
<label for="second-checkbox">Second checkbox</label>
25+
</li>
26+
</ul>
27+
28+
<form id="form" action="/__turbo/refresh" method="post" class="redirect">
29+
<input type="hidden" name="path" value="/src/tests/fixtures/permanent_children.html">
30+
<input type="hidden" name="sleep" value="50">
31+
<input id="form-submit" type="submit" value="form[method=post]">
32+
</form>
33+
34+
</body>
35+
</html>
36+

src/tests/functional/page_refresh_tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ test("it preserves data-turbo-permanent elements that don't match when their ids
331331
await expect(page.locator("#preserve-me")).toHaveText("Preserve me, I have a family!")
332332
})
333333

334+
test("it preserves data-turbo-permanent children", async ({ page }) => {
335+
await page.goto("/src/tests/fixtures/permanent_children.html")
336+
337+
await page.evaluate(() => {
338+
// simulate result of client-side drag-and-drop reordering
339+
document.getElementById("first-li").before(document.getElementById("second-li"))
340+
341+
// set state of data-turbo-permanent checkbox
342+
document.getElementById("second-checkbox").checked = true
343+
})
344+
345+
// morph page back to original li ordering
346+
await page.click("#form-submit")
347+
await nextEventNamed(page, "turbo:render", { renderMethod: "morph" })
348+
349+
// data-turbo-permanent checkbox should still be checked
350+
assert.ok(
351+
await hasSelector(page, "#second-checkbox:checked"),
352+
"retains state of data-turbo-permanent child"
353+
)
354+
})
355+
334356
test("renders unprocessable content responses with morphing", async ({ page }) => {
335357
await page.goto("/src/tests/fixtures/page_refresh.html")
336358

yarn.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,9 +1932,10 @@ [email protected]:
19321932
dependencies:
19331933
safer-buffer ">= 2.1.2 < 3"
19341934

1935-
"idiomorph@https://github.com/bigskysoftware/idiomorph.git":
1936-
version "0.3.0"
1937-
resolved "https://github.com/bigskysoftware/idiomorph.git#b5115add9f7ab04c04af0624385540dff04e0735"
1935+
idiomorph@~0.7.2:
1936+
version "0.7.2"
1937+
resolved "https://registry.yarnpkg.com/idiomorph/-/idiomorph-0.7.2.tgz#0c385343991c5ecf75472ecd58a4515fc7cdd47f"
1938+
integrity sha512-fGqT4uBOetEVvKKQkj5bzCQgQlEjb7i4/AJSi7usmflG6HyCGRXAnJLi4aUjJDlPMM3tE/aRCKsUhDAzKabxWQ==
19381939

19391940
ieee754@^1.1.13:
19401941
version "1.2.1"

0 commit comments

Comments
 (0)