Skip to content

Lightpanda: the headless browser designed for AI and automation

take slotted content through its slot, matching chrome

Karl Seguin779840b1dd5f
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 9c6a1afd9..3317c9767 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -716,13 +716,13 @@ pub fn hasPointerEventsNone(self: *StyleManager, el: *Element) bool {
return self.visibilityProps(el).pointer_events_none;
}
-/// Blink's VisibleToHitTestRequest. Ignores display:none, which callers handle
-/// for the whole subtree.
+/// Blink's VisibleToHitTestRequest, plus display:contents, which generates no
+/// box to hit. Ignores display:none, which callers handle for the whole subtree.
pub fn isHitTestable(self: *StyleManager, el: *Element) bool {
self.assertOwns(el);
self.rebuildIfDirty() catch return true;
const p = self.visibilityProps(el);
- return !p.visibility_hidden and !p.pointer_events_none;
+ return !p.visibility_hidden and !p.pointer_events_none and p.box != .contents;
}
/// Computed text-transform, which inherits: the nearest of `el` and its
@@ -1579,6 +1579,7 @@ const Box = enum(u2) {
fn uaDefault(tag: Tag) Box {
return switch (tag) {
.table => .table,
+ .slot => .contents,
.form, .li, .dd, .dt, .details, .summary, .menu, .dir, .hgroup, .legend, .html, .body, .optgroup, .option => .block_level,
else => if (tag.isBlock()) .block_level else .inline_level,
};
diff --git a/src/browser/tests/document/element_from_point_shadow.html b/src/browser/tests/document/element_from_point_shadow.html
index 71b9693c3..fdf3dc409 100644
--- a/src/browser/tests/document/element_from_point_shadow.html
+++ b/src/browser/tests/document/element_from_point_shadow.html
@@ -72,6 +72,27 @@
}
</script>
+<script id="shadow_root_elements_from_point_slotted">
+{
+ const host = document.createElement('div');
+ host.innerHTML = '<span style="display:block;width:100px;height:40px">light</span>';
+ document.body.appendChild(host);
+ const root = host.attachShadow({mode: 'open'});
+ root.innerHTML = '<div id="wrap"><slot></slot></div>';
+ const light = host.firstChild;
+ const wrap = root.getElementById('wrap');
+ const r = light.getBoundingClientRect();
+ const html = document.documentElement;
+
+ // slotted content's ancestors in the shadow tree count, the <slot> doesn't:
+ // it's display:contents
+ testing.expectEqual(light, root.elementFromPoint(r.x + 1, r.y + 1));
+ testing.expectEqual([light, wrap, host, document.body, html], root.elementsFromPoint(r.x + 1, r.y + 1));
+ testing.expectEqual([light, host, document.body, html], document.elementsFromPoint(r.x + 1, r.y + 1));
+ host.remove();
+}
+</script>
+
<script id="slotted_content_laid_out_in_slot">
{
const host = document.getElementById('slot_host');
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zig
index 581d94051..7026c16b8 100644
--- a/src/browser/webapi/Document.zig
+++ b/src/browser/webapi/Document.zig
@@ -945,13 +945,25 @@ pub fn hitTest(self: *Document, x: f64, y: f64, frame: *Frame) !?*Element {
// document.elementFromPoint only sees the document's tree: a hit inside a
// shadow tree reports its outermost host, as in Chrome.
fn elementFromPoint(self: *Document, x: f64, y: f64, frame: *Frame) !?*Element {
+ return self.elementFromPointIn(self.asNode(), x, y, frame);
+}
+
+/// DocumentOrShadowRoot.elementFromPoint for `scope`, this document or one of
+/// its shadow roots.
+pub fn elementFromPointIn(self: *Document, scope: *Node, x: f64, y: f64, frame: *Frame) !?*Element {
const hit = (try self.hitTest(x, y, frame)) orelse return null;
- return retargetToDocumentTree(hit);
+ return retarget(hit, scope);
}
-fn retargetToDocumentTree(element: *Element) *Element {
+// The spec's retarget: climbs out of shadow trees until `element` is in a tree
+// that `scope` is in or inside of. A hit in a shadow tree nested in `scope`'s
+// reports its host in `scope`'s tree; a hit outside it is returned as is.
+fn retarget(element: *Element, scope: *Node) *Element {
var current = element;
while (current.asNode().containingShadowRoot()) |shadow| {
+ if (shadow.asNode().isHostIncludingInclusiveAncestorOf(scope)) {
+ break;
+ }
current = shadow._host;
}
return current;
@@ -1038,16 +1050,32 @@ fn hitTestImpl(self: *Document, viewport_x: f64, viewport_y: f64, ignore_x: bool
}
fn elementsFromPoint(self: *Document, x: f64, y: f64, frame: *Frame) ![]const *Element {
- // Get topmost element
- var current: ?*Element = (try self.elementFromPoint(x, y, frame)) orelse return &.{};
+ return self.elementsFromPointIn(self.asNode(), x, y, frame);
+}
+
+/// DocumentOrShadowRoot.elementsFromPoint for `scope`, as Blink builds it: the
+/// hit and its flat-tree ancestors that can be hit, each retargeted against
+/// `scope` with repeats dropped, then the root element.
+pub fn elementsFromPointIn(self: *Document, scope: *Node, x: f64, y: f64, frame: *Frame) ![]const *Element {
+ const hit = (try self.hitTest(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| {
- // Chrome always ends the list with the root element.
- if (el.parentElement() == null or style_manager.isHitTestable(el)) {
- try result.append(frame.local_arena, el);
+ var current: ?*Element = hit;
+ while (current) |el| : (current = el.asNode().flatTreeParentElement(frame)) {
+ if (style_manager.isHitTestable(el) == false) {
+ continue;
+ }
+ const target = retarget(el, scope);
+ if (result.items.len > 0 and result.items[result.items.len - 1] == target) {
+ continue;
+ }
+ try result.append(frame.local_arena, target);
+ }
+ // Chrome always ends the list with the root element.
+ if (self.getDocumentElement()) |root| {
+ if (result.items.len == 0 or result.items[result.items.len - 1] != root) {
+ try result.append(frame.local_arena, root);
}
- current = el.parentElement();
}
return result.items;
}
diff --git a/src/browser/webapi/ShadowRoot.zig b/src/browser/webapi/ShadowRoot.zig
index 7712a4181..da1335d7b 100644
--- a/src/browser/webapi/ShadowRoot.zig
+++ b/src/browser/webapi/ShadowRoot.zig
@@ -163,48 +163,14 @@ 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;
+ const node = self.asNode();
+ return node.getDocument(frame).elementFromPointIn(node, x, y, frame);
}
-// 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;
+ const node = self.asNode();
+ return node.getDocument(frame).elementsFromPointIn(node, x, y, frame);
}
pub const JsApi = struct {