Skip to content

Commit

Permalink
Extract findAttribute utility function
Browse files Browse the repository at this point in the history
For cases where we need to find an attribute value from a collection of
elements, use `findAttribute` instead of a long chain of `||` and `?`
operators.
  • Loading branch information
seanpdoyle committed Sep 17, 2021
1 parent 8838d22 commit fdd3a9c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/core/drive/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FetchMethod } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { FormSubmission } from "./form_submission"
import { expandURL, getAnchor, getRequestURL, Locatable } from "../url"
import { findAttribute } from "../../util"
import { Visit, VisitDelegate, VisitOptions } from "./visit"
import { PageSnapshot } from "./page_snapshot"

Expand Down Expand Up @@ -157,7 +158,7 @@ export class Navigator {

getActionForFormSubmission(formSubmission: FormSubmission): Action {
const { formElement, submitter } = formSubmission
const action = submitter?.getAttribute("data-turbo-action") || formElement.getAttribute("data-turbo-action")
const action = findAttribute("data-turbo-action", submitter, formElement)
return isAction(action) ? action : "advance"
}
}
8 changes: 4 additions & 4 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FrameElement, FrameElementDelegate, FrameLoadingStyle } from "../../ele
import { FetchMethod, FetchRequest, FetchRequestDelegate, FetchRequestHeaders } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { AppearanceObserver, AppearanceObserverDelegate } from "../../observers/appearance_observer"
import { parseHTMLDocument } from "../../util"
import { findAttribute, parseHTMLDocument } from "../../util"
import { FormSubmission, FormSubmissionDelegate } from "../drive/form_submission"
import { Snapshot } from "../snapshot"
import { ViewDelegate } from "../view"
Expand Down Expand Up @@ -258,7 +258,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}

private proposeVisitFromFrameResponse(frame: FrameElement, element: Element, submitter?: HTMLElement) {
const action = submitter?.getAttribute("data-turbo-action") || element.getAttribute("data-turbo-action") || frame.getAttribute("data-turbo-action")
const action = findAttribute("data-turbo-action", submitter, element, frame)

if (isAction(action)) {
const proposeVisit = async (event: Event) => {
Expand All @@ -274,7 +274,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}

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

Expand All @@ -301,7 +301,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}

private shouldInterceptNavigation(element: Element, submitter?: Element) {
const id = submitter?.getAttribute("data-turbo-frame") || element.getAttribute("data-turbo-frame") || this.element.getAttribute("target")
const id = findAttribute("data-turbo-frame", submitter, element) || this.element.getAttribute("target")

if (!this.enabled || id == "_top") {
return false
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ export function uuid() {
}
}).join("")
}

export function findAttribute(attributeName: string, ...elements: (Element|undefined)[]) {
const element = elements.find(element => element?.hasAttribute(attributeName))

return element?.getAttribute(attributeName)
}

0 comments on commit fdd3a9c

Please sign in to comment.