-
Notifications
You must be signed in to change notification settings - Fork 88
/
layout.js
584 lines (479 loc) · 18.6 KB
/
layout.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import { html } from "../../src/template/index.js";
import { resolveTimeout } from "../helpers.js";
describe("layout:", () => {
let host;
beforeEach(() => {
host = document.createElement("div");
document.body.appendChild(host);
});
afterEach(() => {
document.body.removeChild(host);
});
it("throws for initializing with more than one root element", () => {
expect(() =>
html`
<template layout="row"></template>
<div></div>
`(host),
).toThrow();
});
it("throws for unsupported rule", () => {
expect(() => html`<template layout="test"></template>`(host)).toThrow();
});
it("throws for expressions inside the root element layout attribute", () => {
expect(() => html`<template layout="${"row"}"></template>`(host)).toThrow();
});
it("throws for expressions inside the nested element layout attribute", () => {
expect(() =>
html`<template layout=""><div layout="${"row"}"></div></template>`(host),
).toThrow();
});
it("for target element turns on for root <template> element", () => {
html`
<template layout="row" class="test">
${html`<div layout="grow">test</div>`}
</template>
`(host);
const hostStyles = window.getComputedStyle(host);
expect(hostStyles.display).toBe("flex");
expect(hostStyles.flexDirection).toBe("row");
const elStyles = window.getComputedStyle(host.children[0]);
expect(elStyles.flexGrow).toBe("1");
expect(host.children[0].hasAttribute("layout")).toBe(false);
});
it("for shadowRoot turns on for root <template> element", () => {
const shadowRoot = host.attachShadow({ mode: "open" });
html`
<template layout="row">
<div></div>
</template>
`.css`
:host { position: relative }
`(host, shadowRoot);
expect(host.className).not.toBeFalsy();
const hostStyles = window.getComputedStyle(host);
expect(hostStyles.position).toBe("relative");
const nestedHost = shadowRoot.children[0];
html`<template layout="row"><div></div></template>`(nestedHost);
expect(nestedHost.className).not.toBeFalsy();
});
it("does not run without the template root element with 'layout*' attribute", () => {
html`<div layout="row">test</div>`(host);
const elStyles1 = window.getComputedStyle(host.children[0]);
expect(elStyles1.display).toBe("block");
expect(host.children[0].hasAttribute("layout")).toBe(true);
html`<template><div layout="row">test</div></template>`(host);
const elStyles2 = window.getComputedStyle(host.children[0]);
expect(elStyles2.display).toBe("block");
expect(host.children[0].hasAttribute("layout")).toBe(true);
});
it("uses layout engine for nested templates", () => {
html`<template layout="row">${html`<div layout="row"></div>`}</template>`(
host,
);
const nestedHost = host.children[0];
const elStyles = window.getComputedStyle(nestedHost);
expect(elStyles.display).toBe("flex");
expect(elStyles.flexDirection).toBe("row");
expect(nestedHost.hasAttribute("layout")).toBe(false);
});
it("uses layout engine for nested templates", () => {
html`
<template layout="row">${html`<div layout="column"></div>`}</template>
`(host, host.attachShadow({ mode: "open" }));
expect(host.shadowRoot.children[0].className).not.toBeFalsy();
});
it("uses layout engine for nested array templates", () => {
html`<template layout="row">${[html`<div layout="row"></div>`]}</template>`(
host,
);
const nestedHost = host.children[0];
const elStyles = window.getComputedStyle(nestedHost);
expect(elStyles.display).toBe("flex");
expect(elStyles.flexDirection).toBe("row");
expect(nestedHost.hasAttribute("layout")).toBe(false);
});
it("uses layout engine for html.resolve method", () => {
html`<template layout="row"
>${html.resolve(
resolveTimeout(() => html`<div layout="row"></div>`, 200),
html`<div layout="column"></div>`,
0,
)}</template
>`(host);
return resolveTimeout(() => {
const el = host.children[0];
const elStyles = window.getComputedStyle(el);
expect(elStyles.display).toBe("flex");
expect(elStyles.flexDirection).toBe("column");
expect(el.hasAttribute("layout")).toBe(false);
return resolveTimeout(() => {
const el = host.children[0];
const elStyles = window.getComputedStyle(el);
expect(elStyles.display).toBe("flex");
expect(elStyles.flexDirection).toBe("row");
expect(el.hasAttribute("layout")).toBe(false);
}, 200);
}, 0);
});
it("replaces class for the host element", () => {
html`<template layout="row"></template>`(host);
const className = host.className;
html`<template layout="column"></template>`(host);
expect(host.className).not.toBe(className);
expect(window.getComputedStyle(host).flexDirection).toBe("column");
});
it("set main element styles with zero specificity for content", () => {
html`<template layout="row">
<style>
.layout-test-column {
flex-flow: column;
}
</style>
</template>`(host);
host.className = `${host.className} layout-test-column`;
expect(window.getComputedStyle(host).flexDirection).toBe("column");
});
it("keeps rules when element is taken out from the document", () => {
const shadowRoot = host.attachShadow({ mode: "open" });
html`<template layout="column"><div layout="row"></div></template>`(
host,
shadowRoot,
);
document.body.removeChild(host);
return resolveTimeout(() => {
document.body.appendChild(host);
return resolveTimeout(() => {
expect(window.getComputedStyle(host).display).toBe("flex");
});
});
});
it("not overwrite hidden selector for the host element", () => {
const shadowRoot = host.attachShadow({ mode: "open" });
html`<template layout="row"><div></div></template>`(host, shadowRoot);
return resolveTimeout(() => {
expect(window.getComputedStyle(host).display).toBe("flex");
host.hidden = true;
expect(window.getComputedStyle(host).display).toBe("none");
});
});
it("supports multiline attribute value", () => {
html`
<template
layout="
row
padding:2
"
>
</template>
`(host);
const hostStyles = window.getComputedStyle(host);
expect(hostStyles.display).toBe("flex");
expect(hostStyles.flexDirection).toBe("row");
expect(hostStyles.paddingTop).toBe("16px");
});
it("supports custom selectors", () => {
html`<template layout="row" layout.active="column"></template>`(host);
const hostStyles = window.getComputedStyle(host);
expect(hostStyles.display).toBe("flex");
expect(hostStyles.flexDirection).toBe("row");
host.classList.add("active");
expect(window.getComputedStyle(host).flexDirection).toBe("column");
});
it("supports predefined media queries", () => {
html`
<template layout="row" layout@200px="column">
<div layout@200px="column"></div>
</template>
`(host);
expect(window.getComputedStyle(host).flexDirection).toBe("column");
expect(window.getComputedStyle(host.children[0]).flexDirection).toBe(
"column",
);
});
it("supports css variables", () => {
html`<template layout="row margin:--sm"></template>`(host);
document.documentElement.style.setProperty("--sm", "4px");
expect(window.getComputedStyle(host).margin).toBe("4px");
document.documentElement.style.removeProperty("--sm");
});
it("supports base rules", () => {
html`
<template layout>
<span layout="block"></span>
<div layout="block:center"></div>
<div layout="inline"></div>
<div layout="hidden"></div>
</template>
`(host);
expect(window.getComputedStyle(host.children[0]).display).toBe("block");
expect(window.getComputedStyle(host.children[1]).textAlign).toBe("center");
expect(window.getComputedStyle(host.children[2]).display).toBe("inline");
expect(window.getComputedStyle(host.children[3]).display).toBe("none");
});
it("supports flexbox rules", () => {
html`
<template layout="row gap">
<div layout="grow shrink:2 order"></div>
<div layout="shrink grow:2 order:2 basis"></div>
<div layout="basis:200px"></div>
<div><div layout="row:wrap inline gap::2"></div></div>
</template>
`(host);
const hostStyles = window.getComputedStyle(host);
expect(hostStyles.columnGap).toBe("8px");
expect(hostStyles.rowGap).toBe("8px");
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.flexGrow).toBe("1");
expect(styles0.flexShrink).toBe("2");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.flexShrink).toBe("1");
expect(styles1.flexGrow).toBe("2");
expect(styles1.order).toBe("2");
expect(styles1.flexBasis).toBe("auto");
expect(window.getComputedStyle(host.children[2]).flexBasis).toBe("200px");
const styles3 = window.getComputedStyle(host.children[3].children[0]);
expect(styles3.flexWrap).toBe("wrap");
expect(styles3.display).toBe("inline-flex");
expect(styles3.rowGap).toBe("16px");
});
it("supports grid main rules", () => {
html`
<template layout="block">
<div layout="grid"></div>
<div layout="grid:2::column"></div>
<div layout="grid:2:2:column:dense"></div>
<div layout="grid::2"></div>
<div layout="grid:100px|1"></div>
<div layout="grid inline"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.gridTemplateColumns.split(" ").length).toBe(1);
expect(styles0.gridTemplateRows).toBe("none");
expect(styles0.gridAutoFlow).toBe("row");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.gridTemplateColumns.split(" ").length).toBe(2);
expect(styles1.gridTemplateRows).toBe("none");
expect(styles1.gridAutoFlow).toBe("column");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.gridTemplateColumns.split(" ").length).toBe(2);
expect(styles2.gridTemplateRows.split(" ").length).toBe(2);
expect(styles2.gridAutoFlow).toBe("column dense");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.gridTemplateColumns).toBe("none");
expect(styles3.gridTemplateRows.split(" ").length).toBe(2);
const styles4 = window.getComputedStyle(host.children[4]);
const grid = styles4.gridTemplateColumns.split(" ");
expect(grid[0]).toBe("100px");
expect(grid[1]).not.toBeFalsy();
const styles5 = window.getComputedStyle(host.children[5]);
expect(styles5.display).toBe("inline-grid");
});
it("supports grid placement rules", () => {
html`
<template layout="grid:2:2">
<div layout="area:2"></div>
<div layout="area:2/3:1/3"></div>
<div layout="area::1"></div>
<div layout="area"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.gridColumnStart).toBe("span 2");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.gridColumnStart).toBe("2");
expect(styles1.gridColumnEnd).toBe("3");
expect(styles1.gridRowStart).toBe("1");
expect(styles1.gridRowEnd).toBe("3");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.gridColumnStart).toBe("auto");
expect(styles2.gridRowStart).toBe("span 1");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.gridColumnStart).toBe("auto");
expect(styles3.gridRowStart).toBe("auto");
});
it("supports alignment rules", () => {
html`
<template layout>
<div layout="grid items content self"></div>
<div layout="grid items:end:start content:end self:end"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.alignItems).toBe("start");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.alignItems).toBe("end");
expect(styles1.justifyItems).toBe("start");
expect(styles1.justifyContent).toBe("end");
expect(styles1.alignSelf).toBe("end");
});
it("supports sizing rules", () => {
html`
<template layout>
<div layout="width:full"></div>
<div layout="height:20px:10px:full padding"></div>
<div layout="ratio:16/9"></div>
<div layout="size:100px"></div>
<div layout="size:100px:50px"></div>
<div layout="size::50px"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.width).not.toBe("0px");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.height).toBe("20px");
expect(styles1.minHeight).toBe("10px");
expect(styles1.maxHeight).toBe("100%");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.aspectRatio).toBe("16 / 9");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.width).toBe("100px");
const styles4 = window.getComputedStyle(host.children[4]);
expect(styles4.width).toBe("100px");
expect(styles4.height).toBe("50px");
const styles5 = window.getComputedStyle(host.children[5]);
expect(styles5.width).not.toBe("50px");
expect(styles5.height).toBe("50px");
});
it("supports overscroll and overflow rules", () => {
html`
<template layout>
<div layout="overflow:auto"></div>
<div layout="overflow:x:scroll"></div>
<div layout="overflow:hidden"></div>
<div layout="overflow"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.overflow).toBe("auto");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.overflowX).toBe("scroll");
expect(styles1.overflowY).toBe("auto");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.overflow).toBe("hidden");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.overflow).toBe("hidden");
});
it("supports margin rules", () => {
html`
<template layout>
<div layout="margin"></div>
<div layout="margin:top:2"></div>
<div layout="margin:2:4"></div>
<div layout="margin:bottom"></div>
<div layout="margin:-2"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.marginTop).toBe("8px");
expect(styles0.marginRight).toBe("8px");
expect(styles0.marginBottom).toBe("8px");
expect(styles0.marginLeft).toBe("8px");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.marginTop).toBe("16px");
expect(styles1.marginRight).toBe("0px");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.marginTop).toBe("16px");
expect(styles2.marginRight).toBe("32px");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.marginBottom).toBe("8px");
const styles4 = window.getComputedStyle(host.children[4]);
expect(styles4.marginTop).toBe("-16px");
});
it("supports padding rules", () => {
html`
<template layout>
<div layout="padding"></div>
<div layout="padding:top:2"></div>
<div layout="padding:2:4"></div>
<div layout="padding:bottom"></div>
<div layout="padding:-2"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.paddingTop).toBe("8px");
expect(styles0.paddingRight).toBe("8px");
expect(styles0.paddingBottom).toBe("8px");
expect(styles0.paddingLeft).toBe("8px");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.paddingTop).toBe("16px");
expect(styles1.paddingRight).toBe("0px");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.paddingTop).toBe("16px");
expect(styles2.paddingRight).toBe("32px");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.paddingBottom).toBe("8px");
const styles4 = window.getComputedStyle(host.children[4]);
expect(styles4.paddingTop).toBe("0px");
});
it("supports position rules", () => {
html`
<template layout>
<div layout="inset layer"></div>
<div layout="top left bottom right layer:2"></div>
<div layout="top:2 left:2 bottom:2 right:2"></div>
<div layout="inset:2"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.top).toBe("0px");
expect(styles0.right).toBe("0px");
expect(styles0.bottom).toBe("0px");
expect(styles0.left).toBe("0px");
expect(styles0.zIndex).toBe("1");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.top).toBe("0px");
expect(styles1.right).toBe("0px");
expect(styles1.bottom).toBe("0px");
expect(styles1.left).toBe("0px");
expect(styles1.zIndex).toBe("2");
const styles2 = window.getComputedStyle(host.children[2]);
expect(styles2.top).toBe("16px");
expect(styles2.right).toBe("16px");
expect(styles2.bottom).toBe("16px");
expect(styles2.left).toBe("16px");
const styles3 = window.getComputedStyle(host.children[3]);
expect(styles3.top).toBe("16px");
expect(styles3.right).toBe("16px");
expect(styles3.bottom).toBe("16px");
expect(styles3.left).toBe("16px");
});
it("supports generic variables as properties with values", () => {
expect(() => {
html`
<template layout>
<div layout="::border"></div>
</template>
`(host);
}).toThrow();
html`
<template layout>
<div layout="::ui:color:red ::border:thin"></div>
<div layout="::font:headline-m"></div>
</template>
`.css`
:root {
--ui-color-red: red;
--border-thin: 1px solid black;
--font-headline-m: 24px sans-serif;
}
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.color).toBe("rgb(255, 0, 0)");
expect(styles0.border).toBe("1px solid rgb(0, 0, 0)");
const styles1 = window.getComputedStyle(host.children[1]);
expect(styles1.font).toBe("24px sans-serif");
});
if (document.startViewTransition) {
it("supports transition view name", () => {
html`
<template layout>
<div layout="view:test"></div>
</template>
`(host);
const styles0 = window.getComputedStyle(host.children[0]);
expect(styles0.viewTransitionName).toBe("test");
});
}
});