Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(style): respect owner media in raw CSS scanners

ldm044ee8f60a5b8
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
index ac2e280f3..d96fac3da 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/style_world.rs
@@ -7,8 +7,9 @@ use crate::{
dom::native::{DomHost, Node},
style_engine::{
FullStyleWorldSnapshot, IncrementalStyleWorldUpdate, PreparedStyleWorldUpdate,
- StyleSourceId, StyleTreeScopeVersions, StyleWorldEnvironment, StyleWorldUpdatePlan,
- StyloStyleEnvironment, StyloStylesheetSource, link_rel_qualifies_as_stylesheet,
+ StyleSourceId, StyleTreeScopeVersions, StyleViewport, StyleWorldEnvironment,
+ StyleWorldUpdatePlan, StyloStyleEnvironment, StyloStylesheetSource,
+ link_rel_qualifies_as_stylesheet,
},
stylesheet_blocking::link_rel_includes_token,
};
@@ -328,10 +329,17 @@ pub(super) fn active_stylesheet_handles(
handles
}
-pub(super) fn stylesheet_text(runtime: &JsContextHost, handle: DomHandle) -> String {
- stylesheet_source(runtime, handle)
- .serialized_css_text()
- .to_string()
+pub(super) fn effective_raw_stylesheet_sources(
+ runtime: &JsContextHost,
+ root: DomHandle,
+ include_detached: bool,
+ viewport: StyleViewport,
+) -> Vec<StyloStylesheetSource> {
+ active_stylesheet_handles(runtime, root, include_detached)
+ .into_iter()
+ .map(|handle| stylesheet_source(runtime, handle))
+ .filter(|source| source.media_matches(runtime.emulated_media(), viewport))
+ .collect()
}
fn stylesheet_source(runtime: &JsContextHost, handle: DomHandle) -> StyloStylesheetSource {
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
index 3ab635fa7..326df6048 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
@@ -31,8 +31,7 @@ use super::observation::{
ComputedStyleRead, RetainedStyleObservation, StyleComputationContext, StyleObservation,
};
use super::style_world::{
- active_stylesheet_handles, style_base_url, stylesheet_source_document_for_handle,
- stylesheet_text,
+ effective_raw_stylesheet_sources, style_base_url, stylesheet_source_document_for_handle,
};
use super::{
StyleMode, all_shorthand_applies_to, animation_shorthand_longhands, css_wide_keyword,
@@ -351,7 +350,9 @@ pub(crate) fn css_animation_start_applies(runtime: &JsContextHost, handle: DomHa
return false;
}
- let names = active_css_animation_names(runtime, handle);
+ let read = ComputedStyleRead::new(runtime, handle);
+ let resolution = read.resolution_context();
+ let names = active_css_animation_names_with_resolution(runtime, handle, resolution);
if names.is_empty() {
return false;
}
@@ -359,9 +360,13 @@ pub(crate) fn css_animation_start_applies(runtime: &JsContextHost, handle: DomHa
let Some(document) = stylesheet_source_document_for_handle(runtime, handle) else {
return false;
};
- for stylesheet in active_stylesheet_handles(runtime, document, false) {
- let css_text = stylesheet_text(runtime, stylesheet);
- if keyframe_has_supported_animation_values(&css_text, &names) {
+ for source in effective_raw_stylesheet_sources(
+ runtime,
+ document,
+ false,
+ resolution.computation.viewport(),
+ ) {
+ if keyframe_has_supported_animation_values(&source.serialized_css_text(), &names) {
return true;
}
}
@@ -470,20 +475,21 @@ fn active_css_animation_property_values_with_resolution(
return None;
}
let document = stylesheet_source_document_for_handle(runtime, handle)?;
- for stylesheet in active_stylesheet_handles(runtime, document, false) {
- let css_text = stylesheet_text(runtime, stylesheet);
- if let Some(values) = keyframe_property_values(&css_text, &names, property) {
+ for source in effective_raw_stylesheet_sources(
+ runtime,
+ document,
+ false,
+ resolution.computation.viewport(),
+ ) {
+ if let Some(values) =
+ keyframe_property_values(&source.serialized_css_text(), &names, property)
+ {
return Some(values);
}
}
None
}
-fn active_css_animation_names(runtime: &JsContextHost, handle: DomHandle) -> Vec<String> {
- let read = ComputedStyleRead::new(runtime, handle);
- active_css_animation_names_with_resolution(runtime, handle, read.resolution_context())
-}
-
fn active_css_animation_names_with_resolution(
runtime: &JsContextHost,
handle: DomHandle,
@@ -4540,7 +4546,7 @@ fn resolve_computed_custom_function_calls(
let Some(scope) = scope else {
return value.to_owned();
};
- let functions = visible_custom_functions_for_scope(runtime, inputs, scope);
+ let functions = visible_custom_functions_for_scope(runtime, inputs, scope, context.viewport());
if functions.is_empty() {
return value.to_owned();
}
@@ -4699,9 +4705,14 @@ fn visible_custom_functions_for_scope(
runtime: &JsContextHost,
inputs: &FullStyleWorldSnapshot,
scope: DomHandle,
+ viewport: StyleViewport,
) -> HashMap<String, CustomCssFunction> {
let mut functions = HashMap::new();
- for source in &inputs.document_stylesheet_sources {
+ for source in inputs
+ .document_stylesheet_sources
+ .iter()
+ .filter(|source| source.media_matches(runtime.emulated_media(), viewport))
+ {
collect_custom_functions_from_stylesheet_source(runtime, source, &mut functions);
}
if runtime
@@ -4714,7 +4725,10 @@ fn visible_custom_functions_for_scope(
let shadow_chain = shadow_root_ancestor_chain(runtime, scope);
for (root, sources) in &inputs.shadow_stylesheet_sources {
if shadow_chain.contains(root) {
- for source in sources {
+ for source in sources
+ .iter()
+ .filter(|source| source.media_matches(runtime.emulated_media(), viewport))
+ {
collect_custom_functions_from_stylesheet_source(runtime, source, &mut functions);
}
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index 63f4af6eb..c8441e734 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -10012,6 +10012,120 @@ async fn css_animation_start_and_midpoint_style_are_observable() {
assert_eq!(result, "1|250px|1");
}
+#[test]
+fn raw_keyframe_scanner_respects_owner_media_changes() {
+ let mut vm = new_parsed_test_vm(
+ "https://keyframe-owner-media.test/",
+ r#"<!doctype html>
+ <style>
+ #item {
+ position: relative;
+ animation-name: print-only;
+ animation-duration: 1s;
+ }
+ </style>
+ <style media="print">
+ @keyframes print-only {
+ from { left: 100px; }
+ to { left: 400px; }
+ }
+ </style>
+ <div id="item"></div>"#,
+ );
+
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ const item = document.getElementById('item');
+ return [getComputedStyle(item).left, item.getAnimations().length].join('|');
+ })()"#,
+ )
+ .expect("screen animation state should evaluate"),
+ "0px|0",
+ "a print-only keyframe rule must be invisible to the screen raw-CSS scanner",
+ );
+
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides {
+ media: Some("print".to_owned()),
+ ..Default::default()
+ });
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ const item = document.getElementById('item');
+ return [getComputedStyle(item).left, item.getAnimations().length].join('|');
+ })()"#,
+ )
+ .expect("print animation state should evaluate"),
+ "250px|1",
+ "the same retained source must become visible after switching to print",
+ );
+
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides::default());
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ const item = document.getElementById('item');
+ return [getComputedStyle(item).left, item.getAnimations().length].join('|');
+ })()"#,
+ )
+ .expect("restored screen animation state should evaluate"),
+ "0px|0",
+ "leaving print must hide the raw keyframe source again",
+ );
+}
+
+#[test]
+fn raw_scanners_follow_live_owner_media_attribute_mutations() {
+ let mut vm = new_parsed_test_vm(
+ "https://raw-scanner-owner-media-mutation.test/",
+ r#"<!doctype html>
+ <style>
+ #item {
+ position: relative;
+ animation: owner-gated 1s;
+ --actual: --owner-gated();
+ }
+ </style>
+ <style id="gated" media="print">
+ @keyframes owner-gated {
+ from { left: 20px; }
+ to { left: 60px; }
+ }
+ @function --owner-gated() { result: 17px; }
+ </style>
+ <div id="item"></div>"#,
+ );
+ let probe = r#"(() => {
+ const item = document.getElementById('item');
+ const computed = getComputedStyle(item);
+ return [
+ computed.left,
+ item.getAnimations().length,
+ computed.getPropertyValue('--actual')
+ ].join('|');
+ })()"#;
+
+ assert_eq!(
+ vm.eval(probe).expect("initial raw scanner state"),
+ "0px|0|--owner-gated()",
+ );
+ vm.eval("document.getElementById('gated').media = 'screen'")
+ .expect("owner media should become effective");
+ assert_eq!(
+ vm.eval(probe).expect("effective raw scanner state"),
+ "40px|1|17px",
+ "owner media mutation must update both raw compatibility scanners",
+ );
+ vm.eval("document.getElementById('gated').media = 'not all'")
+ .expect("owner media should become ineffective");
+ assert_eq!(
+ vm.eval(probe).expect("ineffective raw scanner state"),
+ "0px|0|--owner-gated()",
+ "the retained source must not leak after a second owner media mutation",
+ );
+}
+
#[tokio::test]
async fn registered_length_custom_property_animation_revert_uses_underlying_value() {
let mut vm = new_parsed_test_vm(
@@ -11858,3 +11972,91 @@ fn custom_function_provenance_probe_preserves_the_retained_style_world() {
"rgb(1, 2, 3)"
);
}
+
+#[test]
+fn raw_custom_function_scanner_respects_owner_media_changes() {
+ let mut vm = new_parsed_test_vm(
+ "https://custom-function-owner-media.test/",
+ r#"<!doctype html>
+ <style>#target { --actual: --print-only(); }</style>
+ <style media="print">
+ @function --print-only() { result: 42px; }
+ </style>
+ <div id="target"></div>"#,
+ );
+
+ assert_eq!(
+ vm.eval(
+ "getComputedStyle(document.getElementById('target')).getPropertyValue('--actual')",
+ )
+ .expect("screen custom property should evaluate"),
+ "--print-only()",
+ "a function in a print-only source must not resolve in screen media",
+ );
+
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides {
+ media: Some("print".to_owned()),
+ ..Default::default()
+ });
+ assert_eq!(
+ vm.eval(
+ "getComputedStyle(document.getElementById('target')).getPropertyValue('--actual')",
+ )
+ .expect("print custom property should evaluate"),
+ "42px",
+ "the function must become visible when its owner media becomes effective",
+ );
+
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides::default());
+ assert_eq!(
+ vm.eval(
+ "getComputedStyle(document.getElementById('target')).getPropertyValue('--actual')",
+ )
+ .expect("restored screen custom property should evaluate"),
+ "--print-only()",
+ "leaving print must hide the raw custom function source again",
+ );
+}
+
+#[test]
+fn raw_custom_function_scanner_filters_shadow_owner_media() {
+ let mut vm = new_parsed_test_vm(
+ "https://shadow-custom-function-owner-media.test/",
+ "<!doctype html><body></body>",
+ );
+ vm.eval(
+ r#"(() => {
+ const host = document.createElement('section');
+ document.body.append(host);
+ const shadow = host.attachShadow({ mode: 'open' });
+ shadow.innerHTML = `
+ <style>#target { --actual: --shadow-print-only(); }</style>
+ <style media="print">
+ @function --shadow-print-only() { result: 23px; }
+ </style>
+ <span id="target"></span>`;
+ globalThis.__shadowFunctionTarget = shadow.getElementById('target');
+ })()"#,
+ )
+ .expect("shadow custom function fixture should initialize");
+ let probe = "getComputedStyle(__shadowFunctionTarget).getPropertyValue('--actual')";
+
+ assert_eq!(
+ vm.eval(probe).expect("screen shadow function state"),
+ "--shadow-print-only()",
+ );
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides {
+ media: Some("print".to_owned()),
+ ..Default::default()
+ });
+ assert_eq!(
+ vm.eval(probe).expect("print shadow function state"),
+ "23px",
+ "a shadow-scoped function must become visible only in its effective owner media",
+ );
+ vm.set_emulated_media(&crate::protocol_types::EmulatedMediaOverrides::default());
+ assert_eq!(
+ vm.eval(probe).expect("restored shadow function state"),
+ "--shadow-print-only()",
+ );
+}
diff --git a/moli-renderer-v8/src/style_engine/source/store.rs b/moli-renderer-v8/src/style_engine/source/store.rs
index a84604f2d..3903555fe 100644
--- a/moli-renderer-v8/src/style_engine/source/store.rs
+++ b/moli-renderer-v8/src/style_engine/source/store.rs
@@ -14,7 +14,10 @@ use super::super::{
};
use super::imports::stylesheet_top_level_import_urls;
use super::shared_cache::{SharedStyleSourceContents, shared_style_source_contents};
-use crate::document_runtime::DomHandle;
+use crate::{
+ document_runtime::DomHandle, protocol_types::EmulatedMediaOverrides,
+ style_engine::StyleViewport,
+};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct StylesheetFontFaceDescriptor {
@@ -193,6 +196,20 @@ impl StyloStylesheetSource {
}
}
+ /// Whether this installed source's top-level media list is effective for
+ /// compatibility paths that inspect serialized CSS outside Stylo.
+ pub(crate) fn media_matches(
+ &self,
+ emulated_media: &EmulatedMediaOverrides,
+ viewport: StyleViewport,
+ ) -> bool {
+ crate::style_engine::source_owner::stylesheet_source_media_matches(
+ &self.media_text,
+ emulated_media,
+ viewport,
+ )
+ }
+
/// Projects this source into an independently parsed text source.
///
/// A live stylesheet is owned by the `SharedRwLock` of the retained style
diff --git a/moli-renderer-v8/src/style_engine/source_owner.rs b/moli-renderer-v8/src/style_engine/source_owner.rs
index 0aa3923af..b0427a41a 100644
--- a/moli-renderer-v8/src/style_engine/source_owner.rs
+++ b/moli-renderer-v8/src/style_engine/source_owner.rs
@@ -65,7 +65,7 @@ fn style_element_is_stylesheet_source_enabled(
&& moli_web_mime::is_stylesheet_type_attribute(
host.get_attribute(handle, "type").as_deref(),
)
- && stylesheet_media_matches_for_stylesheet_source(media_text, emulated_media, viewport)
+ && stylesheet_source_media_matches(media_text, emulated_media, viewport)
}
pub(super) fn linked_stylesheet_media_matches_for_stylesheet_source(
@@ -73,10 +73,10 @@ pub(super) fn linked_stylesheet_media_matches_for_stylesheet_source(
emulated_media: &EmulatedMediaOverrides,
viewport: StyleViewport,
) -> bool {
- stylesheet_media_matches_for_stylesheet_source(media_text, emulated_media, viewport)
+ stylesheet_source_media_matches(media_text, emulated_media, viewport)
}
-fn stylesheet_media_matches_for_stylesheet_source(
+pub(super) fn stylesheet_source_media_matches(
media_text: &str,
emulated_media: &EmulatedMediaOverrides,
viewport: StyleViewport,