summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-cascade/layer-important.html
blob: 23bfd167fd2ae7c3523fd99f30c4c0fb6231c04f (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
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html>
<head>
<title>CSS Cascade Layers: !important </title>
<meta name="assert" content="!important functionality of CSS Cascade Layers">
<link rel="author" title="Romain Menke" href="mailto:romainmenke@gmail.com">
<link rel="help" href="https://www.w3.org/TR/css-cascade-5/#cascade-layering">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<target class="first"></target>
<target class="second"></target>
<div id="log"></div>
<script>

// In all test cases, the rule specified as "color: green" should win.
var testCases = [
    {
        title: 'A1 Unlayered !important style',
        style: `
            target { color: green !important; }
            target { color: red; }
        `
    },
    {
        title: 'B1 Same specificity, layered !important first',
        style: `
            @layer { target { color: green !important; } }
            target { color: red; }
        `,
    },
    {
        title: 'C1 Same specificity, layered !important second',
        style: `
            target { color: red; }
            @layer { target { color: green !important; } }
        `,
    },
    {
        title: 'D1 Same specificity, all !important',
        style: `
            @layer { target { color: green !important; } }
            @layer { target { color: red !important; } }
            target { color: red !important; }
        `,
    },
    {
        title: 'D2 Same specificity, all !important',
        style: `
            @layer { target { color: green !important; } }
            target { color: red !important; }
            @layer { target { color: red !important; } }
        `,
    },
    {
        title: 'D3 Same specificity, all !important',
        style: `
            target { color: red !important; }
            @layer { target { color: green !important; } }
            @layer { target { color: red !important; } }
        `,
    },
    {
        title: 'D4 Same specificity, all !important',
        style: `
            @layer A, B;
            @layer B { target { color: red !important; } }
            @layer A { target { color: green !important; } }
            target { color: red !important; }
        `,
    },
    {
        title: 'E1 Different specificity, all !important',
        style: `
            @layer { target { color: green !important; } }
            @layer { target { color: red !important; } }
            target.first { color: red !important; }
        `,
    },
    {
        title: 'E2 Different specificity, all !important',
        style: `
            @layer { target { color: green !important; } }
            @layer { target.first { color: red !important; } }
            target { color: red !important; }
        `,
    },
];

for (let testCase of testCases) {
    const styleElement = document.createElement('style');
    styleElement.textContent = testCase['style'];
    document.head.append(styleElement);

    test(function () {
        var targets = document.querySelectorAll('target');
        for (target of targets)
            assert_equals(window.getComputedStyle(target).color, 'rgb(0, 128, 0)',
                      testCase['title'] + ", target '" + target.classList[0] + "'");
    }, testCase['title']);

    styleElement.remove();
}
</script>
</body>
</html>