summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-cascade/layer-media-query.html
blob: 92a0f55a6a0132deaccbf72b9fcc9a4e7f40bfad (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
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
<!DOCTYPE html>
<html>
<head>
<title>CSS Cascade Layers: Media queries</title>
<meta name="assert" content="CSS Cascade Layers nested in Media Queries">
<link rel="author" title="Antti Koivisto" href="mailto:antti@apple.com">
<link rel="help" href="https://www.w3.org/TR/css-cascade-5/#layering">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<iframe width=300 height=300 frameborder=0></iframe>
<div id="log"></div>
<script>

const imports = {
    "basic-green.css": `
        target { color: green; }
    `,
    "basic-red.css": `
        target { color: red; }
    `,
    "empty.css": "",
};

// For 300px wide iframe the target should be red and for 500px green.
const testCases = [
    {
        title: 'A1 Basic',
        style: `
            @layer { target { color: red } }
            @media (min-width: 500px) {
                @layer {
                    target { color: green; }
                }
            }
        `
    },
    {
        title: 'A2 Basic',
        style: `
            @media (min-width: 500px) {
                @layer {
                    target { color: green; }
                }
            }
            @media (max-width: 300px) {
                @layer {
                    target { color: red; }
                }
            }
        `
    },
    {
        title: 'B1 Basic import',
        style: `
            @import url(basic-red.css) layer;
            @import url(basic-green.css) layer (min-width: 500px);
        `
    },
    {
        title: 'B2 Basic import',
        style: `
            @import url(basic-green.css) layer (min-width: 500px);
            @import url(basic-red.css) layer (max-width: 300px);
        `
    },
    {
        title: 'C1 Reordering',
        style: `
            @media (max-width: 300px) {
                @layer B {
                    target { color: green; }
                }
                @layer A {
                    target { color: red; }
                }
            }
            @media (min-width: 500px) {
                @layer A {
                    target { color: red; }
                }
                @layer B {
                    target { color: green; }
                }
            }
        `
    },
    {
        title: 'C2 Reordering',
        style: `
            @media (max-width: 300px) {
                @layer B { }
                @layer A { target { color: red; } }
            }
            @media (min-width: 500px) {
                @layer A { target { color: red; } }
                @layer B { }
            }
            @layer B {
                target { color: green; }
            }
        `
    },
    {
        title: 'C3 Reordering',
        style: `
            @media (max-width: 300px) {
                @layer B, A;
            }
            @media (min-width: 500px) {
                @layer A, B;
            }
            @layer A {
                target { color: red; }
            }
            @layer B {
                target { color: green; }
            }
        `
    },
    {
        title: 'C4 Reordering',
        style: `
            @import url(empty.css) layer(B) (max-width: 300px);
            @import url(empty.css) layer(A) (max-width: 300px);
            @import url(empty.css) layer(A) (min-width: 500px);
            @import url(empty.css) layer(B) (min-width: 500px);
            @layer A {
                target { color: red; }
            }
            @layer B {
                target { color: green; }
            }
        `
    },
];

let iframe = document.querySelector("iframe");

for (let testCase of testCases) {
    promise_test(async t => {
        const styleText = testCase['style'].replaceAll(/url\((.+?)\)/g, (match, p1) => {
            return `url(data:text/css,${ encodeURI(imports[p1]) })`;
        });

        iframe.width = 300;

        await new Promise(resolve => {
            iframe.onload = resolve;
            iframe.srcdoc = `
                <style>
                ${styleText}
                </style>
                <target></target>
            `;
        });

        const target = iframe.contentDocument.querySelector('target');
        assert_equals(getComputedStyle(target).color, 'rgb(255, 0, 0)', testCase['title']);

        iframe.width = 500;

        assert_equals(getComputedStyle(target).color, 'rgb(0, 128, 0)', testCase['title']);
    }, testCase['title']);
}
</script>
</body>
</html>