Skip to content

Lightpanda: the headless browser designed for AI and automation

Fix crash and better aligned with what Chrome does

Karl Seguin700b4c0efc8e
diff --git a/src/NodeRegistry.zig b/src/NodeRegistry.zig
index 8b555a0d6..cb5cf2477 100644
--- a/src/NodeRegistry.zig
+++ b/src/NodeRegistry.zig
@@ -32,6 +32,9 @@ pub const Id = u32;
pub const Node = struct {
id: Id,
+ // The id of the AX tree's synthetic ListMarker node of this <li>, 0 until
+ // asked for. It comes from node_id, so it never collides with a node's.
+ list_marker_id: Id = 0,
dom: *DOMNode,
};
@@ -40,10 +43,8 @@ allocator: Allocator,
node_pool: std.heap.MemoryPool(Node),
lookup_by_id: std.AutoHashMapUnmanaged(Id, *Node),
lookup_by_node: std.HashMapUnmanaged(*DOMNode, *Node, NodeContext, std.hash_map.default_max_load_percentage),
-// Ids of the AX tree's synthetic ListMarker nodes, keyed by their <li>. They
-// come from node_id, so they never collide with a node's id.
-list_markers: std.HashMapUnmanaged(*DOMNode, Id, NodeContext, std.hash_map.default_max_load_percentage),
-list_marker_ids: std.AutoHashMapUnmanaged(Id, *DOMNode),
+// The <li> of each ListMarker id handed out.
+list_marker_owners: std.AutoHashMapUnmanaged(Id, *Node),
pub fn init(allocator: Allocator) NodeRegistry {
return .{
@@ -51,8 +52,7 @@ pub fn init(allocator: Allocator) NodeRegistry {
.node_pool = .empty,
.lookup_by_id = .{},
.lookup_by_node = .{},
- .list_markers = .{},
- .list_marker_ids = .{},
+ .list_marker_owners = .{},
.allocator = allocator,
};
}
@@ -61,16 +61,14 @@ pub fn deinit(self: *NodeRegistry) void {
const allocator = self.allocator;
self.lookup_by_id.deinit(allocator);
self.lookup_by_node.deinit(allocator);
- self.list_markers.deinit(allocator);
- self.list_marker_ids.deinit(allocator);
+ self.list_marker_owners.deinit(allocator);
self.node_pool.deinit(allocator);
}
pub fn reset(self: *NodeRegistry) void {
self.lookup_by_id.clearRetainingCapacity();
self.lookup_by_node.clearRetainingCapacity();
- self.list_markers.clearRetainingCapacity();
- self.list_marker_ids.clearRetainingCapacity();
+ self.list_marker_owners.clearRetainingCapacity();
_ = self.node_pool.reset(self.allocator, .{ .retain_with_limit = 1024 });
}
@@ -79,8 +77,6 @@ pub fn reset(self: *NodeRegistry) void {
/// each node's document.
pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
const page = frame.page;
- self.resetFrameListMarkers(arena, frame);
-
var doomed: std.ArrayListUnmanaged(*Node) = .empty;
var it = self.lookup_by_id.valueIterator();
while (it.next()) |node_ptr| {
@@ -94,50 +90,28 @@ pub fn resetFrame(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
for (doomed.items) |node| {
_ = self.lookup_by_id.remove(node.id);
_ = self.lookup_by_node.remove(node.dom);
- self.node_pool.destroy(node);
- }
-}
-
-// The markers of the evicted <li>s go too: a later <li> at the same address
-// mustn't inherit one. Dropping every marker is harmless, so that's the
-// fallback when there's no memory to pick them out.
-fn resetFrameListMarkers(self: *NodeRegistry, arena: Allocator, frame: *Frame) void {
- const page = frame.page;
- var doomed: std.ArrayListUnmanaged(*DOMNode) = .empty;
- var it = self.list_markers.keyIterator();
- while (it.next()) |li| {
- if (li.*.getDocument(frame)._page == page) {
- doomed.append(arena, li.*) catch {
- self.list_markers.clearRetainingCapacity();
- self.list_marker_ids.clearRetainingCapacity();
- return;
- };
+ if (node.list_marker_id != 0) {
+ _ = self.list_marker_owners.remove(node.list_marker_id);
}
- }
- for (doomed.items) |li| {
- const marker_id = self.list_markers.fetchRemove(li).?.value;
- _ = self.list_marker_ids.remove(marker_id);
+ self.node_pool.destroy(node);
}
}
/// The id of the ListMarker AX node of `li`, the same on every call.
-pub fn listMarkerId(self: *NodeRegistry, li: *DOMNode) !Id {
- const gop = try self.list_markers.getOrPut(self.allocator, li);
- if (gop.found_existing) {
- return gop.value_ptr.*;
+pub fn listMarkerId(self: *NodeRegistry, li: *Node) !Id {
+ if (li.list_marker_id != 0) {
+ return li.list_marker_id;
}
- errdefer _ = self.list_markers.remove(li);
-
const id = self.node_id;
- try self.list_marker_ids.putNoClobber(self.allocator, id, li);
+ try self.list_marker_owners.putNoClobber(self.allocator, id, li);
self.node_id = id + 1;
- gop.value_ptr.* = id;
+ li.list_marker_id = id;
return id;
}
/// The <li> of the ListMarker AX node `id`, if `id` is a marker's.
-pub fn listMarkerOwner(self: *const NodeRegistry, id: Id) ?*DOMNode {
- return self.list_marker_ids.get(id);
+pub fn listMarkerOwner(self: *const NodeRegistry, id: Id) ?*Node {
+ return self.list_marker_owners.get(id);
}
pub fn register(self: *NodeRegistry, dom_node: *DOMNode) !*Node {
@@ -235,9 +209,14 @@ test "NodeRegistry: resetFrame" {
const ra = try registry.register(a_node);
const rb = try registry.register(b_node);
try testing.expectEqual(2, registry.lookup_by_id.count());
+ const a_marker = try registry.listMarkerId(ra);
+ const b_marker = try registry.listMarkerId(rb);
registry.resetFrame(arena.allocator(), frame_a);
+ // An evicted node's marker goes with it.
+ try testing.expectEqual(null, registry.listMarkerOwner(a_marker));
+ try testing.expectEqual(rb, registry.listMarkerOwner(b_marker).?);
try testing.expectEqual(null, registry.lookup_by_id.get(ra.id));
try testing.expectEqual(null, registry.lookup_by_node.get(a_node));
try testing.expectEqual(1, registry.lookup_by_id.count());
@@ -272,12 +251,12 @@ test "NodeRegistry: listMarkerId" {
const frame = page.frame().?;
const li = (try frame.window._document.querySelector(.wrap("#item"), frame)).?.asNode();
- const marker_id = try registry.listMarkerId(li);
- try testing.expectEqual(marker_id, try registry.listMarkerId(li));
- try testing.expectEqual(li, registry.listMarkerOwner(marker_id));
+ const node = try registry.register(li);
+ const marker_id = try registry.listMarkerId(node);
+ try testing.expectEqual(marker_id, try registry.listMarkerId(node));
+ try testing.expectEqual(node, registry.listMarkerOwner(marker_id));
// Marker and node ids share one counter.
- const node = try registry.register(li);
try testing.expect(node.id != marker_id);
try testing.expectEqual(null, registry.listMarkerOwner(node.id));
diff --git a/src/browser/tests/cdp/ax_relatives.html b/src/browser/tests/cdp/ax_relatives.html
index a9037c980..1e51f31bd 100644
--- a/src/browser/tests/cdp/ax_relatives.html
+++ b/src/browser/tests/cdp/ax_relatives.html
@@ -7,6 +7,7 @@
<ol><li id="item">First</li></ol>
<svg id="pic"><circle r="1"></circle></svg>
<div style="display: none"><p id="in-gone">gone</p></div>
+ <h2><a href="#x">a link long enough that its name buffer covers the ancestor chain <b id="deep">deep</b></a></h2>
<iframe id="frame" srcdoc="<button id=inner>Inner</button>">fallback</iframe>
</body>
</html>
diff --git a/src/server/cdp/AXNode.zig b/src/server/cdp/AXNode.zig
index 04d62de25..dbdaf4a14 100644
--- a/src/server/cdp/AXNode.zig
+++ b/src/server/cdp/AXNode.zig
@@ -46,7 +46,7 @@ pub const Writer = struct {
frame: *Frame,
label_index: *Label.LabelByForIndex,
temp_arena: *lp.Arena,
- // When null, emit the full AX tree (getFullAXTree). When set, walk the
+ // When null, emit the nodes `scope` selects. When set, walk the
// subtree visiting all nodes (including AX-ignored ones, per the
// queryAXTree spec) and emit only nodes whose role + accessible name
// match the filter, in a flat shape.
@@ -62,17 +62,25 @@ pub const Writer = struct {
node,
// `root` alone, as a bare AXNode rather than a list (getRootAXNode).
bare_node,
- // `root`, its ancestors, its siblings and its children
+ // `root`, its children unless it's ignored, then its ancestors
// (getPartialAXTree with fetchRelatives).
relatives,
// `root`, then each ancestor up to its frame's document
// (getAXNodeAndAncestors). Like getFullAXTree, this stays in the
// frame: clients stitch frames together themselves.
ancestors,
- // The children of `root` (getChildAXNodes).
+ // The children of `root`, and through an ignored child its own
+ // (getChildAXNodes).
children,
};
+ // What writeNode wrote: whether the node's children follow it, and
+ // whether it's ignored.
+ const Written = struct {
+ children: bool,
+ ignored: bool,
+ };
+
pub const Filter = struct {
role: ?[]const u8 = null,
accessible_name: ?[]const u8 = null,
@@ -136,74 +144,86 @@ pub const Writer = struct {
}
fn writeScope(self: *const Writer, ignore_cache: *IgnoreCache, w: anytype) !void {
- const root = self.root.dom;
if (self.scope == .subtree) {
return self.writeTree(ignore_cache, w);
}
- // Every other scope is about a node in the AX tree. Like Chrome, a
- // node without an AX node is a lone ignored node, and has no children.
- const chain = try self.axChain(root) orelse {
- if (self.scope != .children) {
- try self.writeNotRendered(self.root.id, w);
+ // Every other scope is about a node in the AX tree. A node outside it
+ // is a lone ignored node under its nearest ancestor in the tree, and
+ // has no children.
+ const root = self.root.dom;
+ const nearest = self.nearestInTree(root);
+ const in_tree = if (nearest) |n| n == root else false;
+ if (!in_tree) {
+ switch (self.scope) {
+ .subtree => unreachable,
+ .children => {},
+ .node, .bare_node => try self.writeNotRendered(nearest, w),
+ .ancestors, .relatives => {
+ try self.writeNotRendered(nearest, w);
+ if (nearest) |n| try self.writeAncestors(n, ignore_cache, w);
+ },
}
return;
- };
+ }
switch (self.scope) {
.subtree => unreachable,
- .node, .bare_node => try self.writeChain(chain[0..1], ignore_cache, w),
- // Chrome lists the node first, then its ancestors, siblings and
- // children.
+ .node, .bare_node => _ = try self.writeSingle(root, ignore_cache, w),
+ .ancestors => try self.writeAncestors(root, ignore_cache, w),
+ // Like Chrome: the node, its children unless it's ignored, then
+ // its ancestors.
.relatives => {
- try self.writeChain(chain, ignore_cache, w);
- if (chain.len > 1) {
- try self.writeChildren(chain[1].node, root, ignore_cache, w);
+ const written = try self.writeSingle(root, ignore_cache, w);
+ if (written.children and !written.ignored) {
+ try self.writeChildren(root, ignore_cache, w);
}
- try self.writeChildren(root, null, ignore_cache, w);
+ if (root.flatParent(self.frame)) |parent| {
+ try self.writeAncestors(parent, ignore_cache, w);
+ }
+ },
+ .children => if (!AXNode.fromNode(root).ignoreChildren()) {
+ try self.writeChildren(root, ignore_cache, w);
},
- .ancestors => try self.writeChain(chain, ignore_cache, w),
- .children => try self.writeChildren(root, null, ignore_cache, w),
}
}
- const ChainEntry = struct {
- node: *DOMNode,
- // Never .pruned: the node is in the AX tree.
- hidden: Hidden,
- };
-
- // `node` then its AX ancestors up to its frame's document. Null when
- // `node` isn't in the AX tree: some node on the way isn't listed in its
- // parent's childIds.
- fn axChain(self: *const Writer, node: *DOMNode) !?[]const ChainEntry {
- const allocator = self.temp_arena.allocator();
- var chain: std.ArrayList(ChainEntry) = .empty;
+ // `node` when it's in the AX tree, else its nearest ancestor that is. Null
+ // when no document is above it (detached, template content). A node is in
+ // the tree when each step up to the document is one childIds lists.
+ fn nearestInTree(self: *const Writer, node: *DOMNode) ?*DOMNode {
+ var nearest: ?*DOMNode = null;
+ // display:none hides the whole subtree: once a node isn't hidden, its
+ // ancestors aren't either.
+ var displayed = false;
var current = node;
while (current.flatParent(self.frame)) |parent| : (current = parent) {
- // An unslotted child of a host, or unused slot fallback: the
- // flat tree skips it, though flatParent still finds the parent.
- if (current.isOutsideFlatTree() or AXNode.fromNode(parent).ignoreChildren()) {
- return null;
+ // An unslotted child of a host, or unused slot fallback: the flat
+ // tree skips it, though flatParent still finds the parent.
+ var listed = !current.isOutsideFlatTree() and
+ !AXNode.fromNode(parent).ignoreChildren() and
+ isAXChildType(current, ignoreText(parent)) and
+ !hidesSubtree(current);
+ if (listed and !displayed) {
+ displayed = hiddenState(current, self.frame) != .pruned;
+ listed = displayed;
+ }
+ if (!listed) {
+ nearest = null;
+ } else if (nearest == null) {
+ nearest = current;
}
- const hidden = axChildHidden(current, ignoreText(parent), self.frame) orelse return null;
- try chain.append(allocator, .{ .node = current, .hidden = hidden });
}
if (current._type != .document) {
return null;
}
- try chain.append(allocator, .{ .node = current, .hidden = .visible });
- return chain.items;
- }
-
- fn writeChain(self: *const Writer, chain: []const ChainEntry, ignore_cache: *IgnoreCache, w: anytype) !void {
- for (chain) |entry| {
- try self.writeSingle(entry.node, entry.hidden, ignore_cache, w);
- }
+ return nearest orelse current;
}
- // Chrome's AXNode for a DOM node that has no AX node.
- fn writeNotRendered(self: *const Writer, id: u32, w: anytype) !void {
+ // Chrome's AXNode for a DOM node outside the AX tree. `parent` is its
+ // nearest ancestor in the tree, which doesn't list it in its childIds.
+ fn writeNotRendered(self: *const Writer, parent: ?*DOMNode, w: anytype) !void {
+ const id = self.root.id;
try w.beginObject();
try w.objectField("nodeId");
try writeIdString(id, w);
@@ -214,6 +234,13 @@ pub const Writer = struct {
try w.objectField("ignored");
try w.write(true);
try self.writeIgnoredReason("notRendered", w);
+ if (parent) |p| {
+ try w.objectField("parentId");
+ try writeIdString((try self.registry.register(p)).id, w);
+ }
+ try w.objectField("childIds");
+ try w.beginArray();
+ try w.endArray();
try w.endObject();
}
@@ -230,26 +257,27 @@ pub const Writer = struct {
try w.endArray();
}
- // Write `dom`, a node in the AX tree, without its descendants. `hidden`
- // is never .pruned for such a node.
- fn writeSingle(self: *const Writer, dom: *DOMNode, hidden: Hidden, ignore_cache: *IgnoreCache, w: anytype) !void {
+ // Write `dom`, a node in the AX tree, without its descendants.
+ fn writeSingle(self: *const Writer, dom: *DOMNode, ignore_cache: *IgnoreCache, w: anytype) !Written {
const node = try self.registry.register(dom);
- _ = try self.writeNode(node.id, .fromNode(dom), hidden, ignore_cache, w);
+ return self.writeNode(node.id, .fromNode(dom), hiddenState(dom, self.frame), ignore_cache, w);
}
- // Write each node listed in `parent`'s childIds, except `skip`, in the
- // same order. `parent` must be in the AX tree.
- fn writeChildren(self: *const Writer, parent: *DOMNode, skip: ?*DOMNode, ignore_cache: *IgnoreCache, w: anytype) !void {
- if (AXNode.fromNode(parent).ignoreChildren()) {
- return;
+ // `start`, a node in the AX tree, then each ancestor up to its frame's
+ // document.
+ fn writeAncestors(self: *const Writer, start: *DOMNode, ignore_cache: *IgnoreCache, w: anytype) !void {
+ var current: ?*DOMNode = start;
+ while (current) |node| : (current = node.flatParent(self.frame)) {
+ _ = try self.writeSingle(node, ignore_cache, w);
}
- var it: AXChildren = .init(parent, self.frame);
- while (it.next()) |child| switch (child) {
- .marker => try self.writeListMarker(parent, w),
- .node => |c| if (c.dom != skip) {
- try self.writeSingle(c.dom, c.hidden, ignore_cache, w);
- },
- };
+ }
+
+ // The nodes in `parent`'s childIds and, through an ignored one, its own
+ // children, like Chrome's getChildAXNodes. `parent` must be in the AX
+ // tree and show its children.
+ fn writeChildren(self: *const Writer, parent: *DOMNode, ignore_cache: *IgnoreCache, w: anytype) !void {
+ var walker: Walker = .init(parent, self.frame);
+ return self.writeDescendants(&walker, .ignored, ignore_cache, w);
}
/// Resolve the displayed role for `axn`, accounting for label-promotion
@@ -278,9 +306,24 @@ pub const Writer = struct {
}
fn writeTree(self: *const Writer, ignore_cache: *IgnoreCache, w: anytype) !void {
- var walker: Walker = .init(self.root.dom, self.frame);
- var descend = try self.writeNode(self.root.id, .fromNode(self.root.dom), walker.hidden(self.root.dom), ignore_cache, w);
- if (descend) try self.writeListMarker(self.root.dom, w);
+ const root = self.root.dom;
+ var walker: Walker = .init(root, self.frame);
+ const written = try self.writeNode(self.root.id, .fromNode(root), walker.hidden(root), ignore_cache, w);
+ if (written.children) {
+ try self.writeDescendants(&walker, .all, ignore_cache, w);
+ }
+ }
+
+ // Which nodes writeDescendants enters: every one (down to `depth`), or
+ // only ignored ones.
+ const Follow = enum { all, ignored };
+
+ // Write the AX nodes under the walker's root, which shows its children,
+ // in pre-order: its ListMarker first, then each node, followed by its
+ // marker and the nodes `follow` enters.
+ fn writeDescendants(self: *const Writer, walker: *Walker, follow: Follow, ignore_cache: *IgnoreCache, w: anytype) !void {
+ try self.writeListMarker(walker.root, w);
+ var descend = true;
while (walker.next(descend)) |dom_node| {
descend = false;
if (self.depth > 0 and self.depth < walker.depth) continue;
@@ -295,7 +338,8 @@ pub const Writer = struct {
continue;
}
const node = try self.registry.register(dom_node);
- descend = try self.writeNode(node.id, .fromNode(dom_node), hidden, ignore_cache, w);
+ const written = try self.writeNode(node.id, .fromNode(dom_node), hidden, ignore_cache, w);
+ descend = written.children and (follow == .all or written.ignored);
// The marker is a child: drop it with the others at the depth
// cut-off.
if (descend and (self.depth <= 0 or walker.depth < self.depth)) {
@@ -313,15 +357,14 @@ pub const Writer = struct {
const parent = li_node._parent.?;
const list_type = parent.as(DOMNode.Element).getTag();
- // Write the ListMarker node
+ const li = try self.registry.register(li_node);
+
+ // Write the ListMarker node. It has no DOM node, so no
+ // backendDOMNodeId.
try w.beginObject();
try w.objectField("nodeId");
- const marker_id = try self.registry.listMarkerId(li_node);
- try writeIdString(marker_id, w);
-
- try w.objectField("backendDOMNodeId");
- try w.write(marker_id);
+ try writeIdString(try self.registry.listMarkerId(li), w);
try w.objectField("role");
try self.writeAXValue(.{ .role = "ListMarker" }, w);
@@ -373,10 +416,8 @@ pub const Writer = struct {
try w.beginArray();
try w.endArray();
- // Get the parent node ID for the parentId field
- const li_registered = try self.registry.register(li_node);
try w.objectField("parentId");
- try writeIdString(li_registered.id, w);
+ try writeIdString(li.id, w);
try w.objectField("childIds");
try w.beginArray();
@@ -734,8 +775,7 @@ pub const Writer = struct {
try w.endObject();
}
- // write a node. returns true if children must be written.
- fn writeNode(self: *const Writer, id: u32, axn: AXNode, hidden: Hidden, ignore_cache: *IgnoreCache, w: anytype) !bool {
+ fn writeNode(self: *const Writer, id: u32, axn: AXNode, hidden: Hidden, ignore_cache: *IgnoreCache, w: anytype) !Written {
try w.beginObject();
try w.objectField("nodeId");
@@ -809,7 +849,7 @@ pub const Writer = struct {
var it: AXChildren = .init(n, self.frame);
while (it.next()) |child| {
const child_id = switch (child) {
- .marker => try self.registry.listMarkerId(n),
+ .marker => try self.registry.listMarkerId(try self.registry.register(n)),
.node => |c| (try self.registry.register(c.dom)).id,
};
try writeIdString(child_id, w);
@@ -819,7 +859,7 @@ pub const Writer = struct {
try w.endObject();
- return write_children;
+ return .{ .children = write_children, .ignored = ignore };
}
fn writeNodeValue(self: *const Writer, axnode: AXNode, w: anytype) !void {
diff --git a/src/server/cdp/domains/accessibility.zig b/src/server/cdp/domains/accessibility.zig
index 629e525c1..1cf91f070 100644
--- a/src/server/cdp/domains/accessibility.zig
+++ b/src/server/cdp/domains/accessibility.zig
@@ -115,36 +115,36 @@ fn getChildAXNodes(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const frame = try findFrame(cmd, bc, params.frameId) orelse return;
- // The AXNodeId is our node registry id, unique across frames: the frame
- // only has to own the node.
+ // The AXNodeId is our node registry id, unique across frames: a given
+ // frame only has to own the node.
const ax_id = std.fmt.parseInt(u32, params.id, 10) catch return error.InvalidParams;
- const node = bc.node_registry.lookup_by_id.get(ax_id) orelse {
- return getListMarkerChildren(cmd, bc, frame, params.frameId != null, ax_id);
- };
- // A frameless node fails in axnodeWriter, like in the other methods.
- if (node.dom.ownerFrame(frame)) |owner| {
- if (params.frameId != null and owner != frame) {
+ const owner_frame: ?*Frame = if (params.frameId != null) frame else null;
+
+ // A ListMarker has no DOM node and no children. It's checked like its
+ // <li>, and is gone once the <li> no longer has one.
+ if (bc.node_registry.listMarkerOwner(ax_id)) |li| {
+ if (!AXNode.hasListMarker(li.dom)) {
return error.NodeNotFound;
}
+ try checkOwnerFrame(li, owner_frame);
+ return cmd.sendResult(.{ .nodes = [0]u32{} }, .{});
}
+ const node = bc.node_registry.lookup_by_id.get(ax_id) orelse return error.NodeNotFound;
+ try checkOwnerFrame(node, owner_frame);
return sendAXNodes(cmd, bc, frame, node, .{
.scope = .children,
});
}
-// A ListMarker has no DOM node, and no children. It's checked like its <li>,
-// and is gone once the <li> no longer has one.
-fn getListMarkerChildren(cmd: *CDP.Command, bc: *CDP.BrowserContext, frame: *Frame, check_frame: bool, ax_id: u32) !void {
- const li = bc.node_registry.listMarkerOwner(ax_id) orelse return error.NodeNotFound;
- if (!AXNode.hasListMarker(li)) {
- return error.NodeNotFound;
- }
- const owner = li.ownerFrame(frame) orelse return error.InvalidNodeId;
- if (check_frame and owner != frame) {
+// Whether `frame`, when given, owns `node`. Without one, a frameless node
+// fails later, in axnodeWriter, like in the other methods.
+fn checkOwnerFrame(node: *const NodeRegistry.Node, frame: ?*Frame) !void {
+ const f = frame orelse return;
+ const owner = node.dom.ownerFrame(f) orelse return error.InvalidNodeId;
+ if (owner != f) {
return error.NodeNotFound;
}
- return cmd.sendResult(.{ .nodes = [0]u32{} }, .{});
}
// Reply with the AX nodes `opts` selects around `node`: a `nodes` list, or a
@@ -334,44 +334,63 @@ test "cdp.accessibility: getAXNodeAndAncestors returns the node then its ancesto
}
}
-test "cdp.accessibility: getChildAXNodes returns the direct children" {
+test "cdp.accessibility: getChildAXNodes follows ignored children" {
var ctx = try testing.context();
defer ctx.deinit();
_ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
- try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ // <html> and <body> are ignored, so the root's children run down to
+ // <main>, as in Chrome, though the root only lists <html>.
+ try ctx.processMessage(.{ .id = 1, .method = "Accessibility.getRootAXNode" });
+ const root = (try sentResult(&ctx, 1)).get("node").?.object;
+ const root_id = root.get("nodeId").?.string;
+ try testing.expectEqual(1, root.get("childIds").?.array.items.len);
+
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getChildAXNodes", .params = .{ .id = root_id } });
+ const top = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ try testing.expectEqual(3, top.len);
+ try testing.expectEqual(true, top[0].object.get("ignored").?.bool);
+ try testing.expectEqual(true, top[1].object.get("ignored").?.bool);
+ try testing.expectEqual("main", axRole(top[2].object));
+ try testing.expectEqual(root_id, top[0].object.get("parentId").?.string);
+ for (top[1..], top[0..2]) |n, parent| {
+ try testing.expectEqual(parent.object.get("nodeId").?.string, n.object.get("parentId").?.string);
+ }
+
+ try ctx.processMessage(.{ .id = 3, .method = "Runtime.evaluate", .params = .{
.expression = "document.querySelector('main')",
} });
- const object_id = try evaluatedObjectId(&ctx, 1);
-
- try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getPartialAXTree", .params = .{
+ const object_id = try evaluatedObjectId(&ctx, 3);
+ try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getPartialAXTree", .params = .{
.objectId = object_id,
.fetchRelatives = false,
} });
- const main = (try sentResult(&ctx, 2)).get("nodes").?.array.items[0].object;
+ const main = (try sentResult(&ctx, 4)).get("nodes").?.array.items[0].object;
const main_id = main.get("nodeId").?.string;
const child_ids = main.get("childIds").?.array.items;
- try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getChildAXNodes", .params = .{ .id = main_id } });
- const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
- try testing.expectEqual(child_ids.len, nodes.len);
- for (nodes, child_ids) |n, cid| {
- try testing.expectEqual(cid.string, n.object.get("nodeId").?.string);
- try testing.expectEqual(main_id, n.object.get("parentId").?.string);
- }
- // Whitespace text between the elements is exposed too.
- var saw_heading = false;
- var saw_text = false;
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getChildAXNodes", .params = .{ .id = main_id } });
+ const nodes = (try sentResult(&ctx, 5)).get("nodes").?.array.items;
+ // Every childId, in order, with nodes from under ignored children in
+ // between: the link inside the visibility:hidden <div>.
+ var listed: usize = 0;
+ var saw_link = false;
for (nodes) |n| {
- const role = axRole(n.object);
- if (std.mem.eql(u8, role, "heading")) saw_heading = true;
- if (std.mem.eql(u8, role, "StaticText")) saw_text = true;
+ const node_id = n.object.get("nodeId").?.string;
+ if (listed < child_ids.len and std.mem.eql(u8, node_id, child_ids[listed].string)) {
+ listed += 1;
+ try testing.expectEqual(main_id, n.object.get("parentId").?.string);
+ } else if (std.mem.eql(u8, axRole(n.object), "link")) {
+ try testing.expectEqual("visible-in-hidden", n.object.get("name").?.object.get("value").?.string);
+ saw_link = true;
+ }
}
- try testing.expect(saw_heading and saw_text);
+ try testing.expectEqual(child_ids.len, listed);
+ try testing.expect(saw_link);
- try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getChildAXNodes", .params = .{ .id = "99999" } });
- try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 4 });
+ try ctx.processMessage(.{ .id = 6, .method = "Accessibility.getChildAXNodes", .params = .{ .id = "99999" } });
+ try ctx.expectSentError(-31998, "NodeNotFound", .{ .id = 6 });
}
test "cdp.accessibility: getPartialAXTree fetchRelatives" {
@@ -394,29 +413,39 @@ test "cdp.accessibility: getPartialAXTree fetchRelatives" {
try testing.expectEqual(1, alone.len);
const label_id = alone[0].object.get("nodeId").?.string;
- // fetchRelatives defaults to true: the node, its ancestors, siblings and
- // children, but nothing deeper.
+ // fetchRelatives defaults to true. Like Chrome: the node, its children,
+ // then its ancestors up to the root, and no siblings.
try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getPartialAXTree", .params = .{
.objectId = object_id,
} });
const nodes = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
try testing.expectEqual(label_id, nodes[0].object.get("nodeId").?.string);
-
- var saw_root = false;
- var heading_id: ?[]const u8 = null;
- var saw_child = false;
- for (nodes) |n| {
- const role = axRole(n.object);
- if (std.mem.eql(u8, role, "RootWebArea")) saw_root = true;
- if (std.mem.eql(u8, role, "heading")) heading_id = n.object.get("nodeId").?.string;
- if (std.mem.eql(u8, role, "textbox")) saw_child = true;
+ const main_at = for (nodes, 0..) |n, i| {
+ if (std.mem.eql(u8, axRole(n.object), "main")) break i;
+ } else return error.MainNotFound;
+
+ var saw_textbox = false;
+ for (nodes[1..main_at]) |n| {
+ try testing.expectEqual(label_id, n.object.get("parentId").?.string);
+ if (std.mem.eql(u8, axRole(n.object), "textbox")) saw_textbox = true;
}
- try testing.expect(saw_root and saw_child);
- // A sibling is listed, but not its children.
- for (nodes) |n| {
- const parent_id = (n.object.get("parentId") orelse continue).string;
- try testing.expect(!std.mem.eql(u8, parent_id, heading_id.?));
+ try testing.expect(saw_textbox);
+ try testing.expectEqual(nodes[main_at].object.get("nodeId").?.string, nodes[0].object.get("parentId").?.string);
+ for (nodes[main_at .. nodes.len - 1], nodes[main_at + 1 ..]) |child, parent| {
+ try testing.expectEqual(parent.object.get("nodeId").?.string, child.object.get("parentId").?.string);
}
+ try testing.expectEqual("RootWebArea", axRole(nodes[nodes.len - 1].object));
+
+ // An ignored node comes without its children: <body>, <html>, the root.
+ try ctx.processMessage(.{ .id = 4, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.body",
+ } });
+ const body = try evaluatedObjectId(&ctx, 4);
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = body } });
+ const body_nodes = (try sentResult(&ctx, 5)).get("nodes").?.array.items;
+ try testing.expectEqual(3, body_nodes.len);
+ try testing.expectEqual(true, body_nodes[0].object.get("ignored").?.bool);
+ try testing.expectEqual("RootWebArea", axRole(body_nodes[2].object));
}
test "cdp.accessibility: getChildAXNodes lists an li's ListMarker first" {
@@ -496,32 +525,93 @@ test "cdp.accessibility: getFullAXTree depth cuts off an li's ListMarker" {
}
}
-test "cdp.accessibility: a node outside the AX tree is a lone ignored node" {
+test "cdp.accessibility: a node outside the AX tree hangs off its nearest ancestor" {
var ctx = try testing.context();
defer ctx.deinit();
_ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+ // Under display:none: the node, then <body> and up.
try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
.expression = "document.getElementById('in-gone')",
} });
const gone = try evaluatedObjectId(&ctx, 1);
try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .objectId = gone } });
- try expectNotRendered((try sentResult(&ctx, 2)).get("nodes").?.array.items);
+ const chain = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ try testing.expectEqual(4, chain.len);
+ try expectNotRendered(chain[0].object);
+ try expectChain(chain);
+ const body_id = chain[1].object.get("nodeId").?.string;
+
+ // Alone, it keeps its parent.
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getPartialAXTree", .params = .{
+ .objectId = gone,
+ .fetchRelatives = false,
+ } });
+ const alone = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual(1, alone.len);
+ try testing.expectEqual(body_id, alone[0].object.get("parentId").?.string);
// <title>: <head> exposes no children.
- try ctx.processMessage(.{ .id = 3, .method = "Runtime.evaluate", .params = .{
+ try ctx.processMessage(.{ .id = 4, .method = "Runtime.evaluate", .params = .{
.expression = "document.querySelector('title')",
} });
- const title = try evaluatedObjectId(&ctx, 3);
- try ctx.processMessage(.{ .id = 4, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = title } });
- const nodes = (try sentResult(&ctx, 4)).get("nodes").?.array.items;
- try expectNotRendered(nodes);
+ const title = try evaluatedObjectId(&ctx, 4);
+ try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = title } });
+ const nodes = (try sentResult(&ctx, 5)).get("nodes").?.array.items;
+ try expectNotRendered(nodes[0].object);
+ try expectChain(nodes);
- try ctx.processMessage(.{ .id = 5, .method = "Accessibility.getChildAXNodes", .params = .{
+ try ctx.processMessage(.{ .id = 6, .method = "Accessibility.getChildAXNodes", .params = .{
.id = nodes[0].object.get("nodeId").?.string,
} });
- try testing.expectEqual(0, (try sentResult(&ctx, 5)).get("nodes").?.array.items.len);
+ try testing.expectEqual(0, (try sentResult(&ctx, 6)).get("nodes").?.array.items.len);
+}
+
+test "cdp.accessibility: a node under inert or aria-hidden" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_tree.html" });
+
+ // As in Chrome: the node, then <main> (not the hiding <div>) and up.
+ inline for (.{ "under-inert", "p-under-aria-hidden" }, 0..) |dom_id, i| {
+ try ctx.processMessage(.{ .id = 2 * i + 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('" ++ dom_id ++ "')",
+ } });
+ const object_id = try evaluatedObjectId(&ctx, 2 * i + 1);
+ try ctx.processMessage(.{ .id = 2 * i + 2, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = object_id } });
+ const nodes = (try sentResult(&ctx, 2 * i + 2)).get("nodes").?.array.items;
+ try testing.expectEqual(5, nodes.len);
+ try expectNotRendered(nodes[0].object);
+ try testing.expectEqual("main", axRole(nodes[1].object));
+ try expectChain(nodes);
+ }
+}
+
+test "cdp.accessibility: ancestors with names computed from content" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-A", .url = "cdp/ax_relatives.html" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{
+ .expression = "document.getElementById('deep')",
+ } });
+ const deep = try evaluatedObjectId(&ctx, 1);
+
+ // b, a, h2, body, html, document: the link's long name is computed
+ // while the ancestors are written.
+ try ctx.processMessage(.{ .id = 2, .method = "Accessibility.getAXNodeAndAncestors", .params = .{ .objectId = deep } });
+ const nodes = (try sentResult(&ctx, 2)).get("nodes").?.array.items;
+ try testing.expectEqual(6, nodes.len);
+ try testing.expectEqual("link", axRole(nodes[1].object));
+ try testing.expectEqual("heading", axRole(nodes[2].object));
+ try expectChain(nodes);
+
+ try ctx.processMessage(.{ .id = 3, .method = "Accessibility.getPartialAXTree", .params = .{ .objectId = deep } });
+ const relatives = (try sentResult(&ctx, 3)).get("nodes").?.array.items;
+ try testing.expectEqual("RootWebArea", axRole(relatives[relatives.len - 1].object));
}
test "cdp.accessibility: an svg is listed in its parent's childIds" {
@@ -591,14 +681,21 @@ test "cdp.accessibility: an iframe's tree is its own" {
}
}
-fn expectNotRendered(nodes: []const std.json.Value) !void {
- try testing.expectEqual(1, nodes.len);
- const node = nodes[0].object;
+fn expectNotRendered(node: std.json.ObjectMap) !void {
try testing.expectEqual("none", axRole(node));
try testing.expectEqual(true, node.get("ignored").?.bool);
try testing.expectEqual("notRendered", node.get("ignoredReasons").?.array.items[0].object.get("name").?.string);
- try testing.expectEqual(null, node.get("parentId"));
- try testing.expectEqual(null, node.get("childIds"));
+ try testing.expectEqual(0, node.get("childIds").?.array.items.len);
+}
+
+// `nodes` is a node then each ancestor up to a root.
+fn expectChain(nodes: []const std.json.Value) !void {
+ for (nodes[0 .. nodes.len - 1], nodes[1..]) |child, parent| {
+ try testing.expectEqual(parent.object.get("nodeId").?.string, child.object.get("parentId").?.string);
+ }
+ const root = nodes[nodes.len - 1].object;
+ try testing.expectEqual("RootWebArea", axRole(root));
+ try testing.expectEqual(null, root.get("parentId"));
}
// The AXNodeId of the node `expression` evaluates to. Uses message ids