Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: innerText visibility fix

Karl Seguin1e06ec918e21
diff --git a/src/browser/tests/element/inner.html b/src/browser/tests/element/inner.html
index f8088357f..5f0dc1601 100644
--- a/src/browser/tests/element/inner.html
+++ b/src/browser/tests/element/inner.html
@@ -185,4 +185,19 @@
// hidden via a CSS class). A hidden block does not contribute a line break.
testing.expectEqual("abc", $('#d5').innerText);
testing.expectEqual("beforeafter", $('#d6').innerText);
+
+ // .innerText read ON a display:none root equals textContent. The own-element
+ // check must keep visible child elements (an ancestor-walk would prune the
+ // <span> while keeping its sibling text nodes, yielding "ac").
+ d1.innerHTML = '<div id=dn style="display:none">a<span>b</span>c</div>';
+ testing.expectEqual("abc", $('#dn').innerText);
+
+ // A display:none <br> contributes no line break.
+ d1.innerHTML = 'a<br style="display:none">b';
+ testing.expectEqual("ab", d1.innerText);
+
+ // Guard: visibility:hidden text stays INCLUDED (the check is display-only),
+ // so a future switch to a visibility-aware check can't silently over-exclude.
+ d1.innerHTML = '<span style="visibility:hidden">X</span>Y';
+ testing.expectEqual("XY", d1.innerText);
</script>
diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zig
index 4571d71cb..eb55b4bc6 100644
--- a/src/browser/webapi/element/Html.zig
+++ b/src/browser/webapi/element/Html.zig
@@ -1331,9 +1331,6 @@ const InnerTextState = struct {
// Needed to reach the StyleManager for CSS-driven visibility (display:none).
frame: *Frame,
- // Tree-local visibility cache shared across the whole walk (see getInnerText).
- cache: Element.VisibilityCache = .{},
-
// number of line breaks we've accumulated for the block. Emitted lazily that
// leading/trailing breaks aren't written and so that we can emit the max
// requested, which can change as we render children.
@@ -1449,6 +1446,14 @@ fn handleChildElement(
saw_cell: *bool,
saw_row: *bool,
) !void {
+ // doesn't use StyleManger's isHidden, because we don't care if the element
+ // is hidden through its parent. If you can el.innerText on an element, the
+ // visibility of el.parent doesn't matter. So we only care about visibility
+ // on the element itself and then on each child. This is much simpler too.
+ if (state.frame._style_manager.hasDisplayNone(he.asElement())) {
+ return;
+ }
+
if (he._type == .br) {
try state.flushBreaks();
try state.writer.writeByte('\n');
@@ -1458,10 +1463,6 @@ fn handleChildElement(
return;
}
- if (state.frame._style_manager.isHidden(he.asElement(), &state.cache, .{})) {
- return;
- }
-
switch (innerTextDisplay(he, tag)) {
// Not rendered: skip the subtree entirely (script/style/metadata/...).
.skip => {},