1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00

fix(ext/web): don't ignore capture in EventTarget.removeEventListener (#25788)

This commit is contained in:
carles escrig royo 2024-09-23 11:19:59 +02:00 committed by GitHub
parent 37cedefb4d
commit 8f32a1577e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 7 deletions

View file

@ -7,7 +7,6 @@
import { core, primordials } from "ext:core/mod.js";
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypeMap,
@ -982,12 +981,7 @@ class EventTarget {
);
const { listeners } = self[eventTargetData];
if (callback !== null && listeners[type]) {
listeners[type] = ArrayPrototypeFilter(
listeners[type],
(listener) => listener.callback !== callback,
);
} else if (callback === null || !listeners[type]) {
if (callback === null || !listeners[type]) {
return;
}

View file

@ -67,6 +67,64 @@ Deno.test(function anEventTargetCanBeSubclassed() {
assertEquals(callCount, 0);
});
Deno.test(function removeEventListenerTest() {
const target = new EventTarget();
let callCount = 0;
const listener = () => {
++callCount;
};
target.addEventListener("incr", listener, true);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 1);
// Should not remove the listener because useCapture does not match
target.removeEventListener("incr", listener, false);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 2);
// Should remove the listener because useCapture matches
target.removeEventListener("incr", listener, true);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 2);
// Only the capture setting matters to removeEventListener
target.addEventListener("incr", listener, { passive: true });
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 3);
// Should not remove the listener because useCapture does not match
target.removeEventListener("incr", listener, { capture: true });
target.removeEventListener("incr", listener, true);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 4);
// Should remove the listener because useCapture matches
target.removeEventListener("incr", listener);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 4);
// Again, should remove the listener because useCapture matches
target.addEventListener("incr", listener, { passive: true });
target.removeEventListener("incr", listener, false);
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 4);
// Again, should remove the listener because useCapture matches
target.addEventListener("incr", listener, { passive: true });
target.removeEventListener("incr", listener, { capture: false });
target.dispatchEvent(new Event("incr"));
assertEquals(callCount, 4);
});
Deno.test(function removingNullEventListenerShouldSucceed() {
const document = new EventTarget();
assertEquals(document.removeEventListener("x", null, false), undefined);