[core] Sanitiser: the dark reader's colours (v0.2.0) #2

Open
mnovak wants to merge 0 commits from feature/dark-reader into main
Owner

The dark reader's colours, for the app's "mail body in the dark" (Epistula docs/06_screens.md, "Making a mail's HTML dark"). Release: tag v0.2.0.

What changes

  • SanitizeOptions.colours: Colours — Original (the default) or Dark(DarkPalette). DarkPalette::DOMI is the Domi theme: body #111726, rule #6b7a99, highlight the --second tint rgba(244, 201, 93, 0.13).
  • In Dark:
    • background, background-color and bgcolor are removed everywhere. A coloured background on span, font, mark, b, i, u, strong or em becomes the tint. A white or grey one there just goes.
    • Black, grey and white text is removed. A colour counts as grey when its HSL saturation is under 10 % or its chroma is under 10 %, so #1b2130 and #fff8f0 go too.
    • Other colours keep their hue and saturation. Their HSL lightness goes up in 1 % steps until they reach 4.5 : 1 on the body. The design's three examples come out exactly: #c0392b→#d7594c, #2e7d32→#36933b, #1a43a6→#517ce4.
    • A colour the parser does not know is removed. This applies to color and <font color>.
    • Border colours, and the colour words in border shorthands, become the rule colour.
    • Links lose their own color, background and text-decoration, so the reader's colour and underline win.
    • !important is cut (everything after a !) before the forbidden-CSS check. Note: in Original a declaration with !important is still dropped whole, because "important" contains "import". That is today's behaviour, kept on purpose.
    • The blocked-image placeholder is BLOCKED_IMAGE_SRC_DARK (#6b7a99).
    • Images are never touched.
  • Sanitized.colours_changed: true when a background that named something was removed, or a colour was lightened. Removing grey text is no change. It is always false in Original.
  • sanitize::colour (private module, its functions re-exported): parsing #rgb[a], #rrggbb[aa], rgb[a](), hsl[a]() and the CSS named colours, plus WCAG luminance and contrast, is_grey and readable_on.
  • Breaking: struct literals of SanitizeOptions need ..Default::default(), and literals of Sanitized need colours_changed. That is why it is 0.2.0.

Tests (first, red at f65c959)

cargo test: lib 3 · compose 10 · mime 16 · sanitize 25 (was 9) · thread_rules_search 20 · doc 0. All pass. Clippy: only the warning that was already there (filename.rs:36).

New in tests/sanitize.rs:

  • Original is byte for byte what v0.1.0 wrote, against a golden recorded before the change.
  • A property over random coloured fragments (2000 cases), plus the hostile corpus run in Dark: no background but the tint on a word, every kept colour ≥ 4.5 : 1 and not grey, borders in the rule colour, links without their own colours, no !, no bgcolor, and nothing dangerous.
  • A property over every RGB colour: either grey and removed, or readable. A changed colour sets the flag. No channel gets darker, and the order of the channels (the hue) is kept.
  • Separate tests for greys (no change flagged), unreadable values, !important, highlights, bgcolor, borders, links as buttons, the palette and placeholder, and the old link colour failing on the dark.

Self-review

  • Safety: the dark rewrite runs after the same allow-list and forbidden-value checks. It only removes declarations or writes ones it builds itself: hex colours, the tint, the rule. The hostile corpus and the fuzz property run in Dark too.
  • Hostile colours: parse_colour never slices inside a character. Hex digits are checked first, and non-finite numbers (1e999, nan, inf) give None. The unit tests cover these cases.
  • Lightening always ends, at white at the latest. body must be dark, as documented on DarkPalette.
  • Not here: the reader's own style sheet in the frame, and the bar. Both are in the app.
The dark reader's colours, for the app's "mail body in the dark" (Epistula `docs/06_screens.md`, "Making a mail's HTML dark"). Release: tag `v0.2.0`. ## What changes - `SanitizeOptions.colours: Colours` — `Original` (the default) or `Dark(DarkPalette)`. `DarkPalette::DOMI` is the Domi theme: body `#111726`, rule `#6b7a99`, highlight the `--second` tint `rgba(244, 201, 93, 0.13)`. - In `Dark`: - `background`, `background-color` and `bgcolor` are removed everywhere. A coloured background on `span`, `font`, `mark`, `b`, `i`, `u`, `strong` or `em` becomes the tint. A white or grey one there just goes. - Black, grey and white text is removed. A colour counts as grey when its HSL saturation is under 10 % or its chroma is under 10 %, so `#1b2130` and `#fff8f0` go too. - Other colours keep their hue and saturation. Their HSL lightness goes up in 1 % steps until they reach 4.5 : 1 on the body. The design's three examples come out exactly: `#c0392b`→`#d7594c`, `#2e7d32`→`#36933b`, `#1a43a6`→`#517ce4`. - A colour the parser does not know is removed. This applies to `color` and `<font color>`. - Border colours, and the colour words in border shorthands, become the rule colour. - Links lose their own `color`, background and `text-decoration`, so the reader's colour and underline win. - `!important` is cut (everything after a `!`) before the forbidden-CSS check. Note: in `Original` a declaration with `!important` is still dropped whole, because "important" contains "import". That is today's behaviour, kept on purpose. - The blocked-image placeholder is `BLOCKED_IMAGE_SRC_DARK` (`#6b7a99`). - Images are never touched. - `Sanitized.colours_changed`: true when a background that named something was removed, or a colour was lightened. Removing grey text is no change. It is always false in `Original`. - `sanitize::colour` (private module, its functions re-exported): parsing `#rgb[a]`, `#rrggbb[aa]`, `rgb[a]()`, `hsl[a]()` and the CSS named colours, plus WCAG luminance and contrast, `is_grey` and `readable_on`. - Breaking: struct literals of `SanitizeOptions` need `..Default::default()`, and literals of `Sanitized` need `colours_changed`. That is why it is 0.2.0. ## Tests (first, red at f65c959) `cargo test`: lib 3 · compose 10 · mime 16 · **sanitize 25** (was 9) · thread_rules_search 20 · doc 0. All pass. Clippy: only the warning that was already there (`filename.rs:36`). New in `tests/sanitize.rs`: - `Original` is byte for byte what v0.1.0 wrote, against a golden recorded before the change. - A property over random coloured fragments (2000 cases), plus the hostile corpus run in `Dark`: no background but the tint on a word, every kept colour ≥ 4.5 : 1 and not grey, borders in the rule colour, links without their own colours, no `!`, no `bgcolor`, and nothing dangerous. - A property over every RGB colour: either grey and removed, or readable. A changed colour sets the flag. No channel gets darker, and the order of the channels (the hue) is kept. - Separate tests for greys (no change flagged), unreadable values, `!important`, highlights, `bgcolor`, borders, links as buttons, the palette and placeholder, and the old link colour failing on the dark. ## Self-review - Safety: the dark rewrite runs after the same allow-list and forbidden-value checks. It only removes declarations or writes ones it builds itself: hex colours, the tint, the rule. The hostile corpus and the fuzz property run in `Dark` too. - Hostile colours: `parse_colour` never slices inside a character. Hex digits are checked first, and non-finite numbers (`1e999`, `nan`, `inf`) give `None`. The unit tests cover these cases. - Lightening always ends, at white at the latest. `body` must be dark, as documented on `DarkPalette`. - Not here: the reader's own style sheet in the frame, and the bar. Both are in the app.
mnovak added 2 commits 2026-11-22 21:25:25 +01:00
This branch is already included in the target branch. There is nothing to merge.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feature/dark-reader:feature/dark-reader
git checkout feature/dark-reader
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mnovak/EpistulaPublic#2