Skip to content

Commit 69402f6

Browse files
committed
feat(core polyfills): Add polyfills module for modern browsers.
Currently this holds a polyfill for SubmitEvent.submitter, which isn't available in Safari < 15.4 and jsDOM.
1 parent f7b4654 commit 69402f6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/core/polyfills.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Polyfills for modern browsers
2+
3+
// SubmitEvent.submitter polyfill for Safari < 15.4 and jsDOM
4+
// Original code: https://stackoverflow.com/a/61110260/1337474
5+
// Also see: https://caniuse.com/?search=submitter
6+
//
7+
!(function () {
8+
let last_btn = null;
9+
const submitable_buttons = `button, input[type="button"], input[type="submit"], input[type="image"]`;
10+
document.addEventListener(
11+
"click",
12+
function (e) {
13+
if (!e.target.closest) return;
14+
last_btn = e.target.closest(submitable_buttons);
15+
},
16+
true
17+
);
18+
document.addEventListener(
19+
"submit",
20+
function (e) {
21+
if ("submitter" in e) return;
22+
const canditates = [document.activeElement, last_btn];
23+
last_btn = null;
24+
for (const candidate of canditates) {
25+
if (!candidate) continue;
26+
if (!candidate.form) continue;
27+
if (!candidate.matches(submitable_buttons)) continue;
28+
e.submitter = candidate;
29+
return;
30+
}
31+
e.submitter = e.target.querySelector(submitable_buttons);
32+
},
33+
true
34+
);
35+
})();

0 commit comments

Comments
 (0)