Implement CSS random-item()

#537461414 Area: Blink>CSS CL 8816057 In Review Flag: CSSRandomItemFunction

What it does

random-item() is a new css-values-5 arbitrary-substitution function: give it a random-key and a comma-separated list of items, and it substitutes one of the items.

The key can be auto, a <dashed-ident> with optional element-scoped, property-scoped or property-index-scoped, or a fixed <number> for deterministic picks. Sibling elements with an element-scoped key pick independently; the same dashed-ident across declarations shares a base value.

Implementation reuses random()'s base-value cache and RandomCacheKey. random-item() auto and property-*-scoped buckets stay disjoint from random()'s by prefixing the synthesized ua-ident with "ri-"; named-ident and bare element-scoped keys share the bucket per spec. Inside @container style() there is no property context so random-item() becomes IACVT, matching random()'s behavior.

Spec grammar

<random-item()> = random-item( <random-key>,
                              [ <declaration-value>? ]# )

<random-key> = <random-caching-key> | fixed <number>
<random-caching-key> =
    [ [ auto | <dashed-ident> ] || element-scoped ]
  | <dashed-ident> [ property-scoped | property-index-scoped ]

Showcase (palette, rotation, typography, fixed, shared bucket)

Live render. In Chrome stable random-item() is unknown and falls back to initial values -- run with --enable-blink-features=CSSRandomItemFunction or on a build that includes this CL to see the picks.
Before: random-item() unresolved, chips and cards missing
Without the flag: every random-item() declaration is IACVT and the sections collapse to initial.
After: palette, cards, type, fixed and shared sections populated
With the flag: palette picks colours per chip; cards get per-element rotation, shape and background; letters pick font-weight / colour / italic; fixed 0.5 always picks index 2 ("lime"); the two shared-key boxes always land on the same colour.
Diff mask: filled areas under every section
163466 px differ -- every section is purely driven by random-item().

WPT coverage

All eight external/wpt/css/css-values/random-item-*.html tests pass 100% of their subtests locally:

testsubtestspass
random-item-valid2121
random-item-invalid1414
random-item-serialize1515
random-item-computed3737
random-item-nested44
random-item-in-keyframe11
random-item-in-container-query.tentative44
random-item-unresolvable-argument33
total9999

wpt.fyi currently reports 0/0 for Chrome, Firefox and Safari on these tests (the suite is not yet enabled on their public runs), so landing this makes Blink the first stable run to report full coverage.

How to run locally

# Enable the flag
content_shell --enable-blink-features=CSSRandomItemFunction sampler.html

# or via the generic umbrella flag
content_shell --enable-experimental-web-platform-features sampler.html

Downloads

☍ Open sampler ⬇ before.png ⬇ after.png ⬇ diff.png