diff --git a/src/core/session.ts b/src/core/session.ts index 2e68cacd6..388277b16 100644 --- a/src/core/session.ts +++ b/src/core/session.ts @@ -230,7 +230,7 @@ export class Session return ( this.elementIsNavigatable(form) && - (!submitter || this.formElementIsNavigatable(submitter)) && + (!submitter || this.submitterIsNavigatable(submitter)) && locationIsVisitable(expandURL(action), this.snapshot.rootLocation) ) } @@ -382,13 +382,12 @@ export class Session // Helpers - formElementIsNavigatable(element?: Element) { + submitterIsNavigatable(element: Element) { if (this.formMode == "off") { return false } - if (this.formMode == "optin") { - const form = element?.closest("form[data-turbo]") - return form?.getAttribute("data-turbo") == "true" + if (this.formMode == "optin" && hasForm(element) && element.form) { + return element.form.closest('[data-turbo="true"]') != null } return this.elementIsNavigatable(element) } @@ -449,3 +448,7 @@ const deprecatedLocationPropertyDescriptors = { }, }, } + +function hasForm(element: Element): element is Element & { form: HTMLFormElement | null } { + return "form" in element +} diff --git a/src/tests/fixtures/form.html b/src/tests/fixtures/form.html index 937b85285..db01a9914 100644 --- a/src/tests/fixtures/form.html +++ b/src/tests/fixtures/form.html @@ -82,10 +82,12 @@