Debian Codegraph @

plugins/postcss-light-dark-function/test/wpt/light-dark-basic.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <link rel="help" href="https://drafts.csswg.org/css-color-adjust/#color-scheme-effect">
7 <link rel="help" href="https://github.com/w3c/csswg-drafts/issues/7561">
8 <title>light-dark() color-scheme propagation</title>
9 </head>
10 <body>
11 <style>
12 .box {
13 width: 100px;
14 height: 100px;
15 margin: 8px;
16 }
17 </style>
18 <div class="box" id="system"></div>
19 <div class="box" id="light"></div>
20 <div class="box" id="dark"></div>
21
22 <div id="log"></div>
23 <script type="module">
24 window.addEventListener('click', () => {
25 window.runTest();
26 });
27
28 window.runTest = async function runTest() {
29 const system_is_dark = matchMedia("(prefers-color-scheme: dark)").matches;
30 const elements = ["system", "light", "dark"].map(document.getElementById.bind(document));
31
32 function assert_equals(actual, expected, message) {
33 if (actual != expected) {
34 log.innerHTML = message + ": expected " + expected + ", got " + actual;
35 throw new Error(message + ": expected " + expected + ", got " + actual);
36 }
37 }
38
39 var testCases = [
40 {
41 title: 'Light Dark Basic - A',
42 style: `
43 :root {
44 color-scheme: light dark;
45 }
46
47 #light {
48 color-scheme: light;
49 }
50
51 #dark {
52 color-scheme: dark;
53 }
54
55 .box {
56 background-color: light-dark(white, black);
57 }
58 `,
59 expected_light: 'rgb(255, 255, 255)',
60 expected_dark: 'rgb(0, 0, 0)',
61 },
62 {
63 title: 'Light Dark Basic - B',
64 style: `
65 :root {
66 color-scheme: light dark;
67 }
68
69 #light {
70 color-scheme: light;
71 }
72
73 #dark {
74 color-scheme: dark;
75 }
76
77 .box {
78 background-color: light-dark(light-dark(white, red), red);
79 }
80 `,
81 expected_light: 'rgb(255, 255, 255)',
82 expected_dark: 'rgb(255, 0, 0)',
83 },
84 {
85 title: 'Light Dark Basic - C',
86 style: `
87 #light {
88 color-scheme: light;
89 }
90
91 #dark {
92 color-scheme: dark;
93 }
94
95 .box {
96 background-color: light-dark(white, black);
97 }
98 `,
99 expected_light: 'rgb(255, 255, 255)',
100 expected_dark: 'rgb(0, 0, 0)',
101 root_supports_only_light: true,
102 },
103 {
104 title: 'Light Dark Cascade Layers - A',
105 style: `
106 :root {
107 color-scheme: light dark;
108 }
109
110 @layer {
111 #light {
112 color-scheme: light;
113 }
114
115 #dark {
116 color-scheme: dark;
117 }
118
119 .box {
120 background-color: light-dark(white, black);
121 }
122 }
123 `,
124 expected_light: 'rgb(255, 255, 255)',
125 expected_dark: 'rgb(0, 0, 0)',
126 },
127 {
128 title: 'Light Dark Cascade Layers - B',
129 style: `
130 :root {
131 color-scheme: light dark;
132 }
133
134 @layer {
135 #light {
136 color-scheme: light;
137 }
138
139 #dark {
140 color-scheme: dark;
141 }
142
143 .box {
144 background-color: light-dark(light-dark(white, red), red);
145 }
146 }
147 `,
148 expected_light: 'rgb(255, 255, 255)',
149 expected_dark: 'rgb(255, 0, 0)',
150 },
151 ];
152
153 for (let testCase of testCases) {
154 for (let params of ['', '?native=true']) {
155 const styleElement = document.createElement('style');
156
157 const testCaseStyle = await (fetch(`/test/styles.css${params}`, {
158 method: 'POST',
159 body: testCase['style'],
160 }).then((response) => response.text()));
161
162 styleElement.textContent = testCaseStyle;
163 document.head.append(styleElement);
164
165 for (let element of elements) {
166 let should_be_dark = element.id == "dark" || (element.id == "system" && system_is_dark && !testCase.root_supports_only_light);
167 let actual_value = getComputedStyle(element).backgroundColor;
168 let expected_value = should_be_dark ? testCase.expected_dark : testCase.expected_light;
169
170 assert_equals(
171 actual_value,
172 expected_value,
173 `${testCase.title} (${element.id}, ${system_is_dark ? 'is dark' : 'is not dark'})`
174 );
175 }
176
177 styleElement.remove();
178 }
179 }
180
181 return true;
182 }
183 </script>
184 </body>
185 </html>