Skip to content

The headless browser for AI agents and web scraping

fix(js): leave the style attribute alone when a declaration does not change

Florian Lepratdaac81087b1e
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 5ed990db..76f6429b 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -1785,14 +1785,32 @@ class CSSStyleDeclaration {
// Storage is keyed by the dashed CSS name, matching CSSOM. The proxy maps the
// camelCase IDL access (el.style.fontSize) onto the dashed key (font-size), so
// getPropertyValue('font-size') and el.style.fontSize stay in sync.
+ // CSSOM: the style attribute is only rewritten when the declaration
+ // changed. Rewriting it on a no-op set queued a mutation record, so a
+ // MutationObserver that re-applies the same style re-triggered itself
+ // forever (a page pinning body `top: 0` spun at 100% CPU).
setProperty(name, value) {
this._pull();
const k = _cssCamelToKebab(String(name));
- if (value === "" || value == null) delete this._props[k];
- else this._props[k] = String(value);
+ const old = this._props[k];
+ if (value === "" || value == null) {
+ if (old === undefined) return;
+ delete this._props[k];
+ } else {
+ if (old === String(value)) return;
+ this._props[k] = String(value);
+ }
+ this._push();
+ }
+ removeProperty(name) {
+ this._pull();
+ const k = _cssCamelToKebab(String(name));
+ const old = this._props[k];
+ if (old === undefined) return "";
+ delete this._props[k];
this._push();
+ return old;
}
- removeProperty(name) { this._pull(); const k = _cssCamelToKebab(String(name)); const old = this._props[k]; delete this._props[k]; this._push(); return old || ""; }
getPropertyValue(name) { this._pull(); return this._props[_cssCamelToKebab(String(name))] || ""; }
getPropertyPriority() { return ""; }
get cssText() { this._pull(); return _serializeCss(this._props); }
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 7193f86e..4d63f55f 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -20590,6 +20590,47 @@ mod tests {
assert_eq!(result.value.unwrap(), serde_json::json!(true));
}
+ // 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>");