[core] Sanitiser: Colours::Dark for the dark reader (backgrounds out, colours lifted to 4.5 : 1, rule borders, theme links, colours_changed)

This commit is contained in:
2026-11-22 21:24:54 +01:00
parent f65c959bb4
commit 15c1d0e7c2
9 changed files with 585 additions and 22 deletions
+6 -6
View File
@@ -533,18 +533,16 @@ fn dark_rewrites_a_coloured_mail() {
#[test]
fn colours_are_lightened_in_their_hue_to_the_minimum_contrast() {
// The examples of the design: each lands within a few steps of the drawn colour.
// The examples of the design: exactly the drawn colours (HSL lightness raised in steps of 1 %).
for (from, drawn) in [("#c0392b", "#d7594c"), ("#2e7d32", "#36933b"), ("#1a43a6", "#517ce4")] {
let out = dark(&format!("<p style=\"color: {from}\">x</p>"));
assert!(out.colours_changed, "{from} was lightened");
let html = decoded(&out.html);
let value = first_colour(&html).unwrap_or_default();
let got = parse_colour(&value).unwrap_or_else(|| panic!("{from}: no colour in {html}"));
let want = parse_colour(drawn).unwrap();
assert!(contrast(got, PALETTE.body) >= MIN_CONTRAST, "{from} -> {got}");
assert!(contrast(got, PALETTE.body) < MIN_CONTRAST + 0.4, "{from} -> {got}: lightened more than needed");
let far = |a: u8, b: u8| a.abs_diff(b) > 8;
assert!(!(far(got.r, want.r) || far(got.g, want.g) || far(got.b, want.b)), "{from} -> {got}, drawn {drawn}");
assert_eq!(got.to_string(), drawn, "{from}");
}
// A colour that is already readable stays as it is, and is no change.
let light = dark("<p style=\"color: #8fd8b5\">x</p>");
@@ -571,7 +569,8 @@ fn black_grey_and_white_text_goes_and_is_no_change() {
"currentColor",
"hsl(0, 5%, 40%)",
] {
let out = dark(&format!("<p style=\"color: {colour}; font-size: 14px\">x</p><font color=\"{colour}\">y</font>"));
let out =
dark(&format!("<p style=\"color: {colour}; font-size: 14px\">x</p><font color=\"{colour}\">y</font>"));
let html = decoded(&out.html);
assert!(!html.contains("color"), "{colour}: {html}");
assert!(html.contains("font-size: 14px"), "{colour}: everything else stays ({html})");
@@ -605,7 +604,8 @@ fn a_highlight_on_words_becomes_the_tint_and_blocks_lose_theirs() {
}
for tag in ["div", "p", "td"] {
let html = decoded(
&dark(&format!("<table><tr><td><{tag} style=\"background-color: #ffeb3b\">x</{tag}></td></tr></table>")).html,
&dark(&format!("<table><tr><td><{tag} style=\"background-color: #ffeb3b\">x</{tag}></td></tr></table>"))
.html,
);
assert!(!html.contains("background"), "{tag}: {html}");
}