summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/support/inheritance-testcommon.js
blob: 38ac94eb12e9820732958455238b50699ad373ee (plain)
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
'use strict';

(function() {

function assert_initial(property, initial) {
  let initialDesc = initial;
  if (Array.isArray(initial))
    initialDesc = '[' + initial.map(e => "'" + e + "'").join(' or ') + ']';

  test(() => {
    const target = document.getElementById('target');
    assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
    target.style[property] = 'initial';
    if (Array.isArray(initial)) {
      assert_in_array(getComputedStyle(target)[property], initial);
    } else {
      assert_equals(getComputedStyle(target)[property], initial);
    }
    target.style[property] = '';
  }, 'Property ' + property + ' has initial value ' + initialDesc);
}

/**
 * Create tests that a CSS property inherits and has the given initial value.
 *
 * The current document must have an element #target within element #container.
 *
 * @param {string}        property  The name of the CSS property being tested.
 * @param {string|array}  initial   The computed value for 'initial' or a list
 *                                  of acceptable computed value serializations.
 * @param {string}        other     An arbitrary value for the property that
 *                                  round trips and is distinct from the initial
 *                                  value.
 */
function assert_inherited(property, initial, other) {
  if (initial)
    assert_initial(property, initial);

  test(() => {
    const container = document.getElementById('container');
    const target = document.getElementById('target');
    assert_true(property in getComputedStyle(target), property + " doesn't seem to be supported in the computed style");
    container.style[property] = 'initial';
    target.style[property] = 'unset';
    assert_not_equals(getComputedStyle(container)[property], other);
    assert_not_equals(getComputedStyle(target)[property], other);
    container.style[property] = other;
    assert_equals(getComputedStyle(container)[property], other);
    assert_equals(getComputedStyle(target)[property], other);
    target.style[property] = 'initial';
    assert_equals(getComputedStyle(container)[property], other);
    assert_not_equals(getComputedStyle(target)[property], other);
    target.style[property] = 'inherit';
    assert_equals(getComputedStyle(target)[property], other);
    container.style[property] = '';
    target.style[property] = '';
  }, 'Property ' + property + ' inherits');
}

/**
 * Create tests that a CSS property does not inherit, and that it has the
 * given initial value.
 *
 * The current document must have an element #target within element #container.
 *
 * @param {string}        property  The name of the CSS property being tested.
 * @param {string|array}  initial   The computed value for 'initial' or a list
 *                                  of acceptable computed value serializations.
 * @param {string}        other     An arbitrary value for the property that
 *                                  round trips and is distinct from the initial
 *                                  value.
 */
function assert_not_inherited(property, initial, other) {
  assert_initial(property, initial);

  test(() => {
    const container = document.getElementById('container');
    const target = document.getElementById('target');
    assert_true(property in getComputedStyle(target));
    container.style[property] = 'initial';
    target.style[property] = 'unset';
    assert_not_equals(getComputedStyle(container)[property], other);
    assert_not_equals(getComputedStyle(target)[property], other);
    container.style[property] = other;
    assert_equals(getComputedStyle(container)[property], other);
    assert_not_equals(getComputedStyle(target)[property], other);
    target.style[property] = 'inherit';
    assert_equals(getComputedStyle(target)[property], other);
    container.style[property] = '';
    target.style[property] = '';
  }, 'Property ' + property + ' does not inherit');
}

window.assert_inherited = assert_inherited;
window.assert_not_inherited = assert_not_inherited;
})();