Created
April 27, 2021 20:14
-
-
Save jimmont/30b197a46f7abc636dbd24afbe0995af to your computer and use it in GitHub Desktop.
Event related polyfills (for evergreen browsers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if( !('SubmitEvent' in self && 'submitter' in SubmitEvent.prototype) ){ | |
// polyfill SubmitEvent.submitter (a Safari issue as-of 2021) | |
// https://developer.mozilla.org/docs/Web/API/SubmitEvent | |
const submitter = Symbol.for('submitter'); | |
Event[ submitter ] = null; | |
const submitterSelector = 'input[type=submit], input[type=image], input[type=button], button'; | |
Object.defineProperty(Event.prototype, 'submitter', { | |
get: function(){ | |
if('submit' === this.type){ | |
let node = Event[ submitter ]; | |
const form = this.target; | |
if(!node || !form.contains(node)){ | |
node = form.querySelector(submitterSelector); | |
} | |
// always return a node, default as though form.submit called | |
return node || form; | |
} | |
return undefined; | |
}, | |
set: function(value){ | |
if('submit' === this.type){ | |
this.submitter = value; | |
} | |
} | |
}); | |
self.addEventListener('click', function polyfill_SubmitEvent_submitter_click(event){ | |
const node = event.composedPath()[0]; | |
const closest = node.closest?.(submitterSelector) ?? null; | |
Event[ submitter ] = closest; | |
}, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment