Skip to content

Commit c9dec0c

Browse files
benjamingrcodebytere
authored andcommitted
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]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 16b6981 commit c9dec0c

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
@@ -35,12 +35,13 @@ class Event {
3535
#cancelable = false;
3636
#timestamp = perf_hooks.performance.now();
3737

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

4546

4647
constructor(type, options) {
@@ -51,6 +52,7 @@ class Event {
5152
this.#bubbles = !!bubbles;
5253
this.#composed = !!composed;
5354
this.#type = `${type}`;
55+
this.#propagationStopped = false;
5456
// isTrusted is special (LegacyUnforgeable)
5557
Object.defineProperty(this, 'isTrusted', {
5658
get() { return false; },
@@ -109,11 +111,14 @@ class Event {
109111
get eventPhase() {
110112
return this[kTarget] ? 2 : 0; // Equivalent to AT_TARGET or NONE
111113
}
112-
cancelBubble() {
113-
// Non-op in Node.js. Alias for stopPropagation
114+
get cancelBubble() { return this.#propagationStopped; }
115+
set cancelBubble(value) {
116+
if (value) {
117+
this.stopPropagation();
118+
}
114119
}
115120
stopPropagation() {
116-
// Non-op in Node.js
121+
this.#propagationStopped = true;
117122
}
118123

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

test/parallel/test-eventtarget.js

+19
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,30 @@ ok(EventTarget);
3535
strictEqual(ev.composed, false);
3636
strictEqual(ev.isTrusted, false);
3737
strictEqual(ev.eventPhase, 0);
38+
strictEqual(ev.cancelBubble, false);
3839

3940
// Not cancelable
4041
ev.preventDefault();
4142
strictEqual(ev.defaultPrevented, false);
4243
}
44+
{
45+
const ev = new Event('foo');
46+
strictEqual(ev.cancelBubble, false);
47+
ev.cancelBubble = true;
48+
strictEqual(ev.cancelBubble, true);
49+
}
50+
{
51+
const ev = new Event('foo');
52+
strictEqual(ev.cancelBubble, false);
53+
ev.stopPropagation();
54+
strictEqual(ev.cancelBubble, true);
55+
}
56+
{
57+
const ev = new Event('foo');
58+
strictEqual(ev.cancelBubble, false);
59+
ev.cancelBubble = 'some-truthy-value';
60+
strictEqual(ev.cancelBubble, true);
61+
}
4362

4463
{
4564
const ev = new Event('foo', { cancelable: true });

0 commit comments

Comments
 (0)