nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.54k stars 29.04k forks source link

EventTarget is not DOM spec Compliant #54617

Open RedYetiDev opened 2 weeks ago

RedYetiDev commented 2 weeks ago

While Node.js's node:events module provides similar functionality to chromium, it does not comply with the DOM spec.

Consider the following code snippet:

const target = new EventTarget();
const event = new Event('foo');
target.dispatchEvent(event);
console.log(event.currentTarget);

In Node.js (and Chromium), this code will log EventTarget {}. However, in other engines and browsers, it will log null. As a result, Node.js may not be fully compliant with EventTarget-constructible.any.js.

RedYetiDev commented 2 weeks ago

CC @nodejs/web-standards @nodejs/events

RedYetiDev commented 2 weeks ago

WebKit (https://github.com/WebKit/WebKit/blob/main/Source/WebCore/dom/EventTarget.cpp#L240) does the following:

    EventPath eventPath { *this };
    event.setTarget(this);
    event.setCurrentTarget(this);
    event.setEventPhase(Event::AT_TARGET);
    event.resetBeforeDispatch();
    event.setEventPath(eventPath);
    fireEventListeners(event, EventInvokePhase::Capturing);
    fireEventListeners(event, EventInvokePhase::Bubbling);
    event.resetAfterDispatch();

Where resetAfterDispatch sets the event target to null, among other things.

KhafraDev commented 2 weeks ago

https://dom.spec.whatwg.org/#concept-event-dispatch

  1. Set event’s currentTarget attribute to null.