forked from w3c/css-houdini-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverview.bs
1556 lines (1145 loc) · 61.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: 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:dom; type:dfn; for:/; text:element
spec:infra; type:dfn; text:list
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
</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/box.html#; type: dfn
url: box-dimensions; text: box model edges
</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</a> tree 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.
<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''.
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;
};
</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.
<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,
});
let blockOffset = edges.all.blockStart;
const childFragments = yeild children.map((child) => {
return child.layoutNextFragment(childConstraintSpace);
});
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 contentSize = blockOffset + edges.all.blockEnd;
const blockSize = resolveBlockSize(
constraintSpace, styleMap, contentSize);
return {
inlineSize: inlineSize,
blockSize: blockSize,
childFragments: childFragments,
};
}
});
</pre>
</div>
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 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;
BlockFragmentationType blockFragmentationType = "none";
any data = null;
};
enum BlockFragmentationType { "none", "page", "column", "region" };
</pre>
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", "inline", "inline-hyphen", "column", "page", "region" };
</pre>
Issue: Fill out other inline type break types.
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 displayType 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 inline layout which places child fragments in the inline direction.
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('basic-inline', class {
static displayType = 'normal';
*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;
const childFragments = [];
let currentLine = [];
let usedInlineSize = 0;
let lineOffset = 0;
let maxLineBlockSize = 0;
// Just a small little function which will update the above variables.
const nextLine = function() {
currentLine = [];
usedInlineSize = 0;
lineOffset += maxLineBlockSize;
maxLineBlockSize = 0;
}
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 child = children.shift();
while (child) {
// Make sure we actually have space on the current line.
if (usedInlineSize > availableInlineSize) {
nextLine();
}
// The constraint space here will have the inline size of the
// remaining space on the line.
const remainingInlineSize = availableInlineSize - usedInlineSize;
const constraintSpace = new ConstraintSpace({
inlineSize: availableInlineSize - usedInlineSize,
blockSize: availableBlockSize,
percentageInlineSize: availableInlineSize,
});
const fragment = yield child.layoutNextFragment(constraintSpace,
childBreakToken);
childFragments.push(fragment);
// Check if there is still space on the current line.
if (fragment.inlineSize > remainingInlineSize) {
nextLine();
// Check if we have gone over the block fragmentation limit.
if (constraintSpace.blockFragmentationType != 'none' &&
lineOffset > constraintSpace.blockSize) {
break;
}
}
// Insert fragment on the current line.
currentLine.push(fragment);
fragment.inlineOffset = usedInlineSize;
// Go through each of the fragments on the line and update their
// block offsets.
for (let fragmentOnLine of currentLine) {
fragmentOnLine.blockOffset = lineOffset;
const lineBlockSize =
fragmentOnLine.blockOffset + fragmentOnLine.blockSize;
if (maxLineBlockSize < lineBlockSize) {
maxLineBlockSize = lineBlockSize;
}
}
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;
}
}
// Determine our block size.
nextLine();
const contentSize = lineOffset + edges.all.block;
const blockSize = resolveBlockSize(space,
styleMap,
contentSize);
// 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 contentSize);
};
</pre>
Issue: Specify the behaviour of these functions.
Interactions with other Modules {#interactions-with-other-modules}
==================================================================
This section describes how other CSS modules interact with the CSS Layout API.
Sizing {#interaction-sizing}
----------------------------
Positioning {#interaction-positioning}
--------------------------------------
Overflow {#interaction-overflow}
--------------------------------
Fragmentation {#interaction-fragmentation}
------------------------------------------
Alignment {#interaction-alignment}
----------------------------------
Layout {#layout}
================
This section describes how the CSS Layout API interacts with the user agent's layout engine.
Layout Invalidation {#layout-invalidation}
------------------------------------------
A <a>document</a> has an associated <dfn>layout name to input properties map</dfn> and a <dfn>layout
name to child input properties map</dfn>. Initially these maps are empty and are populated when
{{registerLayout(name, layoutCtor)}} is called.
Each <a>box</a> has an associated <dfn>layout valid flag</dfn>. It may be either
<dfn>layout-valid</dfn> or <dfn>layout-invalid</dfn>. It is initially set to <a>layout-invalid</a>.
TODO comments. <dfn>intrinsic sizes valid flag</dfn>. <dfn>intrinsic-sizes-valid</dfn>.
<dfn>intrinsic-sizes-invalid</dfn>.
Issue: The above flag is too restrictive on user agents, change.
When the computed style for a |box| changes, the user agent must run the following steps:
1. Let |layoutFunction| be the ''layout()'' function of the 'display' property on the computed
style for the |box| if it exists. If it is a different type of value (e.g. ''grid'') then
abort all these steps.
2. Let |name| be the first argument of the |layoutFunction|.
3. Let |inputProperties| be the result of looking up |name| on <a>layout name to input
properties map</a>.
4. Let |childInputProperties| be the result of looking up |name| on <a>layout name to child
input properties map</a>.
5. For each |property| in |inputProperties|, if the |property|'s <a>computed value</a> has
changed, set the <a>layout valid flag</a> on the <a>box</a> to <a>layout-invalid</a>.
6. For each |property| in |childInputProperties|, if the |property|'s <a>computed value</a> has
changed, set the <a>layout valid flag</a> on the <a>box</a> to <a>layout-invalid</a>.
When a child <a>box</a> represented by a {{LayoutChild}} is added or removed from the <a>box
tree</a> or has its layout invalidated (from a computed style change, or a descentdant change). Set
the <a>layout valid flag</a> on the current <a>box</a> to <a>layout-invalid</a>.
Note: This only describes layout invalidation as it relates to the CSS Layout API. All
<a>boxes</a> conceptually have a <a>layout valid flag</a> and these changes are propagated
through the <a>box tree</a>.
Concepts {#concepts}
--------------------
A <dfn>layout definition</dfn> is a <a>struct</a> which describes the information needed by the
{{LayoutWorkletGlobalScope}} about hte 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 <a>constructor</a>.
- <dfn for="layout definition">layout generator function</dfn> which is the layout <a>generator
function</a> callback.
- <dfn for="layout definition">intrinsic sizes generator function</dfn> which is the intrinsic sizes
<a>generator function</a> callback.
- <dfn for="layout definition">constructor valid flag</dfn>.
- <dfn for="layout definition">input properties</dfn> which is a <a>list</a> of
<code>DOMStrings</code>
- <dfn for="layout definition">child input properties</dfn> which is a <a>list</a> of
<code>DOMStrings</code>.
- <dfn for="layout definition">child display</dfn> a {{ChildDisplayType}}.
A <dfn>document layout definition</dfn> is a <a>struct</a> which describes the information needed by
the <a>document</a> 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 <a>list</a> of
<code>DOMStrings</code>
- <dfn for="document layout definition">child input properties</dfn> which is a <a>list</a> of
<code>DOMStrings</code>.
- <dfn for="document layout definition">child display</dfn> a {{ChildDisplayType}}.
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 interface 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]
enum ChildDisplayType {
"block",
"normal",
};
</pre>
Issue: "normal" is a bad name?
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.
The {{LayoutWorkletGlobalScope}} 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 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, <a>throw</a> a <a>TypeError</a> and abort all these steps.
2. Let |layoutDefinitionMap| be {{LayoutWorkletGlobalScope}}'s <a>layout definitions</a> map.
3. If |layoutDefinitionMap|[|name|] <a for=map>exists</a> <a>throw</a> a
"<a>InvalidModificationError</a>" <a>DOMException</a> and abort all these steps.
4. Let |inputProperties| be an empty <code>sequence<DOMString></code>.
5. Let |inputPropertiesIterable| be the result of <a>Get</a>(|layoutCtor|, "inputProperties").
6. If |inputPropertiesIterable| is not undefined, then set |inputProperties| to the result of
<a>converting</a> |inputPropertiesIterable| to a <code>sequence<DOMString></code>. If an
exception is <a>thrown</a>, rethrow the exception and abort all these steps.
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>.
7. Let |childInputProperties| be an empty <code>sequence<DOMString></code>.
8. Let |childInputPropertiesIterable| be the result of <a>Get</a>(|layoutCtor|,
"childInputProperties").
9. If |childInputPropertiesIterable| is not undefined, then set |childInputProperties| to the
result of <a>converting</a> |childInputPropertiesIterable| to a
<code>sequence<DOMString></code>. If an exception is <a>thrown</a>, rethrow the exception
and abort all these steps.
10. Let |childDisplay| be a {{ChildDisplayType}} set to <code>"block"</code>.
11. Let |childDisplayValue| be the result of <a>Get</a>(|layoutCtor|, "childDisplay").
12. If |childDisplayValue| if not undefined, then then |childDisplay| to the result of
<a>converting</a> |childDisplayValue| to a {{ChildDisplayType}}. If an exception is
<a>thrown</a>, rethrow the exception and abort all these steps.
13. If the result of <a>IsConstructor</a>(|layoutCtor|) is false, <a>throw</a> a
<a>TypeError</a> and abort all these steps.
14. Let |prototype| be the result of <a>Get</a>(|layoutCtor|, "prototype").
15. If the result of <a>Type</a>(|prototype|) is not Object, <a>throw</a> a <a>TypeError</a> and
abort all these steps.
16. Let |layout| be the result of <a>Get</a>(|prototype|, <code>"layout"</code>).
17. If the result of <a>IsCallable</a>(|layout|) is false, <a>throw</a> a <a>TypeError</a> and
abort all these steps.
18. If |layout|'s <code>\[[FunctionKind]]</code> internal slot is not <code>"generator"</code>,
<a>throw</a> a <a>TypeError</a> and abort all these steps.
19. Let |intrinsicSizes| be the result of <a>Get</a>(|prototype|,
<code>"intrinsicSizes"</code>).
20. If the result of <a>IsCallable</a>(|intrinsicSizes|) is false, <a>throw</a> a
<a>TypeError</a> and abort all these steps.
21. If |intrinsicSizes|'s <code>\[[FunctionKind]]</code> internal slot is not
<code>"generator"</code>, <a>throw</a> a <a>TypeError</a> and abort all these steps.
22. Let |definition| be a new <a>layout definition</a> with:
- <a>class constructor</a> being |layoutCtor|.
- <a>layout generator function</a> being |layout|.
- <a>intrinsic sizes generator function</a> being |intrinsicSizes|.
- <a>constructor valid flag</a> being <b>true</b>.
- <a for="layout definition">input properties</a> being |inputProperties|.
- <a for="layout definition">child input properties</a> being |childInputProperties|.
- <a for="layout definition">child display</a> being |childDisplay|.
23. <a for=map>Set</a> |layoutDefinitionMap|[|name|] to |definition|.
24. <a>Queue a task</a> to run the following steps:
1. Let |documentLayoutDefinitionMap| be the associated <a>document's</a> <a>document layout
definitions</a> <a>map</a>.
2. Let |documentDefinition| be a new <a>document layout definition</a> with: