Lightpanda: the headless browser designed for AI and automation
Merge branch 'main' into style-cache-followups
Adrià Arrufat3e2035f84c17
diff --cc src/SemanticTree.zigindex 1678fd7ce,ebe2528d9..a78c42d24--- a/src/SemanticTree.zig+++ b/src/SemanticTree.zig@@@ -798,27 -803,9 +798,27 @@@ test "SemanticTree max_depth"try st.textStringify(&aw.writer);const text_str = aw.written();- try testing.expect(std.mem.indexOf(u8, text_str, "other") == null);+ try testing.expect(std.mem.find(u8, text_str, "other") == null);}+test "SemanticTree: a slotted element inherits its slot's display" {+ var registry: NodeRegistry = .init(testing.allocator);+ defer registry.deinit();++ var page = try testing.pageTest("cdp/slotted_hidden.html", .{});+ defer page.close();+ const frame = page.frame().?;++ const st: Self = try .init(testing.arena_allocator, frame.window._document.asNode(), ®istry, frame, .{ .prune = false });++ var aw: std.Io.Writer.Allocating = .init(testing.allocator);+ defer aw.deinit();+ try st.textStringify(&aw.writer);+- try testing.expect(std.mem.indexOf(u8, aw.written(), "slotted-shown") != null);- try testing.expect(std.mem.indexOf(u8, aw.written(), "slotted-hidden") == null);++ try testing.expect(std.mem.find(u8, aw.written(), "slotted-shown") != null);++ try testing.expect(std.mem.find(u8, aw.written(), "slotted-hidden") == null);+}+test "SemanticTree: deep nesting doesn't overflow the native stack" {var registry: NodeRegistry = .init(testing.allocator);defer registry.deinit();diff --cc src/browser/StyleManager.zigindex fcf6e3eab,405324daa..e5cab9747--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@@ -1282,7 -1317,7 +1289,7 @@@ const Visibility = struct};// The element's own values, from the cascade.- const Cascaded = struct {- pub const Cascaded = packed struct(u6) {++ pub const Cascaded = struct {// Author value (inline or sheet). Without `author_display` it's the UA// fallback: .none when matchesUaDisplayNoneRule, else .other.display: Display = .other,diff --cc src/server/cdp/AXNode.zigindex 7c32887e0,e8232897f..430f906d1--- a/src/server/cdp/AXNode.zig+++ b/src/server/cdp/AXNode.zig@@@ -1275,41 -1291,20 +1275,41 @@@ fn scratchAllocator(temp_arena: ?*lp.Arreturn if (temp_arena) |a| a.allocator() else frame.call_arena;}-/// Chromium's AX tree prunes display:none and visibility:hidden alike.-fn isHidden(elt: *DOMNode.Element, frame: *Frame) bool {- if (hasHidingAttribute(elt)) {- return true;+/// `invisible` (visibility:hidden) ignores only the node: a descendant can set+/// `visibility: visible`. `pruned` ignores the whole subtree.+const Hidden = enum { visible, invisible, pruned };++/// Text takes its flat-tree parent's state.+fn hiddenState(node: *DOMNode, frame: *Frame) Hidden {+ const elt = node.is(DOMNode.Element) orelse node.flatTreeParentElement(frame) orelse return .visible;+ if (hidesSubtree(elt.asNode())) {+ return .pruned;}- const owner = elt.ownerFrame(frame) orelse return false;- return owner._style_manager.isHidden(elt, .{ .check_visibility = true });+ const owner = elt.ownerFrame(frame) orelse return .visible;+ const style_manager = &owner._style_manager;+ if (style_manager.isHidden(elt, .{})) {+ return .pruned;+ }+ return if (style_manager.hasVisibilityHiddenInherited(elt)) .invisible else .visible;}-fn hasHidingAttribute(elt: *DOMNode.Element) bool {- if (hasAriaHiddenTrue(elt)) {- return true;+/// Not [hidden]: an author display rule can override it, so the StyleManager+/// owns it.+fn hidesSubtree(node: *DOMNode) bool {+ const elt = node.is(DOMNode.Element) orelse return false;+ const aria_hidden = elt.getAttributeInterned("aria-hidden") orelse "";- return std.mem.eql(u8, aria_hidden, "true") or elt.hasAttributeSafe(comptime .wrap("inert"));++ return std.ascii.eqlIgnoreCase(aria_hidden, "true") or elt.hasAttributeSafe(comptime .wrap("inert"));+}++/// The first flat-tree ancestor of `node` whose attributes hide its subtree.+fn hidingAncestor(node: *DOMNode, frame: *Frame) ?*DOMNode {+ var current = node.flatTreeParent(frame);+ while (current) |ancestor| : (current = ancestor.flatTreeParent(frame)) {+ if (hidesSubtree(ancestor)) {+ return ancestor;+ }}- return elt.hasAttributeInterned("hidden") or elt.hasAttributeSafe(comptime .wrap("inert"));+ return null;}fn ignoreText(node: *DOMNode) bool {@@@ -1553,33 -1554,6 +1553,33 @@@ test "AXnode: stripWhitespaces"}const testing = @import("testing.zig");+/// The first serialized node with `role` (any when null) whose name contains+/// `name_needle`.+fn findNode(nodes: []const std.json.Value, role: ?[]const u8, name_needle: []const u8) ?std.json.ObjectMap {+ for (nodes) |node_val| {+ const obj = node_val.object;+ if (role) |r| {+ const role_val = (obj.get("role") orelse continue).object.get("value") orelse continue;+ if (!std.mem.eql(u8, role_val.string, r)) continue;+ }+ const name_val = (obj.get("name") orelse continue).object.get("value") orelse continue;- if (name_val == .string and std.mem.indexOf(u8, name_val.string, name_needle) != null) {++ if (name_val == .string and std.mem.find(u8, name_val.string, name_needle) != null) {+ return obj;+ }+ }+ return null;+}++/// The AXValue of a serialized node's property.+fn nodeProperty(node: std.json.ObjectMap, name: []const u8) ?std.json.ObjectMap {+ for ((node.get("properties") orelse return null).array.items) |prop| {+ if (std.mem.eql(u8, prop.object.get("name").?.string, name)) {+ return prop.object.get("value").?.object;+ }+ }+ return null;+}+test "AXNode: writer" {var registry = NodeRegistry.init(testing.allocator);defer registry.deinit();@@@ -1889,51 -1948,6 +1889,51 @@@ test "AXNode: Writer query filters by a}}+test "AXNode: Writer query and subtree root see a hiding ancestor" {+ var registry = NodeRegistry.init(testing.allocator);+ defer registry.deinit();++ var page = try testing.pageTest("cdp/ax_tree.html", .{});+ defer page.close();++ const frame = page.frame().?;+ var label_index: Label.LabelByForIndex = .{};+ const temp_arena = try frame.getArena(.medium, "AXNode");+ defer temp_arena.release();++ var writer: Writer = .{+ .root = try registry.register(frame.window._document.asNode()),+ .registry = ®istry,+ .frame = frame,+ .label_index = &label_index,+ .temp_arena = temp_arena,+ .filter = .{ .accessible_name = "under-inert" },+ };+ const query = try std.json.Stringify.valueAlloc(testing.allocator, writer, .{});+ defer testing.allocator.free(query);++ const parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, query, .{});+ defer parsed.deinit();+ // Chrome returns no match (an ignored node has no name); we may, but ignored+ for (parsed.value.array.items) |n| {+ try testing.expectEqual(true, n.object.get("ignored").?.bool);+ }++ const button = (try frame.window._document.querySelector(.wrap("#under-inert"), frame)).?;+ const host = (try frame.window._document.querySelector(.wrap("#inert-host"), frame)).?;+ const shadow_button = host.getShadowRoot(frame).?.getElementById("in-inert-shadow", frame).?;++ writer.filter = null;+ for ([_]*DOMNode.Element{ button, shadow_button }) |root| {+ writer.root = try registry.register(root.asNode());+ const tree = try std.json.Stringify.valueAlloc(testing.allocator, writer, .{});+ defer testing.allocator.free(tree);+- try testing.expect(std.mem.indexOf(u8, tree, "\"ignored\":true") != null);- try testing.expect(std.mem.indexOf(u8, tree, "\"childIds\":[]") != null);++ try testing.expect(std.mem.find(u8, tree, "\"ignored\":true") != null);++ try testing.expect(std.mem.find(u8, tree, "\"childIds\":[]") != null);+ }+}+test "AXNode: Writer query combined role+name filter promotes hidden-input labels" {var registry = NodeRegistry.init(testing.allocator);defer registry.deinit();@@@ -2108,6 -2122,50 +2108,47 @@@ test "AXNode: generic containers sharevar fresh: IgnoreCache = .{ .allocator = testing.arena_allocator };for (expected[1..]) |e| {const el = (try root.querySelector(e[0], frame)).?;- try testing.expectEqual(e[1], try AXNode.fromNode(el.asNode()).isIgnore(frame, false, false, &fresh));+ try testing.expectEqual(e[1], try AXNode.fromNode(el.asNode()).isIgnore(frame, &fresh));}}++ test "AXNode: aria-hidden is case-insensitive" {+ const frame = try testing.base.createFrame();+ defer testing.base.test_session.closeAllPages();++ const root = try frame.window._document.createElement("div", null, frame);+ try root.setInnerHTML(+ \\<div id="hidden-upper" aria-hidden="TRUE"><p>hidden-upper</p></div>+ \\<div id="hidden-mixed" aria-hidden="True"><p>hidden-mixed</p></div>+ \\<div id="visible-false" aria-hidden="false"><p>visible-false</p></div>+ , frame);++ const hidden_upper = (try root.querySelector("#hidden-upper", frame)).?;+ const hidden_mixed = (try root.querySelector("#hidden-mixed", frame)).?;+ const visible_false = (try root.querySelector("#visible-false", frame)).?;+- try testing.expect(hasAriaHiddenTrue(hidden_upper));- try testing.expect(hasHidingAttribute(hidden_upper));- try testing.expect(hasAriaHiddenTrue(hidden_mixed));- try testing.expect(hasHidingAttribute(hidden_mixed));- try testing.expect(!hasAriaHiddenTrue(visible_false));- try testing.expect(!hasHidingAttribute(visible_false));++ try testing.expect(hidesSubtree(hidden_upper.asNode()));++ try testing.expect(hidesSubtree(hidden_mixed.asNode()));++ try testing.expect(!hidesSubtree(visible_false.asNode()));++ var registry = NodeRegistry.init(testing.allocator);+ defer registry.deinit();++ const node = try registry.register(root.asNode());+ var label_index: Label.LabelByForIndex = .{};+ const temp_arena = try frame.getArena(.medium, "AXNode");+ defer temp_arena.release();++ const json = try std.json.Stringify.valueAlloc(testing.allocator, Writer{+ .root = node,+ .registry = ®istry,+ .frame = frame,+ .label_index = &label_index,+ .temp_arena = temp_arena,+ }, .{});+ defer testing.allocator.free(json);++ try testing.expect(std.mem.find(u8, json, "hidden-upper") == null);+ try testing.expect(std.mem.find(u8, json, "hidden-mixed") == null);+ try testing.expect(std.mem.find(u8, json, "visible-false") != null);+ }