mirror of
https://github.com/twbs/bootstrap.git
synced 2024-12-01 13:24:25 +01:00
Workaround for M$ Edge resetting defaultPrevented flag upon dispatchEvent
This commit is contained in:
parent
33211eefdf
commit
53ca76ebc0
@ -16,6 +16,8 @@ const workingDefaultPrevented = (() => {
|
|||||||
return e.defaultPrevented
|
return e.defaultPrevented
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
let defaultPreventedPreservedOnDispatch = true
|
||||||
|
|
||||||
// CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
|
// CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
|
||||||
if (typeof window.CustomEvent !== 'function') {
|
if (typeof window.CustomEvent !== 'function') {
|
||||||
window.CustomEvent = (event, params) => {
|
window.CustomEvent = (event, params) => {
|
||||||
@ -46,6 +48,20 @@ if (typeof window.CustomEvent !== 'function') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.CustomEvent.prototype = window.Event.prototype
|
window.CustomEvent.prototype = window.Event.prototype
|
||||||
|
} else {
|
||||||
|
// MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached
|
||||||
|
defaultPreventedPreservedOnDispatch = (() => {
|
||||||
|
const e = new CustomEvent('Bootstrap', {
|
||||||
|
cancelable: true
|
||||||
|
})
|
||||||
|
|
||||||
|
const element = document.createElement('div')
|
||||||
|
element.addEventListener('Bootstrap', () => null)
|
||||||
|
|
||||||
|
e.preventDefault()
|
||||||
|
element.dispatchEvent(e)
|
||||||
|
return e.defaultPrevented
|
||||||
|
})()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event constructor shim
|
// Event constructor shim
|
||||||
@ -276,7 +292,7 @@ const EventHandler = {
|
|||||||
|
|
||||||
if (isNative) {
|
if (isNative) {
|
||||||
evt = document.createEvent('HTMLEvents')
|
evt = document.createEvent('HTMLEvents')
|
||||||
evt.initEvent(typeEvent, true, true)
|
evt.initEvent(typeEvent, bubbles, true)
|
||||||
} else {
|
} else {
|
||||||
evt = new CustomEvent(event, {
|
evt = new CustomEvent(event, {
|
||||||
bubbles,
|
bubbles,
|
||||||
@ -291,6 +307,12 @@ const EventHandler = {
|
|||||||
|
|
||||||
if (defaultPrevented) {
|
if (defaultPrevented) {
|
||||||
evt.preventDefault()
|
evt.preventDefault()
|
||||||
|
|
||||||
|
if (!defaultPreventedPreservedOnDispatch) {
|
||||||
|
Object.defineProperty(evt, 'defaultPrevented', {
|
||||||
|
get: () => true
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nativeDispatch) {
|
if (nativeDispatch) {
|
||||||
|
@ -59,6 +59,24 @@ $(function () {
|
|||||||
EventHandler.trigger(element, 'foobar.namespace')
|
EventHandler.trigger(element, 'foobar.namespace')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
QUnit.test('should mirror preventDefault for native events', function (assert) {
|
||||||
|
assert.expect(2)
|
||||||
|
|
||||||
|
var element = document.createElement('div')
|
||||||
|
document.body.appendChild(element)
|
||||||
|
|
||||||
|
$(element).on('click', function (event) {
|
||||||
|
event.preventDefault()
|
||||||
|
assert.ok(true, 'first listener called')
|
||||||
|
})
|
||||||
|
element.addEventListener('click', function (event) {
|
||||||
|
assert.ok(event.defaultPrevented, 'defaultPrevented is true in second listener')
|
||||||
|
})
|
||||||
|
|
||||||
|
EventHandler.trigger(element, 'click')
|
||||||
|
document.body.removeChild(element)
|
||||||
|
})
|
||||||
|
|
||||||
QUnit.test('on should add event listener', function (assert) {
|
QUnit.test('on should add event listener', function (assert) {
|
||||||
assert.expect(1)
|
assert.expect(1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user