Skip to content

Commit

Permalink
Treat response turbo-frame[disabled] as missing
Browse files Browse the repository at this point in the history
When a response body has a `turbo-frame` element with an `[id]` that
matches the requesting frame, ignore it if it's `[disabled]`.
  • Loading branch information
seanpdoyle committed Nov 18, 2021
1 parent 6aba5c6 commit 6eb8444
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
const id = CSS.escape(this.id)

try {
if (element = activateElement(container.querySelector(`turbo-frame#${id}`), this.currentURL)) {
if (element = activateElement(container.querySelector(`turbo-frame:not([disabled])#${id}`), this.currentURL)) {
return element
}

if (element = activateElement(container.querySelector(`turbo-frame[src][recurse~=${id}]`), this.currentURL)) {
if (element = activateElement(container.querySelector(`turbo-frame:not([disabled])[src][recurse~=${id}]`), this.currentURL)) {
await element.loaded
return await this.extractForeignFrameElement(element)
}
Expand Down
7 changes: 7 additions & 0 deletions src/tests/fixtures/frames.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ <h2>Frames: #nested-child</h2>
<input id="outer-frame-submit" type="submit" value="Outer form submit">
</form>

<turbo-frame id="disabled" src="/src/tests/fixtures/frames/disabled.html">
</turbo-frame>

<turbo-frame id="disabled_recursive" recurse="disabled_composer" src="/src/tests/fixtures/frames/disabled_recursive.html">
</turbo-frame>


<button id="propose-visit-when-frame-missing" type="button">Propose Visit when frame missing</button>
</body>
</html>
13 changes: 13 additions & 0 deletions src/tests/fixtures/frames/disabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Disabled Frame</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
</head>
<body>
<turbo-frame id="disabled" disabled>
<h1>Frame: #disabled loaded</h1>
</turbo-frame>
</body>
</html>
16 changes: 16 additions & 0 deletions src/tests/fixtures/frames/disabled_recursive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Disabled Recursive Frame</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
</head>
<body>
<turbo-frame id="disabled_recursive" disabled>
<h2>Frame: #disabled_recursive loaded</h2>
<turbo-frame id="disabled_composer">
<h2>Frame: #disabled_composer loaded</h2>
</turbo-frame>
</turbo-frame>
</body>
</html>
15 changes: 13 additions & 2 deletions src/tests/functional/frame_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ export class FrameTests extends TurboDriveTestCase {
this.assert.ok(await this.querySelector("#recursive details:not([open])"))
}

async "test loading a page with a <turbo-frame disabled recurse> does not lazily loads the matching frame"() {
await this.nextBeat

this.assert.notOk(await this.hasSelector("#disabled_recursive h2"))
}

async "test submitting a form that redirects to a page with a <turbo-frame recurse> which lazily loads a matching frame"() {
await this.nextBeat
await this.clickSelector("#recursive summary")
Expand All @@ -158,6 +164,12 @@ export class FrameTests extends TurboDriveTestCase {
this.assert.ok(await this.querySelector("#recursive details:not([open])"))
}

async "test loading a page with a <turbo-frame disabled> does not lazily load the matching frame"() {
await this.nextBeat

this.assert.notOk(await this.hasSelector("#disabled h2"))
}

async "test removing [disabled] attribute from eager-loaded frame navigates it"() {
await this.remote.execute(() => document.getElementById("frame")?.setAttribute("disabled", ""))
await this.remote.execute((src: string) => document.getElementById("frame")?.setAttribute("src", "/src/tests/fixtures/frames/frame.html"))
Expand Down Expand Up @@ -207,8 +219,7 @@ export class FrameTests extends TurboDriveTestCase {
async "test 'turbo:frame-render' is triggered after frame has finished rendering"() {
await this.clickSelector("#frame-part")

await this.nextEventNamed("turbo:frame-render") // recursive
const { fetchResponse } = await this.nextEventNamed("turbo:frame-render")
const { fetchResponse } = await this.nextEventOnTarget("part", "turbo:frame-render")

this.assert.include(fetchResponse.response.url, "/src/tests/fixtures/frames/part.html")
}
Expand Down

0 comments on commit 6eb8444

Please sign in to comment.