events: expose Event statics

PR-URL: https://github.com/nodejs/node/pull/34015
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Benjamin Gruenbaum
2020-05-30 02:56:01 +03:00
committed by James M Snell
parent a30babf672
commit 9d324f0441
2 changed files with 19 additions and 2 deletions

View File

@@ -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, {

View File

@@ -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'));
}