forked from w3c/css-houdini-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverview.bs
1843 lines (1377 loc) · 73.2 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: DREAM
Group: houdini
ED: https://drafts.css-houdini.org/css-layout-api-1/
Shortname: css-layout-api
Level: 1
Abstract:
Editor: Greg Whitworth, gwhit@microsoft.com
Editor: Ian Kilpatrick, ikilpatrick@chromium.org
Editor: Tab Atkins, jackalmage@gmail.com
Editor: Shane Stephens, shanestephens@google.com
Editor: Robert O'Callahan, robert@ocallahan.org
Editor: Rossen Atanassov, rossen.atanassov@microsoft.com
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:max-width
text:min-height
text:min-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: IsArray
text: IsCallable
text: IsConstructor
text: HasProperty
url: ecmascript-data-types-and-values; text: type
url: get-o-p; text: Get
url: generatorfunction; text: generator function
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}
==============================================
<pre class="propdef">
Name: <<display-inside>>
New values: layout(<<ident>>)
</pre>
<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 auther 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 repect the 'margin' property on children.
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">child display</a> (set by <code>childDisplay</code> on the class).
If the value of <a for="document layout definition">child display</a> 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">child display</a> 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 {{Fragment}} 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 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</a>'s 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();
FragmentRequest layoutNextFragment(ConstraintSpace space, ChildBreakToken breakToken);
};
</pre>
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 {{Fragment}}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).
The {{LayoutChild}} has computed style which can be accessed by {{LayoutChild/styleMap}}. The
{{LayoutChild/styleMap}} will only contain properties which are listed in the child input properties
array.
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 {{Fragment}} but is from
three non-<a>atomic inlines</a>:
<pre class="lang-html">
ع<span style="color: blue">ع</span>ع
</pre>
</div>
Note: In a future level of the specification there may be a way to query the computed style
of <a>inline boxes</a> inside a <a>root inline box</a> represented by a {{LayoutChild}}.
An array of {{LayoutChild}}ren is passed into the layout method which represents the children of the
current box which is being laid out.
To perform layout on a box the author can invoke the {{LayoutChild/layoutNextFragment()}} method.
This will produce a {{Fragment}} which contains layout information.
The {{LayoutChild/layoutNextFragment()}} method may be invoked multiple times with different
arguments to query the {{LayoutChild}} for different layout information.
Layout Fragments {#layout-fragments}
------------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface Fragment {
readonly attribute double inlineSize;
readonly attribute double blockSize;
attribute double inlineOffset;
attribute double blockOffset;
readonly attribute any data;
readonly attribute ChildBreakToken? breakToken;
};
/* NOTE EVERYTHING BELOW HERE IS SPECULATIVE */
interface LineFragment : Fragment {
readonly attribute sequence<LineFragmentSegment> segments;
};
interface LineFragmentSegment {
readonly attribute LineFragmentSegmentType type;
readonly attribute LineFragmentSegmentBreakType breakType;
readonly attribute FontMetrics? metrics;
readonly attribute double inlineOffset;
};
enum LineFragmentSegmentType {
"word-break",
"atomic-inline",
/* TODO add others here, not sure yet. */
};
enum LineFragmentSegmentBreakType {
"something"
/* TODO add others here, not sure yet. */
};
</pre>
A {{Fragment}} 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 {{Fragment}} has {{Fragment/inlineSize}} and {{Fragment/blockSize}} attributes, which are set by
the respective child's layout algorithm. They cannot be changed. If the <a>current layout</a>
requires a different {{Fragment/inlineSize}} or {{Fragment/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 {{Fragment}} by setting its
{{Fragment/inlineOffset}} and {{Fragment/blockOffset}} attributes. If not set by the author they
default to zero. The {{Fragment/inlineOffset}} and {{Fragment/blockOffset}} attributes represent the
position of the {{Fragment}} relative to its parent's border box, 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 {
*intrinsicSizes(styleMap, children) {
const childrenSizes = yield children.map((child) => {
return child.intrinsicSizes();
});
const maxContentSize = childrenSizes.reduce((max, childSizes) => {
return Math.max(max, childSizes.maxContentContribution);
}, 0);
const minContentSize = childrenSizes.reduce((max, childSizes) => {
return Math.max(max, childSizes.minContentContribution);
}, 0);
return {maxContentSize, minContentSize};
}
*layout(space, children, styleMap, edges) {
const inlineSize = resolveInlineSize(space, styleMap);
const availableInlineSize = inlineSize - edges.all.inline;
const availableBlockSize =
resolveBlockSize(constraintSpace, styleMap) - edges.all.block;
const childFragments = [];
const childConstraintSpace = new ConstraintSpace({
inlineSize: availableInlineSize,
blockSize: availableBlockSize,
});
const childFragments = yeild children.map((child) => {
return child.layoutNextFragment(childConstraintSpace);
});
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;
const blockSize = resolveBlockSize(
constraintSpace, styleMap, autoBlockSize);
return {
inlineSize: inlineSize,
blockSize: blockSize,
childFragments: childFragments,
};
}
});
</pre>
</div>
A <a>layout API container</a> can communicate with other <a>layout API containers</a> by using the
{{Fragment/data}} attribute. This is set by the {{FragmentResultOptions/data}} member in the
{{FragmentResultOptions}} dictionary.
The {{Fragment}}'s {{Fragment/breakToken}} specifies where the {{LayoutChild}} last fragmented. If
the {{Fragment/breakToken}} is null the {{LayoutChild}} wont produce any more {{Fragment}}s for that
token chain. The {{Fragment/breakToken}} can be passed to the {{LayoutChild/layoutNextFragment()}}
function to produce the next {{Fragment}} for a particular child. The {{Fragment/breakToken}} cannot
be changed.
If the <a>current layout</a> requires a different {{Fragment/breakToken}} the author must perform
{{LayoutChild/layoutNextFragment()}} again with different arguments.
Note: In a future level of the specification there may be a way to query for additional baseline
information, for example where the alphabetic or center baseline is positioned.
Constraint Spaces {#constraint-spaces}
--------------------------------------
<pre class='idl'>
[Constructor(optional ConstraintSpaceOptions options),Exposed=LayoutWorklet]
interface ConstraintSpace {
readonly attribute double inlineSize;
readonly attribute double blockSize;
readonly attribute boolean inlineSizeFixed;
readonly attribute boolean blockSizeFixed;
readonly attribute double percentageInlineSize;
readonly attribute double percentageBlockSize;
readonly attribute double? blockFragmentationOffset;
readonly attribute BlockFragmentationType blockFragmentationType;
readonly attribute any data;
};
dictionary ConstraintSpaceOptions {
double inlineSize = Infinity;
double blockSize = Infinity;
boolean inlineSizeFixed = false;
boolean blockSizeFixed = false;
double? percentageInlineSize = null;
double? percentageBlockSize = null;
double? blockFragmentationOffset = null;
BlockFragmentationType blockFragmentationType = "none";
any data = null;
};
enum BlockFragmentationType { "none", "page", "column", "region" };
</pre>
<div class="issue">
Issue: Should {{ConstraintSpaceOptions}} be instead:
<pre class="lang-javascript">
const options = {
availableInlineSize: 100,
availableBlockSize: Infinity,
fixedInlineSize: 150, // This makes the engine ignore availableInlineSize.
fixedBlockSize: 150, // This makes the engine ignore availableBlockSize.
percentageInlineSize: 100, // This defaults to fixedInlineSize,
// then availableInlineSize if not set.
percentageBlockSize: 100, // This defaults to fixedBlockSize,
// then availableBlockSize if not set.
blockFragmentationOffset: 200,
blockFragmentationType: 'column',
data: {floatPositions: [{x: 20, y: 30}]}, // Author data.
};
</pre>
The constraint space would mirror this, additionally with the blockSize, and inlineSize attributes
which represent the resolved inline and block sizes for the fragment.
</div>
A {{ConstraintSpace}} is passed into the layout method which represents the available space 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 {{ConstraintSpace}} has {{ConstraintSpace/inlineSize}} and {{ConstraintSpace/blockSize}}
attributes. This represents the <a>available space</a> for a {{Fragment}} which the layout should
respect.
Note: Some layouts may need to produce a {{Fragment}} 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 {{ConstraintSpace/inlineSizeFixed}} or {{ConstraintSpace/blockSizeFixed}} are true the
<a>current layout</a> should produce a {{Fragment}} with a fixed 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 constraint spaces which specify that a child should be a fixed inline size.
<pre class="lang-javascript">
registerLayout('flex-distribution-like', class {
*intrinsicSizes(styleMap, children) {
const childrenSizes = yield children.map((child) => {
return child.intrinsicSizes();
});
const maxContentSize = childrenSizes.reduce((sum, childSizes) => {
return sum + childSizes.maxContentContribution;
}, 0);
const minContentSize = childrenSizes.reduce((max, childSizes) => {
return sum + childSizes.minContentContribution;
}, 0);
return {maxContentSize, minContentSize};
}
*layout(space, children, styleMap, edges, breakToken) {
const inlineSize = resolveInlineSize(space, styleMap);
const availableInlineSize = inlineSize - edges.all.inline;
const availableBlockSize =
resolveBlockSize(space, styleMap) - edges.all.block;
const childConstraintSpace = new ConstraintSpace({
inlineSize: availableInlineSize,
blockSize: availableBlockSize,
});
const unconstrainedChildFragments = yield children.map((child) => {
return child.layoutNextFragment(childConstraintSpace);
});
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 = yield children.map((child, i) => {
return child.layoutNextFragment(new ConstraintSpace({
inlineSize: unconstrainedSizes[i] + extraSpace,
inlineSizeFixed: true,
blockSize: 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);
}
// Resolve our block size.
const blockSize = resolveBlockSize(space, styleMap, maxChildBlockSize);
return {
inlineSize: inlineSize,
blockSize: blockSize,
childFragments: childFragments,
};
}
});
</pre>
</div>
The {{ConstraintSpace}} has {{ConstraintSpace/percentageInlineSize}} and
{{ConstraintSpace/percentageBlockSize}} attributes. These represent the size that a layout
percentages should be resolved against while performing layout.
The {{ConstraintSpace}} has a {{ConstraintSpace/blockFragmentationType}} attribute. The <a>current
layout</a> should produce a {{Fragment}} which fragments at the {{ConstraintSpace/blockSize}} if
possible.
The <a>current layout</a> may choose not to fragment a {{LayoutChild}} based on the
{{ConstraintSpace/blockFragmentationType}}, for example if the child has a property like
''break-inside: avoid-page;''.
Breaking and Fragmentation {#breaking-and-fragmentation}
--------------------------------------------------------
<pre class="idl">
[Exposed=LayoutWorklet]
interface ChildBreakToken {
readonly attribute BreakType breakType;
readonly attribute LayoutChild child;
};
[Exposed=LayoutWorklet]
interface BreakToken {
readonly attribute sequence<ChildBreakToken> childBreakTokens;
readonly attribute any data;
};
dictionary BreakTokenOptions {
sequence<ChildBreakToken> childBreakTokens;
any data = null;
};
enum BreakType { "none", "line", "column", "page", "region" };
</pre>
A {{LayoutChild}} can produce multiple {{Fragment}}s. A {{LayoutChild}} may fragment in the block
direction if a {{ConstraintSpace/blockFragmentationType}} is not none. Additionally {{LayoutChild}}
which represents <a>inline-level</a> content, may fragment line by line if the <a for="document
layout definition">child display</a> (set by <code>childDisplay</code>) is <code>"normal"</code>.
A subsequent {{Fragment}} is produced by using the previous {{Fragment}}'s {{Fragment/breakToken}}.
This tells the <a>child layout</a> to produce a {{Fragment}} starting at the point encoded in the
{{ChildBreakToken}}.
Issue: Explain resuming the author defined layout.
<div class="example">
This example shows a simple layout which places indents child fragments for a certain number of
lines.
This example also demonstrates using the previous {{Fragment/breakToken}} of a {{Fragment}} to
produce the next fragment for the {{LayoutChild}}.
It also demonstrates using the {{BreakToken}} to respect the {{ConstraintSpace}}'s
{{ConstraintSpace/blockFragmentationType}}, it resumes it layout from the previous {{BreakToken}}.
It returns a {{FragmentResultOptions}} with a {{FragmentResultOptions/breakToken}} which is used to
resume the layout.
<pre class="lang-javascript">
registerLayout('indent-lines', class {
static childDisplay = 'normal';
static inputProperties = ['--indent', '--indent-lines'];
*layout(space, children, styleMap, edges, breakToken) {
// Resolve our inline size.
const inlineSize = resolveInlineSize(space, styleMap);
// Determine our (inner) available size.
const availableInlineSize = inlineSize - edges.all.inline;
const availableBlockSize =
resolveBlockSize(space, styleMap) - edges.all.block;
// Detrermine the number of lines to indent, and the indent amount.
const indent = resolveLength(space, styleMap.get('--indent'));
let lines = styleMap.get('--indent-lines').value;
const childFragments = [];
let childBreakToken = null;
if (breakToken) {
childBreakToken = breakToken.childBreakTokens[0];
// Remove all the children we have already produced fragments for.
children.splice(0, children.indexOf(childBreakToken.child));
}
let blockOffset = edges.all.blockStart;
let child = children.shift();
while (child) {
const shouldIndent = lines-- > 0;
// Adjust the inline size for the indent.
const childAvailableInlineSize = shouldIndent ?
availableInlineSize - indent : availableInlineSize;
const childSpace = new ConstraintSpace({
inlineSize: childAvailableInlineSize,
blockSize: availableBlockSize,
percentageInlineSize: availableInlineSize,
blockFragmentationType: space.blockFragmentationType,
});
const fragment = yield child.layoutNextFragment(childSpace,
childBreakToken);
childFragments.push(fragment);
// Position the fragment.
fragment.inlineOffset = shouldIndent ?
edges.all.inlineStart + indent : edges.all.inlineStart;
fragment.blockOffset = blockOffset;
blockOffset += fragment.blockSize;
// Check if we have gone over the block fragmentation limit.
if (constraintSpace.blockFragmentationType != 'none' &&
blockOffset > constraintSpace.blockSize) {
break;
}
if (fragment.breakToken) {
childBreakToken = fragment.breakToken;
} else {
// If a fragment doesn't have a break token, we move onto the
// next child.
child = children.shift();
childBreakToken = null;
}
}
const autoBlockSize = blockOffset + edges.all.blockEnd;
const blockSize = resolveBlockSize(space,
styleMap,
autoBlockSize);
// Return our fragment.
const result = {
inlineSize: inlineSize,
blockSize: blockSize,
childFragments: childFragments,
}
if (childBreakToken) {
result.breakToken = {
childBreakTokens: [childBreakToken],
};
}
return result;
}
});
</pre>
</div>
Edges {#edges}
--------------
<pre class='idl'>
[Exposed=LayoutWorklet]
interface LayoutEdgeSizes {
readonly attribute double inlineStart;
readonly attribute double inlineEnd;
readonly attribute double blockStart;
readonly attribute double blockEnd;
// Convenience attributes for the sum in one direction.
readonly attribute double inline;
readonly attribute double block;
};
[Exposed=LayoutWorklet]
interface LayoutEdges {
readonly attribute LayoutEdgeSizes border;
readonly attribute LayoutEdgeSizes scrollbar;
readonly attribute LayoutEdgeSizes padding;
readonly attribute LayoutEdgeSizes all;
};
</pre>
A {{LayoutEdges}} object is passed into the layout method. This represents size of the <a>box
model edges</a> for the current box which is being laid out.
The {{LayoutEdges}} has {{LayoutEdges/border}}, {{LayoutEdges/scrollbar}}, and
{{LayoutEdges/padding}} attributes. Each of these represent the width of their respective edge.
The {{LayoutEdges}} has the {{LayoutEdges/all}} attribute. This is a convenience attribute which
represents the sum of the {{LayoutEdges/border}}, {{LayoutEdges/scrollbar}}, {{LayoutEdges/padding}}
edges.
The {{LayoutEdgeSizes}} object represents the width in CSS pixels of an edge in each of the
<a>abstract dimensions</a> ({{LayoutEdgeSizes/inlineStart}}, {{LayoutEdgeSizes/inlineEnd}},
{{LayoutEdgeSizes/blockStart}}, {{LayoutEdgeSizes/blockEnd}}).
The {{LayoutEdgeSizes/inline}}, and {{LayoutEdgeSizes/block}} on the {{LayoutEdgeSizes}} object are
convenience attributes which represent the sum in that direction.
<div class="example">
This example shows an node styled by CSS, and what its respective {{LayoutEdges}} could contain.
<pre class="lang-html">
<style>
.container {
width: 50px;
height: 50px;
}
.box {
display: layout(box-edges);
padding: 10%;
border: solid 2px;
overflow-y: scroll;
}
</style>
<div class="container">
<div class="box"></div>
</div>
</pre>
<pre class="lang-javascript">
registerLayout('box-edges', class {
*layout(space, children, styleMap, edges, breakToken) {
edges.padding.inlineStart; // 5 (as 10% * 50px = 5px).
edges.border.blockEnd; // 2
edges.scrollbar.inlineEnd; // UA-dependent, may be 0 or >0 (e.g. 16).
edges.all.block; // 14 (2 + 5 + 5 + 2).
}
}
</pre>
</div>
Utility Functions {#utility-functions}
--------------------------------------
<pre class='idl'>
[Exposed=LayoutWorklet]
partial interface LayoutWorkletGlobalScope {
double resolveInlineSize(ConstraintSpace constraintSpace,
StylePropertyMapReadOnly styleMap);
double resolveBlockSize(ConstraintSpace constraintSpace,
StylePropertyMapReadOnly styleMap,
optional double autoBlockSize);
double resolveLength(ConstraintSpace constraintSpace, CSSStyleValue value);
};
</pre>
<div algorithm>
When the <dfn method for=LayoutWorkletGlobalScope>resolveBlockSize(|constraintSpace|, |styleMap|,
|autoBlockSize|)</dfn> method is called, the user agent <em>must</em> run the following steps:
Issue: Define that this algorithm should do exactly what CSS does for block sizes.
|constraintSpace|, |styleMap|, |autoBlockSize|.
</div>
<div algorithm>
When the <dfn method for=LayoutWorkletGlobalScope>resolveLength(|constraintSpace|, |value|)</dfn>
method is called, the user agent <em>must</em> run the following steps:
Issue: Define that this algorithm should do exactly what CSS does for generic lengths.
|constraintSpace|, |value|.
</div>
Interactions with other Modules {#interactions-with-other-modules}
==================================================================
This section describes how other CSS modules interact with the CSS Layout API.
Sizing {#interaction-sizing}
----------------------------
User agents must use the {{ConstraintSpace}} to communicate to the <a>current layout</a> the size
they would like the fragment to be.
If the user agent wishes to force a size on the box, it can use the
{{ConstraintSpace/inlineSizeFixed}} and {{ConstraintSpace/blockSizeFixed}} attributes to do so.
Issue: Do we want the inlineSize to always be fixed? This would remove the resolveInlineSize call,
and just rely on the pre-defined behaviour by the user agent.
The downside to this is that we won't be able to things like MathML in the initial version of
the specifcation, which is able to "bubble" up inline sizes to its parent.
We can't do a similar thing for the blockSize due to fragmentation. E.g. the size of the
fragment which is undergoing fragmentation, isn't able to be automatically resolved.
If the <a>layout API container</a> is within a <a>block formatting context</a>, is inflow, and has
an ''width/auto'' inline size, the user agent <em>must</em> set the {{ConstraintSpace/inlineSize}}
to the <a>stretch-fit inline size</a>.
<div class="note">
Note: In the example below the <a>layout API container</a> has its inline size set to 50.
<pre class="lang-html">
<style>
#container {
width: 100px;
height: 100px;
box-sizing: border-box;
padding: 5px;
}
#layout-api {
display: layout(foo);
margin: 0 20px;
}
</style>
<div id="container">
<div id="layout-api"></div>
</div>
</pre>
</div>
### Positioned layout sizing ### {#interaction-sizing-positiong-layout}
If a <a>layout API container</a> is out-of-flow positioned the user agent <em>must</em> solve the
positioned size equations ([[css-position-3#abs-non-replaced-width]],
[[css-position-3#abs-non-replaced-height]]), and set the appropriate fixed
{{ConstraintSpace/inlineSize}} and {{ConstraintSpace/blockSize}}.
<div class="note">
Note: In the example below the <a>layout API container</a> has its inline and block size fixed
to 80.
<pre class="lang-html">
<style>
#container {
position: relative;
width: 100px;
height: 100px;
}
#layout-api {
display: layout(foo);
top: 10px;
bottom: 10px;
left: 10px;
right: 10px;
position: absolute;
}
</style>
<div id="container">
<div id="layout-api"></div>
</div>
</pre>
</div>
Positioning {#interaction-positioning}
--------------------------------------
All positioning in this level of the specification is handled by the user agent.
As a result:
- Out-of-flow children do not appear as {{LayoutChild}}ren.
- <a>Layout API containers</a> establish <a>containing blocks</a> <a
href="https://www.w3.org/TR/CSS2/visudet.html#containing-block-details">exactly like block
containers do</a>. [[!CSS21]]
- The {{Fragment/inlineOffset}} and {{Fragment/blockOffset}} represent the position of the
fragment before any positioning and transforms have occured.
- The <a>static position</a> of an absolutely-positioned child of a <a>layout API container</a> is
set to the <a>inline-start</a>, <a>block-start</a> padding edge of the <a>layout API
container</a>. Auto margins are treated as zero for the child.
<div class="note">
Note: In the example below:
- "child-relative" would be the only child passed to the author's layout. If it was positioned
at ({{Fragment/inlineOffset}} <code>= 20</code>, {{Fragment/blockOffset}} <code> =
30</code>), its final position would be (<code>25</code>, <code>40</code>) as the relative
positioning was handled by the user agent.
- "child-absolute" would not appear as a {{LayoutChild}}, and instead would be laid out and
positioned by the user agent.
- The examples above also apply in a similar way to sticky and fixed positioned children.
<pre class="lang-html">
<style>
#container {
display: layout(foo);
position: relative; /* container is a containing block */
width: 100px;
height: 100px;
}
#child-relative {
position: relative;
left: 5px;
top: 10px;
}
</style>
<div id="container">
<div id="child-relative"></div>
<div id="child-absolute"></div>
</div>
</pre>
</div>
Overflow {#interaction-overflow}
--------------------------------