Skip to content

Commit

Permalink
Extract getAttribute 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 `getAttribute` instead of a long chain of `||` and `?`
operators.
  • Loading branch information
seanpdoyle committed Oct 25, 2021
1 parent b4e000d commit 32b1303
Show file tree
Hide file tree
Showing 3 changed files with 14 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, locationIsVisitable } from "../url"
import { getAttribute } from "../../util"
import { Visit, VisitDelegate, VisitOptions } from "./visit"
import { PageSnapshot } from "./page_snapshot"

Expand Down Expand Up @@ -158,7 +159,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 = getAttribute("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 { getAttribute, parseHTMLDocument } from "../../util"
import { FormSubmission, FormSubmissionDelegate } from "../drive/form_submission"
import { Snapshot } from "../snapshot"
import { ViewDelegate } from "../view"
Expand Down Expand Up @@ -259,7 +259,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}

private proposeVisitIfNavigatedWithAction(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 = getAttribute("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 = getAttribute("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 = getAttribute("data-turbo-frame", submitter, element) || this.element.getAttribute("target")

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

export function getAttribute(attributeName: string, ...elements: (Element | undefined)[]): string | null {
for (const value of elements.map(element => element?.getAttribute(attributeName))) {
if (typeof value == "string") return value
}

return null
}

0 comments on commit 32b1303

Please sign in to comment.