Skip to content

The headless browser for AI agents and web scraping

Merge main and repair visibility checks

SGavrl41b79e24d29c
diff --cc crates/obscura-js/js/bootstrap.js
index dd5fbc56,6fccfe2c..81b56702
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@@ -5109,34 -5786,10 +5786,38 @@@ class Element extends Node
}
return new DOMRectList([this.getBoundingClientRect()]);
}
- // No layout engine: a stub that always returns true unblocks Playwright's
- // actionability polling. With a real layout we'd check display, visibility,
- // opacity and rect dimensions per spec.
- checkVisibility(opts) { return true; }
+ // CSSOM View checkVisibility(). Box existence is judged from display along
+ // the flattened ancestor chain rather than from geometry, since some
+ // visible inline elements report 0x0 rects (#722). Playwright calls this
+ // before every action, so render builds answer from the retained cascade
+ // in one native call.
+ checkVisibility(opts) {
++ if (_cache.get(this._nid) !== this) {
++ const check = _documentRealmMember(this, 'checkVisibility');
++ if (check) return check.call(this, opts);
++ }
+ if (!this.isConnected) return false;
+ const options = opts && typeof opts === 'object' ? opts : {};
+ const checkOpacity = !!(options.opacityProperty || options.checkOpacity);
+ const checkVisibilityCSS = !!(options.visibilityProperty || options.checkVisibilityCSS);
+ if (typeof __obscuraCore.ops.op_check_visibility === 'function') {
- return __obscuraCore.ops.op_check_visibility(this._nid | 0, checkOpacity, checkVisibilityCSS);
++ return __obscuraCore.ops.op_check_visibility(this._nid | 0, checkOpacity, checkVisibilityCSS, _realmFrameId);
+ }
+ // Without the renderer, computed style only reflects inline declarations,
+ // so read them directly and resolve inheritance of `visibility` here.
+ let visibility = '';
+ for (let el = this; el; el = el.parentElement || (el.parentNode && el.parentNode.host) || null) {
+ const style = el.style;
+ if (!style) continue;
+ const display = style.display;
+ if (display === 'none' || (el === this && display === 'contents')) return false;
+ if (checkOpacity && style.opacity !== '' && parseFloat(style.opacity) <= 0) return false;
+ if (!visibility && style.visibility && style.visibility !== 'inherit') {
+ visibility = style.visibility;
+ }
+ }
+ return !(checkVisibilityCSS && visibility && visibility !== 'visible');
+ }
// ARIA reflection properties. Without an accessibility tree we expose the
// raw aria-* attributes so Playwright's getByRole / getByLabel locators can
// at least find elements that author them explicitly.
@@@ -15652,6 -18207,123 +18235,124 @@@ if (typeof ShadowRoot !== 'undefined' &
};
}
+ // Ops are bound after snapshot restoration, so probe the renderer at realm
+ // initialization, not while building the snapshot with an empty op table.
+ function _installCaretGeometry() {
+ if (typeof __obscuraCore.ops.op_layout_caret !== 'function') return {};
+ const positions = new WeakMap();
+ function positionState(receiver) {
+ const state = positions.get(receiver);
+ if (!state) throw new TypeError('Illegal invocation');
+ return state;
+ }
+ const CaretPosition = class CaretPosition {
+ constructor() { throw new TypeError('Illegal constructor'); }
+ get offsetNode() { return positionState(this).node; }
+ get offset() { return positionState(this).offset; }
+ getClientRect() {
+ const { node, offset } = positionState(this);
+ const raw = __obscuraCore.ops.op_layout_caret_rect(String(node._nid), offset, _realmFrameId);
+ const rect = raw ? JSON.parse(raw) : null;
+ return rect ? new DOMRect(rect.x, rect.y, rect.width, rect.height) : new DOMRect();
+ }
+ };
+ Object.defineProperty(globalThis, 'CaretPosition', {
+ value: CaretPosition, writable: true, configurable: true,
+ });
+ Object.defineProperty(CaretPosition.prototype, Symbol.toStringTag, { value: 'CaretPosition', configurable: true });
+ for (const name of ['offsetNode', 'offset', 'getClientRect']) {
+ const descriptor = Object.getOwnPropertyDescriptor(CaretPosition.prototype, name);
+ descriptor.enumerable = true;
+ Object.defineProperty(CaretPosition.prototype, name, descriptor);
+ _markNative(descriptor.value || descriptor.get);
+ }
+ _markNative(CaretPosition);
+ function nativeCaret(receiver, x, y) {
+ if (receiver !== globalThis.document && !(receiver instanceof Document)) throw new TypeError('Illegal invocation');
+ if (receiver._nid !== globalThis.document?._nid) return null;
+ const raw = __obscuraCore.ops.op_layout_caret(x, y, _realmFrameId);
+ if (!raw) return null;
+ const [nid, offset] = JSON.parse(raw);
+ const node = _wrap(nid);
+ return node ? { node, offset } : null;
+ }
+ Document.prototype.caretRangeFromPoint = function caretRangeFromPoint(x = 0, y = 0) {
+ const method = _documentRealmMember(this, 'caretRangeFromPoint');
+ if (method) return Reflect.apply(method, this, [x, y]);
+ // The legacy Chromium extension takes optional signed integer coordinates.
+ const state = nativeCaret(this, +x | 0, +y | 0);
+ if (!state) return null;
+ const range = new Range();
+ // Widget value offsets are not DOM Range boundaries. The legacy API
+ // exposes the position before the control, not its internal editing text.
+ for (let node = state.node; node; node = node.parentNode) {
+ if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement) {
+ range.setStartBefore(node);
+ range.collapse(true);
+ return range;
+ }
+ }
+ range.setStart(state.node, state.offset);
+ range.collapse(true);
+ return range;
+ };
+ Document.prototype.caretPositionFromPoint = function caretPositionFromPoint(x, y, options = {}) {
+ const method = _documentRealmMember(this, 'caretPositionFromPoint');
+ if (method) return Reflect.apply(method, this, arguments);
+ if (arguments.length < 2) throw new TypeError('Two coordinates are required');
+ x = +x; y = +y;
+ if (!Number.isFinite(x) || !Number.isFinite(y)) throw new TypeError('Coordinates must be finite');
+ if (options != null && typeof options !== 'object' && typeof options !== 'function') {
+ throw new TypeError('Options must be a dictionary');
+ }
+ const shadowRoots = options?.shadowRoots;
+ if (shadowRoots !== undefined && (shadowRoots == null || typeof shadowRoots[Symbol.iterator] !== 'function')) {
+ throw new TypeError('Shadow roots must be iterable');
+ }
+ const roots = shadowRoots === undefined ? [] : Array.from(shadowRoots);
+ for (const root of roots) {
+ if (!(root instanceof ShadowRoot)) throw new TypeError('Expected a ShadowRoot');
+ }
+ const state = nativeCaret(this, x, y);
+ if (!state) return null;
+ let root = state.node.getRootNode();
+ while (root instanceof ShadowRoot && !roots.some(allowed => {
+ for (let current = allowed; current instanceof ShadowRoot; current = current.host.getRootNode()) {
+ if (current === root) return true;
+ }
+ return false;
+ })) {
+ const host = root.host, parent = host.parentNode;
+ if (!parent) return null;
+ state.offset = Array.prototype.indexOf.call(parent.childNodes, host);
+ state.node = parent;
+ root = parent.getRootNode();
+ }
+ const position = Object.create(CaretPosition.prototype);
+ positions.set(position, state);
+ return position;
+ };
+ _markNative(Document.prototype.caretRangeFromPoint);
+ _markNative(Document.prototype.caretPositionFromPoint);
+ return {
+ caretRangeFromPoint: Document.prototype.caretRangeFromPoint,
+ caretPositionFromPoint: Document.prototype.caretPositionFromPoint,
+ };
+ }
+
+ // Capture late-defined members too, before page code can replace them.
+ const _nativeElementClick = Element.prototype.click;
+ const _documentMembers = Object.freeze(Object.fromEntries([
+ ...['URL', 'defaultView', 'readyState', 'compatMode', 'contentType', 'getElementById', 'querySelector', 'querySelectorAll', 'open', 'write', 'close', 'elementFromPoint', 'queryCommandSupported'].map(name => {
+ const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, name);
+ return [name, descriptor.value || descriptor.get];
+ }),
+ ['activateElement', function(trusted) {
+ return _nativeElementClick.call(this, trusted ? _TRUSTED_ACTIVATION : undefined);
+ }],
++ ['checkVisibility', Element.prototype.checkVisibility],
+ ]));
+
globalThis.__obscura_init = function() {
// The host sets __obscura_frameId on a frame realm before calling this.
_realmFrameId = globalThis.__obscura_frameId >>> 0;
diff --cc crates/obscura-js/src/ops.rs
index 4297fab4,63581ef2..36a71d3c
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@@ -6119,7 -7800,7 +7800,8 @@@ pub fn build_extension() -> Extension
ops.push(op_resize_observer_measurements());
ops.push(op_intersection_observer_measurements());
ops.push(op_computed_style());
+ ops.push(op_check_visibility());
+ ops.push(op_set_hovered());
ops.push(op_css_supports());
ops.push(op_layout_metrics());
ops.push(op_element_scroll_metrics());
@@@ -7236,37 -9120,25 +9121,54 @@@ fn op_computed_style
for (name, value) in custom {
object.insert(name, serde_json::Value::String(value));
}
- serde_json::Value::Object(object).to_string()
+ serde_json::json!([true, object]).to_string()
+ }
+
+ #[cfg(feature = "render")]
+ #[op2(fast)]
+ fn op_set_hovered(state: &OpState, nid: u32, frame_id: u32) {
+ let shared = frame_state(state, frame_id);
+ let mut state = shared.borrow_mut();
+ let changed = state.dom.as_ref().is_some_and(|dom| {
+ dom.set_hovered((nid != 0).then(|| NodeId::new(nid)))
+ });
+ if changed {
+ state.activity_generation = state.activity_generation.wrapping_add(1);
+ state.prepared_render = None;
+ state.pending_style_mutations.clear();
+ state.resolved_scroll = None;
+ }
}
+/// `Element.checkVisibility()` in one native call. Walking ancestors through
+/// `getComputedStyle()` cost a JS proxy per ancestor, and Playwright calls
+/// this before every action.
+#[cfg(feature = "render")]
+#[op2(fast)]
+fn op_check_visibility(
+ state: &OpState,
+ nid: u32,
+ check_opacity: bool,
+ check_visibility_css: bool,
++ frame_id: u32,
+) -> bool {
- let shared = state.borrow::<SharedState>().clone();
++ let shared = frame_state(state, frame_id);
+ let mut gs = shared.borrow_mut();
+ sample_live_document_animations(&mut gs);
+ if ensure_prepared_render(&mut gs).is_none() {
+ return false;
+ }
+ let (Some(prepared), Some(dom)) = (gs.prepared_render.as_ref(), gs.dom.as_ref()) else {
+ return false;
+ };
+ prepared.check_visibility(
+ dom,
+ obscura_dom::tree::NodeId::new(nid),
+ check_opacity,
+ check_visibility_css,
+ )
+}
+
/// Use the renderer's declaration parser as the single feature-query source
/// of truth. Keeping this bridge synchronous and state-free makes the common
/// two-argument `CSS.supports()` overload a single native call.
diff --cc crates/obscura-js/src/runtime.rs
index f4001e82,1ca6135c..dab53a44
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@@ -19851,52 -24783,6 +24781,84 @@@ mod tests
assert_eq!(typeof_method, serde_json::json!("function"));
}
+ /// Issue #1067: checkVisibility() follows the CSSOM View algorithm:
+ /// elements without a box (display:none on self or an ancestor,
+ /// display:contents, disconnected) are hidden, and the opacity /
+ /// visibility options are honored, including their legacy aliases.
+ #[test]
+ fn element_check_visibility_follows_css_boxes_and_options() {
+ let mut rt = setup_runtime(
+ r#"
+ <div style="display:none"><a id="a" href="/x">hidden</a></div>
+ <div style="visibility:hidden"><a id="b" href="/x">invisible</a></div>
+ <div style="opacity:0"><span id="c">transparent</span></div>
+ <span id="e" style="display:contents">contents</span>
+ <span id="f" style="display:none">self none</span>
+ <div style="visibility:hidden"><span id="g" style="visibility:visible">back</span></div>
+ <a id="v" href="/x">visible</a>
+ "#,
+ );
+ let result = rt
+ .evaluate(
+ r#"
+ const $ = id => document.getElementById(id);
+ return [
+ $('a').checkVisibility(),
+ $('b').checkVisibility(),
+ $('b').checkVisibility({visibilityProperty: true}),
+ $('b').checkVisibility({checkVisibilityCSS: true}),
+ $('c').checkVisibility(),
+ $('c').checkVisibility({opacityProperty: true}),
+ $('c').checkVisibility({checkOpacity: true}),
+ $('e').checkVisibility(),
+ $('f').checkVisibility(),
+ $('g').checkVisibility({visibilityProperty: true}),
+ $('v').checkVisibility({visibilityProperty: true, opacityProperty: true}),
+ document.createElement('div').checkVisibility(),
+ ];
+ "#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ serde_json::json!([
+ false, true, false, false, true, false, false, false, false, true, true, false
+ ])
+ );
+ }
+
++ #[cfg(feature = "render")]
++ #[test]
++ fn element_check_visibility_uses_flat_tree_box_presence() {
++ let mut rt = setup_runtime(r#"<details><summary id="summary">show</summary>
++ <span id="closed">closed</span></details><div id="host"><span id="unslotted">light</span></div>
++ <div id="zero" style="width:0;height:0"></div><span id="empty"></span>
++ <table><tr id="row" style="display:none"><td>hidden</td></tr></table>"#);
++ let result = rt.evaluate(r#"(() => {
++ const root = document.getElementById('host').attachShadow({mode:'open'});
++ root.innerHTML = '<span id="shadow">shadow</span>';
++ return ['summary','closed','unslotted','zero','empty','row'].map(id =>
++ document.getElementById(id).checkVisibility()).concat(root.getElementById('shadow').checkVisibility());
++ })()"#).unwrap();
++ assert_eq!(result, serde_json::json!([true,false,false,true,true,false,true]));
++ }
++
++ #[cfg(feature = "render")]
++ #[test]
++ fn element_check_visibility_uses_the_owning_frame() {
++ let mut rt = setup_runtime("<div style='display:none'><span>parent hidden</span></div>");
++ assert_eq!(rt.evaluate(r#"(() => {
++ const iframe = document.createElement('iframe');
++ document.body.appendChild(iframe);
++ const d = iframe.contentDocument;
++ d.body.innerHTML = '<div id="shown">child</div><div id="hidden" style="display:none">hidden</div>';
++ const shown = d.getElementById('shown'), hidden = d.getElementById('hidden');
++ const parentCheck = Element.prototype.checkVisibility;
++ return [shown.checkVisibility(),hidden.checkVisibility(),
++ parentCheck.call(shown),parentCheck.call(hidden)];
++ })()"#).unwrap(), serde_json::json!([true,false,true,false]));
++ }
++
/// Playwright's `getByRole` / `getByLabel` locators resolve via ARIA
/// reflection properties. Without the getters those locators always
/// fail. Reflect the underlying aria-* attributes.
diff --cc crates/obscura-render/src/paint.rs
index 0ea7259b,5be283aa..320d0413
--- a/crates/obscura-render/src/paint.rs
+++ b/crates/obscura-render/src/paint.rs
@@@ -1343,41 -1494,19 +1494,61 @@@ impl PreparedRender
))
}
- /// Whether hit testing must ignore this element's generated box.
+ /// Untransformed border-box size used by CSSOM View's `offsetWidth` and
+ /// `offsetHeight`. Unlike `getBoundingClientRect()`, transforms do not
+ /// participate in these integer layout metrics.
+ pub fn border_size(&self, id: obscura_dom::tree::NodeId) -> Option<(f32, f32)> {
+ let rect = self.layout.rects.get(&id)?;
+ let style = self.layout.styles.get(&id)?;
+ if style.ignores_used_box_sizes() {
+ return Some((0.0, 0.0));
+ }
+ Some((rect.width.max(0.0), rect.height.max(0.0)))
+ }
+
+ /// CSSOM View `checkVisibility()` over the retained cascade. An element
- /// without a style, with `display: contents`, or under a `display: none`
- /// flattened-tree ancestor has no box. `content-visibility: hidden` is
++ /// without a generated box is hidden, including unslotted light children
++ /// and closed details content. Zero-size boxes still count. `content-visibility: hidden` is
+ /// not implemented by the renderer, which paints that content, so it is
+ /// not consulted here either.
+ pub fn check_visibility(
+ &self,
+ tree: &DomTree,
+ id: obscura_dom::tree::NodeId,
+ check_opacity: bool,
+ check_visibility_css: bool,
+ ) -> bool {
+ let Some(style) = self.layout.styles.get(&id) else {
+ return false;
+ };
- if style.display_contents || (check_visibility_css && style.visibility_hidden_computed) {
++ if style.display_contents || (check_visibility_css && style.computed_visibility_hidden) {
+ return false;
+ }
+ let document = tree.document();
+ let mut current = Some(id);
++ let mut box_known = self.layout.rects.contains_key(&id);
+ while let Some(node) = current.filter(|&node| node != document) {
+ let Some(style) = self.layout.styles.get(&node) else {
+ return false;
+ };
+ if style.display == crate::Display::None
+ || (check_opacity && style.opacity.is_some_and(|value| value <= 0.0))
+ {
+ return false;
+ }
- current = crate::dom::rendered_parent(tree, node);
++ let Some(parent) = crate::dom::rendered_parent(tree, node) else { return false; };
++ // Empty inline boxes may have no retained rect, but must still
++ // belong to the rendered child list (not closed details or slot fallback).
++ if !box_known {
++ if !crate::dom::rendered_children(tree, parent).contains(&node) { return false; }
++ box_known = self.layout.rects.contains_key(&parent);
++ }
++ current = Some(parent);
+ }
- true
++ current == Some(document)
+ }
+
+ /// Whether hit testing must ignore this element's generated box.
pub fn pointer_events_none(&self, id: obscura_dom::tree::NodeId) -> bool {
self.layout
.styles
diff --cc docs/Connect-Puppeteer-or-Playwright.md
index 09c3b25a,ecce07a9..09ad38f6
--- a/docs/Connect-Puppeteer-or-Playwright.md
+++ b/docs/Connect-Puppeteer-or-Playwright.md
@@@ -99,6 -99,21 +99,37 @@@ Rendering is included in official binar
for source builds. The client-specific guides cover scrolling, raw CDP
screencasting, and current output limits.
+ ## Raw CDP node queries
+
+ `DOM.getAttributes`, `DOM.requestNode`, `DOM.requestChildNodes`, and
+ `DOM.getNodeForLocation` support the top-level light DOM. Request the document
+ with `DOM.getDocument` before publishing frontend node IDs. Ancestors and
+ requested children arrive through session-scoped `DOM.setChildNodes` events;
+ navigation clears the node cache and emits `DOM.documentUpdated`.
+
+ Live mutation events are not implemented yet. Request the document again to
+ publish newly inserted children of an already-published parent. `requestChildNodes` accepts
+ `pierce: true` on plain light-DOM subtrees, but rejects requests that would
+ require frame or shadow-tree traversal. Detached-node publication and
+ `ignorePointerEventsNone: true` hit testing are not supported. The remaining work is tracked in
+ [#1291](https://github.com/h4ckf0r0day/obscura/issues/1291).
+
++## Element visibility queries
++
++Render builds implement `element.checkVisibility()` using the rendered flat tree.
++Disconnected elements, `display: none` subtrees, `display: contents` elements,
++closed-details content, and unassigned light-DOM children have no visible box.
++Zero-sized boxes are not automatically rejected. Queries use the element's owning
++document, including iframe documents.
++
++The `opacityProperty` / `checkOpacity` and `visibilityProperty` /
++`checkVisibilityCSS` options enable the corresponding CSS checks. Without
++rendering, only inline styles are checked. CSS `content-visibility: hidden` and
++`content-visibility: auto` skipping are not implemented, including the
++`contentVisibilityAuto` option.
++Listbox option/optgroup boxes are also not rendered, so their visibility queries
++currently return `false` even when Chromium returns `true`.
++
## Current limits
- Pages share one V8 isolate. CPU-bound JavaScript on one page can delay others.