Skip to content

Commit

Permalink
Fix: Polyfill CustomEvent constructor
Browse files Browse the repository at this point in the history
This was causing IE11 to brick. Apparently not supported by core-js :(
zloirock/core-js#354
  • Loading branch information
cdrini committed Sep 14, 2020
1 parent 30c288a commit ab802ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/js/BookReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ BookReader.prototype.init = function() {
BookReader.prototype.trigger = function(name, props = this) {
const eventName = 'BookReader:' + name;
$(document).trigger(eventName, props);

utils.polyfillCustomEvent(window);
window.dispatchEvent(new CustomEvent(eventName, {
bubbles: true,
composed: true,
Expand Down
22 changes: 22 additions & 0 deletions src/js/BookReader/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,25 @@ export function throttle(fn, threshold, delay) {
}
};
}

/**
* FIXME we need a better way to do this :/ This is not automatically poly-filled by
* core-js https://github.com/zloirock/core-js/issues/354
* @param {Window} window
*/
export function polyfillCustomEvent(window) {
if (typeof window.CustomEvent === "function") return false;
window.CustomEvent = PolyfilledCustomEvent;
}

/**
* https://caniuse.com/customevent has issues on older browsers where it can't be
* called as a constructor, so we have to use older methods.
* @param {String} eventName
* @return {CustomEvent}
*/
export function PolyfilledCustomEvent(eventName, {bubbles = false, cancelable = false, detail = null} = {}) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, bubbles, cancelable, detail);
return event;
}
31 changes: 31 additions & 0 deletions tests/BookReader/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
decodeURIComponentPlus,
encodeURIComponentPlus,
escapeHTML,
polyfillCustomEvent,
PolyfilledCustomEvent,
} from '../../src/js/BookReader/utils.js';

test('clamp function returns Math.min(Math.max(value, min), max)', () => {
Expand Down Expand Up @@ -61,3 +63,32 @@ test('testing debounce', () => {
clock.tick(1000);
expect(func).toHaveBeenCalledTimes(1); // func called
});


describe('polyfillCustomEvent', () => {
test('Overrides when missing', () => {
const win = {};
polyfillCustomEvent(win);
expect(win).toHaveProperty('CustomEvent');
});

test('Overrides when not a function', () => {
const win = { CustomEvent: {} };
polyfillCustomEvent(win);
expect(typeof win.CustomEvent).toBe('function');
});
});

describe('PolyfilledCustomEvent', () => {
test('Can be called as a constructor', () => {
new PolyfilledCustomEvent('foo');
});

test('Calls deprecated browser methods', () => {
const createEventSpy = sinon.spy(document, 'createEvent');
const initCustomEventSpy = sinon.spy(CustomEvent.prototype, 'initCustomEvent');
new PolyfilledCustomEvent('foo');
expect(createEventSpy.callCount).toBe(1);
expect(initCustomEventSpy.callCount).toBe(1);
});
});

0 comments on commit ab802ad

Please sign in to comment.