Re: [csswg-drafts] [css-values-5] Caching CSS random() values in if() (#13662)

Hm. So, here's my initial thoughts.

The *value* clauses of the if() are unresolved, like fallbacks. Those shouldn't resolve their values until they're parsed for real as part of the property, after substitution. (And I should specify this is the case for all arb-sub functions.) So both of those (`random(3)` and `random(4)`) should end up with a random-ua-ident of `ua-color-1`.
 
The *test* clauses have to resolve at computed-value time, before substitution occurs (*during* substitution, in fact). My kneejerk reaction was to just ban them, but no, `style(random(0, 1) < .5))` is a meaningful test, and you can do that *manually* by putting the `random()` in a typed custom property and then using `style(var(--my-rand) < .5)`, so banning doesn't help.

So, we just need to define a random-ua-ident for it that gives useful behavior. It should include the name of the property the `if()` is in, so values aren't accidentally linked across properties, but it should be distinct from the values that actually show up in the property, again to prevent accidental linkage. So I think going with a scheme similar to what you proposed for custom functions should work - generate a random-ua-ident of "ua-if()-color-1".

Only remaining question is how we should number the values. Does each test-clause restart the numbering, so they're linked across clauses and potentially across `if()` functions? I think the answer should be no, so we should keep a single incrementing counter across all instances in all test clauses. What about between separate `if()`s in the same property? I think these should be distinct as well, so we should chain the incrementing counter across all if `if()`s.

Maybe this can just be a generic arb-sub functionality, then: if a `random()` is evaluated within an arb-sub function as part of substitution (rather than as the substitution value itself), its random-ua-ident is something like "ua-early-PROPERTY-INDEX", where PROPERTY is the name of the property it's used in, and INDEX is the nth usage of random() across all arb-subs in the property, in parse order. So every usage in every arb-sub function is distinct within a property.

-- 
GitHub Notification of comment by tabatkins
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/13662#issuecomment-4255519169 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Wednesday, 15 April 2026 21:13:15 UTC