forked from w3c/css-houdini-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverview.bs
2463 lines (1774 loc) · 96.4 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
Editor: Rossen Atanassov, rossen.atanassov@microsoft.com, w3cid 49885
Former Editor: Shane Stephens, shanestephens@google.com, w3cid 47691
Former Editor: Robert O'Callahan, robert@ocallahan.org
Ignored Terms: LayoutWorklet
</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://tc39.github.io/ecma262/#sec-; type: dfn;
text: constructor
text: Construct
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://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 [=fragments=] from the [=box
tree=].
This specification describes an API which allows developers to layout a [=box=] in response to
computed style and [=box tree=] changes.
For a high level overview of this API, see the <a
href="https://github.com/w3c/css-houdini-drafts/blob/master/css-layout-api/EXPLAINER.md">EXPLAINER</a>.
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 [=layout API container=] box.
</dl>
A <dfn>layout API container</dfn> is the box generated by an element with a <<display-inside>>
[=computed value=] ''layout()''.
A [=layout API container=] 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.
[=Layout API containers=] 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 [=layout API containers=]. 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, if the CSS
Layout API is supported.
<pre class="lang-html">
<!DOCTYPE html>
<style>
@supports (display: layout(centering)) {
.centering-layout { display: layout(centering); }
}
</style>
<div class="centering-layout"></div>
</pre>
</div>
Layout API Container Painting {#painting}
-----------------------------------------
[=Layout API Container=] 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 inflow children of a [=layout API container=] can act in different ways depending on the value
of [=document layout definition/layout options'=] {{LayoutOptions/childDisplay}} (set by
<code>layoutOptions</code> on the class).
If the value of [=document layout definition/layout options'=] {{LayoutOptions/childDisplay}} is
<code>"block"</code> the 'display' value of that child is [=blockified=]. This is similar to
children of [=flex containers=] or [=grid containers=]. See [[!css3-display]].
If the value of [=document layout definition/layout options'=] {{LayoutOptions/childDisplay}} is
<code>"normal"</code>, no [=blockification=] occurs. Instead children with a <<display-outside>>
[=computed value=] of ''inline'' (a [=root inline box=]) 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 [=root inline box=] also have some additional
transformations.
- A [=block-level=] box inside a [=inline-level=] box is [=inlinified=] I.e. its
<<display-outside>> is set to ''inline''.
- A [=float=] inside a [=inline-level=] box is not taken out of flow. Instead it must be treated as
inflow, and be [=inlinified=].
In both of the above cases the children become [=atomic inlines=].
Note: User agents would not perform any "inline splitting" or fragmenting when they encounter a
[=block-level=] box.
<div class="note">
Note: In the example below "inline-span" would be represented as a single {{LayoutChild}} with
both "block" and "float" being [=atomic inlines=].
<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 [=worklet global scope type=] 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 {
undefined registerLayout(DOMString name, VoidFunction layoutCtor);
};
</pre>
<div class='example'>
Web developers can feature detect by:
<pre class='lang-javascript'>
if ('layoutWorklet' in CSS) {
console.log('CSS Layout API available!');
}
</pre>
</div>
Concepts {#concepts}
--------------------
This section describes internal data-structures created when {{registerLayout(name, layoutCtor)}} is
called.
A <dfn>layout definition</dfn> is a [=struct=] which describes the information needed by the
{{LayoutWorkletGlobalScope}} about the author defined layout (which can be referenced by the
''layout()'' function). It consists of:
- <dfn for="layout definition">class constructor</dfn> which is the class [=constructor=].
- <dfn for="layout definition">layout function</dfn> which is the layout [=function=] callback.
- <dfn for="layout definition">intrinsic sizes function</dfn> which is the intrinsic sizes
[=function=] callback.
- <dfn for="layout definition">constructor valid flag</dfn>.
- <dfn for="layout definition">input properties</dfn> which is a [=list=] of
<code>DOMStrings</code>.
- <dfn for="layout definition">child input properties</dfn> which is a [=list=] of
<code>DOMStrings</code>.
- <dfn for="layout definition">layout options</dfn> a {{LayoutOptions}}.
A <dfn>document layout definition</dfn> is a [=struct=] which describes the information needed by
the [=document=] about the author defined layout (which can be referenced by the ''layout()''
function). It consists of:
- <dfn for="document layout definition">input properties</dfn> which is a [=list=] of
<code>DOMStrings</code>
- <dfn for="document layout definition">child input properties</dfn> which is a [=list=] of
<code>DOMStrings</code>.
- <dfn for="document layout definition">layout options</dfn> a {{LayoutOptions}}.
Registering A Layout {#registering-layout}
------------------------------------------
The section describes how a web developer uses {{registerLayout(name, layoutCtor)}} to register a
layout.
<pre class='idl'>
dictionary LayoutOptions {
ChildDisplayType childDisplay = "block";
LayoutSizingMode sizing = "block-like";
};
enum ChildDisplayType {
"block", // default - "blockifies" the child boxes.
"normal",
};
enum LayoutSizingMode {
"block-like", // default - Sizing behaves like block containers.
"manual", // Sizing is specified by the web developer.
};
</pre>
The [=document=] has a [=map=] of <dfn>document layout definitions</dfn>. Initially this map is
empty; it is populated when {{registerLayout(name, layoutCtor)}} is called.
The {{LayoutWorkletGlobalScope}} has a [=map=] of <dfn>layout definitions</dfn>. Initially this map
is empty; it is populated when {{registerLayout(name, layoutCtor)}} is called.
Each [=box=] representing a [=layout API container=] has a [=map=] of <dfn>layout class
instances</dfn>. Initially this map is empty; it is populated when the user agent calls either
[=determine the intrinsic sizes=] or [=generate a fragment=] for a [=box=].
Each [=box=] representing a [=layout API container=] has a <dfn>styleMap</dfn> internal slot.
This is a {{StylePropertyMapReadOnly}} which contains the properties listed in
<code>inputProperties</code>.
The user agent <em>clear</em> the [=styleMap=] internal slot for a [=box=] when:
- The [=computed values=] of [=document layout definition/input properties=] for the [=box=]
changes.
- When the [=box=] is removed from the [=box tree=].
- Every 1000 layout passes.
Note: The above rule exists to ensure that web developers do not rely on being able to store
non-regeneratable state on the {{StylePropertyMapReadOnly}} object.
The 1000 limit was picked as a high upper bound, this limit may improve (downwards) over
time.
<div class='note'>
Note: The shape of the class should be:
<pre class='lang-javascript'>
registerLayout('example', class {
static inputProperties = ['--foo'];
static childInputProperties = ['--bar'];
static layoutOptions = {
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>
The algorithm below is run when the {{registerLayout(name, layoutCtor)}} is called. It notifies the
user agent layout 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 [=Get=](|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 definition/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>
Terminology {#terminology}
--------------------------
We define the following terms to be clear about which layout algorithm (formatting context) we are
talking about.
The <dfn>current layout</dfn> is the layout algorithm for the [=box=] we are currently performing
layout for.
The <dfn>parent layout</dfn> is the layout algorithm for the [=box's=] direct parent, (the layout
algorithm which is requesting the [=current layout=] to be performed).
A <dfn>child layout</dfn> is the layout algorithm for a {{LayoutChild}} of the [=current layout=].
Layout API {#layout-api}
========================
This section describes the objects of the Layout API provided to web developers.
Layout Children {#layout-children}
----------------------------------
A {{LayoutChild}} represents a inflow CSS generated [=box=] before layout has occurred. (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
user agent rendering engine (post style resolution).
An array of {{LayoutChild}}ren is passed into the layout/intrinsicSizes methods which represents the
children of the current box which is being laid out.
<pre class='idl'>
[Exposed=LayoutWorklet]
interface LayoutChild {
readonly attribute StylePropertyMapReadOnly styleMap;
Promise<IntrinsicSizes> intrinsicSizes();
Promise<LayoutFragment> layoutNextFragment(LayoutConstraintsOptions constraints, ChildBreakToken breakToken);
};
</pre>
The {{LayoutChild}} has internal slot(s):
- <dfn attribute for=LayoutChild>\[[box]]</dfn> a CSS [=box=].
- <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>.
- <dfn attribute for=LayoutChild>[[unique id]]</dfn> the [=unique id=] of the current [=layout
api context=]. This slot is used so that a {{LayoutChild}} used outside the current layout
pass is invalid.
The {{LayoutChild/[[styleMap]]}} may be pre-populated when the [=computed value=] for properties
listed in the in [=layout definition/child input properties=] for the {{LayoutChild/[[box]]}}.
<div class=example>
The example below shows the basic usage of a {{LayoutChild}}.
<pre class='lang-javascript'>
registerLayout('example-layout-child', class {
static childInputProperties = ['--foo'];
async layout(children, edges, constraints, styleMap) {
// An array of LayoutChildren is passed into both the layout function,
// and intrinsic sizes function below.
const child = children[0];
// You can query the any properties listed in "childInputProperties".
const fooValue = child.styleMap.get('--foo');
// And perform layout!
const fragment = await child.layoutNextFragment({});
}
async intrinsicSizes(children, edges, styleMap) {
// Or request the intrinsic size!
const childIntrinsicSize = await children[0].intrinsicSizes();
}
});
</pre>
</div>
A {{LayoutChild}} could be generated by:
- An [=element=].
- A [=root inline box=].
- 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 [=anonymous box=]. For example an anonymous box may be inserted as a result of:
- A text node which has undergone [=blockification=]. (Or more generally a [=root inline box=]
which has undergone [=blockification=]).
- 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
[=root inline box=]:
<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-[=atomic inlines=] 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-[=atomic inlines=]:
<pre class="lang-html">
ع<span style="color: blue">ع</span>ع
</pre>
</div>
Note: When accessing the {{LayoutChild/styleMap}} the user agent can create a new
{{StylePropertyMapReadOnly}} if none exists yet.
<div algorithm>
The <dfn attribute for=LayoutChild>styleMap</dfn>, on getting from a {{LayoutChild}} |this|, the
user agent must perform the following steps:
1. If |this|' {{[[styleMap]]}} is null, then:
1. Let |box| be |this|' {{LayoutChild/[[box]]}}.
2. Let |definition| be the result of [=get a layout definition=].
3. Let |childInputProperties| be |definition|'s [=layout definition/child input
properties=].
4. Let |styleMap| be a new {{StylePropertyMapReadOnly}} populated with <em>only</em> the
[=computed values=] for properties listed in |childInputProperties| for |box|.
5. Set |this|' {{LayoutChild/[[styleMap]]}} internal slot to |styleMap|.
Note: If the user agent always pre-populates {{LayoutChild/[[styleMap]]}} then this branch
of the algorithm won't be reached.
2. Return |this|' {{StylePropertyMapReadOnly}} contained in the {{LayoutChild/[[styleMap]]}}
internal slot.
</div>
Note: The {{intrinsicSizes()}} method allows the web developer to query the intrinsic sizes of the
{{LayoutChild}}.
<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 |p| be a new promise.
2. Let |context| be the [=current layout's=] [=layout API context=].
3. If |this|' {{LayoutChild/[[unique id]]}} is not equal to |context|'s [=unique id=], reject
|p| with a "{{InvalidStateError}}" {{DOMException}}, and abort all these steps.
Note: This is to ensure that only {{LayoutChild}}ren passed in as arguments to either the
layout or intrinsicSizes method are used.
4. Let |task| be a new [=layout API work task=] with:
- [=layout api work task/layout child=] being |this|.
- [=layout api work task/task type=] being <code>"intrinsic-sizes"</code>.
- [=layout api work task/promise=] being |p|.
5. [=list/Append=] |task| to |context|'s [=work queue=].
6. Return |p|.
</div>
Note: The {{layoutNextFragment()}} method allows the web developer to produce a {{LayoutFragment}}
for a given {{LayoutChild}} (the result of performing layout).
<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 |p| be a new promise.
2. Let |context| be the [=current layout's=] [=layout API context=].
3. If |this|' {{LayoutChild/[[unique id]]}} is not equal to |context|'s [=unique id=], reject
|p| with a "{{InvalidStateError}}" {{DOMException}}, and abort all these steps.
Note: This is to ensure that only {{LayoutChild}}ren passed in as arguments to either the
layout or intrinsicSizes method are used.
4. If |breakToken|'s {{ChildBreakToken/[[unique id]]} is not equal to |context|'s [=unique id=],
reject |p| with a "{{InvalidStateError}}" {{DOMException}}, and abort all these steps.
5. If |context|'s [=layout API context/mode=] is <code>"intrinsic-sizes"</code>, reject |p| with
a "{{NotSupportedError}}" {{DOMException}}.
Note: This is to ensure that inside a <code>intrinsicSizes</code> callback,
{{LayoutChild/layoutNextFragment()}} cannot be called.
6. Let |task| be a new [=layout API work task=] with:
- [=layout api work task/layout constraints=] being |constraints|.
- [=layout api work task/layout child=] being |this|.
- [=layout api work task/child break token=] being |breakToken|.
- [=layout api work task/task type=] being <code>"layout"</code>.
- [=layout api work task/promise=] being |p|.
7. [=list/Append=] |task| to |context|'s [=work queue=].
8. Return |p|.
</div>
### LayoutChildren and the Box Tree ### {#layout-child-box-tree}
Each [=box=] has a <dfn attribute for=box>\[[layoutChildMap]]</dfn> internal slot, which is a
[=map=] of {{LayoutWorkletGlobalScope}}s to {{LayoutChild}}ren.
Note: [=Get a layout child=] returns a {{LayoutChild}} object for the correct
{{LayoutWorkletGlobalScope}} and creates one if it doesn't exist yet.
<div algorithm="get a layout child">
When the user agent wants to <dfn>get a layout child</dfn> given |workletGlobalScope|, |name|,
|box|, and |uniqueId|, it <em>must</em> run the following steps:
1. Assert that:
- |box| is currently attached to the [=box tree=].
- |box|'s [=containing block=] is a [=layout API container=].
- The [=containing block'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 [=get a layout definition=] given |name|, and
|workletGlobalScope|.
Assert that [=get a layout definition=] 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):
- {{LayoutChild/[[box]]}} set to |box|.
- {{[[styleMap]]}} set to a new {{StylePropertyMapReadOnly}} populated with
<em>only</em> the [=computed values=] for properties listed in
|childInputProperties|.
4. <a for=map>Set</a> |layoutChildMap|[|workletGlobalScope|] to |layoutChild|.
4. Let |layoutChild| be the result of <a for=map>get</a> |layoutChildMap|[|workletGlobalScope|].
5. Set |layoutChild|'s {{LayoutChild/[[unique id]]}} internal slot to |uniqueId|.
6. Return |layoutChild|.
</div>
When a [=box=] is inserted into the [=box tree=] the user agent <em>may</em> pre-populate the
{{[[layoutChildMap]]}} for all {{LayoutWorkletGlobalScope}}s.
When a [=box=] is removed from the [=box tree=] the user agent <em>must</em> clear the
{{[[layoutChildMap]]}}.
The user agent <em>must</em> clear the {{[[layoutChildMap]]}} internal slot every 1000 layout
passes.
Note: The above rule exists to ensure that web developers do not rely on being able to store
non-regeneratable state on the {{LayoutChild}} object.
The 1000 limit was picked as a high upper bound, this limit may improve (downwards) over time.
<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 [=box tree=].
2. If |box|'s [=containing block=] is not a [=layout API container=], abort all these
steps.
3. Let |layoutChildMap| be |box|'s {{[[layoutChildMap]]}}.
4. <a for=map>For each</a> |layoutChild| in |layoutChildMap|:
1. |layoutChild|'s {{[[styleMap]]}} to null.
</div>
When the [=computed values=] of [=document layout definition/child input properties=] for a [=box=]
changes the user agent must run the [=update a layout child style=] algorithm.
Layout Fragments {#layout-fragments}
------------------------------------
A {{LayoutFragment}} represents a CSS [=fragment=] of a {{LayoutChild}} after layout has occurred on
that child. This is produced by the {{LayoutChild/layoutNextFragment()}} method.
<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>[[unique id]]</dfn> the [=unique id=] of the [=layout api
context=] which produced this child fragment. This slot is used so that a {{LayoutFragment}}
from a previous layout pass is invalid.
<hr>
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 [=fragment=], and are relative to the [=current layout's=] writing mode.
The {{LayoutFragment/inlineSize}} and {{LayoutFragment/blockSize}} attributes cannot be changed. If
the [=current layout=] 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 [=relatively positioned=]) has
been applied.
<figure>
<img src="images/layout-fragment-offsets.png" width="800"
alt="An example of position a fragment in different writing modes.">
<figcaption>
A simple visualization showing positioning a {{LayoutFragment}} using
{{LayoutFragment/inlineOffset}} and {{LayoutFragment/blockOffset}} in different writing
modes.
</figcaption>
</figure>
<div class=example>
The example below shows the basic usage of a {{LayoutFragment}}.
<pre class='lang-javascript'>
registerLayout('example-layout-fragment', class {
async layout(children, edges, constraints, styleMap) {
// You must perform layout to generate a fragment.
const fragment = await child.layoutNextFragment({});
// You can query the size of the fragment produced:
console.log(fragment.inlineSize);
console.log(fragment.blockSize);
// You can set the position of the fragment, e.g. this will set it to the
// top-left corner:
fragment.inlineOffset = edges.inlineStart;
fragment.blockOffset = edges.blockStart;
// Data may be passed from the child layout:
console.log(fragment.data);
// If the child fragmented, you can use the breakToken to produce the next
// fragment in the chain.
const nextFragment = await child.layoutNextFragment({}, fragment.breakToken);
}
});
</pre>
</div>
A [=layout API container=] can communicate with other [=layout API containers=] 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 [=current layout=] requires a different {{LayoutFragment/breakToken}} the author must perform
{{LayoutChild/layoutNextFragment()}} again with different arguments.
Intrinsic Sizes {#intrinsic-sizes}
----------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface IntrinsicSizes {
readonly attribute double minContentSize;
readonly attribute double maxContentSize;
};
</pre>
A {{IntrinsicSizes}} object represents the [=min-content size=] and [=max-content size=] of a CSS
[=box=]. It has {{IntrinsicSizes/minContentSize}} and {{IntrinsicSizes/maxContentSize}} attributes
which represent the <b>border box</b> min/max-content contribution of the {{LayoutChild}} for the
[=current layout=]. The attributes are relative to the inline direction of the [=current layout's=]
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>
Layout Constraints {#layout-constraints}
----------------------------------------
A {{LayoutConstraints}} object is passed into the layout method which represents the all the
constraints for the [=current layout=] to perform layout within.
<pre class='idl'>
[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;
};
enum BlockFragmentationType { "none", "page", "column", "region" };
</pre>
The {{LayoutConstraints}} object has {{LayoutConstraints/availableInlineSize}} and
{{LayoutConstraints/availableBlockSize}} attributes. This represents the [=available space=] for the
[=current layout=] to respect.
Note: Some layouts may need to produce a {{LayoutFragment}} which exceed this size. For example a
[=replaced element=]. The [=parent layout=] should expect this to occur and deal with it
appropriately.
A [=parent layout=] may require the [=current layout=] to be exactly a particular size. If the
{{LayoutConstraints/fixedInlineSize}} or {{LayoutConstraints/fixedBlockSize}} are specified the
[=current layout=] should produce a {{LayoutFragment}} with a the specified size in the appropriate
direction.
The {{LayoutConstraints}} object has {{LayoutConstraints/percentageInlineSize}} and
{{LayoutConstraints/percentageBlockSize}} attributes. These represent the size that percentages
should be resolved against while performing layout.
The {{LayoutConstraints}} has a {{LayoutConstraints/blockFragmentationType}} attribute. The
[=current layout=] should produce a {{LayoutFragment}} which fragments at the
{{LayoutConstraints/blockFragmentationOffset}} if possible.
The [=current layout=] 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 class="example">
The example below shows the basic usage of the {{LayoutConstraints}} object.
<pre class="lang-javascript">
// The class below is registered with a "block-like" sizingMode, and can use the fixedInlineSize,
// fixedBlockSize attributes.
registerLayout('layout-constraints-example', class {
async layout(children, edges, constraints, styleMap) {
// Calculate the available size.
const availableInlineSize = constraints.fixedInlineSize - edges.inline;
const availableBlockSize = constraints.fixedBlockSize ?