use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_theme",
app = "cms",
display = "name",
admin(
list_display = "name, slug, is_admin_default, is_default, default_mode",
ordering = "name",
list_filter = "is_default, is_admin_default",
)
)]
pub struct Theme {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(max_length = 100)]
pub name: String,
#[rustango(max_length = 64, unique)]
pub slug: String,
pub is_admin_default: bool,
pub is_default: bool,
#[rustango(max_length = 16)]
pub default_mode: String,
#[rustango(max_length = 32)]
pub color_bg: String,
#[rustango(max_length = 32)]
pub color_surface: String,
#[rustango(max_length = 32)]
pub color_text_primary: String,
#[rustango(max_length = 32)]
pub color_text_secondary: String,
#[rustango(max_length = 32)]
pub color_text_muted: String,
#[rustango(max_length = 32)]
pub color_border: String,
#[rustango(max_length = 32)]
pub color_bg_dark: String,
#[rustango(max_length = 32)]
pub color_surface_dark: String,
#[rustango(max_length = 32)]
pub color_text_primary_dark: String,
#[rustango(max_length = 32)]
pub color_text_secondary_dark: String,
#[rustango(max_length = 32)]
pub color_text_muted_dark: String,
#[rustango(max_length = 32)]
pub color_border_dark: String,
#[rustango(max_length = 32)]
pub color_success: String,
#[rustango(max_length = 32)]
pub color_warning: String,
#[rustango(max_length = 32)]
pub color_error: String,
#[rustango(max_length = 32)]
pub color_info: String,
#[rustango(max_length = 32)]
pub color_link: String,
#[rustango(max_length = 32)]
pub color_focus_ring: String,
#[rustango(max_length = 255)]
pub font_heading: String,
#[rustango(max_length = 255)]
pub font_body: String,
#[rustango(max_length = 500)]
pub font_url: String,
#[rustango(max_length = 16)]
pub radius_sm: String,
#[rustango(max_length = 16)]
pub radius_md: String,
#[rustango(max_length = 16)]
pub radius_lg: String,
#[rustango(max_length = 255)]
pub shadow_sm: String,
#[rustango(max_length = 255)]
pub shadow_md: String,
#[rustango(max_length = 255)]
pub shadow_lg: String,
#[rustango(auto_now_add)]
pub created_at: Auto<DateTime<Utc>>,
#[rustango(auto_now)]
pub updated_at: Auto<DateTime<Utc>>,
}
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_brand_color",
app = "cms",
display = "name",
admin(
list_display = "theme_id, name, slug, sort_order, color_value",
ordering = "theme_id, sort_order",
)
)]
pub struct BrandColor {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(fk = "cms_theme", on = "id", index)]
pub theme_id: i64,
#[rustango(max_length = 64)]
pub slug: String,
#[rustango(max_length = 100)]
pub name: String,
#[rustango(max_length = 255)]
pub color_value: String,
#[rustango(max_length = 255)]
pub color_value_dark: String,
pub sort_order: i32,
}
#[must_use]
pub fn emit_css(theme: &Theme, brand_colors: &[BrandColor]) -> String {
let mut out = String::with_capacity(8192);
out.push_str("<style data-rcms-theme=\"");
out.push_str(&html_escape_attr(&theme.slug));
out.push_str("\">\n");
out.push_str(":root {\n");
push_var(&mut out, "color-bg", &theme.color_bg);
push_var(&mut out, "color-surface", &theme.color_surface);
push_var(&mut out, "color-text-primary", &theme.color_text_primary);
push_var(
&mut out,
"color-text-secondary",
&theme.color_text_secondary,
);
push_var(&mut out, "color-text-muted", &theme.color_text_muted);
push_var(&mut out, "color-border", &theme.color_border);
push_var(&mut out, "color-success", &theme.color_success);
push_var(&mut out, "color-warning", &theme.color_warning);
push_var(&mut out, "color-error", &theme.color_error);
push_var(&mut out, "color-info", &theme.color_info);
push_var(&mut out, "color-link", &theme.color_link);
push_var(&mut out, "color-focus-ring", &theme.color_focus_ring);
push_var(&mut out, "font-heading", &theme.font_heading);
push_var(&mut out, "font-body", &theme.font_body);
push_var(&mut out, "radius-sm", &theme.radius_sm);
push_var(&mut out, "radius-md", &theme.radius_md);
push_var(&mut out, "radius-lg", &theme.radius_lg);
push_var(&mut out, "shadow-sm", &theme.shadow_sm);
push_var(&mut out, "shadow-md", &theme.shadow_md);
push_var(&mut out, "shadow-lg", &theme.shadow_lg);
emit_md3_bridge(
&mut out,
" ",
Md3Bridge {
bg: &theme.color_bg,
surface: &theme.color_surface,
text_primary: &theme.color_text_primary,
text_secondary: &theme.color_text_secondary,
text_muted: &theme.color_text_muted,
border: &theme.color_border,
link: &theme.color_link,
success: &theme.color_success,
warning: &theme.color_warning,
error: &theme.color_error,
mode: BridgeMode::Light,
},
);
for bc in brand_colors {
emit_brand_color_block(&mut out, &bc.slug, &bc.color_value);
}
out.push_str("}\n");
out.push_str(":root[data-theme=\"dark\"] {\n");
push_var(&mut out, "color-bg", &theme.color_bg_dark);
push_var(&mut out, "color-surface", &theme.color_surface_dark);
push_var(
&mut out,
"color-text-primary",
&theme.color_text_primary_dark,
);
push_var(
&mut out,
"color-text-secondary",
&theme.color_text_secondary_dark,
);
push_var(&mut out, "color-text-muted", &theme.color_text_muted_dark);
push_var(&mut out, "color-border", &theme.color_border_dark);
emit_md3_bridge(
&mut out,
" ",
Md3Bridge {
bg: pick_with_fallback(&theme.color_bg_dark, &theme.color_bg),
surface: pick_with_fallback(&theme.color_surface_dark, &theme.color_surface),
text_primary: pick_with_fallback(
&theme.color_text_primary_dark,
&theme.color_text_primary,
),
text_secondary: pick_with_fallback(
&theme.color_text_secondary_dark,
&theme.color_text_secondary,
),
text_muted: pick_with_fallback(&theme.color_text_muted_dark, &theme.color_text_muted),
border: pick_with_fallback(&theme.color_border_dark, &theme.color_border),
link: &theme.color_link,
success: &theme.color_success,
warning: &theme.color_warning,
error: &theme.color_error,
mode: BridgeMode::Dark,
},
);
for bc in brand_colors {
let dark_value = if bc.color_value_dark.is_empty() {
&bc.color_value
} else {
&bc.color_value_dark
};
emit_brand_color_block(&mut out, &bc.slug, dark_value);
}
out.push_str("}\n");
out.push_str("@media (prefers-color-scheme: dark) {\n");
out.push_str(" :root:not([data-theme=\"light\"]) {\n");
push_var_indented(&mut out, "color-bg", &theme.color_bg_dark);
push_var_indented(&mut out, "color-surface", &theme.color_surface_dark);
push_var_indented(
&mut out,
"color-text-primary",
&theme.color_text_primary_dark,
);
push_var_indented(
&mut out,
"color-text-secondary",
&theme.color_text_secondary_dark,
);
push_var_indented(&mut out, "color-text-muted", &theme.color_text_muted_dark);
push_var_indented(&mut out, "color-border", &theme.color_border_dark);
emit_md3_bridge(
&mut out,
" ",
Md3Bridge {
bg: pick_with_fallback(&theme.color_bg_dark, &theme.color_bg),
surface: pick_with_fallback(&theme.color_surface_dark, &theme.color_surface),
text_primary: pick_with_fallback(
&theme.color_text_primary_dark,
&theme.color_text_primary,
),
text_secondary: pick_with_fallback(
&theme.color_text_secondary_dark,
&theme.color_text_secondary,
),
text_muted: pick_with_fallback(&theme.color_text_muted_dark, &theme.color_text_muted),
border: pick_with_fallback(&theme.color_border_dark, &theme.color_border),
link: &theme.color_link,
success: &theme.color_success,
warning: &theme.color_warning,
error: &theme.color_error,
mode: BridgeMode::Dark,
},
);
for bc in brand_colors {
let dark_value = if bc.color_value_dark.is_empty() {
&bc.color_value
} else {
&bc.color_value_dark
};
emit_brand_color_block_indented(&mut out, &bc.slug, dark_value);
}
out.push_str(" }\n");
out.push_str("}\n");
out.push_str("</style>\n");
out
}
struct Md3Bridge<'a> {
bg: &'a str,
surface: &'a str,
text_primary: &'a str,
text_secondary: &'a str,
text_muted: &'a str,
border: &'a str,
link: &'a str,
success: &'a str,
warning: &'a str,
error: &'a str,
mode: BridgeMode,
}
#[derive(Clone, Copy)]
enum BridgeMode {
Light,
Dark,
}
fn emit_md3_bridge(buf: &mut String, indent: &str, b: Md3Bridge<'_>) {
let push = |buf: &mut String, name: &str, value: &str| push_var_at(buf, indent, name, value);
let (cstep_low, cstep, cstep_high, cstep_highest, cstep_dim, cstep_bright) = match b.mode {
BridgeMode::Light => (0.03_f32, 0.06, 0.10, 0.14, 0.05, -0.05),
BridgeMode::Dark => (-0.05_f32, -0.10, -0.15, -0.20, 0.05, -0.10),
};
push(buf, "md-sys-color-surface", b.bg);
push(
buf,
"md-sys-color-surface-dim",
&mix_or_same(b.bg, cstep_dim),
);
push(
buf,
"md-sys-color-surface-bright",
&mix_or_same(b.bg, cstep_bright),
);
push(buf, "md-sys-color-surface-container-lowest", b.surface);
push(
buf,
"md-sys-color-surface-container-low",
&mix_or_same(b.surface, cstep_low),
);
push(
buf,
"md-sys-color-surface-container",
&mix_or_same(b.surface, cstep),
);
push(
buf,
"md-sys-color-surface-container-high",
&mix_or_same(b.surface, cstep_high),
);
push(
buf,
"md-sys-color-surface-container-highest",
&mix_or_same(b.surface, cstep_highest),
);
push(buf, "md-sys-color-surface-variant", b.border);
push(buf, "md-sys-color-on-surface", b.text_primary);
push(buf, "md-sys-color-on-surface-variant", b.text_secondary);
push(buf, "md-sys-color-outline", b.text_muted);
push(buf, "md-sys-color-outline-variant", b.border);
push(buf, "md-sys-color-inverse-surface", b.text_primary);
push(buf, "md-sys-color-inverse-on-surface", b.surface);
let (p_container_t, p_fixed_t, p_fixed_dim_t, p_on_fixed_variant_t) = match b.mode {
BridgeMode::Light => (-0.35_f32, -0.78, -0.55, 0.35),
BridgeMode::Dark => (0.30_f32, -0.55, -0.30, 0.55),
};
let primary_container = mix_or_same(b.link, p_container_t);
let primary_fixed = mix_or_same(b.link, p_fixed_t);
let primary_fixed_dim = mix_or_same(b.link, p_fixed_dim_t);
let on_primary_fixed_variant = mix_or_same(b.link, p_on_fixed_variant_t);
push(buf, "md-sys-color-primary", b.link);
push(buf, "md-sys-color-on-primary", wcag_foreground(b.link));
push(buf, "md-sys-color-primary-container", &primary_container);
push(
buf,
"md-sys-color-on-primary-container",
wcag_foreground(&primary_container),
);
push(buf, "md-sys-color-primary-fixed", &primary_fixed);
push(buf, "md-sys-color-primary-fixed-dim", &primary_fixed_dim);
push(buf, "md-sys-color-on-primary-fixed", b.link);
push(
buf,
"md-sys-color-on-primary-fixed-variant",
&on_primary_fixed_variant,
);
push(buf, "md-sys-color-surface-tint", b.link);
push(buf, "md-sys-color-inverse-primary", &primary_fixed_dim);
let secondary_container = mix_or_same(
b.text_muted,
match b.mode {
BridgeMode::Light => -0.55,
BridgeMode::Dark => 0.35,
},
);
push(buf, "md-sys-color-secondary", b.text_muted);
push(
buf,
"md-sys-color-on-secondary",
wcag_foreground(b.text_muted),
);
push(
buf,
"md-sys-color-secondary-container",
&secondary_container,
);
push(
buf,
"md-sys-color-on-secondary-container",
wcag_foreground(&secondary_container),
);
let tertiary_container = mix_or_same(
b.link,
match b.mode {
BridgeMode::Light => -0.25,
BridgeMode::Dark => 0.20,
},
);
let tertiary_fixed_t = match b.mode {
BridgeMode::Light => -0.65,
BridgeMode::Dark => -0.40,
};
let tertiary_fixed = mix_or_same(b.link, tertiary_fixed_t);
let tertiary_fixed_dim = mix_or_same(b.link, tertiary_fixed_t - 0.20);
push(buf, "md-sys-color-tertiary", b.link);
push(buf, "md-sys-color-on-tertiary", wcag_foreground(b.link));
push(buf, "md-sys-color-tertiary-container", &tertiary_container);
push(
buf,
"md-sys-color-on-tertiary-container",
wcag_foreground(&tertiary_container),
);
push(buf, "md-sys-color-tertiary-fixed", &tertiary_fixed);
push(buf, "md-sys-color-tertiary-fixed-dim", &tertiary_fixed_dim);
emit_semantic_pair(buf, indent, "success", b.success, b.mode);
emit_semantic_pair(buf, indent, "warning", b.warning, b.mode);
emit_semantic_pair(buf, indent, "error", b.error, b.mode);
}
fn emit_semantic_pair(buf: &mut String, indent: &str, name: &str, value: &str, mode: BridgeMode) {
if value.is_empty() {
return;
}
let container_t = match mode {
BridgeMode::Light => -0.55_f32,
BridgeMode::Dark => 0.40,
};
let on_container_t = match mode {
BridgeMode::Light => 0.40_f32,
BridgeMode::Dark => -0.55,
};
let base = match mode {
BridgeMode::Light => value.to_string(),
BridgeMode::Dark => mix_or_same(value, -0.60),
};
let container = mix_or_same(value, container_t);
let on_container = mix_or_same(value, on_container_t);
let write = |buf: &mut String, var: &str, val: &str| {
push_var_at(buf, indent, &format!("md-sys-color-{var}"), val);
};
write(buf, name, &base);
write(buf, &format!("on-{name}"), wcag_foreground(&base));
write(buf, &format!("{name}-container"), &container);
write(buf, &format!("on-{name}-container"), &on_container);
}
fn mix_or_same(hex: &str, t: f32) -> String {
mix_with_neutral(hex, t).unwrap_or_else(|| hex.to_string())
}
fn pick_with_fallback<'a>(primary: &'a str, fallback: &'a str) -> &'a str {
if primary.is_empty() {
fallback
} else {
primary
}
}
fn push_var(buf: &mut String, name: &str, value: &str) {
push_var_at(buf, " ", name, value);
}
fn push_var_indented(buf: &mut String, name: &str, value: &str) {
push_var_at(buf, " ", name, value);
}
fn push_var_at(buf: &mut String, indent: &str, name: &str, value: &str) {
if value.is_empty() || !is_css_ident(name) || !is_inert_css_value(value) {
return;
}
buf.push_str(indent);
buf.push_str("--");
buf.push_str(name);
buf.push_str(": ");
buf.push_str(value);
buf.push_str(";\n");
}
fn is_css_ident(name: &str) -> bool {
!name.is_empty()
&& name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}
fn is_inert_css_value(value: &str) -> bool {
!value.contains("/*")
&& !value
.chars()
.any(|c| matches!(c, ';' | '{' | '}' | '<' | '>' | '\\') || c.is_control())
}
fn html_escape_attr(s: &str) -> String {
s.replace('&', "&")
.replace('"', """)
.replace('<', "<")
.replace('>', ">")
}
fn emit_brand_color_block(buf: &mut String, slug: &str, value: &str) {
push_var(buf, &format!("color-{slug}"), value);
if let Some(rgb) = hex_to_rgb_triplet(value) {
push_var(buf, &format!("color-{slug}-rgb"), &rgb);
push_var(
buf,
&format!("color-{slug}-contrast"),
wcag_foreground(value),
);
for (suffix, t) in SHADE_LEVELS {
if let Some(shade) = mix_with_neutral(value, *t) {
push_var(buf, &format!("color-{slug}-{suffix}"), &shade);
}
}
}
}
fn emit_brand_color_block_indented(buf: &mut String, slug: &str, value: &str) {
push_var_indented(buf, &format!("color-{slug}"), value);
if let Some(rgb) = hex_to_rgb_triplet(value) {
push_var_indented(buf, &format!("color-{slug}-rgb"), &rgb);
push_var_indented(
buf,
&format!("color-{slug}-contrast"),
wcag_foreground(value),
);
for (suffix, t) in SHADE_LEVELS {
if let Some(shade) = mix_with_neutral(value, *t) {
push_var_indented(buf, &format!("color-{slug}-{suffix}"), &shade);
}
}
}
}
const SHADE_LEVELS: &[(&str, f32)] = &[
("50", -0.92),
("100", -0.82),
("200", -0.62),
("300", -0.40),
("400", -0.20),
("500", 0.0),
("600", 0.14),
("700", 0.30),
("800", 0.48),
("900", 0.66),
("950", 0.82),
];
pub(crate) fn parse_rgb6(hex: &str) -> Option<(u8, u8, u8)> {
let s = hex.trim_start_matches('#');
if s.len() != 6 || !s.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
let byte = |i: usize| u8::from_str_radix(&s[i..i + 2], 16).ok();
Some((byte(0)?, byte(2)?, byte(4)?))
}
fn hex_to_rgb_triplet(hex: &str) -> Option<String> {
let (r, g, b) = parse_rgb6(hex.trim())?;
Some(format!("{r} {g} {b}"))
}
fn mix_with_neutral(hex: &str, t: f32) -> Option<String> {
let (r, g, b) = parse_rgb6(hex.trim())?;
let (r, g, b) = (r as f32, g as f32, b as f32);
let (target, ratio) = if t < 0.0 { (255.0, -t) } else { (0.0, t) };
let mix = |c: f32| (c + (target - c) * ratio).clamp(0.0, 255.0) as u8;
Some(format!("#{:02x}{:02x}{:02x}", mix(r), mix(g), mix(b)))
}
fn wcag_foreground(hex: &str) -> &'static str {
let Some((r, g, b)) = parse_rgb6(hex.trim()) else {
return "#fff";
};
let lum = 0.2126 * (r as f32) + 0.7152 * (g as f32) + 0.0722 * (b as f32);
if lum > 140.0 {
"#0a0a0a"
} else {
"#ffffff"
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[test]
fn hostile_theme_values_never_leave_their_declaration() {
let mut theme = fixture_theme();
theme.slug = "x\"><script>alert(1)</script>".into();
theme.color_link = "red}</style><svg onload=alert(1)>".into();
theme.font_body = "a; } body { display: none".into();
theme.color_bg = "#fff /* swallow the rest".into();
theme.color_surface = "\"Inter\", 'Helvetica Neue', sans-serif".into();
let brand = BrandColor {
id: Auto::Unset,
theme_id: 1,
slug: "x:root{}".into(),
name: "Hostile".into(),
color_value: "#123456".into(),
color_value_dark: String::new(),
sort_order: 0,
};
let css = emit_css(&theme, &[brand]);
assert!(!css.contains("</style><") && !css.contains("<svg") && !css.contains("<script"));
assert!(!css.contains("display: none") && !css.contains("swallow"));
assert!(!css.contains("x:root"), "a hostile brand slug is not a property name");
assert!(css.contains("--color-surface: \"Inter\", 'Helvetica Neue', sans-serif;"));
assert_eq!(css.matches("<style").count(), 1);
assert_eq!(css.matches("</style>").count(), 1);
}
#[test]
fn six_byte_non_ascii_colours_are_rejected_not_panicked_on() {
let bad = "a\u{e9}\u{20ac}";
assert_eq!(parse_rgb6(bad), None);
assert_eq!(hex_to_rgb_triplet(bad), None);
assert_eq!(mix_with_neutral(bad, 0.5), None);
assert_eq!(wcag_foreground(bad), "#fff");
assert_eq!(parse_rgb6("#3B82f6"), Some((59, 130, 246)));
assert_eq!(parse_rgb6("+1+2+3"), None, "from_str_radix alone accepts a sign");
}
#[test]
fn hex_to_rgb_parses_six_digit() {
assert_eq!(hex_to_rgb_triplet("#3b82f6"), Some("59 130 246".to_owned()));
assert_eq!(hex_to_rgb_triplet("3b82f6"), Some("59 130 246".to_owned()));
}
#[test]
fn hex_to_rgb_rejects_non_hex() {
assert_eq!(hex_to_rgb_triplet("linear-gradient(...)"), None);
assert_eq!(hex_to_rgb_triplet("#fff"), None); assert_eq!(hex_to_rgb_triplet("rgb(0,0,0)"), None);
}
#[test]
fn mix_toward_white_brightens() {
let lighter = mix_with_neutral("#2E7D32", -0.5).unwrap();
let original_r = 0x2e;
let lighter_r =
u8::from_str_radix(lighter.trim_start_matches('#').get(0..2).unwrap(), 16).unwrap();
assert!(lighter_r > original_r);
}
#[test]
fn mix_toward_black_darkens() {
let darker = mix_with_neutral("#2E7D32", 0.5).unwrap();
let original_g = 0x7d;
let darker_g =
u8::from_str_radix(darker.trim_start_matches('#').get(2..4).unwrap(), 16).unwrap();
assert!(darker_g < original_g);
}
#[test]
fn foreground_picks_white_on_dark() {
assert_eq!(wcag_foreground("#1B5E20"), "#ffffff");
}
#[test]
fn foreground_picks_black_on_light() {
assert_eq!(wcag_foreground("#fafdf6"), "#0a0a0a");
}
pub(crate) fn fixture_theme() -> Theme {
Theme {
id: Auto::default(),
name: "Fixture".into(),
slug: "fixture".into(),
is_admin_default: false,
is_default: false,
default_mode: "light".into(),
color_bg: "#ffffff".into(),
color_surface: "#fafafa".into(),
color_text_primary: "#111111".into(),
color_text_secondary: "#444444".into(),
color_text_muted: "#777777".into(),
color_border: "#dddddd".into(),
color_bg_dark: "#0a0a0a".into(),
color_surface_dark: "#141414".into(),
color_text_primary_dark: "#f5f5f5".into(),
color_text_secondary_dark: "#bbbbbb".into(),
color_text_muted_dark: "#888888".into(),
color_border_dark: "#222222".into(),
color_success: "#22a06b".into(),
color_warning: "#d97706".into(),
color_error: "#ba1a1a".into(),
color_info: "#3b82f6".into(),
color_link: "#6366f1".into(),
color_focus_ring: "#6366f1".into(),
font_heading: "system-ui".into(),
font_body: "system-ui".into(),
font_url: String::new(),
radius_sm: "2px".into(),
radius_md: "4px".into(),
radius_lg: "8px".into(),
shadow_sm: "0 1px 2px #0001".into(),
shadow_md: "0 4px 12px #0001".into(),
shadow_lg: "0 12px 28px #0001".into(),
created_at: Auto::default(),
updated_at: Auto::default(),
}
}
#[test]
fn emit_css_bridges_md3_namespace() {
let css = emit_css(&fixture_theme(), &[]);
assert!(
css.contains("--color-bg: #ffffff"),
"legacy --color-bg missing"
);
assert!(css.contains("--color-link: #6366f1"));
assert!(css.contains("--md-sys-color-surface: #ffffff"));
assert!(css.contains("--md-sys-color-surface-container-lowest: #fafafa"));
assert!(css.contains("--md-sys-color-surface-container-low:"));
assert!(css.contains("--md-sys-color-surface-container:"));
assert!(css.contains("--md-sys-color-surface-container-high:"));
assert!(css.contains("--md-sys-color-surface-container-highest:"));
assert!(css.contains("--md-sys-color-on-surface: #111111"));
assert!(css.contains("--md-sys-color-on-surface-variant: #444444"));
assert!(css.contains("--md-sys-color-outline: #777777"));
assert!(css.contains("--md-sys-color-outline-variant: #dddddd"));
assert!(css.contains("--md-sys-color-primary: #6366f1"));
assert!(css.contains("--md-sys-color-primary-container:"));
assert!(css.contains("--md-sys-color-primary-fixed:"));
assert!(css.contains("--md-sys-color-on-primary:"));
assert!(css.contains("--md-sys-color-success: #22a06b"));
assert!(css.contains("--md-sys-color-on-success:"));
assert!(css.contains("--md-sys-color-success-container:"));
assert!(css.contains("--md-sys-color-warning: #d97706"));
assert!(css.contains("--md-sys-color-error: #ba1a1a"));
assert!(css.contains("--md-sys-color-error-container:"));
}
#[test]
fn emit_css_uses_root_qualified_selectors_for_dark_and_system() {
let css = emit_css(&fixture_theme(), &[]);
assert!(
css.contains(":root[data-theme=\"dark\"] {"),
"dark block must be :root-qualified to match cms.css specificity"
);
assert!(
css.contains(":root:not([data-theme=\"light\"]) {"),
"system block (inside @media) must be :root-qualified and match cms.css's OS-dark selector"
);
assert!(
!css.contains("\n[data-theme=\"dark\"] {"),
"stray bare [data-theme=\"dark\"] would lose cascade"
);
assert!(
!css.contains("[data-theme=\"system\"]"),
"`system` is the absence of data-theme, so no selector should name it"
);
}
#[test]
fn emit_css_dark_block_uses_dark_columns() {
let css = emit_css(&fixture_theme(), &[]);
let dark = css
.split(":root[data-theme=\"dark\"] {")
.nth(1)
.unwrap()
.split("}\n")
.next()
.unwrap();
assert!(dark.contains("--md-sys-color-surface: #0a0a0a"));
assert!(dark.contains("--md-sys-color-surface-container-lowest: #141414"));
assert!(dark.contains("--md-sys-color-on-surface: #f5f5f5"));
}
#[test]
fn emit_css_dark_block_lightens_semantic_bases() {
let css = emit_css(&fixture_theme(), &[]);
let dark = css
.split(":root[data-theme=\"dark\"] {")
.nth(1)
.unwrap()
.split("}\n")
.next()
.unwrap();
assert!(
!dark.contains("--md-sys-color-error: #ba1a1a"),
"dark block must not reuse the light error hue verbatim"
);
let err_line = dark
.lines()
.find(|l| l.contains("--md-sys-color-error:"))
.expect("dark block emits an error base");
let hex = err_line
.split(':')
.nth(1)
.unwrap()
.trim()
.trim_end_matches(';');
let r = u8::from_str_radix(&hex[1..3], 16).unwrap();
assert!(
r > 0xba,
"dark error base should be lighter than the light hue, got {hex}"
);
}
}