|
204 | 204 | assert_equals(m, Math.min(400, Math.floor(w / 100) * 100), 'random() and random-item() share the element-scoped base value'); |
205 | 205 | }, 'random() and random-item() share the base value for a bare element-scoped key'); |
206 | 206 |
|
| 207 | +// A derived property scope records which function produced it, so an auto random() and an auto |
| 208 | +// random-item() in one value draw independently. With random() scaled to 0-400px and two items |
| 209 | +// 0/1000px, a shared draw would make the item choice track whether random() landed above half; |
| 210 | +// over 40 samples that agreeing every time is about 1 in 10^12. |
| 211 | +function sharedDrawCount(declaration) { |
| 212 | + let consistentWithSharing = 0; |
| 213 | + const holder = document.createElement('div'); |
| 214 | + document.body.appendChild(holder); |
| 215 | + try { |
| 216 | + for (let i = 0; i < 40; ++i) { |
| 217 | + const element = document.createElement('div'); |
| 218 | + element.style.cssText = declaration; |
| 219 | + holder.appendChild(element); |
| 220 | + const width = parseFloat(getComputedStyle(element).width); |
| 221 | + const selectedSecondItem = width >= 1000; |
| 222 | + const randomFraction = (width - (selectedSecondItem ? 1000 : 0)) / 400; |
| 223 | + if ((randomFraction >= 0.5) === selectedSecondItem) |
| 224 | + ++consistentWithSharing; |
| 225 | + } |
| 226 | + } finally { |
| 227 | + document.body.removeChild(holder); |
| 228 | + } |
| 229 | + return consistentWithSharing; |
| 230 | +} |
| 231 | + |
| 232 | +test(() => { |
| 233 | + const shared = sharedDrawCount('width: calc(random(auto, 0px, 400px) + random-item(auto, 0px, 1000px))'); |
| 234 | + assert_less_than(shared, 40, 'random() and random-item() drew the same base value'); |
| 235 | +}, 'auto random() and auto random-item() in one value do not share a base value'); |
| 236 | + |
| 237 | +test(() => { |
| 238 | + const shared = sharedDrawCount('--n: random(auto, 0, 1); width: calc(var(--n) * 400px + random-item(auto, 0px, 1000px))'); |
| 239 | + assert_less_than(shared, 40, 'random() reached through var() drew the same base value as random-item()'); |
| 240 | +}, 'auto random() substituted through var() does not share a base value with random-item()'); |
| 241 | + |
207 | 242 | // A <dashed-ident> as short as "--" is a valid key (parity with random()); the value resolves rather |
208 | 243 | // than becoming guaranteed-invalid. |
209 | 244 | test_computed_value('color', 'random-item(--, rgb(1, 0, 0), rgb(0, 1, 0))', ['rgb(1, 0, 0)', 'rgb(0, 1, 0)']); |
|
0 commit comments