Skip to content

Commit

Permalink
Avoid polyfill for SubmitEvent for newer Safari versions (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtnin committed Jun 18, 2023
1 parent 4593d06 commit 96a4f58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/polyfills/submit-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ <h2>Frame: Form</h2>
<select id="external-select" form="form-with-external-inputs" name="greeting">
<option selected>Hello from a replace Visit</option>
</select>
<form id="form-with-buttons-triggered-by-js" action="/__turbo/redirect" data-turbo-frame="_top" method="post">
<input type="hidden" name="path" value="/src/tests/fixtures/one.html">
<button type="submit" formaction="/src/tests/fixtures/frames/hello.html" formmethod="get" id="button-triggered-by-js" data-turbo-action="replace" data-turbo-frame="hello">Navigate other Frame with GET</button>
<button type="button" onclick="this.form.requestSubmit(document.getElementById('button-triggered-by-js'))" id="request-submit-trigger">Trigger Navigate other Frame with GET</button>
</form>
</turbo-frame>
<a href="/src/tests/fixtures/frames/hello.html" data-turbo-method="get" id="link-method-outside-frame">Method link outside frame</a><br />
<a href="/__turbo/messages?content=Link!&type=stream" data-turbo-method="post" id="stream-link-method-outside-frame">Stream link outside frame</a>
Expand Down
9 changes: 9 additions & 0 deletions src/tests/functional/form_submission_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <form> 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")

Expand Down

0 comments on commit 96a4f58

Please sign in to comment.