Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: scroll-blocking listeners are passive by default on root targets

Francis Bouvier46e9497d35b1
diff --git a/src/browser/webapi/EventTarget.zig b/src/browser/webapi/EventTarget.zig
index ce130a143..0da77ee3b 100644
--- a/src/browser/webapi/EventTarget.zig
+++ b/src/browser/webapi/EventTarget.zig
@@ -93,14 +93,45 @@ const AddEventListenerOptions = union(enum) {
// The signal is kept as a raw js.Value so that an explicit null (or any
// non-AbortSignal value) can be told apart from an absent or undefined
// member, and rejected with a TypeError per the dictionary conversion.
+ // passive is optional so that an absent (or undefined) member falls back
+ // to the type- and target-dependent default passive value.
const Options = struct {
once: bool = false,
capture: bool = false,
- passive: bool = false,
+ passive: ?bool = null,
signal: ?js.Value = null,
};
};
+// The DOM spec's "default passive value": listeners for the scroll-blocking
+// event types are passive by default on the window, the document, and the
+// html and body elements.
+fn defaultPassiveValue(self: *EventTarget, typ: []const u8) bool {
+ const scroll_blocking = std.mem.eql(u8, typ, "touchstart") or
+ std.mem.eql(u8, typ, "touchmove") or
+ std.mem.eql(u8, typ, "wheel") or
+ std.mem.eql(u8, typ, "mousewheel");
+ if (!scroll_blocking) {
+ return false;
+ }
+
+ switch (self._type) {
+ .window => return true,
+ .node => |n| {
+ const Element = @import("Element.zig");
+ if (n._type == .document) {
+ return true;
+ }
+ const element = n.is(Element) orelse return false;
+ return switch (element.getTag()) {
+ .html, .body => true,
+ else => false,
+ };
+ },
+ else => return false,
+ }
+}
+
pub const EventListenerCallback = union(enum) {
function: js.Function,
object: js.Object,
@@ -109,12 +140,12 @@ pub fn addEventListener(self: *EventTarget, typ: []const u8, callback_: ?EventLi
// Convert the options before the null-callback early return: per spec,
// the dictionary conversion throws even when the callback is null.
const options = blk: {
- const o = opts_ orelse break :blk RegisterOptions{};
+ const o = opts_ orelse break :blk RegisterOptions{ .passive = self.defaultPassiveValue(typ) };
break :blk switch (o) {
.options => |opts| RegisterOptions{
.once = opts.once,
.capture = opts.capture,
- .passive = opts.passive,
+ .passive = opts.passive orelse self.defaultPassiveValue(typ),
.signal = signal: {
const signal = opts.signal orelse break :signal null;
if (signal.isUndefined()) {
@@ -123,7 +154,7 @@ pub fn addEventListener(self: *EventTarget, typ: []const u8, callback_: ?EventLi
break :signal try signal.toZig(*AbortSignal);
},
},
- .capture => |capture| RegisterOptions{ .capture = capture },
+ .capture => |capture| RegisterOptions{ .capture = capture, .passive = self.defaultPassiveValue(typ) },
};
};