Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add elementFromPoint and elementsFromPoint to ShadowRoot

Jay Hemnani62e335888865
diff --git a/src/browser/tests/document/element_from_point_shadow.html b/src/browser/tests/document/element_from_point_shadow.html
index b15df33ba..71b9693c3 100644
--- a/src/browser/tests/document/element_from_point_shadow.html
+++ b/src/browser/tests/document/element_from_point_shadow.html
@@ -30,6 +30,48 @@
}
</script>
+<script id="shadow_root_element_from_point">
+{
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const root = host.attachShadow({mode: 'open'});
+ root.innerHTML = '<button id="sb" style="width:100px;height:40px">In shadow</button><div id="inner"></div>';
+ const button = root.getElementById('sb');
+ const inner = root.getElementById('inner');
+ const nested = inner.attachShadow({mode: 'open'});
+ nested.innerHTML = '<i id="deep" style="display:block;width:100px;height:40px">deep</i>';
+ const deep = nested.getElementById('deep');
+ const status = document.getElementById('after');
+
+ const center = (el) => {
+ const r = el.getBoundingClientRect();
+ return [r.x + 1, r.y + 1];
+ };
+
+ const [bx, by] = center(button);
+ testing.expectEqual(host, document.elementFromPoint(bx, by));
+ testing.expectEqual(button, root.elementFromPoint(bx, by));
+ testing.expectEqual(button, nested.elementFromPoint(bx, by));
+ const all = root.elementsFromPoint(bx, by);
+ testing.expectEqual(button, all[0]);
+ testing.expectEqual(host, all[1]);
+ testing.expectEqual(document.documentElement, all[all.length - 1]);
+
+ // a hit in a nested shadow tree reports its host in this tree
+ const [dx, dy] = center(deep);
+ testing.expectEqual(inner, root.elementFromPoint(dx, dy));
+ testing.expectEqual(deep, nested.elementFromPoint(dx, dy));
+ testing.expectEqual([deep, inner, host], nested.elementsFromPoint(dx, dy).slice(0, 3));
+
+ // a hit outside the shadow tree is returned as is
+ const [sx, sy] = center(status);
+ testing.expectEqual(status, root.elementFromPoint(sx, sy));
+ testing.expectEqual(null, root.elementFromPoint(-5, -5));
+ testing.expectEqual(0, root.elementsFromPoint(-5, -5).length);
+ host.remove();
+}
+</script>
+
<script id="slotted_content_laid_out_in_slot">
{
const host = document.getElementById('slot_host');
diff --git a/src/browser/webapi/ShadowRoot.zig b/src/browser/webapi/ShadowRoot.zig
index 348dff903..7712a4181 100644
--- a/src/browser/webapi/ShadowRoot.zig
+++ b/src/browser/webapi/ShadowRoot.zig
@@ -163,6 +163,50 @@ fn setAdoptedStyleSheets(self: *ShadowRoot, sheets: js.Object) !void {
self._adopted_style_sheets = try sheets.persist();
}
+// DocumentOrShadowRoot: the topmost element at the point, retargeted against
+// this shadow root. A hit in a shadow tree nested in this one reports its host
+// in this tree; a hit outside this tree is returned as is, as in Chrome.
+fn elementFromPoint(self: *ShadowRoot, x: f64, y: f64, frame: *Frame) !?*Element {
+ const document = self.asNode().ownerDocument(frame) orelse return null;
+ const hit = (try document.hitTest(x, y, frame)) orelse return null;
+ var current = hit;
+ while (current.asNode().containingShadowRoot()) |shadow| {
+ if (self.isInTreeOf(shadow)) {
+ break;
+ }
+ current = shadow._host;
+ }
+ return current;
+}
+
+// Whether this shadow root is `shadow` or sits in a tree inside it.
+fn isInTreeOf(self: *ShadowRoot, shadow: *ShadowRoot) bool {
+ var current: ?*ShadowRoot = self;
+ while (current) |root| : (current = root._host.asNode().containingShadowRoot()) {
+ if (root == shadow) {
+ return true;
+ }
+ }
+ return false;
+}
+
+// The element at the point and its ancestors, leaving each shadow tree through
+// its host, like Document.elementsFromPoint.
+fn elementsFromPoint(self: *ShadowRoot, x: f64, y: f64, frame: *Frame) ![]const *Element {
+ var current: ?*Element = (try self.elementFromPoint(x, y, frame)) orelse return &.{};
+ const style_manager = &(self.asNode().ownerFrame(frame) orelse return &.{})._style_manager;
+ var result: std.ArrayList(*Element) = .empty;
+ while (current) |el| {
+ const parent = el.parentElement() orelse if (el.asNode().containingShadowRoot()) |shadow| shadow._host else null;
+ // Chrome always ends the list with the root element.
+ if (parent == null or style_manager.isHitTestable(el)) {
+ try result.append(frame.local_arena, el);
+ }
+ current = parent;
+ }
+ return result.items;
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(ShadowRoot);
@@ -190,6 +234,8 @@ pub const JsApi = struct {
return self.getElementById(try value.toZig([]const u8));
}
pub const adoptedStyleSheets = bridge.accessor(ShadowRoot.getAdoptedStyleSheets, ShadowRoot.setAdoptedStyleSheets, .{});
+ pub const elementFromPoint = bridge.function(ShadowRoot.elementFromPoint, .{});
+ pub const elementsFromPoint = bridge.function(ShadowRoot.elementsFromPoint, .{});
pub const setHTML = bridge.function(ShadowRoot.setHTML, .{ .ce_reactions = true });
pub const setHTMLUnsafe = bridge.function(ShadowRoot.setHTMLUnsafe, .{ .ce_reactions = true });
pub const getHTML = bridge.function(_getHTML, .{});