The headless browser for AI agents and web scraping
fix(js): offsetParent is null for the root and body elements
Florian Leprat5b9d62c4e632
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 5ed990db..f56c41b9 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -4915,6 +4915,10 @@ class Element extends Node {}get offsetParent() {if (!this.isConnected || this._renderBoxGeometry() === null) return null;+ // CSSOM View: null for the root element and the body element. Returning+ // document.body here made body.offsetParent === body, so the common+ // `while (el) { x += el.offsetLeft; el = el.offsetParent; }` never ended.+ if (this === document.documentElement || this === document.body) return null;const ownStyle = globalThis.getComputedStyle(this);if (ownStyle.position === 'fixed') return null;diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 7193f86e..6b67c750 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -20590,6 +20590,34 @@ mod tests {assert_eq!(result.value.unwrap(), serde_json::json!(true));}+ // body.offsetParent returned body itself, so walking offsetParent to+ // the top (how pages compute an element's page position) looped forever.+ #[test]+ fn test_offset_parent_of_body_and_root_is_null() {+ let mut rt = setup_runtime(+ "<html><body><div style=\"position:relative\"><p id=\"p\">x</p></div></body></html>",+ );+ let result = rt+ .evaluate(+ r#"(() => {+ const walk = [];+ for (let el = document.getElementById("p"); el && walk.length < 10; el = el.offsetParent) {+ walk.push(el.tagName);+ }+ return {+ body: document.body.offsetParent,+ root: document.documentElement.offsetParent,+ walk: walk.join(">"),+ };+ })()"#,+ )+ .unwrap();+ assert_eq!(+ result,+ serde_json::json!({"body": null, "root": null, "walk": "P>DIV>BODY"})+ );+ }+#[test]fn test_text_decoder_respects_typed_array_view() {let mut rt = setup_runtime("<html><body></body></html>");