diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index fd26a37df3fe32..7c34acd5c5dbe1 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -125,7 +125,7 @@ class Event { get bubbles() { return this.#bubbles; } get composed() { return this.#composed; } get eventPhase() { - return this[kTarget] ? 2 : 0; // Equivalent to AT_TARGET or NONE + return this[kTarget] ? Event.AT_TARGET : Event.NONE; } get cancelBubble() { return this.#propagationStopped; } set cancelBubble(value) { @@ -136,6 +136,11 @@ class Event { stopPropagation() { this.#propagationStopped = true; } + + static NONE = 0; + static CAPTURING_PHASE = 1; + static AT_TARGET = 2; + static BUBBLING_PHASE = 3; } Object.defineProperty(Event.prototype, SymbolToStringTag, { diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 2ff77d4028b8d9..947477de5a7768 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -432,7 +432,6 @@ ok(EventTarget); target.removeEventListener('foo', a, { capture: false }); target.dispatchEvent(new Event('foo')); } - { const target = new EventTarget(); strictEqual(target.toString(), '[object EventTarget]'); @@ -464,3 +463,16 @@ ok(EventTarget); }); }); } + +{ + strictEqual(Event.NONE, 0); + strictEqual(Event.CAPTURING_PHASE, 1); + strictEqual(Event.AT_TARGET, 2); + strictEqual(Event.BUBBLING_PHASE, 3); + strictEqual(new Event('foo').eventPhase, Event.NONE); + const target = new EventTarget(); + target.addEventListener('foo', common.mustCall((e) => { + strictEqual(e.eventPhase, Event.AT_TARGET); + }), { once: true }); + target.dispatchEvent(new Event('foo')); +}