Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for SubmitEvent polyfill breaking requestSubmit() for newer Safari versions #933

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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