Skip to content

The headless browser for AI agents and web scraping

fix(js): integrate main and convert style values once

SGavrl4724f4b75664
diff --cc crates/obscura-js/js/bootstrap.js
index 76f6429b,0a815d4e..ab770808
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@@ -1792,25 -1788,11 +1792,26 @@@ class CSSStyleDeclaration
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);
++ value = String(value);
++ if (old === value) return;
++ this._props[k] = 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 --cc crates/obscura-js/src/runtime.rs
index 4d63f55f,e712c961..14bd9ef5
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@@ -20590,47 -20850,6 +20850,60 @@@ mod tests
assert_eq!(result.value.unwrap(), serde_json::json!(true));
}
++ #[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>");