Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(css): honor document color schemes

ldm09cef93dc594b
diff --git a/Cargo.lock b/Cargo.lock
index 68ffecd43..0d92ae543 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4247,7 +4247,7 @@ dependencies = [
[[package]]
name = "selectors"
version = "0.40.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"bitflags",
"cssparser",
@@ -4379,7 +4379,7 @@ dependencies = [
[[package]]
name = "servo_arc"
version = "0.4.3"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"serde",
"stable_deref_trait",
@@ -4626,7 +4626,7 @@ dependencies = [
[[package]]
name = "stylo"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"app_units",
"arrayvec",
@@ -4682,7 +4682,7 @@ dependencies = [
[[package]]
name = "stylo_atoms"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"string_cache",
"string_cache_codegen",
@@ -4691,7 +4691,7 @@ dependencies = [
[[package]]
name = "stylo_derive"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"darling",
"proc-macro2",
@@ -4703,7 +4703,7 @@ dependencies = [
[[package]]
name = "stylo_dom"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"bitflags",
"stylo_malloc_size_of",
@@ -4712,7 +4712,7 @@ dependencies = [
[[package]]
name = "stylo_malloc_size_of"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"app_units",
"cssparser",
@@ -4729,7 +4729,7 @@ dependencies = [
[[package]]
name = "stylo_static_prefs"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"toml",
]
@@ -4747,7 +4747,7 @@ dependencies = [
[[package]]
name = "stylo_traits"
version = "0.20.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"app_units",
"bitflags",
@@ -5033,7 +5033,7 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "to_shmem"
version = "0.5.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"cssparser",
"servo_arc",
@@ -5046,7 +5046,7 @@ dependencies = [
[[package]]
name = "to_shmem_derive"
version = "0.1.0"
-source = "git+https://github.com/ldm0/stylo?rev=74cd014af14d84db1fc8faed85086895012fc687#74cd014af14d84db1fc8faed85086895012fc687"
+source = "git+https://github.com/ldm0/stylo?rev=713ba89c8e23dd76fd350c5bee68590bfdb885ae#713ba89c8e23dd76fd350c5bee68590bfdb885ae"
dependencies = [
"darling",
"proc-macro2",
diff --git a/Cargo.toml b/Cargo.toml
index 5e8bff65e..a58973fda 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -92,12 +92,12 @@ curl = { git = "https://github.com/ldm0/curl-rust", branch = "moli" }
curl-sys = { git = "https://github.com/ldm0/curl-rust", branch = "moli" }
cookie = { git = "https://github.com/ldm0/cookie-rs", branch = "priority" }
v8 = { path = "vendor/v8-146.9.0" }
-selectors = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-stylo = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-stylo_atoms = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-stylo_dom = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-stylo_static_prefs = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
-stylo_traits = { git = "https://github.com/ldm0/stylo", rev = "74cd014af14d84db1fc8faed85086895012fc687" }
+selectors = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
+stylo = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
+stylo_atoms = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
+stylo_dom = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
+stylo_static_prefs = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
+stylo_traits = { git = "https://github.com/ldm0/stylo", rev = "713ba89c8e23dd76fd350c5bee68590bfdb885ae" }
taffy = { git = "https://github.com/ldm0/taffy", rev = "722358e39cd42cb95cc93d5bf06f3a2940a05748" }
[workspace.lints.clippy]
diff --git a/moli-benchmark/wpt-cross-current/failed-cases.txt b/moli-benchmark/wpt-cross-current/failed-cases.txt
index 0dd377c01..af6c67f6a 100644
--- a/moli-benchmark/wpt-cross-current/failed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/failed-cases.txt
@@ -81,7 +81,6 @@ css/css-color-hdr/parsing.html
css/css-color/animation/color-composition.html
css/css-color/animation/color-interpolation.html
css/css-color/animation/opacity-interpolation.html
-css/css-color/light-dark-basic.html
css/css-color/light-dark-currentcolor-in-color.html
css/css-color/light-dark-image.html
css/css-color/parsing/alpha-color-computed.html
@@ -98,7 +97,6 @@ css/css-color/parsing/color-valid-rgb.html
css/css-color/parsing/opacity-valid.html
css/css-color/system-color-compute.html
css/css-color/system-color-consistency.html
-css/css-color/system-color-support.html
css/css-conditional/container-queries/container-computed.html
css/css-conditional/container-queries/container-ident-function.html
css/css-conditional/container-queries/container-inner-at-rules.html
@@ -3206,18 +3204,6 @@ html/semantics/document-metadata/the-base-element/base_multiple.html
html/semantics/document-metadata/the-base-element/base_srcdoc.html
html/semantics/document-metadata/the-link-element/link-load-error-events.html
html/semantics/document-metadata/the-link-element/stylesheet-not-removed-until-next-stylesheet-loads.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-attribute-changes.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-empty-content-value.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-first-valid-applies.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-insert.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-no-content-value.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-normal-descendant-change.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-remove-head.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-remove.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-body.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-head.html
-html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-shadow-tree.html
-html/semantics/document-metadata/the-meta-element/http-equiv-and-name-1.html
html/semantics/document-metadata/the-style-element/tentative/style-element-basic-import.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-allowed.html
html/semantics/document-metadata/the-style-element/tentative/style-element-csp-nonce-allowed.html
diff --git a/moli-benchmark/wpt-cross-current/passed-cases.txt b/moli-benchmark/wpt-cross-current/passed-cases.txt
index ce94c5df5..04e006b0f 100644
--- a/moli-benchmark/wpt-cross-current/passed-cases.txt
+++ b/moli-benchmark/wpt-cross-current/passed-cases.txt
@@ -340,6 +340,7 @@ css/css-color/color-mix-currentcolor-visited-getcomputedstyle.html
css/css-color/color-mix-missing-components.html
css/css-color/contrast-color-currentcolor-inherited.html
css/css-color/inheritance.html
+css/css-color/light-dark-basic.html
css/css-color/nested-color-mix-with-currentcolor.html
css/css-color/parsing/alpha-color-parsing-invalid.html
css/css-color/parsing/color-computed-color-function.html
@@ -374,6 +375,7 @@ css/css-color/parsing/opacity-invalid.html
css/css-color/parsing/relative-color-out-of-gamut.html
css/css-color/relative-color-with-zoom.html
css/css-color/relative-currentcolor-visited-getcomputedstyle.html
+css/css-color/system-color-support.html
css/css-conditional/at-supports-whitespace.html
css/css-conditional/container-queries/at-container-parsing.html
css/css-conditional/container-queries/at-container-serialization.html
@@ -5294,6 +5296,18 @@ html/semantics/document-metadata/the-link-element/link-style-data-url-quirks.htm
html/semantics/document-metadata/the-link-element/stylesheet-bad-mime-type.html
html/semantics/document-metadata/the-link-element/stylesheet-media-change-no-fetch.html
html/semantics/document-metadata/the-link-element/stylesheet-non-OK-status.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-attribute-changes.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-empty-content-value.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-first-valid-applies.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-insert.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-no-content-value.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-normal-descendant-change.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-remove-head.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-remove.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-body.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-head.html
+html/semantics/document-metadata/the-meta-element/color-scheme/meta-color-scheme-single-value-in-shadow-tree.html
+html/semantics/document-metadata/the-meta-element/http-equiv-and-name-1.html
html/semantics/document-metadata/the-meta-element/http-equiv-and-name-2.html
html/semantics/document-metadata/the-meta-element/pragma-directives/attr-meta-http-equiv-refresh/allow-scripts-flag-changing-1.html
html/semantics/document-metadata/the-meta-element/pragma-directives/attr-meta-http-equiv-refresh/allow-scripts-flag-changing-2.html
diff --git a/moli-css-parse/src/color.rs b/moli-css-parse/src/color.rs
index e2161ad98..d97de6294 100644
--- a/moli-css-parse/src/color.rs
+++ b/moli-css-parse/src/color.rs
@@ -7,7 +7,7 @@ use style::{
custom_properties::AttrTaint,
parser::{Parse, ParserContext},
stylesheets::{CssRuleType, Origin, UrlExtraData},
- values::specified::color::{Color, SystemColor},
+ values::specified::color::{Color, ColorScheme, ColorSchemeFlags, SystemColor},
};
use style_traits::ParsingMode;
@@ -51,6 +51,30 @@ pub fn css_system_color_srgb(value: &str) -> Option<(u8, u8, u8)> {
Some((red, green, blue))
}
+/// Parse the CSS `color-scheme` grammar and return its known-scheme flags.
+///
+/// A successful value with no flags is the valid `normal` keyword. Custom
+/// schemes are preserved by Stylo's validation but do not set a known flag.
+pub fn parse_css_color_scheme_flags(value: &str) -> Option<ColorSchemeFlags> {
+ let url_data = UrlExtraData::from(url::Url::parse("about:blank").ok()?);
+ let context = ParserContext::new(
+ Origin::Author,
+ &url_data,
+ Some(CssRuleType::Style),
+ ParsingMode::DEFAULT,
+ QuirksMode::NoQuirks,
+ Default::default(),
+ None,
+ None,
+ AttrTaint::default(),
+ );
+ let mut input = ParserInput::new(value);
+ Parser::new(&mut input)
+ .parse_entirely(|input| ColorScheme::parse(&context, input))
+ .ok()
+ .map(|scheme| scheme.bits)
+}
+
fn parse_specified_color(value: &str) -> Option<Color> {
let url_data = UrlExtraData::from(url::Url::parse("about:blank").ok()?);
let context = ParserContext::new(
@@ -161,4 +185,27 @@ mod tests {
assert_eq!(css_system_color_srgb("ActiveBorder"), Some((169, 169, 169)));
assert_eq!(css_system_color_srgb("red"), None);
}
+
+ #[test]
+ fn color_scheme_flags_use_stylo_grammar() {
+ assert_eq!(
+ parse_css_color_scheme_flags("normal"),
+ Some(ColorSchemeFlags::empty())
+ );
+ assert_eq!(
+ parse_css_color_scheme_flags("light dark"),
+ Some(ColorSchemeFlags::LIGHT | ColorSchemeFlags::DARK)
+ );
+ assert_eq!(
+ parse_css_color_scheme_flags("BLUE light"),
+ Some(ColorSchemeFlags::LIGHT)
+ );
+ assert_eq!(
+ parse_css_color_scheme_flags("only light"),
+ Some(ColorSchemeFlags::ONLY | ColorSchemeFlags::LIGHT)
+ );
+ for invalid in ["", "light,dark", "light, dark", "only"] {
+ assert_eq!(parse_css_color_scheme_flags(invalid), None, "{invalid}");
+ }
+ }
}
diff --git a/moli-css-parse/src/lib.rs b/moli-css-parse/src/lib.rs
index 8bb9621ef..82614bd5d 100644
--- a/moli-css-parse/src/lib.rs
+++ b/moli-css-parse/src/lib.rs
@@ -14,7 +14,8 @@ mod util;
mod value;
pub use color::{
- css_system_color_srgb, parse_css_color_to_opaque_srgb_hex, parse_css_color_to_srgb_bytes,
+ css_system_color_srgb, parse_css_color_scheme_flags, parse_css_color_to_opaque_srgb_hex,
+ parse_css_color_to_srgb_bytes,
};
pub use declaration::{CssDeclaration, DeclarationParseOptions, parse_declaration_list};
pub use font_face::{
diff --git a/moli-renderer-v8/src/document_color_scheme.rs b/moli-renderer-v8/src/document_color_scheme.rs
new file mode 100644
index 000000000..8e8298b48
--- /dev/null
+++ b/moli-renderer-v8/src/document_color_scheme.rs
@@ -0,0 +1,31 @@
+use style::values::specified::color::ColorSchemeFlags;
+
+use crate::{
+ document_runtime::DomHandle,
+ dom::native::{DomHost, Node},
+};
+
+/// Return the known schemes from the first valid document-tree
+/// `<meta name=color-scheme>` in tree order.
+///
+/// The value is a page-level used-color input. It is deliberately not exposed
+/// as the root element's computed `color-scheme`, which remains `normal`.
+pub(crate) fn document_page_color_schemes(
+ dom_host: &DomHost,
+ document: DomHandle,
+) -> ColorSchemeFlags {
+ dom_host
+ .html_elements_by_local_name_in_document_tree_order(document, "meta")
+ .into_iter()
+ .find_map(|handle| {
+ let element = dom_host.node(handle).and_then(Node::as_element)?;
+ if !element
+ .attribute("name")
+ .is_some_and(|name| name.eq_ignore_ascii_case("color-scheme"))
+ {
+ return None;
+ }
+ moli_css_parse::parse_css_color_scheme_flags(element.attribute("content")?)
+ })
+ .unwrap_or_else(ColorSchemeFlags::empty)
+}
diff --git a/moli-renderer-v8/src/lib.rs b/moli-renderer-v8/src/lib.rs
index 0dfb5841e..c96534611 100644
--- a/moli-renderer-v8/src/lib.rs
+++ b/moli-renderer-v8/src/lib.rs
@@ -42,6 +42,7 @@ mod detached_css_style;
mod detached_dom_surface;
mod detached_event_target;
mod devtools;
+mod document_color_scheme;
mod document_cookie_owner;
mod document_language;
mod document_last_modified;
diff --git a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
index 7b4de935c..cdee65c7f 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/host_environment.rs
@@ -1696,12 +1696,20 @@ impl JsContextHost {
tree_scope_versions: crate::style_engine::StyleTreeScopeVersions,
) -> StyleObservationSnapshot {
let document_context = self.style_source_document_context_for_read_document(read_document);
+ let page_color_schemes = self
+ .dom_host()
+ .owner_document_handle(handle)
+ .map(|document| {
+ crate::document_color_scheme::document_page_color_schemes(self.dom_host(), document)
+ })
+ .unwrap_or_default();
self.style_engine
.computed_style_snapshot_from_current_observation(
self.dom_host(),
self.document_url(),
handle,
- StyloStyleEnvironment::from_emulated_media(self.emulated_media()),
+ StyloStyleEnvironment::from_emulated_media(self.emulated_media())
+ .with_page_color_schemes(page_color_schemes),
document_context.as_ref(),
read_document,
viewport,
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 c3f111749..e668d5974 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
@@ -92,7 +92,7 @@ pub(super) fn prepare_style_world_update(
context: StyleComputationContext,
plan: &StyleWorldUpdatePlan,
) -> Rc<PreparedStyleWorldUpdate> {
- let media = StyloStyleEnvironment::from_emulated_media(runtime.emulated_media());
+ let media = stylo_style_environment(runtime, key.source_document());
let quirks_mode = quirks_mode(runtime, key.source_document());
let environment = StyleWorldEnvironment::new(
context.viewport(),
@@ -156,7 +156,23 @@ pub(super) fn stylesheet_query_fallback(
runtime,
key,
context,
- StyloStyleEnvironment::from_emulated_media(runtime.emulated_media()),
+ stylo_style_environment(runtime, key.source_document()),
+ )
+}
+
+fn stylo_style_environment(
+ runtime: &JsContextHost,
+ document: Option<DomHandle>,
+) -> StyloStyleEnvironment {
+ StyloStyleEnvironment::from_emulated_media(runtime.emulated_media()).with_page_color_schemes(
+ document
+ .map(|document| {
+ crate::document_color_scheme::document_page_color_schemes(
+ runtime.dom_host(),
+ document,
+ )
+ })
+ .unwrap_or_default(),
)
}
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 e21f7bc82..3871116bc 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
@@ -8712,6 +8712,76 @@ fn computed_style_resolves_system_color_properties() {
);
}
+#[test]
+fn meta_color_scheme_controls_used_system_colors_without_changing_computed_property() {
+ let mut vm = new_parsed_test_vm(
+ "https://meta-color-scheme.test/",
+ r#"<!doctype html>
+ <html><head>
+ <meta id="scheme" http-equiv="content-language" name="color-scheme" content="dark">
+ </head><body>
+ <div id="light" style="color-scheme: only light; color: CanvasText"></div>
+ <div id="dark" style="color-scheme: only dark; color: CanvasText"></div>
+ <div id="normal" style="color-scheme: normal; color: CanvasText"></div>
+ </body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const meta = document.getElementById('scheme');
+ const light = getComputedStyle(document.getElementById('light')).color;
+ const dark = getComputedStyle(document.getElementById('dark')).color;
+ const normal = () => getComputedStyle(document.getElementById('normal')).color;
+ const root = () => getComputedStyle(document.documentElement);
+ const states = {
+ explicitSchemesDiffer: light !== dark,
+ initialRootUsesDark: root().color === dark,
+ initialNormalUsesDark: normal() === dark,
+ rootComputedSchemeStaysNormal: root().colorScheme === 'normal',
+ metaDoesNotChangePreference: !matchMedia('(prefers-color-scheme: dark)').matches
+ };
+
+ meta.content = 'light';
+ states.lightMutationUpdatesRoot = root().color === light;
+ states.lightMutationUpdatesNormal = normal() === light;
+ states.explicitDarkStaysDark =
+ getComputedStyle(document.getElementById('dark')).color === dark;
+
+ meta.content = ',,invalid';
+ states.invalidContentUsesInitial = root().color === light;
+ meta.removeAttribute('content');
+ states.missingContentUsesInitial = root().color === light;
+
+ meta.content = 'dark';
+ const earlier = document.createElement('meta');
+ earlier.name = 'color-scheme';
+ earlier.content = 'light';
+ document.head.insertBefore(earlier, meta);
+ states.firstValidWins = root().color === light;
+ earlier.remove();
+ states.removalRestoresNextValid = root().color === dark;
+
+ meta.remove();
+ const host = document.body.appendChild(document.createElement('div'));
+ const shadowMeta = document.createElement('meta');
+ shadowMeta.name = 'color-scheme';
+ shadowMeta.content = 'dark';
+ host.attachShadow({ mode: 'open' }).appendChild(shadowMeta);
+ states.shadowMetaIsIgnored = root().color === light;
+ return JSON.stringify(states);
+})()
+"#,
+ )
+ .expect("meta color-scheme should control used system colors");
+
+ assert_eq!(
+ result,
+ r#"{"explicitSchemesDiffer":true,"initialRootUsesDark":true,"initialNormalUsesDark":true,"rootComputedSchemeStaysNormal":true,"metaDoesNotChangePreference":true,"lightMutationUpdatesRoot":true,"lightMutationUpdatesNormal":true,"explicitDarkStaysDark":true,"invalidContentUsesInitial":true,"missingContentUsesInitial":true,"firstValidWins":true,"removalRestoresNextValid":true,"shadowMetaIsIgnored":true}"#
+ );
+}
+
#[test]
fn computed_style_resolves_env_color_fallbacks() {
let mut vm = new_storage_test_vm("https://computed-env-color.test/");
diff --git a/moli-renderer-v8/src/style_engine/retained.rs b/moli-renderer-v8/src/style_engine/retained.rs
index d24eff2d1..6eef109b1 100644
--- a/moli-renderer-v8/src/style_engine/retained.rs
+++ b/moli-renderer-v8/src/style_engine/retained.rs
@@ -40,6 +40,7 @@ pub(super) struct RetainedStyleInvalidations {
pub(super) shadow_scope_fallbacks: Vec<DomHandle>,
pub(super) removed_shadow_scopes: Vec<DomHandle>,
pub(super) viewport_size_changed: bool,
+ pub(super) used_color_scheme_changed: bool,
}
/// Compares an explicit full-world snapshot with the canonical retained
@@ -76,6 +77,7 @@ impl RetainedStyleInvalidations {
shadow_scope_fallbacks: Vec::new(),
removed_shadow_scopes: Vec::new(),
viewport_size_changed: false,
+ used_color_scheme_changed: false,
}
}
}
@@ -204,6 +206,7 @@ pub(super) fn update_retained_style_system(
invalidations.document = document_update.invalidations;
invalidations.document_scope_fallback = document_update.scope_fallback;
invalidations.viewport_size_changed = document_update.viewport_size_changed;
+ invalidations.used_color_scheme_changed = document_update.used_color_scheme_changed;
let shadow_reconciliation = reconcile_shadow_scopes(
retained,
@@ -264,6 +267,7 @@ pub(super) fn update_retained_style_system_incrementally(
invalidations.document = document_update.invalidations;
invalidations.document_scope_fallback = document_update.scope_fallback;
invalidations.viewport_size_changed = document_update.viewport_size_changed;
+ invalidations.used_color_scheme_changed = document_update.used_color_scheme_changed;
let shadow_reconciliation = reconcile_dirty_shadow_scopes(
retained,
@@ -321,6 +325,7 @@ struct DocumentScopeUpdate {
scope_fallback: bool,
device_changed: bool,
viewport_size_changed: bool,
+ used_color_scheme_changed: bool,
device_affected_origins: OriginSet,
stylesheets_changed: bool,
}
@@ -366,6 +371,10 @@ fn update_document_scope(
let viewport_size_changed = retained.key.viewport_width_bits != key.viewport_width_bits
|| retained.key.viewport_height_bits != key.viewport_height_bits;
+ let used_color_scheme_changed = retained.key.environment.stylo_prefers_color_scheme()
+ != key.environment.stylo_prefers_color_scheme()
+ || retained.key.environment.stylo_page_color_schemes()
+ != key.environment.stylo_page_color_schemes();
let device_changed = viewport_size_changed
|| retained.key.screen_width_bits != key.screen_width_bits
|| retained.key.screen_height_bits != key.screen_height_bits
@@ -428,6 +437,7 @@ fn update_document_scope(
scope_fallback,
device_changed,
viewport_size_changed,
+ used_color_scheme_changed,
device_affected_origins,
stylesheets_changed,
}
diff --git a/moli-renderer-v8/src/style_engine/stylesheet.rs b/moli-renderer-v8/src/style_engine/stylesheet.rs
index e379588fb..96864d5a2 100644
--- a/moli-renderer-v8/src/style_engine/stylesheet.rs
+++ b/moli-renderer-v8/src/style_engine/stylesheet.rs
@@ -426,6 +426,7 @@ pub(super) fn new_style_device_with_viewport_bits(
PointerCapabilities::default(),
);
device.set_media_feature_preferences(environment.stylo_media_feature_preferences());
+ device.set_page_color_schemes(environment.stylo_page_color_schemes());
device
}
diff --git a/moli-renderer-v8/src/style_engine/ua/html.css b/moli-renderer-v8/src/style_engine/ua/html.css
index c9a8e1255..0faf403c6 100644
--- a/moli-renderer-v8/src/style_engine/ua/html.css
+++ b/moli-renderer-v8/src/style_engine/ua/html.css
@@ -29,6 +29,10 @@
@namespace url(http://www.w3.org/1999/xhtml);
+html {
+ color: CanvasText;
+}
+
html, body, address, article, aside, blockquote, center, dd, dir, div, dl, dt,
fieldset, figcaption, figure, footer, form, header, hgroup, legend, main, nav,
noframes, p, search, section, details, summary, optgroup,
diff --git a/moli-renderer-v8/src/style_engine/world_environment.rs b/moli-renderer-v8/src/style_engine/world_environment.rs
index 0637f3a23..440bb65ad 100644
--- a/moli-renderer-v8/src/style_engine/world_environment.rs
+++ b/moli-renderer-v8/src/style_engine/world_environment.rs
@@ -1,7 +1,9 @@
use style::{
- device::servo::ServoMediaFeaturePreferences, media_queries::MediaType,
- queries::values::PrefersColorScheme, servo::media_features::PrefersContrast,
- values::specified::color::ForcedColors,
+ device::servo::ServoMediaFeaturePreferences,
+ media_queries::MediaType,
+ queries::values::PrefersColorScheme,
+ servo::media_features::PrefersContrast,
+ values::specified::color::{ColorSchemeFlags, ForcedColors},
};
use crate::{document_runtime::DomHandle, dom::native::DomHost};
@@ -92,6 +94,7 @@ impl From<Option<f64>> for StyleViewport {
pub(crate) struct StyloStyleEnvironment {
media_type: StyloStyleMediaType,
color_scheme: StyloStyleColorScheme,
+ page_color_scheme_bits: u8,
reduced_motion: StyloStyleReducedPreference,
reduced_data: StyloStyleReducedPreference,
reduced_transparency: StyloStyleReducedPreference,
@@ -151,6 +154,7 @@ impl StyloStyleEnvironment {
} else {
StyloStyleColorScheme::Light
},
+ page_color_scheme_bits: 0,
reduced_motion: match overrides.reduced_motion.as_deref() {
Some("reduce") => StyloStyleReducedPreference::Reduce,
Some("no-preference") | None => StyloStyleReducedPreference::NoPreference,
@@ -173,6 +177,11 @@ impl StyloStyleEnvironment {
}
}
+ pub(crate) fn with_page_color_schemes(mut self, color_schemes: ColorSchemeFlags) -> Self {
+ self.page_color_scheme_bits = color_schemes.bits();
+ self
+ }
+
pub(super) fn stylo_media_type(self) -> MediaType {
match self.media_type {
StyloStyleMediaType::Screen => MediaType::screen(),
@@ -187,6 +196,10 @@ impl StyloStyleEnvironment {
}
}
+ pub(super) fn stylo_page_color_schemes(self) -> ColorSchemeFlags {
+ ColorSchemeFlags::from_bits_retain(self.page_color_scheme_bits)
+ }
+
pub(super) fn stylo_media_feature_preferences(self) -> ServoMediaFeaturePreferences {
ServoMediaFeaturePreferences {
prefers_reduced_motion: self.reduced_motion.prefers_reduced(),
diff --git a/moli-renderer-v8/src/style_engine/world_lifecycle.rs b/moli-renderer-v8/src/style_engine/world_lifecycle.rs
index 5d288157c..6c8d0fe10 100644
--- a/moli-renderer-v8/src/style_engine/world_lifecycle.rs
+++ b/moli-renderer-v8/src/style_engine/world_lifecycle.rs
@@ -300,6 +300,7 @@ fn apply_retained_stylesheet_invalidations(
shadow_scope_fallbacks,
removed_shadow_scopes,
viewport_size_changed,
+ used_color_scheme_changed,
} = invalidations;
if let Some(invalidations) = document_invalidations {
// HTML documents normally have one document element. The native DOM
@@ -336,6 +337,9 @@ fn apply_retained_stylesheet_invalidations(
if viewport_size_changed {
invalidate_viewport_unit_styles(host, dom_adapter, document);
}
+ if used_color_scheme_changed {
+ cache_cleanup.invalidate_subtrees(host, [document]);
+ }
cache_cleanup.invalidate_stylesheet_dirty_subtrees(host);
if document_scope_fallback {
cache_cleanup.invalidate_subtrees(host, [document]);