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 21, 2021
1 parent 2007d53 commit ed273ca
Show file tree
Hide file tree
Showing 6 changed files with 50 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, fetchResponse)
}
Expand Down
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>
6 changes: 6 additions & 0 deletions src/tests/fixtures/loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<turbo-frame id="frame" src="/src/tests/fixtures/frames/frame.html" loading="eager"></turbo-frame>
</details>

<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>

<p><a id="one" href="one.html">One</a></p>
</body>
</html>
3 changes: 1 addition & 2 deletions src/tests/functional/frame_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,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
12 changes: 12 additions & 0 deletions src/tests/functional/loading_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ export class LoadingTests extends TurboDriveTestCase {
const requestLogs = eventLogs.filter(([ name ]) => name == "turbo:before-fetch-request")
this.assert.equal(requestLogs.length, 0)
}

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 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"))
}
}

LoadingTests.registerSuite()

0 comments on commit ed273ca

Please sign in to comment.