diff --git a/src/core/drive/navigator.ts b/src/core/drive/navigator.ts index 851b8742e..c5293050e 100644 --- a/src/core/drive/navigator.ts +++ b/src/core/drive/navigator.ts @@ -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" @@ -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" } } diff --git a/src/core/frames/frame_controller.ts b/src/core/frames/frame_controller.ts index c585f78cf..638570853 100644 --- a/src/core/frames/frame_controller.ts +++ b/src/core/frames/frame_controller.ts @@ -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" @@ -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) => { @@ -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 } @@ -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 diff --git a/src/util.ts b/src/util.ts index 2fbff7ff6..ff3ce2036 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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) +}