Skip to content

Commit df71b58

Browse files
committed
events: expose Event statics
1 parent e0586d0 commit df71b58

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/internal/event_target.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Event {
114114
get bubbles() { return this.#bubbles; }
115115
get composed() { return this.#composed; }
116116
get eventPhase() {
117-
return this[kTarget] ? 2 : 0; // Equivalent to AT_TARGET or NONE
117+
return this[kTarget] ? Event.AT_TARGET : Event.NONE;
118118
}
119119
get cancelBubble() { return this.#propagationStopped; }
120120
set cancelBubble(value) {
@@ -127,6 +127,10 @@ class Event {
127127
}
128128

129129
get [Symbol.toStringTag]() { return 'Event'; }
130+
static NONE = 0;
131+
static CAPTURING_PHASE = 1;
132+
static AT_TARGET = 2;
133+
static BUBBLING_PHASE = 3;
130134
}
131135

132136
// The listeners for an EventTarget are maintained as a linked list.

test/parallel/test-eventtarget.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,21 @@ ok(EventTarget);
432432
target.removeEventListener('foo', a, { capture: false });
433433
target.dispatchEvent(new Event('foo'));
434434
}
435-
436435
{
437436
const target = new EventTarget();
438437
strictEqual(target.toString(), '[object EventTarget]');
439438
const event = new Event('');
440439
strictEqual(event.toString(), '[object Event]');
441440
}
441+
{
442+
strictEqual(Event.NONE, 0);
443+
strictEqual(Event.CAPTURING_PHASE, 1);
444+
strictEqual(Event.AT_TARGET, 2);
445+
strictEqual(Event.BUBBLING_PHASE, 3);
446+
strictEqual(new Event().eventPhase, Event.NONE);
447+
const target = new EventTarget();
448+
target.addEventListener('foo', common.mustCall((e) => {
449+
strictEqual(e.eventPhase, Event.AT_TARGET);
450+
}), { once: true });
451+
target.dispatchEvent(new Event('foo'));
452+
}

0 commit comments

Comments
 (0)