The headless browser for AI agents and web scraping
merge: refresh media contribution on upstream 6393af1
Antonio Rossi5db5c21bdc95
diff --cc crates/obscura-js/js/bootstrap.jsindex cc8bd0d4,82576e07..f1d5a368--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@@ -8441,28 -8534,17 +8537,31 @@@ function _evaluateMediaQueryList(queryglobalThis.matchMedia = _markNative(function matchMedia(q) {const media = q == null ? '' : String(q);- return {- get matches() { return _evaluateMediaQueryList(media); },- media,- onchange: null,- addListener(){},- removeListener(){},- addEventListener(){},- removeEventListener(){},- dispatchEvent(){return true;}- };+ return new MediaQueryList(_mediaQueryToken, media);});+const _mediaQueryToken = {};+const _mediaQueries = new Set();+// A document keeps query lists with change listeners alive even when author+// code does not retain the object returned from matchMedia().+const _activeMediaQueries = new Set();+const _mediaQueryState = new WeakMap();+globalThis.__obscura_recompute_media_queries = () => {++ // Media/viewport changes also invalidate live computed-style snapshots.++ // Keep the epoch in this private scope, not in host-injected page script.++ _domMutationEpoch++;+ for (const ref of _mediaQueries) {+ const query = ref.deref();+ if (!query) { _mediaQueries.delete(ref); continue; }+ const state = _mediaQueryState.get(query);+ const matches = query.matches;+ if (matches === state.matches) continue;+ state.matches = matches;+ // Capture the value at this rendering change, not at eventual delivery.+ setTimeout(() => query.dispatchEvent(new MediaQueryListEvent('change', {+ matches, media: query.media,+ })), 0);+ }+};// getComputedStyle() returns a fresh declaration object, but those objects all// observe the same computed style for an element until the document mutates.// Share the immutable native snapshot behind them. Frameworks routinely calldiff --cc crates/obscura-js/src/runtime.rsindex 9f85ecf3,53440fea..b6637255--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@@ -1419,20 -1483,6 +1484,19 @@@ impl ObscuraJsRuntime);}+ /// Keep JavaScript media queries and CSS selection on the same preference.+ pub fn set_reduced_motion(&mut self, reduce: bool) {+ #[cfg(feature = "render")]+ {+ let media = self.state.borrow().render_media.with_reduced_motion(reduce);+ self.set_render_media(media);+ }+ let _ = self.execute_runtime_script("<reduced-motion>", format!(+ "globalThis.__obscura_reduced_motion={reduce};\- globalThis.__obscura_recompute_media_queries();\- _domMutationEpoch++;"++ globalThis.__obscura_recompute_media_queries();"+ ));+ }+/// Override the physical screen metrics exposed to page JavaScript./// Unlike the CSS viewport, CDP only changes these when both optional/// screen dimensions are supplied. Passing `None` restores the nativediff --cc crates/obscura-js/tests/reduced_motion.rsindex 7df05e52,00000000..36c6368dmode 100644,000000..100644--- a/crates/obscura-js/tests/reduced_motion.rs+++ b/crates/obscura-js/tests/reduced_motion.rs@@@ -1,33 -1,0 +1,47 @@@+use obscura_js::runtime::ObscuraJsRuntime;+use serde_json::json;+++#[cfg(feature = "render")]++#[test]++fn reduced_motion_refreshes_existing_computed_style_objects() {++ let mut rt = ObscuraJsRuntime::new();++ rt.set_dom(obscura_dom::parse_html("<style>div{width:100px}@media(prefers-reduced-motion:reduce){div{width:40px}}</style><div>probe</div>"));++ rt.run_page_init();++ rt.evaluate("globalThis.liveStyle = getComputedStyle(document.querySelector('div'))").unwrap();++ assert_eq!(rt.evaluate("liveStyle.width").unwrap(), json!("100px"));++ rt.set_reduced_motion(true);++ assert_eq!(rt.evaluate("liveStyle.width").unwrap(), json!("40px"));++ rt.set_reduced_motion(false);++ assert_eq!(rt.evaluate("liveStyle.width").unwrap(), json!("100px"));++}+++#[tokio::test]+async fn reduced_motion_updates_queries_and_change_events() {+ let mut rt = ObscuraJsRuntime::new();+ rt.set_dom(obscura_dom::parse_html("<body></body>"));+ rt.run_page_init();+ rt.evaluate("(() => { globalThis.q = matchMedia('(prefers-reduced-motion: reduce)'); globalThis.changes = []; q.onchange = e => changes.push(e.matches); })()").unwrap();+ rt.set_reduced_motion(true);+ rt.run_event_loop_bounded(30).await.unwrap();+ rt.set_reduced_motion(false);+ rt.run_event_loop_bounded(30).await.unwrap();+ assert_eq!(rt.evaluate("[q.matches, changes]").unwrap(), json!([false, [true, false]]));+}++#[cfg(feature = "render")]+#[test]+fn reduced_motion_invalidates_css_geometry() {+ let mut rt = ObscuraJsRuntime::new();+ rt.set_dom(obscura_dom::parse_html("<body></body>"));+ rt.run_page_init();+ rt.evaluate(r#"(() => {+ const html = '<style>div{width:100px}@media(prefers-reduced-motion:reduce){div{width:40px}}</style><div>probe</div>';+ document.body.innerHTML = html;+ globalThis.widths = () => [document.querySelector('div').getBoundingClientRect().width];+ })()"#).unwrap();+ assert_eq!(rt.evaluate("widths()").unwrap(), json!([100]));+ rt.set_reduced_motion(true);+ assert_eq!(rt.evaluate("widths()").unwrap(), json!([40]));+ rt.set_reduced_motion(false);+ assert_eq!(rt.evaluate("widths()").unwrap(), json!([100]));+}