Skip to content

Commit 3f3d99c

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 9fb05e3 commit 3f3d99c

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
@@ -424,7 +424,7 @@ export class FrameController {
424424

425425
#findFrameElement(element, submitter) {
426426
const id = getAttribute("data-turbo-frame", submitter, element) || this.element.getAttribute("target")
427-
return getFrameElementById(id) ?? this.element
427+
return FrameElement.getElementById(id) ?? this.element
428428
}
429429

430430
async extractForeignFrameElement(container) {
@@ -468,7 +468,7 @@ export class FrameController {
468468
}
469469

470470
if (id) {
471-
const frameElement = getFrameElementById(id)
471+
const frameElement = FrameElement.getElementById(id)
472472
if (frameElement) {
473473
return !frameElement.disabled
474474
}
@@ -554,15 +554,6 @@ export class FrameController {
554554
}
555555
}
556556

557-
function getFrameElementById(id) {
558-
if (id != null) {
559-
const element = document.getElementById(id)
560-
if (element instanceof FrameElement) {
561-
return element
562-
}
563-
}
564-
}
565-
566557
function activateElement(element, currentURL) {
567558
if (element) {
568559
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
@@ -163,9 +163,9 @@ export class Session {
163163
const frameTarget = element.getAttribute("data-turbo-frame")
164164
const frame = frameTarget == "_top" ?
165165
null :
166-
document.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame:not([disabled])")
166+
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame")
167167

168-
if (isUnsafe || isStream || frame instanceof FrameElement) {
168+
if (isUnsafe || isStream || (frame && !frame.disabled)) {
169169
return false
170170
} else {
171171
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", "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)