Skip to content

Commit b750a8f

Browse files
committed
Introduce FrameElement.getElementById(id: string)
The `FrameElement.getElementById(id: string)` static method unifies the internal logic to locate a `<turbo-frame>` element by its `[id]`, while providing that consistent behavior to consumer applications.
1 parent 95c98d4 commit b750a8f

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/core/frames/frame_controller.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export class FrameController {
432432

433433
#findFrameElement(element, submitter) {
434434
const id = getAttribute("data-turbo-frame", submitter, element) || this.element.getAttribute("target")
435-
return getFrameElementById(id) ?? this.element
435+
return FrameElement.getElementById(id) ?? this.element
436436
}
437437

438438
async extractForeignFrameElement(container) {
@@ -476,7 +476,7 @@ export class FrameController {
476476
}
477477

478478
if (id) {
479-
const frameElement = getFrameElementById(id)
479+
const frameElement = FrameElement.getElementById(id)
480480
if (frameElement) {
481481
return !frameElement.disabled
482482
}
@@ -564,15 +564,6 @@ export class FrameController {
564564
}
565565
}
566566

567-
function getFrameElementById(id) {
568-
if (id != null) {
569-
const element = document.getElementById(id)
570-
if (element instanceof FrameElement) {
571-
return element
572-
}
573-
}
574-
}
575-
576567
function activateElement(element, currentURL) {
577568
if (element) {
578569
const src = element.getAttribute("src")

src/core/frames/frame_redirector.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export class FrameRedirector {
7676
#findFrameElement(element, submitter) {
7777
const id = submitter?.getAttribute("data-turbo-frame") || element.getAttribute("data-turbo-frame")
7878
if (id && id != "_top") {
79-
const frame = this.element.querySelector(`#${id}:not([disabled])`)
80-
if (frame instanceof FrameElement) {
79+
const frame = FrameElement.getElementById(id)
80+
81+
if (frame && !frame.disabled && this.element.contains(frame)) {
8182
return frame
8283
}
8384
}

src/core/session.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ export class Session {
162162
const frameTarget = element.getAttribute("data-turbo-frame")
163163
const frame = frameTarget == "_top" ?
164164
null :
165-
document.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame:not([disabled])")
165+
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame")
166166

167-
if (isUnsafe || isStream || frame instanceof FrameElement) {
167+
if (isUnsafe || isStream || (frame && !frame.disabled)) {
168168
return false
169169
} else {
170170
const location = new URL(element.href)

src/elements/frame_element.js

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ export class FrameElement extends HTMLElement {
2828
return ["disabled", "complete", "loading", "src"]
2929
}
3030

31+
/**
32+
* Returns the `<turbo-frame>` element located by its `[id]` attribute.
33+
* Returns `null` when there is not element with a matching `[id]`, or when
34+
* the element with a matching `[id]` is not a `<turbo-frame>`.
35+
*/
36+
static getElementById(id) {
37+
if (id) {
38+
const element = document.getElementById(id)
39+
40+
if (element instanceof this) {
41+
return element
42+
}
43+
}
44+
}
45+
3146
constructor() {
3247
super()
3348
this.delegate = new FrameElement.delegateConstructor(this)

0 commit comments

Comments
 (0)