Skip to content

Commit e1f5ee4

Browse files
committed
lib: fix passive listener implementation with cancelable checks
Complete the DOM passive listener specification implementation by adding missing cancelable condition checks and improving state management. Key improvements: - Add kInPassiveListener symbol property initialization in Event constructor - Fix preventDefault() method to check both cancelable and passive state (ignore preventDefault if not cancelable OR in passive listener) - Fix returnValue setter with same cancelable condition for consistency - Improve cleanup logic in finally block with conditional check instead of unsafe delete operation This fixes the previous incomplete implementation to properly comply with WHATWG DOM specification where preventDefault() calls should be ignored in passive listeners only when the event is cancelable.
1 parent 15ac7dc commit e1f5ee4

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

lib/internal/event_target.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class Event {
128128

129129
this[kTarget] = null;
130130
this[kIsBeingDispatched] = false;
131+
this[kInPassiveListener] = false;
131132
}
132133

133134
/**
@@ -179,9 +180,7 @@ class Event {
179180
preventDefault() {
180181
if (!isEvent(this))
181182
throw new ERR_INVALID_THIS('Event');
182-
if (this[kInPassiveListener]) {
183-
return;
184-
}
183+
if (!this.#cancelable || this[kInPassiveListener]) return;
185184
this.#defaultPrevented = true;
186185
}
187186

@@ -278,9 +277,7 @@ class Event {
278277
throw new ERR_INVALID_THIS('Event');
279278

280279
if (!value) {
281-
if (this[kInPassiveListener]) {
282-
return;
283-
}
280+
if (!this.#cancelable || this[kInPassiveListener]) return;
284281
this.#defaultPrevented = true;
285282
}
286283
}
@@ -855,7 +852,8 @@ class EventTarget {
855852
} catch (err) {
856853
emitUncaughtException(err);
857854
} finally {
858-
delete arg[kInPassiveListener];
855+
if (arg?.[kInPassiveListener])
856+
arg[kInPassiveListener] = false;
859857
}
860858

861859
handler = next;

0 commit comments

Comments
 (0)