Merge pull request #1985 from lightpanda-io/intersection_observer_root_document

Allow Document to be the root of an intersection observer
This commit is contained in:
Karl Seguin
2026-03-26 07:41:40 +08:00
committed by GitHub

View File

@@ -25,6 +25,8 @@ const Allocator = std.mem.Allocator;
const Page = @import("../Page.zig"); const Page = @import("../Page.zig");
const Session = @import("../Session.zig"); const Session = @import("../Session.zig");
const Node = @import("Node.zig");
const Element = @import("Element.zig"); const Element = @import("Element.zig");
const DOMRect = @import("DOMRect.zig"); const DOMRect = @import("DOMRect.zig");
@@ -55,7 +57,7 @@ var zero_rect: DOMRect = .{
}; };
pub const ObserverInit = struct { pub const ObserverInit = struct {
root: ?*Element = null, root: ?*Node = null,
rootMargin: ?[]const u8 = null, rootMargin: ?[]const u8 = null,
threshold: Threshold = .{ .scalar = 0.0 }, threshold: Threshold = .{ .scalar = 0.0 },
@@ -81,11 +83,25 @@ pub fn init(callback: js.Function.Temp, options: ?ObserverInit, page: *Page) !*I
.array => |arr| try arena.dupe(f64, arr), .array => |arr| try arena.dupe(f64, arr),
}; };
const root: ?*Element = blk: {
const root_opt = opts.root orelse break :blk null;
switch (root_opt._type) {
.element => |el| break :blk el,
.document => {
// not strictly correct, `null` means the viewport, not the
// entire document, but since we don't render anything, this
// should be fine.
break :blk null;
},
else => return error.TypeError,
}
};
const self = try arena.create(IntersectionObserver); const self = try arena.create(IntersectionObserver);
self.* = .{ self.* = .{
._arena = arena, ._arena = arena,
._callback = callback, ._callback = callback,
._root = opts.root, ._root = root,
._root_margin = root_margin, ._root_margin = root_margin,
._threshold = threshold, ._threshold = threshold,
}; };