File: random-computed.tentative.html

package info (click to toggle)
thunderbird 1%3A140.3.1esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,608,628 kB
  • sloc: cpp: 7,671,698; javascript: 5,901,131; ansic: 3,898,955; python: 1,413,270; xml: 653,997; asm: 462,284; java: 180,948; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (474 lines) | stat: -rw-r--r-- 20,247 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-values-5/#random">
<link rel="author" title="sam@webkit.org">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../support/computed-testcommon.js"></script>
<div id="container">
  <div id="target"></div>
</div>
<style>
  .randomNoIdentifier {
    width: random(0px, 100px);
    height: random(0px, 100px);
    left: random(0px, 100000px);
    right: random(0px, 100000px);
    margin: random(0px, 100000px) random(0px, 100000px);
  }
  .randomMatchElement {
    width: random(element-shared, 0px, 100px);
    height: random(element-shared, 0px, 100px);
    left: random(element-shared, 0px, 100000px);
    right: random(element-shared, 0px, 100000px);
    margin: random(element-shared 0px, 100000px) random(element-shared 0px, 100000px);
  }
  .randomIdentifier {
    width: random(--identifier, 0px, 100px);
    height: random(--identifier, 0px, 100px);
    left: random(--identifier, 0px, 100000px);
    right: random(--identifier, 0px, 100000px);
    margin: random(--identifier 0px, 100000px) random(--identifier 0px, 100000px);
  }
  .randomMatchElementAndIdentifier {
    width: random(element-shared --other-identifier, 0px, 100px);
    height: random(element-shared --other-identifier, 0px, 100px);
    left: random(element-shared --other-identifier, 0px, 100000px);
    right: random(element-shared --other-identifier, 0px, 100000px);
    margin: random(element-shared --other-identifier 0px, 100000px) random(element-shared --other-identifier 0px, 100000px);
  }
  .randomFixed {
    width: random(fixed 0.5, 10px, 100px);
    height: random(fixed 0.5, 10px, 100px);
    left: random(fixed 0.5, 0px, 100000px);
    right: random(fixed 0.5, 0px, 100000px);
    margin: random(fixed 0.5 0px, 100000px) random(fixed 0.5 0px, 100000px);
  }
</style>
<script>

// Run each test a number of times to increase the likelyhood that failure is not the cause of random chance.
const iterations = 5;

