Lightpanda: the headless browser designed for AI and automation
css: skip visibility:hidden elements in hit testing
Adrià Arrufat97bb27ccddeb
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex 2c8ddfa54..594b1a91a 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -665,6 +665,15 @@ pub fn hasPointerEventsNone(self: *StyleManager, el: *Element) bool {return self.visibilityProps(el).pointer_events_none;}+/// Blink's VisibleToHitTestRequest. Ignores display:none, which callers handle+/// for the whole subtree.+pub fn isHitTestable(self: *StyleManager, el: *Element) bool {+ self.assertOwns(el);+ self.rebuildIfDirty() catch return true;+ const p = self.visibilityProps(el);+ return !p.visibility_hidden and !p.pointer_events_none;+}+/// The axes along which `el` is a scroll container: its own computed overflow/// on that axis is auto, scroll or overlay. No ancestor walk.pub fn overflowAxes(self: *StyleManager, el: *Element) Element.ScrollAxes {diff --git a/src/browser/tests/document/element_from_point_pointer_events.html b/src/browser/tests/document/element_from_point_hit_testing.htmlsimilarity index 59%rename from src/browser/tests/document/element_from_point_pointer_events.htmlrename to src/browser/tests/document/element_from_point_hit_testing.htmlindex b589c2eb6..3ea24b531 100644--- a/src/browser/tests/document/element_from_point_pointer_events.html+++ b/src/browser/tests/document/element_from_point_hit_testing.html@@ -13,6 +13,13 @@<div id="inherits">Inherits</div><div id="live" style="pointer-events: auto">Live</div></div>+ <div id="under_hidden">+ <span id="hidden" style="visibility: hidden">Hidden</span>+ <span id="collapsed" style="visibility: collapse">Collapsed</span>+ </div>+ <div id="hidden_wrap" style="visibility: hidden">+ <div id="shown" style="visibility: visible">Shown</div>+ </div></body><script id="computed_pointer_events">@@ -34,15 +41,12 @@return [(r.left + r.right) / 2, (r.top + r.bottom) / 2];};- // The hit falls through to an element underneath: #under in a real- // layout, possibly a further ancestor in ours.+ // #under in Chrome; our layout can land on a further ancestor.const dead = document.getElementById('dead');const hit = document.elementFromPoint(...center(dead));testing.expectTrue(hit !== dead && hit.contains(dead));testing.expectFalse(document.elementsFromPoint(...center(dead)).includes(dead));- // A pointer-events:auto child of a pointer-events:none parent is hit, and- // the parent is left out of the stack.const live = document.getElementById('live');testing.expectEqual(live, document.elementFromPoint(...center(live)));const stack = document.elementsFromPoint(...center(live));@@ -51,3 +55,25 @@testing.expectEqual(document.documentElement, stack[stack.length - 1]);}</script>++<script id="hit_skips_visibility_hidden">+{+ const center = (el) => {+ const r = el.getBoundingClientRect();+ return [(r.left + r.right) / 2, (r.top + r.bottom) / 2];+ };++ for (const id of ['hidden', 'collapsed']) {+ const el = document.getElementById(id);+ const hit = document.elementFromPoint(...center(el));+ testing.expectTrue(hit !== el && hit.contains(el));+ testing.expectFalse(document.elementsFromPoint(...center(el)).includes(el));+ }++ const shown = document.getElementById('shown');+ testing.expectEqual(shown, document.elementFromPoint(...center(shown)));+ const stack = document.elementsFromPoint(...center(shown));+ testing.expectEqual(shown, stack[0]);+ testing.expectFalse(stack.includes(document.getElementById('hidden_wrap')));+}+</script>diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex fe148ae64..421a75ac8 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -1020,9 +1020,9 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:preorder_index += 1;if (node.is(Element)) |element| {hidden = hidden or style_manager.hasDisplayNone(element);- // pointer-events:none lets the hit fall through to what's under- // it, but a child can opt back in, so the subtree is still walked.- if (!hidden and !style_manager.hasPointerEventsNone(element)) {+ // Unlike display:none, a child can opt back in, so the subtree is+ // still walked.+ if (!hidden and style_manager.isHitTestable(element)) {if (y >= pos and y <= pos + element.boxAxis(frame, .height)) {if (ignore_x) {topmost = element;@@ -1052,10 +1052,11 @@ fn elementFromPointImpl(self: *Document, x: f64, y: f64, ignore_x: bool, frame:fn elementsFromPoint(self: *Document, x: f64, y: f64, frame: *Frame) ![]const *Element {// Get topmost elementvar current: ?*Element = (try self.elementFromPoint(x, y, frame)) orelse return &.{};+ const style_manager = &(self.asNode().ownerFrame(frame) orelse return &.{})._style_manager;var result: std.ArrayList(*Element) = .empty;while (current) |el| {- // The root element is always last, as in Chrome, even when it isn't hit.- if (el.parentElement() == null or !el.hasPointerEventsNone(frame)) {+ // Chrome always ends the list with the root element.+ if (el.parentElement() == null or style_manager.isHitTestable(el)) {try result.append(frame.local_arena, el);}current = el.parentElement();diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zigindex 030111868..96ad8905f 100644--- a/src/browser/webapi/css/CSSStyleDeclaration.zig+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig@@ -80,7 +80,7 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const// Computed styles must reflect stylesheet rules, not just the element's// inline `style=` attribute. Limited to display/visibility — what aria// tree builders (Playwright ariaSnapshot) consult on every element — and- // pointer-events, which tells agents a control can't be clicked.+ // pointer-events.if (self._is_computed) {if (self._element) |element| {if (element.ownerFrame(frame)) |owner| {