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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test that the rule-view content is correct when the page uses nested CSS rules
const TEST_URI = `
<style type="text/css">
body {
background: tomato;
container-type: inline-size;
@media screen {
container-name: main;
& h1 {
border-color: gold;
.foo {
color: white;
}
#bar {
text-decoration: underline;
}
@container main (width > 10px) {
& + nav {
border: 1px solid;
[href] {
background-color: lightgreen;
}
}
}
}
}
}
</style>
<h1>Hello <i class="foo">nested</i> <em id="bar">rules</em>!</h1>
<nav>
<ul>
<li><a href="#">Leaf</a></li>
<li><a>Nowhere</a></li>
</ul>
</nav>
`;
add_task(async function () {
await addTab(
"https://example.com/document-builder.sjs?html=" +
encodeURIComponent(TEST_URI)
);
const { inspector, view } = await openRuleView();
await selectNode("body", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `&`,
// prettier-ignore
ancestorRulesData: [
`body {`,
` @media screen {`
],
declarations: [{ name: "container-name", value: "main" }],
},
{
selector: `body`,
ancestorRulesData: null,
declarations: [
{ name: "background", value: "tomato" },
{ name: "container-type", value: "inline-size" },
],
},
]);
await selectNode("h1", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `& h1`,
// prettier-ignore
ancestorRulesData: [
`body {`,
` @media screen {`
],
declarations: [{ name: "border-color", value: "gold" }],
},
]);
await selectNode("h1 > .foo", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `& .foo`,
// prettier-ignore
ancestorRulesData: [
`body {`,
` @media screen {`,
` & h1 {`
],
declarations: [{ name: "color", value: "white" }],
},
]);
await selectNode("h1 > #bar", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `& #bar`,
// prettier-ignore
ancestorRulesData: [
`body {`,
` @media screen {`,
` & h1 {`
],
declarations: [{ name: "text-decoration", value: "underline" }],
},
]);
await selectNode("nav", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `& + nav`,
ancestorRulesData: [
`body {`,
` @media screen {`,
` & h1 {`,
` @container main (width > 10px) {`,
],
declarations: [{ name: "border", value: "1px solid" }],
},
]);
await selectNode("nav a", inspector);
checkRuleViewContent(view, [
{ selector: "element", ancestorRulesData: null, declarations: [] },
{
selector: `& [href]`,
ancestorRulesData: [
`body {`,
` @media screen {`,
` & h1 {`,
` @container main (width > 10px) {`,
` & + nav {`,
],
declarations: [{ name: "background-color", value: "lightgreen" }],
},
]);
});
function checkRuleViewContent(view, expectedRules) {
const rulesInView = Array.from(view.element.children);
is(
rulesInView.length,
expectedRules.length,
"All expected rules are displayed"
);
for (let i = 0; i < expectedRules.length; i++) {
const expectedRule = expectedRules[i];
info(`Checking rule #${i}: ${expectedRule.selector}`);
const ruleInView = rulesInView[i];
const selector = ruleInView.querySelector(
".ruleview-selectors-container"
).innerText;
is(selector, expectedRule.selector, `Expected selector for ${selector}`);
if (expectedRule.ancestorRulesData == null) {
is(
getRuleViewAncestorRulesDataElementByIndex(view, i),
null,
`No ancestor rules data displayed for ${selector}`
);
} else {
is(
getRuleViewAncestorRulesDataTextByIndex(view, i),
expectedRule.ancestorRulesData.join("\n"),
`Expected ancestor rules data displayed for ${selector}`
);
}
const declarations = ruleInView.querySelectorAll(".ruleview-property");
is(
declarations.length,
expectedRule.declarations.length,
"Got the expected number of declarations"
);
for (let j = 0; j < declarations.length; j++) {
const expectedDeclaration = expectedRule.declarations[j];
const [propName, propValue] = Array.from(
declarations[j].querySelectorAll(
".ruleview-propertyname, .ruleview-propertyvalue"
)
);
is(
propName.innerText,
expectedDeclaration?.name,
"Got expected property name"
);
is(
propValue.innerText,
expectedDeclaration?.value,
"Got expected property value"
);
}
}
}
|