Lightpanda: the headless browser designed for AI and automation
Check inherited hiding in SemanticTree and interactive walks
Adrià Arrufat405a80370620
diff --git a/src/SemanticTree.zig b/src/SemanticTree.zigindex 575337cd2..1678fd7ce 100644--- a/src/SemanticTree.zig+++ b/src/SemanticTree.zig@@ -187,14 +187,9 @@ fn visitNode(// We handle options/optgroups natively inside their parents, skip them in the general walkif (tag == .datalist or tag == .option or tag == .optgroup) return;- // Hidden subtrees are never entered, so below the root only the- // element's own display matters.- const style_manager = &self.frame._style_manager;- const hidden = if (current_depth == 0)- style_manager.isHidden(el, .{})- else- style_manager.hasDisplayNone(el);- if (hidden) {+ // Not just the element's own display: a slotted element inherits+ // from its slot, which this light-tree walk never visits.+ if (self.frame._style_manager.isHidden(el, .{})) {return;}@@ -806,6 +801,24 @@ test "SemanticTree max_depth" {try testing.expect(std.mem.indexOf(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);+}+test "SemanticTree: deep nesting doesn't overflow the native stack" {var registry: NodeRegistry = .init(testing.allocator);defer registry.deinit();diff --git a/src/browser/interactive.zig b/src/browser/interactive.zigindex c6f4e219f..dcb2e1352 100644--- a/src/browser/interactive.zig+++ b/src/browser/interactive.zig@@ -213,11 +213,11 @@ fn walkInteractive(else => {},}- if (el.ownerFrame(frame)) |owner| {- if (owner._style_manager.hasDisplayNone(el)) {- tw.skipChildren();- continue;- }+ // Not just the element's own display: a slotted element inherits+ // from its slot, which this light-tree walk never visits.+ if (!el.isVisible(frame)) {+ tw.skipChildren();+ continue;}const html_el = el.is(Element.Html) orelse continue;@@ -683,6 +683,16 @@ test "browser.interactive: disabled by fieldset" {try testing.expect(!elements[1].disabled);}+test "browser.interactive: a slotted element inherits its slot's display" {+ var page = try testing.pageTest("cdp/slotted_hidden.html", .{});+ defer page.close();+ const frame = page.frame().?;++ const elements = try collectInteractiveElements(frame.window._document.asNode(), frame.call_arena, frame);+ try testing.expectEqual(1, elements.len);+ try testing.expectEqual("slotted-shown", elements[0].name.?);+}+test "browser.interactive: pointer-events none" {const elements = try testInteractive("<button style=\"pointer-events: none;\">Click me</button>");defer testing.test_session.closeAllPages();diff --git a/src/browser/tests/cdp/slotted_hidden.html b/src/browser/tests/cdp/slotted_hidden.htmlnew file mode 100644index 000000000..d0390ac46--- /dev/null+++ b/src/browser/tests/cdp/slotted_hidden.html@@ -0,0 +1,10 @@+<!DOCTYPE html>+<html>+<body>+ <div id="host"><button id="slotted-hidden">slotted-hidden</button><button id="slotted-shown" slot="shown">slotted-shown</button></div>+ <script>+ document.getElementById("host").attachShadow({ mode: "open" }).innerHTML =+ '<slot name="shown"></slot><div style="display: none"><slot></slot></div>';+ </script>+</body>+</html>diff --git a/src/browser/tests/element/check_visibility.html b/src/browser/tests/element/check_visibility.htmlindex a9e286ab8..4e8e5f210 100644--- a/src/browser/tests/element/check_visibility.html+++ b/src/browser/tests/element/check_visibility.html@@ -814,13 +814,18 @@testing.expectEqual(true, button.checkVisibility());const hidden = document.createElement("div");- hidden.style.cssText = "visibility: hidden; --c: red";+ hidden.style.cssText = "visibility: hidden; --c: red; font-size: 30px";document.body.appendChild(hidden);const span = document.createElement("span");- hidden.attachShadow({ mode: "open" }).appendChild(span);+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");+ svg.setAttribute("x", "2em");+ const hidden_shadow = hidden.attachShadow({ mode: "open" });+ hidden_shadow.appendChild(span);+ hidden_shadow.appendChild(svg);testing.expectEqual(false, span.checkVisibility({ visibilityProperty: true }));testing.expectEqual("hidden", getComputedStyle(span).visibility);testing.expectEqual("red", getComputedStyle(span).getPropertyValue("--c"));+ testing.expectEqual(60, svg.x.baseVal.value);none.remove();hidden.remove();