forked from w3c/css-houdini-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverview.bs
2196 lines (1625 loc) · 88.3 KB
/
Overview.bs
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: CSS Layout API Level 1
Status: ED
Group: houdini
TR: https://www.w3.org/TR/css-layout-api-1/
ED: https://drafts.css-houdini.org/css-layout-api-1/
Shortname: css-layout-api
Level: 1
Abstract:
An API for allowing web developers to define their own layout modes with javascript.
See <a href="https://github.com/w3c/css-houdini-drafts/blob/master/css-layout-api/EXPLAINER.md">EXPLAINER</a>.
Editor: Greg Whitworth, gwhit@microsoft.com, w3cid 69511
Editor: Ian Kilpatrick, ikilpatrick@chromium.org, w3cid 73001
Editor: Tab Atkins-Bittner, Google, http://xanthir.com/contact/, w3cid 42199
Former Editor: Shane Stephens, shanestephens@google.com, w3cid 47691
Editor: Robert O'Callahan, robert@ocallahan.org
Editor: Rossen Atanassov, rossen.atanassov@microsoft.com, w3cid 49885
Ignored Terms: LayoutWorklet
Ignored Terms: create a workletglobalscope
</pre>
<style>
/* Put nice boxes around each algorithm. */
[data-algorithm]:not(.heading) {
padding: .5em;
border: thin solid #ddd; border-radius: .5em;
margin: .5em calc(-0.5em - 1px);
}
[data-algorithm]:not(.heading) > :first-child {
margin-top: 0;
}
[data-algorithm]:not(.heading) > :last-child {
margin-bottom: 0;
}
</style>
<pre class="link-defaults">
spec:css-break-3; type:dfn; text:fragment
spec:css-display-3; type:dfn; text:box
spec:css-display-3; type:value; for:display; text:none
spec:css-display-3; type:value; for:<display-inside>; text:grid
spec:css-display-3; type:value; for:<display-outside>; text:inline
spec:css-pseudo-4; type:selector; text:::after
spec:css-pseudo-4; type:selector; text:::before
spec:css-pseudo-4; type:selector; text:::first-letter
spec:css-pseudo-4; type:selector; text:::first-line
spec:dom; type:dfn; for:/; text:element
spec:infra; type:dfn; text:list
spec:html; type:dfn; for:global object; text:realm
spec:css22; type:property;
text:max-height
text:min-height
text:min-width
spec:css2; type:property; text:max-width
</pre>
<pre class="anchors">
urlPrefix: https://heycam.github.io/webidl/; type: dfn;
text: InvalidModificationError
urlPrefix: #dfn-;
url: throw; text: thrown
urlPrefix: #idl-;
text: boolean
text: DOMException
url: es-type-mapping; text: converting
url: es-invoking-callback-functions; text: Invoke
urlPrefix: https://tc39.github.io/ecma262/#sec-; type: dfn;
text: constructor
text: Construct
text: GetMethod
text: IsArray
text: IsCallable
text: IsConstructor
text: HasProperty
url: ecmascript-data-types-and-values; text: type
url: get-o-p; text: Get
url: terms-and-definitions-function; text: function
urlPrefix: native-error-types-used-in-this-standard-
text: TypeError
urlPrefix: https://drafts.csswg.org/css-display-3/#; type: dfn
text: flow layout
urlPrefix: https://drafts.csswg.org/css-sizing/#; type: dfn
text: intrinsic sizes
urlPrefix: https://drafts.csswg.org/css-break/#; type: dfn
text: fragmentation break
urlPrefix: https://www.w3.org/TR/CSS21/; type:dfn
urlPrefix: box.html#;
url: box-dimensions; text: box model edges
urlPrefix: visudet.html#;
text: static position
urlPrefix: https://html.spec.whatwg.org/#; type: dfn
text: structuredserializeforstorage
text: structureddeserialize
</pre>
Introduction {#intro}
=====================
<em>This section is not normative.</em>
The layout stage of CSS is responsible for generating and positioning <a>fragments</a> from the
<a>box tree</a>.
This specification describes an API which allows developers to layout a <a>box</a> in response to
computed style and <a>box tree</a> changes.
Layout API Containers {#layout-api-containers}
==============================================
A new <a href="https://www.w3.org/TR/css-values-3/#comb-one">alternative</a> value is added
to the <<display-inside>> production: <code>layout(<<ident>>)</code>.
<dl dfn-for="display" dfn-type=value>
<dt><dfn>layout()</dfn>
<dd>
This value causes an element to generate a <a>layout API container</a> box.
</dl>
A <dfn>layout API container</dfn> is the box generated by an element with a <<display-inside>>
<a>computed value</a> ''layout()''.
A <a>layout API container</a> establishes a new <dfn>layout API formatting context</dfn> for its
contents. This is the same as establishing a block formatting context, except that the layout
provided by the author is used instead of the block layout.
For example, floats do not intrude into the layout API container, and the layout API container's
margins do not collapse with the margins of its contents.
All inflow children of a <a>layout API container</a> are called <dfn>layout API children</dfn> and
are laid out using the author defined layout.
<a>Layout API containers</a> form a containing block for their contents
<a href="https://www.w3.org/TR/CSS2/visudet.html#containing-block-details">exactly like block
containers do</a>. [[!CSS21]]
Note: In a future level of the specification there may be a way to override the containing block
behaviour.
The 'overflow' property applies to <a>layout API containers</a>. This is discussed in
[[#interaction-overflow]].
As the layout is entirely up to the author, properties which are used in other layout modes (e.g.
flex, block) may not apply. For example an author may not respect the 'margin' property on children.
<div class="example">
The HTML below shows an example of setting the ''display'' to a ''layout()'' function.
<pre class="lang-html">
<!DOCTYPE html>
<style>
.centering-layout { display: layout(centering); }
</style>
<div class="centering-layout"></div>
</pre>
</div>
Layout API Container Painting {#painting}
-----------------------------------------
<a>Layout API Container</a> children paint exactly the same as inline blocks [[!CSS21]], except that
the order in which they are returned from the layout method (via
{{FragmentResultOptions/childFragments}}) is used in place of raw document order, and 'z-index'
values other than ''z-index/auto'' create a stacking context even if 'position' is ''static''.
Box Tree Transformations {#layout-api-box-tree}
-----------------------------------------------
The <a>layout API children</a> can act in different ways depending on the value of <a for="document
layout definition">layout options'</a> {{LayoutOptions/childDisplay}} (set by
<code>layoutOptions</code> on the class).
If the value of <a for="document layout definition">layout options'</a>
{{LayoutOptions/childDisplay}} is <code>"block"</code> the 'display' value of that child is
<a>blockified</a>. This is similar to children of <a>flex containers</a> or <a>grid containers</a>.
See [[!css3-display]].
If the value of <a for="document layout definition">layout options'</a>
{{LayoutOptions/childDisplay}} is <code>"normal"</code>, no <a>blockification</a> occurs. Instead
children with a <<display-outside>> <a>computed value</a> of ''inline'' (a <a>root inline box</a>)
will produce a single {{LayoutFragment}} representing each line when
{{LayoutChild/layoutNextFragment()}} is called.
Note: This allows authors to adjust the available inline size of each line, and position each line
separately.
Children of a {{LayoutChild}} which represents <a>root inline box</a> also have some additional
transformations.
- A <a>block-level</a> box inside a <a>inline-level</a> box is <a>inlinified</a> I.e. its
<<display-outside>> is set to ''inline''.
- A <a>float</a> inside a <a>inline-level</a> box is not taken out of flow. Instead it must be
treated as inflow, and be <a>inlinified</a>.
In both of the above cases the children become <a>atomic inlines</a>.
Note: User agents would not perform any "inline splitting" or fragmenting when they encounter a
<a>block-level</a> box.
<div class="note">
Note: In the example below "inline-span" would be represented as a single {{LayoutChild}} with
both "block" and "float" being <a>atomic inlines</a>.
<pre class="lang-html">
<span id="inline-span">
Text
<div id="block"></div>
<div id="float"></div>
Text
</span>
</pre>
</div>
Layout Worklet {#layout-worklet}
================================
The {{layoutWorklet}} attribute allows access to the {{Worklet}} responsible for all the classes
which are related to layout.
The {{layoutWorklet}}'s <a>worklet global scope type</a> is {{LayoutWorkletGlobalScope}}.
<pre class='idl'>
partial namespace CSS {
[SameObject] readonly attribute Worklet layoutWorklet;
};
</pre>
The {{LayoutWorkletGlobalScope}} is the global execution context of the {{layoutWorklet}}.
<pre class='idl'>
[Global=(Worklet,LayoutWorklet),Exposed=LayoutWorklet]
interface LayoutWorkletGlobalScope : WorkletGlobalScope {
void registerLayout(DOMString name, VoidFunction layoutCtor);
};
</pre>
Registering A Layout {#registering-layout}
------------------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
dictionary LayoutOptions {
ChildDisplayType childDisplay = "block";
LayoutSizingMode sizing = "block-like";
};
[Exposed=LayoutWorklet]
enum ChildDisplayType {
"block",
"normal",
};
[Exposed=LayoutWorklet]
enum LayoutSizingMode {
"block-like",
"manual",
};
</pre>
The <a>document</a> has a <a>map</a> of <dfn>document layout definitions</dfn>. Initially this map
is empty; it is populated when {{registerLayout(name, layoutCtor)}} is called.
The {{LayoutWorkletGlobalScope}} has a <a>map</a> of <dfn>layout definitions</dfn>. Initially this
map is empty; it is populated when {{registerLayout(name, layoutCtor)}} is called.
Each <a>box</a> representing a <a>layout API container</a> has a <a>map</a> of <dfn>layout class
instances</dfn>. Initially this map is empty; it is populated when the user agent calls either
<a>determine the intrinsic sizes</a> or <a>generate a fragment</a> for a <a>box</a>.
<div class='note'>
Note: The shape of the class should be:
<pre class='lang-javascript'>
class MyLayout {
static get inputProperties() { return ['--foo']; }
static get childrenInputProperties() { return ['--bar']; }
static get layoutOptions() {
return {childDisplay: 'normal', sizing: 'block-like'}
}
async intrinsicSizes(children, edges, styleMap) {
// Intrinsic sizes code goes here.
}
async layout(children, edges, constraints, styleMap, breakToken) {
// Layout code goes here.
}
}
</pre>
</div>
</div>
Registering A Layout Algorithm {#registering-a-layout-algorithm}
----------------------------------------------------------------
The algorithm below is run when the {{registerLayout(name, layoutCtor)}} is called. It notifies the
engine about the new user defined layout.
<div algorithm>
When the <dfn method for=LayoutWorkletGlobalScope>registerLayout(|name|, |layoutCtor|)</dfn> method
is called, the user agent <em>must</em> run the following steps:
1. If the |name| is an empty string, [=throw=] a [=TypeError=] and abort all these steps.
2. Let |layoutDefinitionMap| be {{LayoutWorkletGlobalScope}}'s [=layout definitions=] map.
3. If |layoutDefinitionMap|[|name|] [=map/exists=] [=throw=] a "{{InvalidModificationError}}"
{{DOMException}} and abort all these steps.
4. Let |inputProperties| be an empty <code>sequence<DOMString></code>.
5. Let |inputPropertiesIterable| be the result of [=Get=](|layoutCtor|, "inputProperties").
6. If |inputPropertiesIterable| is not undefined, then set |inputProperties| to the result of
[=converting=] |inputPropertiesIterable| to a <code>sequence<DOMString></code>. If an
exception is [=thrown=], rethrow the exception and abort all these steps.
7. Filter |inputProperties| so that it only contains [=supported CSS properties=] and [=custom
properties=].
Note: The list of CSS properties provided by the input properties getter can either be
custom or native CSS properties.
Note: The list of CSS properties may contain shorthands.
Note: In order for a layout class to be forwards compatible, the list of CSS properties can
also contains currently invalid properties for the user agent. For example
<code>margin-bikeshed-property</code>.
8. Let |childInputProperties| be an empty <code>sequence<DOMString></code>.
9. Let |childInputPropertiesIterable| be the result of [=Get=](|layoutCtor|,
"childInputProperties").
10. If |childInputPropertiesIterable| is not undefined, then set |childInputProperties| to the
result of [=converting=] |childInputPropertiesIterable| to a
<code>sequence<DOMString></code>. If an exception is [=thrown=], rethrow the exception
and abort all these steps.
11. Filter |childInputProperties| so that it only contains [=supported CSS properties=] and [=custom
properties=].
12. Let |layoutOptionsValue| be the result of [=Get=](|layoutCtor|, "layoutOptions").
13. Let |layoutOptions| be the result of [=converting=] |layoutOptionsValue| to a
{{LayoutOptions}}. If an exception is [=thrown=], rethrow the exception and abort all these
steps.
14. Let |prototype| be the result of [=Get=](|layoutCtor|, "prototype").
15. If the result of [=Type=](|prototype|) is not Object, [=throw=] a [=TypeError=] and abort
all these steps.
16. Let |intrinsicSizesValue| be the result of [=Get=](|prototype|, "intrinsicSizes").
17. Let |intrinsicSizes| be the result of [=converting=] |intrinsicSizesValue| to the
[=Function=] [=callback function=] type. Rethrow any exceptions from the conversion.
18. Let |layoutValue| be the result of <a>Get</a>(|prototype|, <code>"layout"</code>).
19. Let |layout| be the result of [=converting=] |layoutValue| to the [=Function=] [=callback
function=] type. Rethrow any exceptions from the conversion.
20. Let |definition| be a new [=layout definition=] with:
- [=class constructor=] being |layoutCtor|.
- [=layout function=] being |layout|.
- [=intrinsic sizes function=] being |intrinsicSizes|.
- [=constructor valid flag=] being <b>true</b>.
- [=layout definition/child input properties=] being |childInputProperties|.
- [=layout definition/input properties=] being |inputProperties|.
- [=layout defintion/layout options=] being |layoutOptions|.
21. [=map/Set=] |layoutDefinitionMap|[|name|] to |definition|.
22. [=Queue a task=] to run the following steps:
1. Let |documentLayoutDefinitionMap| be the associated [=document's=] [=document layout
definitions=] [=map=].
2. Let |documentDefinition| be a new [=document layout definition=] with:
- [=document layout definition/child input properties=] being |childInputProperties|.
- [=document layout definition/input properties=] being |inputProperties|.
- [=document layout definition/layout options=] being |layoutOptions|.
3. If |documentLayoutDefinitionMap|[|name|] [=map/exists=], run the following steps:
1. Let |existingDocumentDefinition| be the result of [=map/get=]
|documentLayoutDefinitionMap|[|name|].
2. If |existingDocumentDefinition| is <code>"invalid"</code>, abort all these steps.
3. If |existingDocumentDefinition| and |documentDefinition| are not equivalent, (that is
[=document layout definition/input properties=], [=document layout definition/child
input properties=], and [=document layout definition/layout options=] are
different), then:
[=map/Set=] |documentLayoutDefinitionMap|[|name|] to <code>"invalid"</code>.
Log an error to the debugging console stating that the same class was registered
with different <code>inputProperties</code>, <code>childInputProperties</code>, or
<code>layoutOptions</code>.
4. Otherwise, [=map/set=] |documentLayoutDefinitionMap|[|name|] to |documentDefinition|.
</div>
Layout API Model and Terminology {#layout-api-model-and-terminology}
====================================================================
This section gives an overview of the Layout API given to authors.
The <dfn>current layout</dfn> is the layout algorithm for the <a>box</a> we are currently performing
layout for.
The <dfn>parent layout</dfn> is the layout algorithm for the <a>box's</a> direct parent, (the layout
algorithm which is requesting the <a>current layout</a> to be performed).
A <dfn>child layout</dfn> is the layout algorithm for a {{LayoutChild}} of the <a>current layout</a>.
Layout Children {#layout-children}
----------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface LayoutChild {
readonly attribute StylePropertyMapReadOnly styleMap;
IntrinsicSizesRequest intrinsicSizes();
LayoutFragmentRequest layoutNextFragment(LayoutConstraints constraints, ChildBreakToken breakToken);
};
</pre>
The {{LayoutChild}} has internal slot(s):
- <dfn attribute for=LayoutChild>\[[box]]</dfn> a CSS <a>box</a>.
- <dfn attribute for=LayoutChild>\[[styleMap]]</dfn> a {{StylePropertyMapReadOnly}}, this is the
computed style for the child, it is populated with only the properties listed in
<code>childInputProperties</code>.
<hr>
A {{LayoutChild}} represents either a CSS generated <a>box</a> before layout has occured. (The box
or boxes will all have a computed value of 'display' that is not ''none'').
The {{LayoutChild}} does not contain any layout information itself (like inline or block size) but
can be used to generate {{LayoutFragment}}s which do contain layout information.
An author cannot construct a {{LayoutChild}} with this API, this happens at a separate stage of the
rendering engine (post style resolution).
A {{LayoutChild}} could be generated by:
- An <a>element</a>.
- A <a>root inline box</a>.
- A <a>::before</a> or <a>::after</a> pseudo-element.
Note: Other pseudo-elements such as <a>::first-letter</a> or <a>::first-line</a> do not generate
a {{LayoutChild}} for layout purposes. They are additional styling information for a text
node.
- An <a>anonymous box</a>. For example an anonymous box may be inserted as a result of:
- A text node which has undergone <a>blockification</a>. (Or more generally a <a>root inline
box</a> which has undergone <a>blockification</a>).
- An element with ''display: table-cell'' which doesn't have a parent with ''display: table''.
<div class="note">
Note: As an example the following would be placed into three {{LayoutChild}}ren:
<pre class="lang-html">
<style>
#box::before { content: 'hello!'; }
</style>
<!-- A ::before pseudo-element is inserted here. -->
<div id="box">A block level box with text.</div>
<img src="..." />
</pre>
</div>
<div class="note">
Note: As an example the following would be placed into a single {{LayoutChild}} as they share a
<a>root inline box</a>:
<pre class="lang-html">
This is a next node, <span>with some additional styling,
that may</span> break over<br>multiple lines.
</pre>
</div>
Multiple non-<a>atomic inlines</a> are placed within the same {{LayoutChild}} to allow rendering
engines to perform text shaping across element boundaries.
<div class="note">
Note: As an example the following should produce one {{LayoutFragment}} but is from
three non-<a>atomic inlines</a>:
<pre class="lang-html">
ع<span style="color: blue">ع</span>ع
</pre>
</div>
An array of {{LayoutChild}}ren is passed into the layout method which represents the children of the
current box which is being laid out.
<div algorithm>
The <dfn attribute for=LayoutChild>styleMap</dfn>, on getting from a {{LayoutChild}} |this|, the
user agent must perform the following steps:
1. Return |this|' {{StylePropertyMapReadOnly}} contained in the {{[[styleMap]]}} internal slot.
</div>
<div algorithm>
When the <dfn method for=LayoutChild>layoutNextFragment(|constraints|, |breakToken|)</dfn> method is
called on a {{LayoutChild}} |this|, the user agent must perform the following steps:
1. Let |request| be a new {{LayoutFragmentRequest}} with internal slot(s):
- {{LayoutFragmentRequest/[[layoutChild]]}} set to |this|.
- {{[[layoutConstraints]]}} set to |constraints|.
- {{[[breakToken]]}} set to |breakToken|.
2. Return |request|.
</div>
<div algorithm>
When the <dfn method for=LayoutChild>intrinsicSizes()</dfn> method is called on a {{LayoutChild}}
|this|, the user agent must perform the following steps:
1. Let |request| be a new {{IntrinsicSizesRequest}} with internal slot(s):
- {{IntrinsicSizesRequest/[[layoutChild]]}} set to |this|.
2. Return |request|.
</div>
Note: Both {{LayoutChild/layoutNextFragment()}} and {{LayoutChild/intrinsicSizes()}} don't
synchronously run. See [[#request-objects]] for a full description.
### LayoutChildren and the Box Tree ### {#layout-child-box-tree}
Each <a>box</a> has a <dfn attribute for=box>\[[layoutChildMap]]</dfn> internal slot, which is a
<a>map</a> of {{LayoutWorkletGlobalScope}}s to {{LayoutChild}}ren.
<div algorithm="get a layout child">
When the user agent wants to <dfn>get a layout child</dfn> given |workletGlobalScope|, |name|, and
|box|, it <em>must</em> run the following steps:
1. Assert that:
- |box| is currently attached to the <a>box tree</a>.
- |box|'s <a>containing block</a> is a <a>layout API container</a>.
- The <a>containing block</a>'s ''layout()'' function's first argument is |name|.
2. Let |layoutChildMap| be |box|'s {{[[layoutChildMap]]}}.
3. If |layoutChildMap|[|workletGlobalScope|] does not <a for=map>exist</a>, run the following
steps:
1. Let |definition| be the result of <a>get a layout definition</a> given |name|, and
|workletGlobalScope|.
Assert that <a>get a layout definition</a> succeeded, and |definition| is not
<code>"invalid"</code>.
2. Let |childInputProperties| be |definition|'s <a for="layout definition">child input
properties</a>.
3. Let |layoutChild| be a new {{LayoutChild}} with internal slot(s):
- {{[[box]]}} set to |box|.
- {{[[styleMap]]}} set to a new {{StylePropertyMapReadOnly}} populated with
<em>only</em> the <a>computed values</a> for properties listed in
|childInputProperties|.
4. <a for=map>Set</a> |layoutChildMap|[|workletGlobalScope|] to |layoutChild|.
4. Return the result of <a for=map>get</a> |layoutChildMap|[|workletGlobalScope|]
</div>
When a <a>box</a> is inserted into the <a>box tree</a> the user agent <em>may</em> pre-populate the
{{[[layoutChildMap]]}} for all {{LayoutWorkletGlobalScope}}s.
When a <a>box</a> is removed from the <a>box tree</a> the user agent <em>must</em> clear the
{{[[layoutChildMap]]}}.
<div algorithm="update a layout child style">
When the user agent wants to <dfn>update a layout child style</dfn> given |box|, it <em>must</em>
run the following steps:
1. Assert that:
- |box| is currently attached to the <a>box tree</a>.
2. If |box|'s <a>containing block</a> is not a <a>layout API container</a>, abort all these
steps.
3. Let |layoutChildMap| be |box|'s {{[[layoutChildMap]]}}.
4. <a for=map>For each</a> |layoutChild| in |layoutChildMap|:
1. Let |styleMap| be |layoutChild|'s {{[[styleMap]]}}.
2. Update |styleMap|'s <a>declarations</a> based on the |box|'s new computed style.
</div>
When the computed style of a <a>box</a> changes the user agent must run the <a>update a layout child
style</a> algorithm.
Layout Fragments {#layout-fragments}
------------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface LayoutFragment {
readonly attribute double inlineSize;
readonly attribute double blockSize;
attribute double inlineOffset;
attribute double blockOffset;
readonly attribute any data;
readonly attribute ChildBreakToken? breakToken;
};
</pre>
The {{LayoutFragment}} has internal slot(s):
- <dfn attribute for=LayoutFragment>\[[layoutFragmentRequest]]</dfn> a
{{LayoutFragmentRequest}}, this is the fragment request which generated this fragment.
- <dfn attribute for=LayoutFragment>\[[generator]]</dfn> the generator which produced this
fragment.
<hr>
A {{LayoutFragment}} represents a CSS <a>fragment</a> of a {{LayoutChild}} after layout has occurred
on that child. This is produced by the {{LayoutChild/layoutNextFragment()}} method.
The {{LayoutFragment}} has {{LayoutFragment/inlineSize}} and {{LayoutFragment/blockSize}}
attributes, which are set by the respective child's layout algorithm. They represent the <b>border
box</b> size of the CSS <a>fragment</a>, and are relative to the <a>current layout's</a> writing
mode.
The {{LayoutFragment/inlineSize}} and {{LayoutFragment/blockSize}} attributes cannot be changed. If
the <a>current layout</a> requires a different {{LayoutFragment/inlineSize}} or
{{LayoutFragment/blockSize}} the author must perform {{LayoutChild/layoutNextFragment()}} again with
different arguments in order to get different results.
The author inside the current layout can position a resulting {{LayoutFragment}} by setting its
{{LayoutFragment/inlineOffset}} and {{LayoutFragment/blockOffset}} attributes. If not set by the
author they default to zero. The {{LayoutFragment/inlineOffset}} and {{LayoutFragment/blockOffset}}
attributes represent the position of the {{LayoutFragment}} relative to its parent's <b>border
box</b>, before transform or positioning (e.g. if a fragment is <a>relatively positioned</a>) has
been applied.
<div class="example">
The layout algorithm performs a block-like layout (positioning fragments sequentially in the block
direction), while centering its children in the inline direction.
<pre class="lang-javascript">
registerLayout('block-like', class {
async intrinsicSizes(children, edges, styleMap) {
const childrenSizes = await Promise.all(children.map((child) => {
return child.intrinsicSizes();
}));
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
return Math.max(max, childSizes.maxContentSize);
}, 0) + edges.all.inline;
const minContentSize = childrenSizes.reduce((max, childSizes) => {
return Math.max(max, childSizes.minContentSize);
}, 0) + edges.all.inline;
return {maxContentSize, minContentSize};
}
async layout(children, edges, constraints, styleMap) {
// Determine our (inner) available size.
const availableInlineSize = constraints.fixedInlineSize - edges.all.inline;
const availableBlockSize = constraints.fixedBlockSize ?
constraints.fixedBlockSize - edges.all.block : null;
const childFragments = [];
const childConstraints = { availableInlineSize, availableBlockSize };
const childFragments = await Promise.all(children.map((child) => {
return child.layoutNextFragment(childConstraints);
}));
let blockOffset = edges.all.blockStart;
for (let fragment of childFragments) {
// Position the fragment in a block like manner, centering it in the
// inline direction.
fragment.blockOffset = blockOffset;
fragment.inlineOffset = Math.max(
edges.all.inlineStart,
(availableInlineSize - fragment.inlineSize) / 2);
blockOffset += fragment.blockSize;
}
const autoBlockSize = blockOffset + edges.all.blockEnd;
return {
autoBlockSize,
childFragments,
};
}
});
</pre>
</div>
A <a>layout API container</a> can communicate with other <a>layout API containers</a> by using the
{{LayoutFragment/data}} attribute. This is set by the {{FragmentResultOptions/data}} member in the
{{FragmentResultOptions}} dictionary.
The {{LayoutFragment}}'s {{LayoutFragment/breakToken}} specifies where the {{LayoutChild}} last
fragmented. If the {{LayoutFragment/breakToken}} is null the {{LayoutChild}} wont produce any more
{{LayoutFragment}}s for that token chain. The {{LayoutFragment/breakToken}} can be passed to the
{{LayoutChild/layoutNextFragment()}} function to produce the next {{LayoutFragment}} for a
particular child. The {{LayoutFragment/breakToken}} cannot be changed.
If the <a>current layout</a> requires a different {{LayoutFragment/breakToken}} the author must perform
{{LayoutChild/layoutNextFragment()}} again with different arguments.
<div algorithm>
When the user agent wants to <dfn>create a layout fragment</dfn> given |generator|,
|layoutFragmentRequest|, and |internalFragment|, it <em>must</em> run the following steps:
1. Let |targetRealm| be |generator|'s <a>Realm</a>.
2. Let |fragment| be a new {{LayoutFragment}} with:
- The {{[[layoutFragmentRequest]]}} internal slot being |layoutFragmentRequest|.
- The {{[[generator]]}} internal slot being |generator|.
- {{LayoutFragment/inlineSize}} being |internalFragment|'s <a>inline size</a> relative to
the <a>current layout's</a> writing mode.
- {{LayoutFragment/blockSize}} being |internalFragment|'s <a>block size</a> relative to the
<a>current layout's</a> writing mode.
- {{LayoutFragment/inlineOffset}} initially set to 0.
- {{LayoutFragment/blockOffset}} initially set to 0.
- {{LayoutFragment/breakToken}} being a new {{ChildBreakToken}} representing
|internalFragment|'s internal break token, if any.
- If |internalFragment| has a |clonedData| object stored with it, let
{{LayoutFragment/data}} being the result of
<a>StructuredDeserialize</a>(|clonedData|, |targetRealm|), otherwise null.
3. Return |fragment|.
</div>
Intrinsic Sizes {#intrinsic-sizes}
----------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface IntrinsicSizes {
readonly attribute double minContentSize;
readonly attribute double maxContentSize;
};
</pre>
The {{IntrinsicSizes}} object has internal slot(s):
- <dfn attribute for=IntrinsicSizes>\[[intrinsicSizesRequest]]</dfn> a
{{IntrinsicSizesRequest}}, this is the intrinsic sizes request which generated these
intrinsic sizes.
<hr>
A {{IntrinsicSizes}} object represents the <a>min-content size</a> and <a>max-content size</a> of a
CSS <a>box</a>. It has {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}}
attributes which represent the <b>border box</b> min/max-content contribution of the {{LayoutChild}}
for the <a>current layout</a>. The attributes are relative to the inline direction of the <a>current
layout's</a> writing mode.
The {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}} cannot be changed. They
must not change for a {{LayoutChild}} within the current layout pass.
<div class="example">
The example below shows the border-box intrinsic sizes of two children.
<pre class="lang-html">
<style>
.child-0 {
width: 380px;
border: solid 10px;
}
.child-1 {
border: solid 5px;
}
.box {
display: layout(intrinsic-sizes-example);
font: 25px/1 Ahem;
}
</style>
<div class="box">
<div class="child-0"></div>
<div class="child-1">XXX XXXX</div>
</div>
</pre>
<pre class="lang-javascript">
registerLayout('intrinsic-sizes-example', class {
async intrinsicSizes(children, edges, styleMap) {
const childrenSizes = await Promise.all(children.map((child) => {
return child.intrinsicSizes();
}));
childrenSizes[0].minContentSize; // 400, (380+10+10) child has a fixed size.
childrenSizes[0].maxContentSize; // 400, (380+10+10) child has a fixed size.
childrenSizes[1].minContentSize; // 100, size of "XXXX".
childrenSizes[1].maxContentSize; // 200, size of "XXX XXXX".
}
layout() {}
});
</pre>
</div>
<div algorithm>
When the user agent wants to <dfn>create an intrinsic sizes object</dfn> given
|intrinsicSizesRequest|, and |internalIntrinsicSizes|, it <em>must</em> run the following steps:
1. Let |intrinsicSizes| be a new {{IntrinsicSizes}} with:
- The {{[[intrinsicSizesRequest]]}} internal slot being |intrinsicSizesRequest|.
- {{IntrinsicSizes/minContentSize}} being |internalIntrinsicSizes|' <b>border box</b>
min-content contribution, relative to the <a>current layout's</a> writing mode.
- {{IntrinsicSizes/maxContentSize}} being |internalIntrinsicSizes|'s <b>border box</b>
max-content contribution, relative to the <a>current layout's</a> writing mode.
2. Return |intrinsicSizes|.
</div>
Layout Constraints {#layout-constraints}
----------------------------------------
<pre class='idl'>
[Constructor(optional LayoutConstraintsOptions options),Exposed=LayoutWorklet]
interface LayoutConstraints {
readonly attribute double availableInlineSize;
readonly attribute double availableBlockSize;
readonly attribute double? fixedInlineSize;
readonly attribute double? fixedBlockSize;
readonly attribute double percentageInlineSize;
readonly attribute double percentageBlockSize;
readonly attribute double? blockFragmentationOffset;
readonly attribute BlockFragmentationType blockFragmentationType;
readonly attribute any data;
};
dictionary LayoutConstraintsOptions {
double availableInlineSize = 0;
double availableBlockSize = 0;
double fixedInlineSize;
double fixedBlockSize;
double percentageInlineSize;
double percentageBlockSize;
double blockFragmentationOffset;
BlockFragmentationType blockFragmentationType = "none";
any data;
};
enum BlockFragmentationType { "none", "page", "column", "region" };
</pre>
A {{LayoutConstraints}} object is passed into the layout method which represents the all the
constraints for the <a>current layout</a> to perform layout inside. It is also used to pass
information about the available space into a <a>child layout</a>.
The {{LayoutConstraints}} object has {{LayoutConstraints/availableInlineSize}} and
{{LayoutConstraints/availableBlockSize}} attributes. This represents the <a>available space</a> for
a {{LayoutFragment}} which the layout should respect.
Note: Some layouts may need to produce a {{LayoutFragment}} which exceed this size. For example a
<a>replaced element</a>. The <a>parent layout</a> should expect this to occur and deal with it
appropriately.
A <a>parent layout</a> may require the <a>current layout</a> to be exactly a particular size. If
the {{LayoutConstraints/fixedInlineSize}} or {{LayoutConstraints/fixedBlockSize}} are specified the
<a>current layout</a> should produce a {{LayoutFragment}} with a the specified size in the
appropriate direction.
<div class="example">
The layout algorithm performs a flexbox-like distribution of spare space in the inline direction. It
creates child layout constraints which specify that a child should be a fixed inline size.
<pre class="lang-javascript">
registerLayout('flex-distribution-like', class {
async intrinsicSizes(children, edges, styleMap) {
const childrenSizes = children.map((child) => {
return child.intrinsicSizes();
});
const maxContentSize = childrenSizes.reduce((sum, childSizes) => {
return sum + childSizes.maxContentSize;
}, 0) + edges.all.inline;
const minContentSize = childrenSizes.reduce((max, childSizes) => {
return sum + childSizes.minContentSize;
}, 0) + edges.all.inline;
return {maxContentSize, minContentSize};
}
async layout(children, edges, constraints, styleMap) {
// Determine our (inner) available size.
const availableInlineSize =
constraints.fixedInlineSize - edges.all.inline;
const availableBlockSize = constraints.fixedBlockSize ?
constraints.fixedBlockSize - edges.all.block : null;
const childConstraints = { availableInlineSize, availableBlockSize };
const unconstrainedChildFragments = await Promise.all(children.map((child) => {
return child.layoutNextFragment(childConstraints);
}));
const unconstrainedSizes = [];
const totalSize = unconstrainedChildFragments.reduce((sum, fragment, i) => {
unconstrainedSizes[i] = fragment.inlineSize;
return sum + fragment.inlineSize;
}, 0);
// Distribute spare space between children.
const remainingSpace = Math.max(0, inlineSize - totalSize);
const extraSpace = remainingSpace / children.length;
const childFragments = await Promise.all(children.map((child, i) => {
return child.layoutNextFragment({
fixedInlineSize: unconstrainedSizes[i] + extraSpace,
availableBlockSize
});
}));
// Position the fragments.
let inlineOffset = 0;
let maxChildBlockSize = 0;
for (let fragment of childFragments) {
fragment.inlineOffset = inlineOffset;
fragment.blockOffset = edges.all.blockStart;
inlineOffset += fragment.inlineSize;
maxChildBlockSize = Math.max(maxChildBlockSize, fragment.blockSize);
}
return {
autoBlockSize: maxChildBlockSize + edges.all.block,
childFragments,
};
}
});
</pre>
</div>
The {{LayoutConstraints}} object has {{LayoutConstraints/percentageInlineSize}} and
{{LayoutConstraints/percentageBlockSize}} attributes. These represent the size that a layout
percentages should be resolved against while performing layout.
The {{LayoutConstraints}} has a {{LayoutConstraints/blockFragmentationType}} attribute. The
<a>current layout</a> should produce a {{LayoutFragment}} which fragments at the
{{LayoutConstraints/blockFragmentationOffset}} if possible.
The <a>current layout</a> can choose not to fragment a {{LayoutChild}} based on the
{{LayoutConstraints/blockFragmentationType}}, for example if the child has a property like
''break-inside: avoid-page;''.
<div algorithm>
When the user agent wants to <dfn>create a layout constraints object</dfn> given |sizingMode|, box|,
and |internalLayoutConstraints|, it <em>must</em> run the following steps:
1. If |sizingMode| is <code>"block-like"</code> then:
1. Let |fixedInlineSize| be the result of calculating |box|'s <b>border-box</b>
<a>inline size</a> (relative to |box|'s writing mode) exactly like block containers do.
2. Let |fixedBlockSize| be null if |box|'s <a>block size</a> is ''height/auto'', otherwise
the result of calculating |box|'s <b>border-box</b> <a>block size</a> exactly like block
containers do.
3. Return a new {{LayoutConstraints}} object with:
- {{LayoutConstraints/fixedInlineSize}}, and {{LayoutConstraints/availableInlineSize}}