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

events: expose Event statics #33649

Closed
wants to merge 3 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,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) {
Expand All @@ -132,6 +132,11 @@ class Event {
stopPropagation() {
this.#propagationStopped = true;
}

static NONE = 0;
static CAPTURING_PHASE = 1;
static AT_TARGET = 2;
static BUBBLING_PHASE = 3;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment in here that we currently do not use the CAPTURING_PHASE and BUBBLING_PHASE but that they need to be preserved anyway because of the spec would be good.

}

Object.defineProperty(Event.prototype, SymbolToStringTag, {
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,21 @@ ok(EventTarget);
target.removeEventListener('foo', a, { capture: false });
target.dispatchEvent(new Event('foo'));
}

{
const target = new EventTarget();
strictEqual(target.toString(), '[object EventTarget]');
const event = new Event('');
strictEqual(event.toString(), '[object Event]');
}
{
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'));
}