summaryrefslogtreecommitdiff
path: root/ext/web/02_event.js
diff options
context:
space:
mode:
authorColin Ihrig <cjihrig@gmail.com>2022-05-16 10:46:39 -0400
committerGitHub <noreply@github.com>2022-05-16 10:46:39 -0400
commit1fa75f75c90b744d63e451519311f27ba55c21d7 (patch)
treeeb51197993a50948bba422b219ea781455c10d53 /ext/web/02_event.js
parent8af81d98a6d673fde3a5c2acacf6ad00c53b9223 (diff)
fix(ext/web): throw if listener and signal are null (#14601)
This commit fixes a failing WPT test by making EventTarget's addEventListener() method throw if both the listener and the signal option are null. Fixes: https://github.com/denoland/deno/issues/14593
Diffstat (limited to 'ext/web/02_event.js')
-rw-r--r--ext/web/02_event.js66
1 files changed, 48 insertions, 18 deletions
diff --git a/ext/web/02_event.js b/ext/web/02_event.js
index 06df6429c..9d5169b9e 100644
--- a/ext/web/02_event.js
+++ b/ext/web/02_event.js
@@ -797,20 +797,6 @@
}
}
- function normalizeAddEventHandlerOptions(
- options,
- ) {
- if (typeof options === "boolean" || typeof options === "undefined") {
- return {
- capture: Boolean(options),
- once: false,
- passive: false,
- };
- } else {
- return options;
- }
- }
-
function normalizeEventHandlerOptions(
options,
) {
@@ -889,6 +875,45 @@
};
}
+ // This is lazy loaded because there is a circular dependency with AbortSignal.
+ let addEventListenerOptionsConverter;
+
+ function lazyAddEventListenerOptionsConverter() {
+ addEventListenerOptionsConverter ??= webidl.createDictionaryConverter(
+ "AddEventListenerOptions",
+ [
+ {
+ key: "capture",
+ defaultValue: false,
+ converter: webidl.converters.boolean,
+ },
+ {
+ key: "passive",
+ defaultValue: false,
+ converter: webidl.converters.boolean,
+ },
+ {
+ key: "once",
+ defaultValue: false,
+ converter: webidl.converters.boolean,
+ },
+ {
+ key: "signal",
+ converter: webidl.converters.AbortSignal,
+ },
+ ],
+ );
+ }
+
+ webidl.converters.AddEventListenerOptions = (V, opts) => {
+ if (webidl.type(V) !== "Object" || V === null) {
+ V = { capture: Boolean(V) };
+ }
+
+ lazyAddEventListenerOptionsConverter();
+ return addEventListenerOptionsConverter(V, opts);
+ };
+
class EventTarget {
constructor() {
this[eventTargetData] = getDefaultTargetData();
@@ -899,14 +924,21 @@
callback,
options,
) {
+ const prefix = "Failed to execute 'addEventListener' on 'EventTarget'";
+
webidl.requiredArguments(arguments.length, 2, {
- prefix: "Failed to execute 'addEventListener' on 'EventTarget'",
+ prefix,
});
+
+ options = webidl.converters.AddEventListenerOptions(options, {
+ prefix,
+ context: "Argument 3",
+ });
+
if (callback === null) {
return;
}
- options = normalizeAddEventHandlerOptions(options);
const { listeners } = (this ?? globalThis)[eventTargetData];
if (!(ReflectHas(listeners, type))) {
@@ -936,8 +968,6 @@
this.removeEventListener(type, callback, options);
});
}
- } else if (options?.signal === null) {
- throw new TypeError("signal must be non-null");
}
ArrayPrototypePush(listeners[type], { callback, options });