Skip to content

Commit

Permalink
Extract and use FrameElement.getElementById
Browse files Browse the repository at this point in the history
Unify the query selector construction and retrieval logic to:

* find elements matching `turbo-frame`
* find elements with a matching `[id]` attribute that are not `_top`
* omit elements that declare `[disabled]`
  • Loading branch information
seanpdoyle committed Feb 6, 2021
1 parent 5a416a9 commit 1e35e5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
24 changes: 5 additions & 19 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest

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

async extractForeignFrameElement(container: ParentNode): Promise<FrameElement> {
Expand All @@ -238,16 +238,11 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest

if (!this.enabled || id == "_top") {
return false
} else if (id) {
return !!FrameElement.getElementById(document, id)
} else {
return true
}

if (id) {
const frameElement = getFrameElementById(id)
if (frameElement) {
return !frameElement.disabled
}
}

return true
}

// Computed properties
Expand Down Expand Up @@ -277,15 +272,6 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}
}

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

function activateElement(element: Node | null) {
if (element && element.ownerDocument !== document) {
element = document.importNode(element, true)
Expand Down
8 changes: 1 addition & 7 deletions src/core/frames/frame_redirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ export class FrameRedirector implements LinkInterceptorDelegate, FormInterceptor
}

private findFrameElement(element: Element) {
const id = element.getAttribute("data-turbo-frame")
if (id && id != "_top") {
const frame = this.element.querySelector(`#${id}:not([disabled])`)
if (frame instanceof FrameElement) {
return frame
}
}
return FrameElement.getElementById(this.element, element.getAttribute("data-turbo-frame"))
}
}
9 changes: 9 additions & 0 deletions src/elements/frame_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export class FrameElement extends HTMLElement {
return ["loading", "src"]
}

static getElementById(container: Element | Document, id: string | null): FrameElement | undefined {
if (id && id != "_top") {
const frame = container.querySelector(`#${id}:not([disabled])`)
if (frame instanceof FrameElement) {
return frame
}
}
}

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

0 comments on commit 1e35e5c

Please sign in to comment.