From 5152219bdee9bc18ab0a1cccddab48a813c20b81 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Fri, 17 Mar 2023 09:31:30 +0100 Subject: [PATCH 1/3] fix: Event being polyfilled when not needed --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 82f7f42..7460711 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,16 @@ const root = (typeof self !== "undefined" && self) || (typeof global !== "undefined" && global); -function isConstructor(fn) { +function isConstructor(fn, args) { try { - new fn(); + new fn(args); } catch (error) { return false; } return true; } -if (typeof root.Event !== "function" || !isConstructor(root.Event)) { +if (typeof root.Event !== "function" || !isConstructor(root.Event, 'event')) { root.Event = (function () { function Event(type, options) { this.bubbles = !!options && !!options.bubbles; @@ -25,7 +25,7 @@ if (typeof root.Event !== "function" || !isConstructor(root.Event)) { })(); } -if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event)) { +if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event, 'event')) { root.EventTarget = (function () { function EventTarget() { this.__listeners = new Map(); From 8becf1fbdd9ec547c1ea2097d2cb492f6e4d3984 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Fri, 17 Mar 2023 15:29:28 +0100 Subject: [PATCH 2/3] fix: webos --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7460711..03ccb9c 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ function isConstructor(fn, args) { return true; } -if (typeof root.Event !== "function" || !isConstructor(root.Event, 'event')) { +if (!isConstructor(root.Event, 'event')) { root.Event = (function () { function Event(type, options) { this.bubbles = !!options && !!options.bubbles; @@ -25,7 +25,7 @@ if (typeof root.Event !== "function" || !isConstructor(root.Event, 'event')) { })(); } -if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event, 'event')) { +if (!isConstructor(root.Event, 'event')) { root.EventTarget = (function () { function EventTarget() { this.__listeners = new Map(); @@ -76,7 +76,7 @@ if (typeof root.EventTarget === "undefined" || !isConstructor(root.Event, 'event }; EventTarget.prototype.dispatchEvent = function (event) { - if (!(event instanceof Event)) { + if (!(event instanceof root.Event)) { throw new TypeError( `Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.` ); From 5cb9a0ed6774af1b905b525964316911375726a7 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Mon, 20 Mar 2023 12:11:13 +0100 Subject: [PATCH 3/3] fix: redeclare --- index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 03ccb9c..1171a88 100644 --- a/index.js +++ b/index.js @@ -3,16 +3,18 @@ const root = (typeof self !== "undefined" && self) || (typeof global !== "undefined" && global); -function isConstructor(fn, args) { +function isConstructor(fn) { try { - new fn(args); + new fn(''); } catch (error) { return false; } return true; } +const hasEvent = isConstructor(root.Event) +const hasTarget = isConstructor(root.EventTarget) -if (!isConstructor(root.Event, 'event')) { +if (!hasEvent) { root.Event = (function () { function Event(type, options) { this.bubbles = !!options && !!options.bubbles; @@ -25,7 +27,8 @@ if (!isConstructor(root.Event, 'event')) { })(); } -if (!isConstructor(root.Event, 'event')) { + +if (!hasEvent || !hasTarget) { root.EventTarget = (function () { function EventTarget() { this.__listeners = new Map();