Skip to content

The headless browser for AI agents and web scraping

Merge pull request #1172 from flolep2607/fix/style-setproperty-no-op-mutation

SGavrlbe93ffed58f1
diff --cc crates/obscura-js/src/runtime.rs
index 019d3278,14bd9ef5..891d433c
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@@ -20850,34 -20850,60 +20850,88 @@@ 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 style_set_property_converts_value_once_before_comparing_and_storing() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt.evaluate(r#"(() => {
+ let conversions = 0;
+ document.body.style.setProperty('top', {
+ toString() { return `${++conversions * 10}px`; }
+ });
+ return {conversions:String(conversions), value:document.body.style.top};
+ })()"#).unwrap();
+ assert_eq!(result, serde_json::json!({"conversions":"1", "value":"10px"}));
+ }
+
+ // An unchanged style.setProperty rewrote the style attribute and queued a
+ // mutation record, so an observer that re-applies the same style (pinning
+ // body `top: 0`) re-triggered itself forever.
+ #[tokio::test(flavor = "current_thread")]
+ async fn test_style_set_property_without_change_queues_no_mutation() {
+ let mut rt = setup_runtime("<html><body><p>x</p></body></html>");
+ let result = rt
+ .call_function_on_for_cdp(
+ r#"async () => {
+ const body = document.body;
+ let calls = 0, records = 0;
+ const observer = new MutationObserver((mutations) => {
+ calls++;
+ records += mutations.length;
+ if (calls < 50) body.style.setProperty("top", "0px", "important");
+ });
+ observer.observe(document.documentElement, {
+ subtree: true, attributes: true, attributeFilter: ["style"],
+ });
+ body.style.setProperty("top", "0px", "important");
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ body.style.removeProperty("left");
+ body.style.top = "0px";
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ observer.disconnect();
+ return { calls, records, style: body.getAttribute("style") };
+ }"#,
+ None,
+ &[],
+ true,
+ true,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ result.value.unwrap(),
+ serde_json::json!({"calls": 1, "records": 1, "style": "top: 0px;"})
+ );
+ }
+
#[test]
fn test_text_decoder_respects_typed_array_view() {
let mut rt = setup_runtime("<html><body></body></html>");