Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listener removed and re-added during event dispatch #3687

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsdom/living/events/EventTarget-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function innerInvokeEventListeners(eventImpl, listeners, phase, itemInShadowTree
const { capture, once, passive } = listener.options;

// Check if the event listener has been removed since the listeners has been cloned.
if (!listeners[type].includes(listener)) {
if (!listeners[type].some(l => listener.callback.objectReference === l.callback.objectReference)) {
continue;
}

Expand Down
20 changes: 20 additions & 0 deletions test/to-port-to-wpts/level2/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ describe("level2/events", () => {
this.doc.dispatchEvent(event);
assert.equal(es.length, 1, 'should only be handled by one EventListener');
});

// Two listeners are registered on the same target, first listener removes and immediately adds the second listener, and an event is dispatched. Both listeners should see the event.
specify('listener removed and re-added during event dispatch', () => {
let h1 = 0, h2 = 0;
const handler1 = () => {
h1++;
this.doc.removeEventListener("foo", handler2, false);
this.doc.addEventListener("foo", handler2, false);
};
const handler2 = () => {
h2++;
};
this.doc.addEventListener("foo", handler1, false);
this.doc.addEventListener("foo", handler2, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(h1, 1, "handler1 must be called once");
assert.equal(h2, 1, "handler2 must be called once");
})
})

// The Event.initEvent method is called for event returned by DocumentEvent.createEvent("Events")
Expand Down