Skip to content

Commit f643cf4

Browse files
benjamingrBenjamin Gruenbaum
authored and
Benjamin Gruenbaum
committed
event: cancelBubble is a property
Event#cancelBubble is property (and not a function). Change Event#cancelBubble to a property and add a test. PR-URL: #33613 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 8ae28ff commit f643cf4

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/internal/event_target.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ class Event {
3636
#cancelable = false;
3737
#timestamp = perf_hooks.performance.now();
3838

39-
// Neither of these are currently used in the Node.js implementation
39+
// None of these are currently used in the Node.js implementation
4040
// of EventTarget because there is no concept of bubbling or
4141
// composition. We preserve their values in Event but they are
4242
// non-ops and do not carry any semantics in Node.js
4343
#bubbles = false;
4444
#composed = false;
45+
#propagationStopped = false;
4546

4647

4748
constructor(type, options) {
@@ -54,6 +55,7 @@ class Event {
5455
this.#cancelable = !!cancelable;
5556
this.#bubbles = !!bubbles;
5657
this.#composed = !!composed;
58+
this.#propagationStopped = false;
5759
this.#type = String(type);
5860
// isTrusted is special (LegacyUnforgeable)
5961
Object.defineProperty(this, 'isTrusted', {
@@ -113,11 +115,14 @@ class Event {
113115
get eventPhase() {
114116
return this[kTarget] ? 2 : 0; // Equivalent to AT_TARGET or NONE
115117
}
116-
cancelBubble() {
117-
// Non-op in Node.js. Alias for stopPropagation
118+
get cancelBubble() { return this.#propagationStopped; }
119+
set cancelBubble(value) {
120+
if (value) {
121+
this.stopPropagation();
122+
}
118123
}
119124
stopPropagation() {
120-
// Non-op in Node.js
125+
this.#propagationStopped = true;
121126
}
122127

123128
get [Symbol.toStringTag]() { return 'Event'; }

test/parallel/test-eventtarget.js

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ok(EventTarget);
3636
strictEqual(ev.composed, false);
3737
strictEqual(ev.isTrusted, false);
3838
strictEqual(ev.eventPhase, 0);
39+
strictEqual(ev.cancelBubble, false);
3940

4041
// Not cancelable
4142
ev.preventDefault();
@@ -50,6 +51,24 @@ ok(EventTarget);
5051
const ev = new Event('foo', {}, {});
5152
strictEqual(ev.type, 'foo');
5253
}
54+
{
55+
const ev = new Event('foo');
56+
strictEqual(ev.cancelBubble, false);
57+
ev.cancelBubble = true;
58+
strictEqual(ev.cancelBubble, true);
59+
}
60+
{
61+
const ev = new Event('foo');
62+
strictEqual(ev.cancelBubble, false);
63+
ev.stopPropagation();
64+
strictEqual(ev.cancelBubble, true);
65+
}
66+
{
67+
const ev = new Event('foo');
68+
strictEqual(ev.cancelBubble, false);
69+
ev.cancelBubble = 'some-truthy-value';
70+
strictEqual(ev.cancelBubble, true);
71+
}
5372
{
5473
const ev = new Event('foo', { cancelable: true });
5574
strictEqual(ev.type, 'foo');

0 commit comments

Comments
 (0)