Skip to content

Commit

Permalink
Introduce FrameElement.getElementById(id: string)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seanpdoyle committed Mar 29, 2024
1 parent 9fb05e3 commit 3f3d99c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
13 changes: 2 additions & 11 deletions src/core/frames/frame_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ export class FrameController {

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

async extractForeignFrameElement(container) {
Expand Down Expand Up @@ -468,7 +468,7 @@ export class FrameController {
}

if (id) {
const frameElement = getFrameElementById(id)
const frameElement = FrameElement.getElementById(id)
if (frameElement) {
return !frameElement.disabled
}
Expand Down Expand Up @@ -554,15 +554,6 @@ export class FrameController {
}
}

function getFrameElementById(id) {
if (id != null) {
const element = document.getElementById(id)
if (element instanceof FrameElement) {
return element
}
}
}

function activateElement(element, currentURL) {
if (element) {
const src = element.getAttribute("src")
Expand Down
5 changes: 3 additions & 2 deletions src/core/frames/frame_redirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export class FrameRedirector {
#findFrameElement(element, submitter) {
const id = submitter?.getAttribute("data-turbo-frame") || element.getAttribute("data-turbo-frame")
if (id && id != "_top") {
const frame = this.element.querySelector(`#${id}:not([disabled])`)
if (frame instanceof FrameElement) {
const frame = FrameElement.getElementById(id)

if (frame && !frame.disabled && this.element.contains(frame)) {
return frame
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ export class Session {
const frameTarget = element.getAttribute("data-turbo-frame")
const frame = frameTarget == "_top" ?
null :
document.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame:not([disabled])")
FrameElement.getElementById(frameTarget) || findClosestRecursively(element, "turbo-frame")

if (isUnsafe || isStream || frame instanceof FrameElement) {
if (isUnsafe || isStream || (frame && !frame.disabled)) {
return false
} else {
const location = new URL(element.href)
Expand Down
15 changes: 15 additions & 0 deletions src/elements/frame_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ export class FrameElement extends HTMLElement {
return ["disabled", "loading", "src"]
}

/**
* Returns the `<turbo-frame>` element located by its `[id]` attribute.
* Returns `null` when there is not element with a matching `[id]`, or when
* the element with a matching `[id]` is not a `<turbo-frame>`.
*/
static getElementById(id) {
if (id) {
const element = document.getElementById(id)

if (element instanceof this) {
return element
}
}
}

constructor() {
super()
this.delegate = new FrameElement.delegateConstructor(this)
Expand Down

0 comments on commit 3f3d99c

Please sign in to comment.