Lightpanda: the headless browser designed for AI and automation
Probe own visibility inside walks that prune hidden subtrees
Adrià Arrufatbebbbaf3d8d3
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex ae502270a..fa113fdcb 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -687,6 +687,13 @@ pub fn isHidden(self: *StyleManager, el: *Element, options: CheckVisibilityOptioreturn self.anyInChain(el, .hidden, options);}+/// The element's own properties only, for walks that never enter a hidden+/// subtree and so already know every ancestor is visible.+pub fn isHiddenSelf(self: *StyleManager, el: *Element, options: CheckVisibilityOptions) bool {+ self.rebuildIfDirty() catch return false;+ return self.ownProps(el).probe(.hidden, options);+}+/// Computed display:none for a single element (own property, no ancestor walk)./// Honors the UA stylesheet rules per HTML Rendering §15.3.1 "Hidden elements".pub fn hasDisplayNone(self: *StyleManager, el: *Element) bool {diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex f0b3eb87d..b8ba078b0 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -917,16 +917,23 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:// preorder counter instead of calling calculateDocumentPosition per node// (which itself is O(N)). Once the counter's y passes the query y, no// later element can contain the point, and we can return.+ //+ // Hidden subtrees still count towards the index so positions agree with+ // getBoundingClientRect; the hidden verdict just rides down the stack.var topmost: ?*Element = null;const root = self.asNode();- var stack: std.ArrayList(*Node) = .empty;- try stack.append(frame.local_arena, root);+ const style_manager = &root.ownerFrame(frame)._style_manager;+ const Entry = struct { node: *Node, hidden: bool };+ var stack: std.ArrayList(Entry) = .empty;+ try stack.append(frame.local_arena, .{ .node = root, .hidden = false });var preorder_index: f64 = 0;while (stack.items.len > 0) {- const node = stack.pop() orelse break;+ const entry = stack.pop() orelse break;+ const node = entry.node;+ var hidden = entry.hidden;const pos = preorder_index * 5.0;if (pos > y) {@@ -936,7 +943,8 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:preorder_index += 1;if (node.is(Element)) |element| {- if (element.isVisible(frame)) {+ hidden = hidden or style_manager.hasDisplayNone(element);+ if (!hidden) {if (y >= pos and y <= pos + element.boxAxis(frame, .height)) {if (ignore_x) {topmost = element;@@ -955,7 +963,7 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:// Add children to stack in reverse order so we process them in document ordervar child = node.lastChild();while (child) |c| {- try stack.append(frame.local_arena, c);+ try stack.append(frame.local_arena, .{ .node = c, .hidden = hidden });child = c.previousSibling();}}diff --git a/src/server/cdp/AXNode.zig b/src/server/cdp/AXNode.zigindex e47ed699f..ce7c6cee0 100644--- a/src/server/cdp/AXNode.zig+++ b/src/server/cdp/AXNode.zig@@ -84,7 +84,8 @@ pub const Writer = struct {try self.walkQuery(self.root.dom, false, w);} else {const root = AXNode.fromNode(self.root.dom);- if (try self.writeNode(self.root.id, root, false, w)) {+ const root_hidden = if (self.root.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame) else false;+ if (try self.writeNode(self.root.id, root, false, root_hidden, w)) {try self.writeNodeChildren(root, false, w);}}@@ -146,7 +147,7 @@ pub const Writer = struct {// visibility:hidden, aria-hidden, hidden, inert). Matches// Chromium: these elements aren't exposed to the AX tree.const child_el = dom_node.as(DOMNode.Element);- if (child_in_aria_hidden or isHidden(child_el, self.frame)) {+ if (child_in_aria_hidden or isHiddenSelf(child_el, self.frame)) {continue;}},@@ -155,7 +156,7 @@ pub const Writer = struct {const node = try self.registry.register(dom_node);const axn = AXNode.fromNode(node.dom);- if (try self.writeNode(node.id, axn, child_in_aria_hidden, w)) {+ if (try self.writeNode(node.id, axn, child_in_aria_hidden, false, w)) {try self.writeNodeChildren(axn, child_in_aria_hidden, w);}}@@ -519,7 +520,7 @@ pub const Writer = struct {}// write a node. returns true if children must be written.- fn writeNode(self: *const Writer, id: u32, axn: AXNode, in_aria_hidden: bool, w: anytype) !bool {+ fn writeNode(self: *const Writer, id: u32, axn: AXNode, in_aria_hidden: bool, hidden: bool, w: anytype) !bool {// ignore empty textstry w.beginObject();@@ -535,7 +536,7 @@ pub const Writer = struct {try w.objectField("role");try self.writeAXValue(.{ .role = resolved.role }, w);- const ignore = axn.isIgnore(self.frame, in_aria_hidden);+ const ignore = axn.isIgnore(self.frame, in_aria_hidden, hidden);try w.objectField("ignored");try w.write(ignore);@@ -617,7 +618,7 @@ pub const Writer = struct {// Skip hidden element children so childIds matches the// subtree-pruning done in writeNodeChildren.if (child.is(DOMNode.Element)) |child_el| {- if (child_in_aria_hidden or isHidden(child_el, self.frame)) {+ if (child_in_aria_hidden or isHiddenSelf(child_el, self.frame)) {continue;}}@@ -712,7 +713,8 @@ pub const Writer = struct {}const node = try self.registry.register(axn.dom);- const ignored = axn.isIgnore(self.frame, in_aria_hidden);+ const hidden = if (axn.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame) else false;+ const ignored = axn.isIgnore(self.frame, in_aria_hidden, hidden);try w.beginObject();@@ -1278,28 +1280,25 @@ fn scratchAllocator(temp_arena: ?*lp.Arena, frame: *Frame) std.mem.Allocator {return if (temp_arena) |a| a.allocator() else frame.call_arena;}+// Hidden by its own attributes, or by display:none / visibility:hidden on+// it or an ancestor. Matches Chromium's AX tree which prunes both.fn isHidden(elt: *DOMNode.Element, frame: *Frame) bool {+ return hasHidingAttribute(elt) or frame._style_manager.isHidden(elt, .{ .check_visibility = true });+}++// Own attributes and cascade only: for walks that already pruned hidden+// subtrees, so every ancestor is known visible.+fn isHiddenSelf(elt: *DOMNode.Element, frame: *Frame) bool {+ return hasHidingAttribute(elt) or frame._style_manager.isHiddenSelf(elt, .{ .check_visibility = true });+}++fn hasHidingAttribute(elt: *DOMNode.Element) bool {if (elt.getAttributeInterned("aria-hidden")) |value| {if (std.mem.eql(u8, value, "true")) {return true;}}-- if (elt.hasAttributeInterned("hidden")) {- return true;- }-- if (elt.hasAttributeSafe(comptime .wrap("inert"))) {- return true;- }-- // CSS display:none and visibility:hidden (both inherited from ancestors via- // style computation). Matches Chromium's AX tree which prunes both.- if (frame._style_manager.isHidden(elt, .{ .check_visibility = true })) {- return true;- }-- return false;+ return elt.hasAttributeInterned("hidden") or elt.hasAttributeSafe(comptime .wrap("inert"));}fn ignoreText(node: *DOMNode) bool {@@ -1344,7 +1343,9 @@ fn ignoreChildren(self: AXNode) bool {};}-fn isIgnore(self: AXNode, frame: *Frame, in_aria_hidden: bool) bool {+// `hidden` comes from the caller: the tree walk prunes, so its children never+// are; the root and the query walk probe the whole chain.+fn isIgnore(self: AXNode, frame: *Frame, in_aria_hidden: bool, hidden: bool) bool {const node = self.dom;const role_attr = self.role_attr;@@ -1390,7 +1391,7 @@ fn isIgnore(self: AXNode, frame: *Frame, in_aria_hidden: bool) bool {return true;}- if (isHidden(elt, frame)) {+ if (hidden) {return true;}@@ -1405,7 +1406,8 @@ fn isIgnore(self: AXNode, frame: *Frame, in_aria_hidden: bool) bool {var it = node.childrenIterator();while (it.next()) |child| {const axn = AXNode.fromNode(child);- if (!axn.isIgnore(frame, in_aria_hidden)) {+ const child_hidden = if (child.is(DOMNode.Element)) |child_el| isHiddenSelf(child_el, frame) else false;+ if (!axn.isIgnore(frame, in_aria_hidden, child_hidden)) {return false;}}