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

Update SubmitEvent Polyfill to Workaround Safari 15 bug #405

Merged
merged 1 commit into from
Sep 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface Node {

interface Window {
Turbo: typeof import("./core/index")
SubmitEvent: typeof Event
}
15 changes: 13 additions & 2 deletions src/polyfills/submit-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ function clickCaptured(event: Event) {
}

(function() {
if ("SubmitEvent" in window) return
if ("submitter" in Event.prototype) return

let prototype;
// 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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silly question, since Safari on iOS is probably similar to desktop Safari, is there a chance this will need to be fixed when iOS 15 ships?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears navigator.vendor can only be one of:
"Google Inc.", "Apple Computer Inc.", or ""

So I think we should be okay (you also tested it as well, thank you!)

prototype = window.SubmitEvent.prototype;
} else if ("SubmitEvent" in window) {
return; // polyfill not needed
} else {
prototype = window.Event.prototype;
}

addEventListener("click", clickCaptured, true)

Object.defineProperty(Event.prototype, "submitter", {
Object.defineProperty(prototype, "submitter", {
get(): HTMLElement | undefined {
if (this.type == "submit" && this.target instanceof HTMLFormElement) {
return submittersByForm.get(this.target)
Expand Down