Repository navigation
Commit b8cd632
authored
feat(css): evaluate color(), color-mix() and relative colors (#21928)
* feat(css): evaluate color(), color-mix() and relative colors, and lower four more spellings
The color work rests on one table: every predefined color space CSS Color 4 §10
names, derived in the generator from the primaries, white point and transfer
function the spec states, as the pair of functions taking linear-light sRGB to
its components and back. `lib/css/syntax.js` implements no color space of its
own — it reads a color into a space, works one component at a time, and reads it
back — and a space added to the generator needs no arithmetic beside it.
On that:
- `color(<space> …)` folds to the sRGB it names, and gets the same fallback the
Lab family does where the target reads no `color()`.
- A color sRGB cannot show now takes a `color(display-p3 …)` rung between the
clipped hex and the author's own, wherever the target reads one — so an engine
reading no Lab family still gets the wider gamut. Skipped where P3 cannot show
it either, which is a color no fallback preserves.
- `color-mix()` is evaluated in any of the fifteen interpolation spaces, with
the four hue methods, percentages either side of a color, and the alphas
premultiplied as CSS Color 5 §3 states.
- A relative color (`rgb(from rebeccapurple r calc(g * 2) b)`) is resolved
against its origin: the channels are bound to the function's own keywords and
each argument read against them, an expression included.
And three more spellings the target decides:
- `system-ui` is written as the stack each platform's own engine reads.
- `text-decoration` naming more than its line becomes the line in the shorthand —
which is what resets the family — and a longhand per other slot.
- `optimization.minimize.css.pseudoClasses` writes a pseudo-class as an ordinary
class, at any depth, so a script can apply it where the engine reads none.
Every fold declines rather than guesses: a component that cannot be pinned down,
a channel on a `.5` boundary, a result outside the gamut it would be written in.
A mix in a space reached from sRGB by arithmetic alone is held to the tight
boundary margin rather than the Lab family's wide one.
* feat(css): write light-dark() as the pair the color scheme switches, and hold every lowering to a real engine
`light-dark()` names a color per scheme. For a target that does not read it, the
call becomes `var(--webpack-light, A) var(--webpack-dark, B)` — `initial` is the
guaranteed-invalid value, so the property set to it takes the fallback and the
one set to nothing substitutes nothing — and every rule that says which scheme
it is in says it again as that pair, with a `(prefers-color-scheme:dark)` copy
where it hands the choice to the user.
Two things it does that the ecosystem's version does not. A stylesheet that
names no `color-scheme` at all still reads the light half, from a
`:where(:root)` rule of no specificity — elsewhere that case leaves a
declaration the engine drops. And the call is left alone where a half names a
color the target cannot read: substituted, that is invalid at computed-value
time, which is `unset` rather than the declaration before it. The whole rewrite
needs `:where()`, so a target reading neither is one it leaves alone.
That last point is a defect this turn's new check found in what shipped before
it: a color fallback pair standing before a value a substitution reaches is
bytes nothing ever reads, and is no longer written.
The check is the other half of this commit. `syntaxEquivalence.spectest.js` now
holds every lowering to a real engine: one that reads both spellings has to
compute the same style from either, across color schemes and writing
directions, with colors compared as painted rather than as text — the engine
keeps a mix at the precision it computed it, while the printer writes the byte
it lands on. Beside it, a unit test pins the browser version each rewrite turns
on at, either side of the one the compat data gives.
* feat(css): write a selector list a pseudo does not take as the :is() it means
`:not(a, b)` matches what neither of them matches, which is what
`:not(:is(a, b))` says — and `:is()` carries the specificity of its most
specific argument, which is the specificity the list inside `:not()` has.
`:lang(a, b)` matches either, which is `:is(:lang(a), :lang(b))`. Both need
`:is()` itself, so a target reading neither keeps what the author wrote.
Each has its own window. Gecko took `:is()` six releases before it took a list
inside `:not()`, so that rewrite is written for Firefox 78 through 83 and no
other target. No Chromium has ever taken a list inside `:lang()`, so every
Chrome target is written the second way.
The `:lang()` one is held to a reference rather than to the engine's reading of
the source: Chromium reads no spelling of `:lang(en, fr)` at all, so the
equivalence check compares the rewrite against the pair of rules that state the
same thing.1 parent 2ee1eb5 commit b8cd632
38 files changed
Lines changed: 3825 additions & 265 deletions
File tree
- .changeset
- declarations
- lib
- css
- html
- schemas
- test
- configCases/css
- minimize-colors
- __snapshots__
- minimize-declined
- __snapshots__
- minimize-light-dark
- __snapshots__
- minimize-lightningcss-values/__snapshots__
- minimize-lower-unsupported
- __snapshots__
- minimize-pseudo-classes
- __snapshots__
- tooling
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
| |||
282 | 285 | | |
283 | 286 | | |
284 | 287 | | |
| 288 | + | |
285 | 289 | | |
286 | 290 | | |
287 | 291 | | |
| |||
334 | 338 | | |
335 | 339 | | |
336 | 340 | | |
| 341 | + | |
337 | 342 | | |
338 | 343 | | |
339 | 344 | | |
| |||
373 | 378 | | |
374 | 379 | | |
375 | 380 | | |
376 | | - | |
377 | 381 | | |
378 | 382 | | |
379 | 383 | | |
| 384 | + | |
380 | 385 | | |
381 | 386 | | |
382 | 387 | | |
383 | 388 | | |
384 | 389 | | |
| 390 | + | |
385 | 391 | | |
386 | 392 | | |
387 | 393 | | |
388 | | - | |
389 | 394 | | |
| 395 | + | |
390 | 396 | | |
391 | 397 | | |
392 | 398 | | |
| |||
399 | 405 | | |
400 | 406 | | |
401 | 407 | | |
| 408 | + | |
402 | 409 | | |
403 | 410 | | |
404 | 411 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2448 | 2448 | | |
2449 | 2449 | | |
2450 | 2450 | | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
2451 | 2461 | | |
2452 | 2462 | | |
2453 | 2463 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| 110 | + | |
109 | 111 | | |
110 | 112 | | |
111 | 113 | | |
| |||
0 commit comments