function test_random_computed_value(property, specified, computed, titleExtra, options = {}) {
  if (!computed)
    computed = specified;

  test(() => {
    for (i = 0; i < iterations; ++i) {
      const target = document.getElementById('target');
      assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
      target.style[property] = '';
      target.style[property] = specified;

      let readValue = getComputedStyle(target)[property];
      if (options.comparisonFunction) {
        options.comparisonFunction(readValue, computed);
      } else if (Array.isArray(computed)) {
        assert_in_array(readValue, computed);
      } else {
        assert_equals(readValue, computed);
      }
      if (readValue !== specified) {
        target.style[property] = '';
        target.style[property] = readValue;
        assert_equals(getComputedStyle(target)[property], readValue,
                      'computed value should round-trip');
      }
    }
  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
}

function test_random_computed_value_greater_or_lower_than(property, specified, expected, titleExtra) {
  test(() => {
    for (i = 0; i < iterations; ++i) {
      const target = document.getElementById('target');
      assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
      target.style[property] = '';
      target.style[property] = specified;
      let readValue = parseFloat(getComputedStyle(target)[property]);
      assert_true(isFinite(readValue), specified + " expected finite value but got " + readValue)
      assert_false(isNaN(readValue),   specified + " expected finite value but got " + readValue)
      if (expected > 0)
        assert_greater_than_equal(readValue, expected, specified);
      else
        assert_less_than_equal(readValue, expected, specified);
    }
  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
}

function test_random_computed_value_in_range(property, specified, computedMin, computedMax, titleExtra) {
  test(() => {
    for (i = 0; i < iterations; ++i) {
      const target = document.getElementById('target');
      assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
      target.style[property] = '';
      target.style[property] = specified;

      let readValue = getComputedStyle(target)[property];

      let readValueNumber = parseFloat(readValue);
      let computedMinNumber = parseFloat(computedMin);
      let computedMaxNumber = parseFloat(computedMax);

      assert_greater_than_equal(readValueNumber, computedMinNumber, specified);
      assert_less_than_equal(readValueNumber, computedMaxNumber, specified);
    }
  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
}

function test_pseudo_element_random_computed_value_in_range(property, pseudo_element, specified, computedMin, computedMax, titleExtra) {
  test(() => {
    for (i = 0; i < iterations; ++i) {
      const styleEl = document.head.appendChild(document.createElement("style"));
      styleEl.innerHTML = `#target${pseudo_element} \{ ${property}: ${specified}; \}`;

      try {
        const target = document.getElementById("target");
        let readValue = getComputedStyle(target, pseudo_element)[property];

        let readValueNumber = parseFloat(readValue);
        let computedMinNumber = parseFloat(computedMin);
        let computedMaxNumber = parseFloat(computedMax);

        assert_greater_than_equal(readValueNumber, computedMinNumber, specified);
        assert_less_than_equal(readValueNumber, computedMaxNumber, specified);
      } finally {
        document.head.removeChild(styleEl);
      }
    }
  }, `Property ${property} value on pseudo element '${pseudo_element}' '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
}

function test_random_computed_value_has_fixed(property, specified, minPercentage, maxPercentage, titleExtra) {
  test(() => {
    for (i = 0; i < iterations; ++i) {
      const target = document.getElementById('target');
      assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
      assert_true(CSS.supports(property, specified), "'" + specified + "' is a supported value for " + property + ".");
      target.style[property] = '';
      target.style[property] = specified;

      let readValue = getComputedStyle(target)[property];

      // strip 'random(' and ')'.
      let stippedReadValue = readValue.replace('random(', '').replace(')', '');

      // split into the three main components
      let [fixedComponent, minComponent, maxComponent] = stippedReadValue.split(', ');

      // split fixed component into its two components
      let [fixedString, fixedValue] = fixedComponent.split(' ');

      assert_equals(fixedString, 'fixed', specified);
      assert_greater_than_equal(parseFloat(fixedValue), '0', specified);
      assert_less_than_equal(parseFloat(fixedValue), '1', specified);
      assert_equals(minComponent, minPercentage, specified);
      assert_equals(maxComponent, maxPercentage, specified);
    }
  }, `Property ${property} value '${specified}'${titleExtra ? ' ' + titleExtra : ''}`);
}

const property = 'scale';

test_random_computed_value_in_range(property, 'random(1, 11)', '1', '11');
test_random_computed_value_in_range(property, 'random(--foo, 2, 12)', '2', '12');
test_random_computed_value_in_range(property, 'random(--foo element-shared, 3, 13)', '3', '13');
test_random_computed_value_in_range(property, 'random(element-shared --foo, 4, 14)', '4', '14');

test_random_computed_value(property, 'random(0, 10, 5)', ['0', '5', '10']);
test_random_computed_value(property, 'random(--foo, 10, 20, 5)', ['10', '15', '20']);
test_random_computed_value(property, 'random(--foo element-shared, 20, 30, 5)', ['20', '25', '30']);
test_random_computed_value(property, 'random(element-shared --foo, 30, 40, 5)', ['30', '35', '40']);

// Test out of order.
test_random_computed_value(property, 'random(100, 10)', '100');
test_random_computed_value(property, 'random(-10, -100)', '-10');

// Test negative range values
test_random_computed_value_in_range(property, 'random(-100, -10)', '-100', '-10');

// Test negative step values (treated as if step is not there)
test_random_computed_value_in_range(property, 'random(40, 50, -5)', '40', '50');

// Test nested expressions
test_random_computed_value_in_range(property, 'random(5 * 1, 30 / 2)', '5', '15');

// Test nested in expressions
test_random_computed_value_in_range(property, 'calc(2 * random(6, 16))', '12', '32');

// Test NaN
test_random_computed_value(property, 'random(NaN, 100)', '0');
test_random_computed_value(property, 'random(10, NaN)', '0');
test_random_computed_value(property, 'random(NaN, NaN)', '0');
test_random_computed_value(property, 'random(NaN, 100, 10)', '0');
test_random_computed_value(property, 'random(10, NaN, 10)', '0');
test_random_computed_value(property, 'random(NaN, NaN, 10)', '0');
test_random_computed_value(property, 'random(NaN, 100, NaN)', '0');
test_random_computed_value(property, 'random(10, NaN, NaN)', '0');
test_random_computed_value(property, 'random(NaN, NaN, NaN)', '0');
test_random_computed_value(property, 'random(10, 100, NaN)', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, 100))', '0');
test_random_computed_value(property, 'calc(10 + random(10, NaN))', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, NaN))', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, 100, 10))', '0');
test_random_computed_value(property, 'calc(10 + random(10, NaN, 10))', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, NaN, 10))', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, 100, NaN))', '0');
test_random_computed_value(property, 'calc(10 + random(10, NaN, NaN))', '0');
test_random_computed_value(property, 'calc(10 + random(NaN, NaN, NaN))', '0');
test_random_computed_value(property, 'calc(10 + random(10, 100, NaN))', '0');

// Test infinity

const REALLY_LARGE = 1e6;
const REALLY_LARGE_NEGATIVE = -REALLY_LARGE;

test_random_computed_value_greater_or_lower_than(property, 'random(infinity, 100)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'random(infinity, infinity)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'random(infinity, 100, 10)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'random(infinity, infinity, 10)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'random(infinity, 100, infinity)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'random(infinity, infinity, infinity)', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, 100))', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, infinity))', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, infinity, 10))', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, 100, infinity))', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, infinity, infinity))', REALLY_LARGE);
test_random_computed_value_greater_or_lower_than(property, 'calc(10 + random(infinity, 100, 10))', REALLY_LARGE);
test_random_computed_value(property, 'random(10, infinity)', '0');
test_random_computed_value(property, 'random(10, infinity, 10)', '0');
test_random_computed_value(property, 'random(10, infinity, infinity)', '0');
test_random_computed_value(property, 'calc(10 + random(10, infinity))', '0');
test_random_computed_value(property, 'calc(10 + random(10, infinity, 10))', '0');
test_random_computed_value(property, 'calc(10 + random(10, infinity, infinity))', '0');
test_random_computed_value(property, 'random(10, 100, infinity)', '0');
test_random_computed_value(property, 'calc(10 + random(10, 100, infinity))', '0');
// Negative steps, even infinitely negative steps, are ignored.
test_random_computed_value_in_range(property, 'random(10, 100, -infinity)', '10', '100');
test_random_computed_value_in_range(property, 'calc(10 + random(10, 100, -infinity))', '20', '110');

// Test pseudo on psuedo elements
test_pseudo_element_random_computed_value_in_range(property, '::before', 'random(7, 17)', '7', '17');
test_pseudo_element_random_computed_value_in_range(property, '::before', 'random(--bar, 8, 18)', '8', '18');
test_pseudo_element_random_computed_value_in_range(property, '::before', 'random(element-shared, 9, 19)', '9', '19');
test_pseudo_element_random_computed_value_in_range(property, '::before', 'random(element-shared --foo, 10, 20)', '10', '20');

// Test unresolvable percentage values
test_random_computed_value_has_fixed('translate', 'random(10%, 100%)', '10%', '100%');

// Test random value sharing
test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    const el = document.createElement('div');
    el.className = 'randomNoIdentifier';
    holder.appendChild(el);
    const elComputedLeft = getComputedStyle(el)['left'];

    var allSame = true;
    var allHaveSameLeftAndRight = true;
    for (i = 0; i < iterations; ++i) {
      const other = document.createElement('div');
      other.className = 'randomNoIdentifier';
      holder.appendChild(other);
      const otherComputedLeft = getComputedStyle(other)['left'];
      if (elComputedLeft != otherComputedLeft) {
        allSame = false;
      }
      const otherComputedRight = getComputedStyle(other)['right'];
      if (elComputedLeft != otherComputedRight) {
        allHaveSameLeftAndRight = false;
      }
    }

    assert_false(allSame);
    assert_false(allHaveSameLeftAndRight);
  } finally {
    document.body.removeChild(holder);
  }
}, `Maximum random: 'random(a, b)'`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    var allHaveSameMarginTopAndMarginLeft = true;
    for (i = 0; i < iterations; ++i) {
      const other = document.createElement('div');
      other.className = 'randomNoIdentifier';
      holder.appendChild(other);
      const otherComputedMarginLeft = getComputedStyle(other)['margin-left'];
      const otherComputedMarginTop = getComputedStyle(other)['margin-top'];
      if (otherComputedMarginLeft != otherComputedMarginTop) {
        allHaveSameMarginTopAndMarginLeft = false;
      }
    }

    assert_false(allHaveSameMarginTopAndMarginLeft);
  } finally {
    document.body.removeChild(holder);
  }
}, `Maximum random - shorthand: random(a, b))`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    for (i = 0; i < iterations; ++i) {
      const el = document.createElement('div');
      el.className = 'randomIdentifier';
      holder.appendChild(el);

      let elComputedWidth = getComputedStyle(el)['width'];
      let elComputedHeight = getComputedStyle(el)['height'];

      assert_equals(elComputedWidth, elComputedHeight);
    }
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared by name within an element: 'random(--identifier, a, b)'`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    var allHaveSameMarginTopAndMarginLeft = true;
    for (i = 0; i < iterations; ++i) {
      const other = document.createElement('div');
      other.className = 'randomIdentifier';
      holder.appendChild(other);
      const otherComputedMarginLeft = getComputedStyle(other)['margin-left'];
      const otherComputedMarginTop = getComputedStyle(other)['margin-top'];
      if (otherComputedMarginLeft != otherComputedMarginTop) {
        allHaveSameMarginTopAndMarginLeft = false;
      }
    }

    assert_true(allHaveSameMarginTopAndMarginLeft);
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared by name within an element - shorthand: random(--identifier, a, b))`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    for (i = 0; i < iterations; ++i) {
      const t1 = document.createElement('div');
      t1.className = 'randomMatchElement';
      holder.appendChild(t1);
      const t2 = document.createElement('div');
      t2.className = 'randomMatchElement';
      holder.appendChild(t2);

      let t1ComputedWidth = getComputedStyle(t1)['width'];
      let t2ComputedWidth = getComputedStyle(t2)['width'];

      assert_equals(t1ComputedWidth, t2ComputedWidth);
    }
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared between elements within a property: random(element-shared, a, b)`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    var allHaveSameMarginTopAndMarginLeft = true;
    for (i = 0; i < iterations; ++i) {
      const other = document.createElement('div');
      other.className = 'randomMatchElement';
      holder.appendChild(other);
      const otherComputedMarginLeft = getComputedStyle(other)['margin-left'];
      const otherComputedMarginTop = getComputedStyle(other)['margin-top'];
      if (otherComputedMarginLeft != otherComputedMarginTop) {
        allHaveSameMarginTopAndMarginLeft = false;
      }
    }

    assert_true(allHaveSameMarginTopAndMarginLeft);
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared between elements within a property - shorthand: random(element-shared, a, b))`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    for (i = 0; i < iterations; ++i) {
      const t1 = document.createElement('div');
      t1.className = 'randomMatchElementAndIdentifier';
      holder.appendChild(t1);
      const t2 = document.createElement('div');
      t2.className = 'randomMatchElementAndIdentifier';
      holder.appendChild(t2);

      let t1ComputedWidth = getComputedStyle(t1)['width'];
      let t2ComputedHeight = getComputedStyle(t2)['height'];

      assert_equals(t1ComputedWidth, t2ComputedHeight);
    }
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared globally: random(--identifier element-shared, a, b)`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    var allHaveSameMarginTopAndMarginLeft = true;
    for (i = 0; i < iterations; ++i) {
      const other = document.createElement('div');
      other.className = 'randomMatchElementAndIdentifier';
      holder.appendChild(other);
      const otherComputedMarginLeft = getComputedStyle(other)['margin-left'];
      const otherComputedMarginTop = getComputedStyle(other)['margin-top'];
      if (otherComputedMarginLeft != otherComputedMarginTop) {
        allHaveSameMarginTopAndMarginLeft = false;
      }
    }

    assert_true(allHaveSameMarginTopAndMarginLeft);
  } finally {
    document.body.removeChild(holder);
  }
}, `Shared globally - shorthand: random(element-shared, a, b))`);

test(() => {
  const holder = document.createElement('div');
  document.body.appendChild(holder);

  try {
    for (i = 0; i < iterations; ++i) {
      const t1 = document.createElement('div');
      t1.className = 'randomFixed';
      holder.appendChild(t1);

      let t1ComputedWidth = getComputedStyle(t1)['width'];

      assert_equals(t1ComputedWidth, "55px");
    }
  } finally {
    document.body.removeChild(holder);
  }
}, `Fixed: random(fixed <number>, a, b)`);

</script>