From 96a4f584e8ed977f8cefd7163edfee83f8860edc Mon Sep 17 00:00:00 2001 From: Martin Ulleberg Date: Sun, 18 Jun 2023 15:15:43 +0200 Subject: [PATCH] Avoid polyfill for SubmitEvent for newer Safari versions (#933) --- src/polyfills/submit-event.ts | 12 ++++++++---- src/tests/fixtures/form.html | 5 +++++ src/tests/functional/form_submission_tests.ts | 9 +++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/polyfills/submit-event.ts b/src/polyfills/submit-event.ts index a676bca72..2f246163d 100644 --- a/src/polyfills/submit-event.ts +++ b/src/polyfills/submit-event.ts @@ -23,10 +23,14 @@ function clickCaptured(event: Event) { // Certain versions of Safari 15 have a bug where they won't // populate the submitter. This hurts TurboDrive's enable/disable detection. // See https://bugs.webkit.org/show_bug.cgi?id=229660 - if ("SubmitEvent" in window && /Apple Computer/.test(navigator.vendor)) { - prototype = window.SubmitEvent.prototype - } else if ("SubmitEvent" in window) { - return // polyfill not needed + if ("SubmitEvent" in window) { + const prototypeOfSubmitEvent = window.SubmitEvent.prototype + + if (/Apple Computer/.test(navigator.vendor) && !("submitter" in prototypeOfSubmitEvent)) { + prototype = prototypeOfSubmitEvent + } else { + return // polyfill not needed + } } addEventListener("click", clickCaptured, true) diff --git a/src/tests/fixtures/form.html b/src/tests/fixtures/form.html index b02b9b825..9b27c95b3 100644 --- a/src/tests/fixtures/form.html +++ b/src/tests/fixtures/form.html @@ -312,6 +312,11 @@

Frame: Form

+
+ + + +
Method link outside frame
Stream link outside frame diff --git a/src/tests/functional/form_submission_tests.ts b/src/tests/functional/form_submission_tests.ts index 899e94bf3..a50a1fda0 100644 --- a/src/tests/functional/form_submission_tests.ts +++ b/src/tests/functional/form_submission_tests.ts @@ -234,6 +234,15 @@ test("test standard GET HTMLFormElement.requestSubmit() with Turbo Action", asyn assert.equal(getSearchParam(page.url(), "greeting"), "Hello from a replace Visit", "encodes
into request") }) +test("test GET HTMLFormElement.requestSubmit() triggered by javascript", async ({ page }) => { + await page.click("#request-submit-trigger") + + await nextEventNamed(page, "turbo:load") + + assert.notEqual(pathname(page.url()), "/src/tests/fixtures/one.html", "SubmitEvent was triggered without a submitter") + assert.equal(await page.textContent("#hello h2"), "Hello from a frame", "navigates #hello turbo frame") +}) + test("test standard GET form submission with [data-turbo-stream] declared on the form", async ({ page }) => { await page.click("#standard-get-form-with-stream-opt-in-submit")