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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
export const testEl = document.createElement("div");
export const containerForInflow = document.createElement("div");
export const containerForAbspos = document.createElement("div");
export const containerForFixed = document.createElement("div");
testEl.id = "test";
containerForInflow.id = "container-for-inflow";
containerForAbspos.id = "container-for-abspos";
containerForFixed.id = "container-for-fixed";
containerForInflow.appendChild(testEl);
containerForAbspos.appendChild(containerForInflow);
containerForFixed.appendChild(containerForAbspos);
document.body.appendChild(containerForFixed);
const stylesheet = document.createElement("style");
stylesheet.textContent = `
#container-for-inflow {
/* Content area: 100px tall, 200px wide */
height: 100px;
width: 200px;
padding: 1px 2px;
border-width: 2px 4px;
margin: 4px 8px;
}
#container-for-abspos {
/* Padding area: 200px tall, 400px wide */
height: 184px;
width: 368px;
padding: 8px 16px;
border-width: 16px 32px;
margin: 32px 64px;
position: relative;
}
#container-for-fixed {
/* Padding area: 300px tall, 600px wide */
height: 172px;
width: 344px;
padding: 64px 128px;
border-width: 128px 256px;
margin: 256px 512px;
position: absolute;
transform: scale(1);
visibility: hidden;
}
[id ^= container] {
border-style: solid;
}
`;
document.head.prepend(stylesheet);
function runTestsWithWM(data, testWM, cbWM) {
const {
style,
containingBlockElement,
containingBlockArea,
preservesPercentages,
preservesAuto,
canStretchAutoSize,
staticPositionX,
staticPositionY,
} = data;
let cbHeight = containingBlockElement ? containingBlockElement.clientHeight : NaN;
let cbWidth = containingBlockElement ? containingBlockElement.clientWidth : NaN;
if (containingBlockElement && containingBlockArea == "content") {
const cs = getComputedStyle(containingBlockElement);
cbHeight -= parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
cbWidth -= parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
}
const staticPositionTop = cbWM.blockStart == "top" || cbWM.inlineStart == "top"
? staticPositionY : cbHeight - staticPositionY;
const staticPositionLeft = cbWM.blockStart == "left" || cbWM.inlineStart == "left"
? staticPositionX : cbWidth - staticPositionX;
const staticPositionBottom = cbWM.blockStart == "bottom" || cbWM.inlineStart == "bottom"
? staticPositionY : cbHeight - staticPositionY;
const staticPositionRight = cbWM.blockStart == "right" || cbWM.inlineStart == "right"
? staticPositionX : cbWidth - staticPositionX;
function serialize(declarations) {
return Object.entries(declarations).map(([p, v]) => `${p}: ${v}; `).join("");
}
function wmName(wm) {
return Object.values(wm.style).join(" ");
}
function checkStyle(declarations, expected, msg) {
test(function() {
testEl.style.cssText = style + "; " + serialize(Object.assign({}, declarations, testWM.style));
if (containingBlockElement) {
containingBlockElement.style.cssText = serialize(Object.assign({}, cbWM.style));
}
const cs = getComputedStyle(testEl);
for (let [prop, value] of Object.entries(expected)) {
assert_equals(cs[prop], value, `'${prop}'`);
}
}, `${wmName(testWM)} inside ${wmName(cbWM)} - ${msg}`);
testEl.style.cssText = "";
if (containingBlockElement) {
containingBlockElement.style.cssText = "";
}
}
checkStyle({
top: "1px",
left: "2px",
bottom: "3px",
right: "4px",
}, {
top: "1px",
left: "2px",
bottom: "3px",
right: "4px",
}, "Pixels resolve as-is");
checkStyle({
top: "1em",
left: "2em",
bottom: "3em",
right: "4em",
"font-size": "10px",
}, {
top: "10px",
left: "20px",
bottom: "30px",
right: "40px",
}, "Relative lengths are absolutized into pixels");
if (preservesPercentages) {
checkStyle({
top: "10%",
left: "25%",
bottom: "50%",
right: "75%",
}, {
top: "10%",
left: "25%",
bottom: "50%",
right: "75%",
}, "Percentages resolve as-is");
} else {
checkStyle({
top: "10%",
left: "25%",
bottom: "50%",
right: "75%",
}, {
top: cbHeight * 10 / 100 + "px",
left: cbWidth * 25 / 100 + "px",
bottom: cbHeight * 50 / 100 + "px",
right: cbWidth * 75 / 100 + "px",
}, "Percentages are absolutized into pixels");
checkStyle({
top: "calc(10% - 1px)",
left: "calc(25% - 2px)",
bottom: "calc(50% - 3px)",
right: "calc(75% - 4px)",
}, {
top: cbHeight * 10 / 100 - 1 + "px",
left: cbWidth * 25 / 100 - 2 + "px",
bottom: cbHeight * 50 / 100 - 3 + "px",
right: cbWidth * 75 / 100 - 4 + "px",
}, "calc() is absolutized into pixels");
}
if (canStretchAutoSize) {
// Force overconstraintment by setting size or with insets that would result in
// negative size. Then the resolved value should be the computed one according to
// https://drafts.csswg.org/cssom/#resolved-value-special-case-property-like-top
checkStyle({
top: "1px",
left: "2px",
bottom: "3px",
right: "4px",
height: "0px",
width: "0px",
}, {
top: "1px",
left: "2px",
bottom: "3px",
right: "4px",
}, "Pixels resolve as-is when overconstrained");
checkStyle({
top: "100%",
left: "100%",
bottom: "100%",
right: "100%",
}, {
top: cbHeight + "px",
left: cbWidth + "px",
bottom: cbHeight + "px",
right: cbWidth + "px",
}, "Percentages absolutize the computed value when overconstrained");
}
if (preservesAuto) {
checkStyle({
top: "auto",
left: "auto",
bottom: "3px",
right: "4px",
}, {
top: "auto",
left: "auto",
bottom: "3px",
right: "4px",
}, "If start side is 'auto' and end side is not, 'auto' resolves as-is");
checkStyle({
top: "1px",
left: "2px",
bottom: "auto",
right: "auto",
}, {
top: "1px",
left: "2px",
bottom: "auto",
right: "auto",
}, "If end side is 'auto' and start side is not, 'auto' resolves as-is");
checkStyle({
top: "auto",
left: "auto",
bottom: "auto",
right: "auto",
}, {
top: "auto",
left: "auto",
bottom: "auto",
right: "auto",
}, "If opposite sides are 'auto', they resolve as-is");
} else if (canStretchAutoSize) {
checkStyle({
top: "auto",
left: "auto",
bottom: "3px",
right: "4px",
}, {
top: cbHeight - 3 + "px",
left: cbWidth - 4 + "px",
bottom: "3px",
right: "4px",
}, "If start side is 'auto' and end side is not, 'auto' resolves to used value");
checkStyle({
top: "1px",
left: "2px",
bottom: "auto",
right: "auto",
}, {
top: "1px",
left: "2px",
bottom: cbHeight - 1 + "px",
right: cbWidth - 2 + "px",
}, "If end side is 'auto' and start side is not, 'auto' resolves to used value");
checkStyle({
top: "auto",
left: "auto",
bottom: "auto",
right: "auto",
}, {
top: staticPositionTop + "px",
left: staticPositionLeft + "px",
bottom: staticPositionBottom + "px",
right: staticPositionRight + "px",
}, "If opposite sides are 'auto', they resolve to used value");
} else {
checkStyle({
top: "auto",
left: "auto",
bottom: "3px",
right: "4px",
}, {
top: "-3px",
left: "-4px",
bottom: "3px",
right: "4px",
}, "If start side is 'auto' and end side is not, 'auto' resolves to used value");
checkStyle({
top: "1px",
left: "2px",
bottom: "auto",
right: "auto",
}, {
top: "1px",
left: "2px",
bottom: "-1px",
right: "-2px",
}, "If end side is 'auto' and start side is not, 'auto' resolves to used value");
checkStyle({
top: "auto",
left: "auto",
bottom: "auto",
right: "auto",
}, {
top: "0px",
left: "0px",
bottom: "0px",
right: "0px",
}, "If opposite sides are 'auto', they resolve to used value");
}
}
const writingModes = [{
style: {
"writing-mode": "horizontal-tb",
"direction": "ltr",
},
blockStart: "top",
blockEnd: "bottom",
inlineStart: "left",
inlineEnd: "right",
}, {
style: {
"writing-mode": "horizontal-tb",
"direction": "rtl",
},
blockStart: "top",
blockEnd: "bottom",
inlineStart: "right",
inlineEnd: "left",
}, {
style: {
"writing-mode": "vertical-lr",
"direction": "ltr",
},
blockStart: "left",
blockEnd: "right",
inlineStart: "top",
inlineEnd: "bottom",
}, {
style: {
"writing-mode": "vertical-lr",
"direction": "rtl",
},
blockStart: "left",
blockEnd: "right",
inlineStart: "bottom",
inlineEnd: "top",
}, {
style: {
"writing-mode": "vertical-rl",
"direction": "ltr",
},
blockStart: "right",
blockEnd: "left",
inlineStart: "top",
inlineEnd: "bottom",
}, {
style: {
"writing-mode": "vertical-rl",
"direction": "rtl",
},
blockStart: "right",
blockEnd: "left",
inlineStart: "bottom",
inlineEnd: "top",
}];
export function runTests(data) {
for (let testWM of writingModes) {
for (let cbWM of writingModes) {
runTestsWithWM(data, testWM, cbWM);
}
}
}
|