Skip to content

Lightpanda: the headless browser designed for AI and automation

tree: keep links and buttons inside named headings and code

Adrià Arrufatedd3d85f0ead
diff --git a/src/SemanticTree.zig b/src/SemanticTree.zig
index 5a9483bde..34fa05062 100644
--- a/src/SemanticTree.zig
+++ b/src/SemanticTree.zig
@@ -70,8 +70,8 @@ pub fn init(arena: std.mem.Allocator, node: *Node, registry: *NodeRegistry, fram
};
}
-/// Walk the pruned tree with `visitor`: `visit(*Node, *NodeData) !bool`
-/// returns whether to descend into the children, `leave() !void` closes a
+/// Walk the pruned tree with `visitor`: `visit(*Node, *NodeData) !Walk`
+/// says which descendants to visit, `leave() !void` closes a
/// visited node. An optional `visitPruned(*Node, *NodeData) !bool` sees the
/// nodes pruning skips, and returns whether to get a `leave()` for it too.
fn visitAll(self: @This(), visitor: anytype) error{WriteFailed}!void {
@@ -120,12 +120,12 @@ const TextVisitor = struct {
writer: *std.Io.Writer,
depth: u32 = 0,
- pub fn visit(self: *TextVisitor, node: *Node, data: *NodeData) !bool {
+ pub fn visit(self: *TextVisitor, node: *Node, data: *NodeData) !Walk {
try self.writer.splatByteAll(' ', self.depth);
const line = try writeTextLine(self.writer, node, data);
try self.writer.writeByte('\n');
self.depth += 1;
- return line.walk_children;
+ return line.children;
}
pub fn leave(self: *TextVisitor) !void {
@@ -172,7 +172,7 @@ const LineCollector = struct {
lines: std.ArrayList(Line) = .empty,
current: ?u32 = null,
- pub fn visit(self: *LineCollector, node: *Node, data: *NodeData) !bool {
+ pub fn visit(self: *LineCollector, node: *Node, data: *NodeData) !Walk {
const start = self.text.written().len;
try self.text.writer.splatByteAll(' ', self.depth());
const line = try writeTextLine(&self.text.writer, node, data);
@@ -182,7 +182,7 @@ const LineCollector = struct {
.contentful = line.contentful,
.priority = linePriority(node, data, self.focused),
});
- return line.walk_children;
+ return line.children;
}
pub fn visitPruned(self: *LineCollector, _: *Node, data: *NodeData) !bool {
@@ -439,6 +439,7 @@ const Open = struct {
tag_counts: std.StringArrayHashMapUnmanaged(usize) = .empty,
name: ?[]const u8, // The children's parent_name
invisible: bool, // visibility:hidden, so its text children are too
+ controls_only: bool, // Walk.controls from it or an ancestor
xpath_len: usize,
// Whether the visitor gets a `leave()` once the children are done.
opened: bool,
@@ -448,7 +449,7 @@ fn walk(self: @This(), ctx: *WalkContext, visitor: anytype) !void {
var stack: std.ArrayList(Open) = .empty;
defer stack.deinit(self.arena);
- try self.visitNode(ctx, &stack, self.dom_node, null, false, visitor, 1);
+ try self.visitNode(ctx, &stack, self.dom_node, null, false, false, visitor, 1);
while (stack.items.len > 0) {
// Everything read from `top` is read before visitNode, which can grow (move) the stack.
const top = &stack.items[stack.items.len - 1];
@@ -465,7 +466,7 @@ fn walk(self: @This(), ctx: *WalkContext, visitor: anytype) !void {
}
gop.value_ptr.* += 1;
- try self.visitNode(ctx, &stack, child, top.name, top.invisible, visitor, gop.value_ptr.*);
+ try self.visitNode(ctx, &stack, child, top.name, top.invisible, top.controls_only, visitor, gop.value_ptr.*);
continue;
}
@@ -485,6 +486,7 @@ fn visitNode(
node: *Node,
parent_name: ?[]const u8,
parent_invisible: bool,
+ controls_only: bool,
visitor: anytype,
index: usize,
) !void {
@@ -513,7 +515,7 @@ fn visitNode(
if (html_el.getHidden()) return;
}
} else if (node.is(CData.Text)) |text_node| {
- if (parent_invisible) return;
+ if (parent_invisible or controls_only) return;
const text = text_node.ownData();
if (isAllWhitespace(text)) {
return;
@@ -591,6 +593,8 @@ fn visitNode(
var should_visit = true;
if (invisible) {
should_visit = false;
+ } else if (controls_only) {
+ should_visit = is_interactive or interactive.isInteractiveRole(role);
} else if (self.interactive_only) {
var keep = false;
if (interactive.isInteractiveRole(role)) {
@@ -620,7 +624,7 @@ fn visitNode(
}
var opened = false;
- var should_walk_children = true;
+ var children: Walk = .all;
var data: NodeData = .{
.id = cdp_node.id,
.axn = axn,
@@ -636,16 +640,17 @@ fn visitNode(
};
if (should_visit) {
- should_walk_children = try visitor.visit(node, &data);
+ children = try visitor.visit(node, &data);
opened = true;
} else if (@hasDecl(@TypeOf(visitor.*), "visitPruned")) {
opened = try visitor.visitPruned(node, &data);
}
try stack.append(self.arena, .{
- .next_child = if (should_walk_children) node._first_child else null,
+ .next_child = if (children != .none) node._first_child else null,
.name = name,
.invisible = invisible,
+ .controls_only = controls_only or children == .controls,
.xpath_len = initial_xpath_len,
.opened = opened,
});
@@ -701,7 +706,7 @@ const JsonVisitor = struct {
jw: *std.json.Stringify,
tree: Self,
- pub fn visit(self: *JsonVisitor, node: *Node, data: *NodeData) !bool {
+ pub fn visit(self: *JsonVisitor, node: *Node, data: *NodeData) !Walk {
try self.jw.beginObject();
try self.jw.objectField("nodeId");
@@ -788,10 +793,10 @@ const JsonVisitor = struct {
if (data.options != null) {
// Signal to not walk children, as we handled them natively
- return false;
+ return .none;
}
- return true;
+ return .all;
}
pub fn leave(self: *JsonVisitor) !void {
@@ -822,8 +827,15 @@ const structural_roles = std.StaticStringMap(enum { wrapper, anchor }).initCompt
.{ "region", .anchor },
});
+const Walk = enum {
+ all,
+ none,
+ // Only the interactive descendants: the rest is already in the name.
+ controls,
+};
+
const TextLine = struct {
- walk_children: bool,
+ children: Walk,
// A name, text or value of its own, not only children.
contentful: bool,
};
@@ -878,18 +890,22 @@ fn writeTextLine(writer: *std.Io.Writer, node: *Node, data: *NodeData) !TextLine
}
}
try writer.writeAll("]");
- return .{ .walk_children = false, .contentful = true }; // Native handling complete, do not walk children
+ return .{ .children = .none, .contentful = true }; // Native handling complete, do not walk children
}
const contentful = name_to_print != null or (if (data.value) |v| v.len > 0 else false) or data.checked != null;
- // If this is a leaf-like semantic node and we already have a name,
- // skip children to avoid redundant StaticText or noise.
- const is_leaf_semantic = std.mem.eql(u8, data.role, "link") or
- std.mem.eql(u8, data.role, "button") or
- std.mem.eql(u8, data.role, "heading") or
- std.mem.eql(u8, data.role, "code");
- const walk_children = !(is_leaf_semantic and data.name != null and data.name.?.len > 0);
- return .{ .walk_children = walk_children, .contentful = contentful };
+ // A named one already says what its children say, but a heading or
+ // code can still wrap a link.
+ const named = data.name != null and data.name.?.len > 0;
+ const children: Walk = if (!named)
+ .all
+ else if (std.mem.eql(u8, data.role, "link") or std.mem.eql(u8, data.role, "button"))
+ .none
+ else if (std.mem.eql(u8, data.role, "heading") or std.mem.eql(u8, data.role, "code"))
+ .controls
+ else
+ .all;
+ return .{ .children = children, .contentful = contentful };
}
const NodeDetails = struct {
@@ -1188,6 +1204,31 @@ test "SemanticTree: deep nesting doesn't overflow the native stack" {
try testing.expect(std.mem.endsWith(u8, json_str, "/text()[1]\",\"nodeType\":3,\"nodeValue\":\"deep\",\"children\":[]}]}"));
}
+test "SemanticTree: a named heading or code keeps its links" {
+ var registry: NodeRegistry = .init(testing.allocator);
+ defer registry.deinit();
+
+ var page = try testing.pageTest("cdp/tree_heading_links.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ for ([_]bool{ false, true }) |interactive_only| {
+ const st: Self = try .init(testing.arena_allocator, frame.window._document.asNode(), &registry, frame, .{ .interactive_only = interactive_only });
+
+ var aw: std.Io.Writer.Allocating = .init(testing.allocator);
+ defer aw.deinit();
+ try st.textStringify(&aw.writer);
+ const out = aw.written();
+
+ try testing.expect(std.mem.find(u8, out, "heading 'Post title'\n") != null);
+ try testing.expect(std.mem.find(u8, out, "link 'Post title'\n") != null);
+ try testing.expect(std.mem.find(u8, out, "button 'Follow'\n") != null);
+ try testing.expect(std.mem.find(u8, out, "link 'ls'\n") != null);
+ // The rest of a heading's text is in its name, not repeated below it.
+ try testing.expectEqual(1, std.mem.count(u8, out, "Acme"));
+ }
+}
+
test "SemanticTree: maxBytes keeps alerts and controls, marks the rest" {
var registry: NodeRegistry = .init(testing.allocator);
defer registry.deinit();
diff --git a/src/browser/tests/cdp/tree_heading_links.html b/src/browser/tests/cdp/tree_heading_links.html
new file mode 100644
index 000000000..3c5b10321
--- /dev/null
+++ b/src/browser/tests/cdp/tree_heading_links.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<body>
+<h2><a href="#post">Post title</a></h2>
+<h3>Sold by <span>Acme</span> <button>Follow</button></h3>
+<p>Run <code><a href="#ls">ls</a> -la</code> first.</p>
+<h4>Plain heading</h4>
+</body>
+</html>