diff --git a/js/src/dom/event-handler.js b/js/src/dom/event-handler.js index 12b157467c..f4305b6187 100644 --- a/js/src/dom/event-handler.js +++ b/js/src/dom/event-handler.js @@ -289,7 +289,6 @@ const EventHandler = { let bubbles = true let nativeDispatch = true let defaultPrevented = false - let evt = null if (inNamespace && $) { jQueryEvent = $.Event(event, args) @@ -300,12 +299,9 @@ const EventHandler = { defaultPrevented = jQueryEvent.isDefaultPrevented() } - if (isNative) { - evt = document.createEvent('HTMLEvents') - evt.initEvent(typeEvent, bubbles, true) - } else { - evt = new CustomEvent(event, { bubbles, cancelable: true }) - } + const evt = isNative ? + new Event(event, { bubbles, cancelable: true }) : + new CustomEvent(event, { bubbles, cancelable: true }) // merge custom information in our event if (typeof args !== 'undefined') { diff --git a/js/tests/helpers/fixture.js b/js/tests/helpers/fixture.js index ccb2bb166f..5ad14e1db9 100644 --- a/js/tests/helpers/fixture.js +++ b/js/tests/helpers/fixture.js @@ -24,10 +24,7 @@ export const clearFixture = () => { } export const createEvent = (eventName, parameters = {}) => { - const event = document.createEvent('Event') - - event.initEvent(eventName, Boolean(parameters.bubbles), Boolean(parameters.cancelable)) - return event + return new Event(eventName, parameters) } export const jQueryMock = { diff --git a/js/tests/unit/offcanvas.spec.js b/js/tests/unit/offcanvas.spec.js index f87527fb2f..36ef45dceb 100644 --- a/js/tests/unit/offcanvas.spec.js +++ b/js/tests/unit/offcanvas.spec.js @@ -191,8 +191,7 @@ describe('Offcanvas', () => { const offCanvasEl = fixtureEl.querySelector('div') const offCanvas = new Offcanvas(offCanvasEl, { backdrop: true }) - const clickEvent = document.createEvent('MouseEvents') - clickEvent.initEvent('mousedown', true, true) + const clickEvent = new Event('mousedown', { bubbles: true, cancelable: true }) spyOn(offCanvas._backdrop._config, 'clickCallback').and.callThrough() offCanvasEl.addEventListener('shown.bs.offcanvas', () => { diff --git a/js/tests/unit/util/backdrop.spec.js b/js/tests/unit/util/backdrop.spec.js index 4303693805..f9903c8327 100644 --- a/js/tests/unit/util/backdrop.spec.js +++ b/js/tests/unit/util/backdrop.spec.js @@ -169,8 +169,7 @@ describe('Backdrop', () => { } instance.show(() => { - const clickEvent = document.createEvent('MouseEvents') - clickEvent.initEvent('mousedown', true, true) + const clickEvent = new Event('mousedown', { bubbles: true, cancelable: true }) document.querySelector(CLASS_BACKDROP).dispatchEvent(clickEvent) endTest() })