spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
use std::num::NonZeroUsize;

mod state;

use state::DomFlowTraversalState;

use super::shared::*;
use super::state::{
    AutomaticBlockSizeReplayState, BlockFlowChildTraversalState, ChildFlowTraversalOutcome,
    DomAutomaticBlockSizeReplayCheckpoint,
};
use super::*;
use crate::layout::block::ParentStartClearanceHypothesis;
use crate::layout::inline_collect::TextDecorationPropagationContext;

impl<'a> LayoutBuilder<'a> {
    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn layout_dom_flow_children(
        &mut self,
        fragmentainer_kind: FragmentainerKind,
        element: &Element,
        style: &ComputedStyle,
        stylesheets: &Stylesheets<'_>,
        can_collapse_start_margin: bool,
        can_collapse_end_margin: bool,
        applied_start_margin: LayoutLength,
        start_margin_arrangement: BlockStartMarginArrangement,
        starts_at_page_top: bool,
        has_preceding_inline_flow_content: bool,
        preceding_inline_clamp_block_advance: ContentBoxLength,
        traversal_state: &mut BlockFlowChildTraversalState,
    ) -> ChildFlowTraversalOutcome {
        #[cfg(all(feature = "stack-profile", target_os = "macos"))]
        let mut stack_profile_scope = stack_profile::enter("layout_dom_flow_children");
        let permits_parent_start_collapse =
            start_margin_arrangement.permits_parent_start_collapse();
        // Direct inline content is laid out before this DOM fallback traversal.
        // A following block child can receive the ancestor pseudo only when
        // that earlier source did not provide its first formatted line.
        // <https://drafts.csswg.org/css-pseudo-4/#first-line-pseudo>
        let mut first_formatted_line = FirstFormattedLineState::for_style(style);
        if has_preceding_inline_flow_content {
            first_formatted_line.consume_next_formatted_line();
        }
        // Keep the static-position source separate from the mutable layout
        // cursor. Direct inline content has already advanced that cursor, but
        // an automatic-inset block source is hypothetically placed at the
        // inline run's resolved block position.
        // <https://drafts.csswg.org/css-position-3/#staticpos-rect>
        let out_of_flow_static_capture =
            has_preceding_inline_flow_content.then(|| StaticPositionCapture {
                rectangle: self.block_static_position_rectangle_at(PageTopBlockPosition::new(
                    self.cursor_y - preceding_inline_clamp_block_advance.points(),
                )),
            });
        let vertical_child_inline_origin = style.writing_mode.has_vertical_lines().then(|| {
            self.vertical_child_inline_origin(element, style.writing_mode, style.used_direction())
        });
        let sibling_tags = element_sibling_signature_list(element);
        let text_box_line_trim = self.effective_text_box_line_trim_for_style(style);
        let text_box_trim_start_child = text_box_line_trim
            .trims_block_start
            .then(|| {
                self.text_box_trim_dom_child_node_index(
                    element,
                    &sibling_tags,
                    style,
                    stylesheets,
                    true,
                    false,
                )
            })
            .flatten();
        let text_box_trim_end_child = text_box_line_trim
            .trims_block_end
            .then(|| {
                self.text_box_trim_dom_child_node_index(
                    element,
                    &sibling_tags,
                    style,
                    stylesheets,
                    false,
                    true,
                )
            })
            .flatten();
        // Source snapshots retained for automatic block-boundary marker
        // replay. A following child can overflow before it produces a line;
        // in that case the preceding child's final same-BFC line owns the
        // block ellipsis.
        // <https://drafts.csswg.org/css-overflow-4/#line-clamp-containers>
        let mut dom_state = Box::new(DomFlowTraversalState::new(
            first_formatted_line,
            out_of_flow_static_capture,
            self.float_run_state(),
            style.margin_trim.block_start,
        ));
        while dom_state.child_node_index < element.children.len() {
            #[cfg(all(feature = "stack-profile", target_os = "macos"))]
            stack_profile_scope.set_source_index(dom_state.child_node_index);
            if traversal_state.has_reached_discard_region_limit(self.pages.len()) {
                let child_count = NonZeroUsize::new(dom_state.child_node_index);
                debug_assert!(child_count.is_some(), "a local region break retains source");
                if let Some(child_count) = child_count {
                    traversal_state.capture_discard_source_prefix(child_count);
                } else {
                    traversal_state.mark_local_continuation_cutoff();
                }
                break;
            }
            let replaying_adjoining_target =
                if dom_state.replaying_adjoining_until == Some(dom_state.child_node_index) {
                    dom_state.replaying_adjoining_until = None;
                    self.adjoining_float_origin_y.take();
                    true
                } else {
                    false
                };
            let child = &element.children[dom_state.child_node_index];
            let NodeKind::Element(child_element) = &child.kind else {
                dom_state.child_node_index += 1;
                continue;
            };
            let automatic_replay_before_child =
                dom_state.capture_automatic_marker_checkpoint(self, traversal_state);
            let pending_child_start_candidate = PendingAvoidBreakRunCandidate {
                meta: AvoidBreakRunCandidateMeta {
                    index: dom_state.child_node_index,
                    element_index: dom_state.element_index,
                    previous_flow_bottom_margin: dom_state.previous_flow_bottom_margin,
                    seen_flow_child: dom_state.seen_flow_child,
                    trim_block_start_adjoining_margins: dom_state
                        .trim_block_start_adjoining_margins,
                    collapsed_end_margin: dom_state.collapsed_end_margin,
                    previous_child_page_end: None,
                    float_run: dom_state.float_run,
                    remaining_line_clamp: traversal_state.capture_avoid_replay(),
                    block_extent: layout_pt(0.0),
                },
            };
            let child_signature = ElementSignature::from_sibling_snapshot(
                dom_state.element_index,
                sibling_tags.clone(),
            )
            .expect("source child must have a cached sibling signature");
            dom_state.element_index += 1;
            if is_html_select_item_element(child_element)
                && !has_html_select_context(element, &self.ancestors)
            {
                dom_state.child_node_index += 1;
                continue;
            }
            let mut child_style = Box::new(self.style_for_layout_element_with_parent_font_metrics(
                child_element,
                child_signature.clone(),
                stylesheets,
                Some(style),
            ));
            if child_style.float.layout_role() == FloatLayoutRole::Footnote {
                // Classification normally routes footnote calls through the
                // ordered inline-source path. Retain the same extraction
                // invariant if a specialized DOM traversal reaches one: lay
                // out only its generated call and never its detached body.
                // <https://www.w3.org/TR/css-gcpm-3/#footnote-calls>
                let source = [(dom_state.child_node_index, child.clone())];
                if self.layout_inline_fragment_block_with_first_line_policy(
                    &source,
                    element,
                    style,
                    stylesheets,
                    dom_state.first_formatted_line.applies_to_next_inline_run(),
                ) {
                    dom_state.first_formatted_line.consume_next_formatted_line();
                    dom_state.seen_flow_child.record_in_flow_child();
                    dom_state.previous_flow_bottom_margin = None;
                    self.flush_float_run(&mut dom_state.float_run);
                }
                dom_state.child_node_index += 1;
                continue;
            }
            // Decorations are not inherited computed values, but their line
            // origins continue through this in-flow formatting-context
            // boundary. Materialize that layout-only state before any child
            // sizing, inline collection, or paint dispatch consumes the
            // style.
            // <https://drafts.csswg.org/css-text-decor-4/#line-decoration>
            let decoration_context = TextDecorationPropagationContext::from_style(style);
            *child_style = decoration_context.used_child_style(&child_style);
            if self.positioned_auto_size_child_participation(&child_style)
                == PositionedAutoSizeChildParticipation::ExcludeOutOfFlow
            {
                // The speculative positioned-root measurement owns no static
                // source or positioned output. Skip before this child can
                // alter first-line, break, margin, cursor, or capture state;
                // the committed pass will capture and lay it out normally.
                dom_state.child_node_index += 1;
                continue;
            }
            // A block container's first formatted line is supplied by the
            // first line of its first in-flow block child when it has no
            // preceding inline line of its own. Preserve the originating
            // pseudo while the child starts its own block formatting context.
            // <https://drafts.csswg.org/css-pseudo-4/#first-line-pseudo>
            if dom_state.first_formatted_line.applies_to_next_inline_run()
                && style_is_in_normal_flow(&child_style)
                && child_style.display.is_block_level()
                && let Some(style_with_originating_pseudos) =
                    style_with_originating_typographic_pseudos(&child_style, style)
            {
                *child_style = style_with_originating_pseudos;
            }
            // A descendant that consumes the final remaining line must know
            // about later in-flow source before it selects that line. Compute
            // this from the same cascade inputs as the primary traversal so
            // positioned and floated later siblings cannot spuriously create
            // a block ellipsis.
            let mut later_element_index = dom_state.element_index;
            let has_later_in_flow_child = element.children[dom_state.child_node_index + 1..]
                .iter()
                .any(|candidate| match &candidate.kind {
                    NodeKind::Text(text) => !text.trim().is_empty(),
                    NodeKind::Element(candidate) => {
                        let candidate_signature = ElementSignature::from_sibling_snapshot(
                            later_element_index,
                            sibling_tags.clone(),
                        )
                        .expect("source child must have a cached sibling signature");
                        later_element_index += 1;
                        let candidate_style = self
                            .style_for_layout_element_with_parent_font_metrics(
                                candidate,
                                candidate_signature,
                                stylesheets,
                                Some(style),
                            );
                        style_is_in_normal_flow(&candidate_style)
                    }
                });
            if traversal_state.is_exhausted()
                && (style_is_in_normal_flow(&child_style) || child_style.float != Float::None)
            {
                traversal_state
                    .capture_forced_discard_before_later_child(dom_state.child_node_index);
                // A clamp discards later in-flow source and floats generated
                // after that source boundary. Positioned descendants remain
                // eligible because their containing-block placement is
                // independent of normal-flow continuation.
                dom_state.child_node_index += 1;
                continue;
            }
            let child_shares_clamp_context =
                self.child_shares_line_clamp_formatting_context(child_element, &child_style);
            if child_shares_clamp_context {
                if dom_state.automatic_marker_replay_target == Some(dom_state.child_node_index) {
                    *child_style = traversal_state
                        .automatic_terminal_boundary_style(&child_style)
                        .expect("automatic marker replay requires an active controller");
                } else {
                    traversal_state.apply_to_with_continuation(
                        &mut child_style,
                        BlockFlowChildTraversalState::continuation_for_later_in_flow_source(
                            has_later_in_flow_child,
                        ),
                    );
                    let border = used_border_widths(&child_style);
                    let source_box_envelope = crate::units::content_box_pt(
                        (child_style.margin.top
                            + child_style.margin.bottom
                            + child_style.padding.top
                            + child_style.padding.bottom
                            + border.top
                            + border.bottom)
                            .max(0.0),
                    );
                    BlockFlowChildTraversalState::reserve_automatic_child_non_content(
                        &mut child_style,
                        source_box_envelope,
                    );
                    if has_later_in_flow_child && source_box_envelope.points() > 0.01 {
                        BlockFlowChildTraversalState::require_automatic_terminal_marker_when_full(
                            &mut child_style,
                        );
                    }
                }
            }
            // A following block sibling establishes a clamp point *after*
            // this child; it is not inline overflow inside the child. Passing
            // that fact into the child's inline selector incorrectly paints
            // an ellipsis after a terminal block-in-inline line.
            let child_text_box_line_trim = TextBoxLineTrim {
                trims_block_start: text_box_trim_start_child == Some(dom_state.child_node_index),
                trims_block_end: text_box_trim_end_child == Some(dom_state.child_node_index),
                block_start: if text_box_trim_start_child == Some(dom_state.child_node_index) {
                    text_box_line_trim.block_start
                } else {
                    0.0
                },
                block_end: if text_box_trim_end_child == Some(dom_state.child_node_index) {
                    text_box_line_trim.block_end
                } else {
                    0.0
                },
            };
            let child_is_flow_candidate =
                block_avoid_break_flow_child(element, child_element, &child_style);
            // Inline-level normal-flow children precede a following class-A
            // block boundary but do not supply page values of their own. Keep
            // the enclosing scope as the preceding end value so the following
            // named block can select a new page group.
            // <https://www.w3.org/TR/css-page-3/#using-named-pages>
            if !child_is_flow_candidate
                && style_is_in_normal_flow(&child_style)
                && dom_state.previous_child_page_end.is_none()
            {
                dom_state.previous_child_page_end = Some(self.active_page_value_scope(style));
            }
            let child_page_values = child_is_flow_candidate.then(|| {
                let inherited_page_name = self.active_page_value_scope(style);
                self.dom_page_boundary_values(
                    child_element,
                    &child_style,
                    stylesheets,
                    inherited_page_name.as_deref(),
                )
            });
            let child_page_start = child_page_values
                .as_ref()
                .map(|values| values.start.clone());
            let zero_height_page_boundary =
                child_is_flow_candidate && dom_child_is_zero_height_page_boundary(&child_style);
            let effective_child_page_start = if zero_height_page_boundary {
                Some(self.dom_coalesced_zero_height_page_start(
                    element,
                    dom_state.child_node_index,
                    dom_state.element_index,
                    &sibling_tags,
                    style,
                    stylesheets,
                    child_page_start.clone().flatten(),
                ))
            } else {
                child_page_start.clone()
            };
            if let Some(start) = effective_child_page_start.as_ref()
                && dom_state
                    .previous_child_page_end
                    .as_ref()
                    .is_none_or(|previous| previous != start)
                && (!self.current_page_has_content() || dom_state.previous_child_page_end.is_some())
            {
                // The initial in-flow child has no preceding sibling to
                // supply a class-A end value, but an explicit `page`
                // value still selects its first fragmentainer. Without
                // that selection a first `page: named` box is laid out
                // against the default page size until a later sibling
                // happens to create a named-page boundary.
                // <https://www.w3.org/TR/css-page-3/#using-named-pages>
                self.switch_page_name_at_class_a_boundary(start.as_deref());
            }
            let next_flow_child_break_before = self.next_dom_flow_child_break_before(
                element,
                dom_state.child_node_index,
                dom_state.element_index,
                &sibling_tags,
                style,
                stylesheets,
            );
            let child_break_context = FragmentBreakContext::new(
                PageBreak::Auto,
                child_style.break_before,
                child_style.break_after,
                next_flow_child_break_before.unwrap_or(PageBreak::Auto),
            );
            if child_break_context.next_forced_break_supersedes_after_in(fragmentainer_kind) {
                child_style.break_after = PageBreak::Auto;
            }
            let child_break_opportunity = FragmentBreakOpportunity::before_box_boundary(
                fragmentainer_kind,
                dom_state.child_node_index as f32,
                child_break_context,
                dom_state.previous_break_after,
                false,
            );
            let avoid_run_start_decision =
                FragmentAvoidRunStartDecision::choose(FragmentAvoidRunStartInput {
                    participates_in_flow: child_is_flow_candidate,
                    fragmentainer_kind,
                    break_context: child_break_context,
                    break_opportunity: child_break_opportunity,
                    next_break_before: next_flow_child_break_before,
                    has_avoid_run_candidate: dom_state.avoid_run_candidate.is_some(),
                });
            let child_start_candidate = avoid_run_start_decision
                .should_arm_start_candidate
                .then(|| pending_child_start_candidate.arm(self));
            if let Some(committed) = self
                .committed_inline_floats
                .remove(&InlineFloatId::Element(child_element.id))
            {
                // Inline line selection already owns this float's exclusion
                // and captured paint subtree.  Keep the ordinary traversal's
                // out-of-flow sibling state, but never lay out the source
                // element a second time.
                debug_assert!(committed.is_valid());
                dom_state.previous_flow_bottom_margin = None;
                dom_state.child_node_index += 1;
                continue;
            }
            let placed_float = self.layout_floating_child(
                child_element,
                child_signature.clone(),
                &child_style,
                None,
                None,
                stylesheets,
                FloatPlacementAxes::for_style(style),
                &mut dom_state.float_run,
            );
            if placed_float {
                dom_state.previous_flow_bottom_margin = None;
                // Preserve an armed normal-flow break candidate across
                // out-of-flow floats. If a following sibling avoids a
                // break before itself, restoring that candidate also
                // replays the intervening float in the new
                // fragmentainer.
                // <https://www.w3.org/TR/css-break-3/#possible-breaks>
                dom_state.child_node_index += 1;
                continue;
            }
            self.flush_float_run(&mut dom_state.float_run);
            let mut child_ancestors = self.ancestors.clone();
            child_ancestors.push(child_signature.clone());
            let is_flow_child = child_is_flow_candidate;
            // Block-flow sibling placement and margin collapsing consume the
            // scalar edge cache before the child's own formatting context is
            // entered.  Resolve all computed margin and padding edges here
            // against the containing block's logical inline basis so mixed
            // `calc(<length> + <percentage>)` values cannot be replaced by
            // their legacy fixed-length cache component.
            // <https://www.w3.org/TR/CSS22/box.html#margin-properties>
            let parent_inline_percentage_basis = self
                .current_child_available_space()
                .logical_inline_percentage_basis_for(style.writing_mode);
            apply_used_box_metrics_for_logical_inline_basis(
                &mut child_style,
                parent_inline_percentage_basis,
            );
            let uncollapsed_child_start_margin = child_style.margin.top;
            let block_end_margin_trim = BlockEndMarginTrim::for_child(style, is_flow_child, || {
                has_later_normal_block_flow_child_with_font_metrics(
                    element,
                    dom_state.element_index,
                    &sibling_tags,
                    style,
                    stylesheets,
                    &self.ancestors,
                    &mut self.font_system,
                )
            });
            block_end_margin_trim.apply_to_child(&mut child_style);
            let descendant_start_margin = (is_flow_child
                && can_collapse_block_start_margin(
                    child_element,
                    &child_style,
                    UsedEdges::from_css_edges(used_border_widths(&child_style)),
                    has_direct_inline_content_before_first_flow_child_dom_with_font_metrics(
                        child_element,
                        &child_style,
                        stylesheets,
                        &child_ancestors,
                        &mut self.font_system,
                    ),
                    self.used_overflow_for_element(child_element, &child_style),
                ))
            .then(|| {
                collapsible_first_child_start_margin_dom_with_font_metrics(
                    child_element,
                    &child_style,
                    stylesheets,
                    &child_ancestors,
                    &mut self.font_system,
                    self.document_canvas_overflow,
                )
            })
            .flatten();
            let margin_collapse_style = height_behaves_as_auto_for_margin_collapse(
                &child_style,
                self.block_percentage_context_stack
                    .current_percentage_basis(),
            )
            .then(|| Self::dom_auto_height_margin_collapse_style(&child_style));
            let margin_collapse_style = margin_collapse_style.as_deref().unwrap_or(&child_style);
            let self_collapsing_child = is_flow_child
                && !self.has_in_flow_marker_line(child_element, &child_style)
                && is_self_collapsing_block_dom_with_font_metrics(
                    child_element,
                    margin_collapse_style,
                    stylesheets,
                    &child_ancestors,
                    &mut self.font_system,
                    self.document_canvas_overflow,
                );
            // A self-collapsing child joins its descendant start margin to
            // its block-end edge. If that child itself trims block-end, the
            // descendant portion is discarded at this boundary while the
            // child's own margin remains available to its parent.
            // <https://drafts.csswg.org/css-box-4/#margin-trim-block>
            let collapsed_descendant_start_margin = if child_style.margin_trim.block_end {
                None
            } else {
                descendant_start_margin
            };
            let self_collapsing_margin_set = self_collapsing_child.then(|| {
                self_collapsing_block_margin_set_for_box(
                    &child_style,
                    collapsed_descendant_start_margin,
                )
            });
            let adjoining_start_margin = self_collapsing_margin_set
                .map(AdjoiningBlockStartMargin::from_set)
                .unwrap_or_else(|| {
                    AdjoiningBlockStartMargin::from_child_and_descendant(
                        layout_pt(child_style.margin.top),
                        collapsed_descendant_start_margin,
                    )
                });
            if let Some(set) = self_collapsing_margin_set {
                child_style.margin.top = set.collapsed().points();
                child_style.margin.bottom = 0.0;
            }
            // A self-collapsing final child joins its start and end margins
            // with the preceding sibling's end margin. Once that joined set
            // adjoins the container's trimmed block-end edge, none of it may
            // remain as an inter-sibling cursor advance.
            // <https://drafts.csswg.org/css-box-4/#margin-trim-block>
            let trims_self_collapsing_end_margin_set =
                block_end_margin_trim == BlockEndMarginTrim::Trim && self_collapsing_child;
            if trims_self_collapsing_end_margin_set {
                child_style.margin.top = 0.0;
                child_style.margin.bottom = 0.0;
                discard_consumed_adjoining_block_end_margin(
                    self,
                    dom_state.previous_flow_bottom_margin,
                );
                dom_state.previous_flow_bottom_margin = None;
            }

            let trimmed_block_start_margin = is_flow_child
                && trim_adjoining_block_start_margin(
                    style,
                    &mut child_style,
                    dom_state.trim_block_start_adjoining_margins,
                    descendant_start_margin,
                );
            if trimmed_block_start_margin && self_collapsing_child {
                child_style.margin.bottom = 0.0;
            }

            let mut collapses_with_parent_end = false;
            let mut adjoining_start_margin_paint_offset = None;
            let mut inherited_adjoining_start_margin = None;
            let mut start_margin_without_descendant = None;
            let mut continued_self_collapsing_margin = None;
            if is_flow_child {
                let collapses_with_parent = is_collapsible_block_child(child_element, &child_style);
                let collapses_with_sibling =
                    outer_margins_adjoin_block_siblings(child_element, &child_style);
                // `clear` prevents the actual start-margin adjacency, but
                // CSS 2.2 selects its clearance target from the counterfactual
                // `clear:none` arrangement. Retain both states: the former
                // preserves the child's used margin; the latter carries the
                // parent-start hypothesis into block layout.
                let counterfactually_adjoins_parent_start = !trimmed_block_start_margin
                    && !dom_state.seen_flow_child.has_seen()
                    && permits_parent_start_collapse
                    && can_collapse_start_margin
                    && collapses_with_parent;
                let adjoins_parent_start =
                    counterfactually_adjoins_parent_start && child_style.clear == Clear::None;
                if !self_collapsing_child && descendant_start_margin.is_some() {
                    let own_start_margin = AdjoiningBlockStartMargin::from_child_and_descendant(
                        layout_pt(uncollapsed_child_start_margin),
                        None,
                    );
                    start_margin_without_descendant = Some(if adjoins_parent_start {
                        if let Some(previous_margin) = dom_state.previous_flow_bottom_margin {
                            own_start_margin
                                .child_delta_after_pending_sibling(
                                    previous_margin.with_consumed_margin(applied_start_margin),
                                )
                                .points()
                        } else {
                            own_start_margin
                                .child_delta_at_parent_start(
                                    applied_start_margin,
                                    starts_at_page_top,
                                )
                                .points()
                        }
                    } else if !trimmed_block_start_margin
                        && collapses_with_sibling
                        && FragmentBreakContext::for_standalone_box(&child_style)
                            .forced_break_before_in(fragmentainer_kind)
                            .is_none()
                    {
                        dom_state
                            .previous_flow_bottom_margin
                            .map(|previous_margin| {
                                own_start_margin
                                    .child_delta_after_pending_sibling(previous_margin)
                                    .points()
                            })
                            .unwrap_or(uncollapsed_child_start_margin)
                    } else {
                        uncollapsed_child_start_margin
                    });
                }
                if counterfactually_adjoins_parent_start {
                    let complete_margin = if child_style.clear != Clear::None {
                        let mut resolver =
                            DomStyleResolver::with_font_system(&mut self.font_system);
                        clear_none_hypothetical_start_margin_dom_with_resolver(
                            child_element,
                            &child_style,
                            stylesheets,
                            &child_ancestors,
                            &mut resolver,
                            self.document_canvas_overflow,
                        )
                        .unwrap_or_else(|| adjoining_start_margin.value())
                    } else {
                        adjoining_start_margin.value()
                    };
                    let parent_start_hypothesis = self
                        .inherited_adjoining_start_margins
                        .current_for(element.id)
                        .map(InheritedAdjoiningStartMargin::parent_start_clearance_hypothesis)
                        .unwrap_or_else(|| {
                            ParentStartClearanceHypothesis::new(PageTopBlockPosition::new(
                                self.cursor_y,
                            ))
                        });
                    inherited_adjoining_start_margin =
                        Some(InheritedAdjoiningStartMargin::with_parent_start_hypothesis(
                            complete_margin,
                            parent_start_hypothesis,
                        ));
                }
                if adjoins_parent_start {
                    if self_collapsing_child {
                        let (delta, pending) = if let Some(previous_margin) =
                            dom_state.previous_flow_bottom_margin
                        {
                            let mut pending =
                                previous_margin.with_consumed_margin(applied_start_margin);
                            let delta = pending
                                .merge_set(self_collapsing_margin_set.expect(
                                    "a self-collapsing child has a complete adjoining set",
                                ));
                            (delta, pending)
                        } else {
                            let (pending, delta) = PendingAdjoiningMargin::from_parent_start_set(
                                applied_start_margin,
                                self_collapsing_margin_set
                                    .expect("a self-collapsing child has a complete adjoining set"),
                                starts_at_page_top,
                            );
                            (delta, pending)
                        };
                        child_style.margin.top = delta.points();
                        continued_self_collapsing_margin = Some(pending);
                    } else if let Some(previous_margin) = dom_state.previous_flow_bottom_margin {
                        // Keep the parent's applied start margin in the
                        // collapsed set when a self-collapsing sibling has
                        // zero margin. This mirrors the formatting-box path.
                        child_style.margin.top = adjoining_start_margin
                            .child_delta_after_pending_sibling(
                                previous_margin.with_consumed_margin(applied_start_margin),
                            )
                            .points();
                    } else {
                        child_style.margin.top = adjoining_start_margin
                            .child_delta_at_parent_start(applied_start_margin, starts_at_page_top)
                            .points();
                    }
                } else if !trimmed_block_start_margin
                    && collapses_with_sibling
                    && FragmentBreakContext::for_standalone_box(&child_style)
                        .forced_break_before_in(fragmentainer_kind)
                        .is_none()
                    && let Some(previous_margin) = dom_state.previous_flow_bottom_margin
                {
                    child_style.margin.top = if let Some(set) = self_collapsing_margin_set {
                        let mut pending = previous_margin;
                        let delta = pending.merge_set(set);
                        continued_self_collapsing_margin = Some(pending);
                        delta.points()
                    } else {
                        adjoining_start_margin
                            .child_delta_after_pending_sibling(previous_margin)
                            .points()
                    };
                }
                // A first child's adjoining positive margin lies outside the
                // parent's border box, unless actual clearance separates the
                // margins while laying out that child. Keep the candidate at
                // this collapse boundary and commit it after child layout.
                // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
                if adjoins_parent_start {
                    adjoining_start_margin_paint_offset =
                        Some(layout_pt(child_style.margin.top.max(0.0)));
                }

                collapses_with_parent_end = collapses_with_parent
                    && can_collapse_end_margin
                    && !has_later_normal_block_flow_child_with_font_metrics(
                        element,
                        dom_state.element_index,
                        &sibling_tags,
                        style,
                        stylesheets,
                        &self.ancestors,
                        &mut self.font_system,
                    );
            }
            // Only normal-flow children have margins adjusted by this pass.
            // Preserve the authored `auto` margins of out-of-flow children
            // for the absolute-positioned used-value algorithm.
            // <https://www.w3.org/TR/css-position-3/#abspos-layout>
            if is_flow_child {
                preserve_adjusted_block_margins(&mut child_style);
            }
            let available_outer_width = (self.content_right
                - self.content_left
                - child_style.margin.left
                - child_style.margin.right)
                .max(child_style.font_size);
            let mut run_start_candidate = if is_flow_child {
                if avoid_run_start_decision.is_avoid_boundary {
                    Some(dom_state.avoid_run_candidate.take().unwrap_or_else(|| {
                        child_start_candidate
                            .expect("DOM child start candidate must be armed when avoid boundary has no existing run candidate")
                    }))
                } else if avoid_run_start_decision.seeds_later_avoid_boundary {
                    Some(
                        child_start_candidate
                            .expect("DOM child start candidate must be armed when this child can seed a later avoid boundary"),
                    )
                } else {
                    None
                }
            } else {
                None
            };
            let avoid_run_retry_context =
                run_start_candidate
                    .as_ref()
                    .map(|candidate| AvoidRunRetryContext {
                        current_fragmentainer: self.avoid_break_current_fragmentainer(
                            style.writing_mode,
                            PageTopBlockPosition::new(self.cursor_y),
                        ),
                        empty_destination_fragmentainer: self.next_empty_avoid_break_fragmentainer(
                            fragmentainer_kind,
                            style.writing_mode,
                        ),
                        source_occupancy: candidate.source_fragmentainer_occupancy(),
                    });
            // The estimate is only consumed to move or extend a contiguous
            // `break-inside: avoid` run. Measuring every ordinary child here
            // duplicates expensive table row layout before the real
            // fragmenting pass has selected a destination. An empty source
            // probes only when that destination is strictly larger, so equal
            // temporary columns relax the avoid preference instead of
            // replaying forever.
            // <https://www.w3.org/TR/css-break-3/#break-between>
            let should_measure_avoid_run = (avoid_run_start_decision.is_avoid_boundary
                || avoid_run_start_decision.seeds_later_avoid_boundary)
                && !self.cursor_is_at_page_top()
                && self.fragmentation_suppression_depth == 0
                && avoid_run_retry_context.is_some_and(AvoidRunRetryContext::can_advance);
            let child_avoid_block_extent = should_measure_avoid_run
                .then(|| {
                    let key = AvoidRunPreflightKey::capture(
                        self,
                        dom_state.child_node_index,
                        available_outer_width,
                        style.writing_mode,
                        fragmentainer_kind,
                        avoid_run_retry_context
                            .expect("avoid-run preflight requires a retry context")
                            .current_fragmentainer,
                    );
                    if let Some(extent) = dom_state.avoid_run_preflight_cache.get(&key) {
                        extent
                    } else {
                        let extent = self.avoid_break_fragmentation_extent(
                            child_element,
                            &child_style,
                            stylesheets,
                            available_outer_width,
                            None,
                            style.writing_mode,
                        );
                        dom_state.avoid_run_preflight_cache.insert(key, extent);
                        extent
                    }
                })
                .flatten();
            if is_flow_child
                && avoid_run_start_decision.is_avoid_boundary
                && let Some(child_block_extent) = child_avoid_block_extent
                && let Some(retry_context) = avoid_run_retry_context
                && let Some(candidate) = run_start_candidate.as_ref()
                && should_move_avoid_break_run_to_next_fragmentainer(AvoidRunPrebreakInput {
                    run_block_extent: candidate.block_extent(),
                    next_block_extent: child_block_extent,
                    retry_context,
                })
            {
                let run_start_candidate = run_start_candidate
                    .take()
                    .expect("DOM child avoid boundary must have an armed run candidate");
                let AvoidBreakRunCandidateMeta {
                    index,
                    element_index: saved_element_index,
                    previous_flow_bottom_margin: saved_previous_flow_bottom_margin,
                    seen_flow_child: saved_seen_flow_child,
                    trim_block_start_adjoining_margins: saved_trim_block_start_adjoining_margins,
                    collapsed_end_margin: saved_collapsed_end_margin,
                    previous_child_page_end: _,
                    float_run: saved_float_run,
                    remaining_line_clamp: saved_remaining_line_clamp,
                    block_extent: _,
                } = run_start_candidate.restore(self);
                dom_state.element_index = saved_element_index;
                dom_state.previous_flow_bottom_margin = saved_previous_flow_bottom_margin;
                dom_state.seen_flow_child = saved_seen_flow_child;
                dom_state.trim_block_start_adjoining_margins =
                    saved_trim_block_start_adjoining_margins;
                dom_state.collapsed_end_margin = saved_collapsed_end_margin;
                dom_state.float_run = saved_float_run;
                traversal_state.restore_avoid_replay(saved_remaining_line_clamp);
                dom_state.child_node_index = index;
                dom_state.avoid_run_candidate = None;
                dom_state.previous_break_after = PageBreak::Auto;
                self.materialize_avoid_run_retry(fragmentainer_kind, retry_context);
                continue;
            }

            let closes_adjoining_float_replay = is_flow_child
                && !self_collapsing_child
                && dom_state.previous_flow_bottom_margin.is_some()
                && !replaying_adjoining_target;
            if closes_adjoining_float_replay
                && let Some(replay) = dom_state.adjoining_float_replay.take()
            {
                let replay_origin_y = self.cursor_y - child_style.margin.top;
                let replay_separation = replay
                    .clearance_boundary()
                    .map(|boundary| AdjoiningFloatReplaySeparation::Clearance {
                        border_top: boundary.border_top(),
                    })
                    .unwrap_or_else(|| {
                        self.adjoining_float_replay_separated_by_following_child(
                            &replay,
                            child_element,
                            &child_style,
                            stylesheets,
                            None,
                            replay_origin_y,
                        )
                    });
                if let AdjoiningFloatReplaySeparation::Clearance { border_top }
                | AdjoiningFloatReplaySeparation::MarginSeparation { border_top } =
                    replay_separation
                {
                    child_style.margin.top =
                        layout_pt((self.cursor_y - border_top.points()).max(0.0)).points();
                    preserve_adjusted_block_margins(&mut child_style);
                }
                if replay_separation == AdjoiningFloatReplaySeparation::None
                    && (replay_origin_y - replay.snapshot_cursor_y()).abs() > FLOAT_EPSILON
                {
                    let replay_until = dom_state.child_node_index;
                    let replay_meta = replay.restore(self);
                    dom_state.element_index = replay_meta.element_index;
                    dom_state.previous_flow_bottom_margin = replay_meta.previous_flow_bottom_margin;
                    dom_state.seen_flow_child = replay_meta.seen_flow_child;
                    dom_state.trim_block_start_adjoining_margins =
                        replay_meta.trim_block_start_adjoining_margins;
                    dom_state.collapsed_end_margin = replay_meta.collapsed_end_margin;
                    dom_state.float_run = replay_meta.float_run;
                    dom_state.previous_break_after = replay_meta.previous_break_after;
                    dom_state.avoid_run_candidate = None;
                    dom_state.child_node_index = replay_meta.index;
                    self.adjoining_float_origin_y = Some(replay_origin_y);
                    dom_state.replaying_adjoining_until = Some(replay_until);
                    continue;
                }
            }

            let adjoining_candidate = if child_style.float == Float::None
                && self_collapsing_child
                && dom_state.adjoining_float_replay.is_none()
                && dom_state.replaying_adjoining_until.is_none()
            {
                Some(PendingAdjoiningFloatReplayCandidate {
                    snapshot: Box::new(self.snapshot()),
                    meta: AdjoiningFloatReplayCandidateMeta {
                        index: dom_state.child_node_index,
                        element_index: dom_state.element_index.saturating_sub(1),
                        previous_flow_bottom_margin: dom_state.previous_flow_bottom_margin,
                        seen_flow_child: dom_state.seen_flow_child,
                        trim_block_start_adjoining_margins: dom_state
                            .trim_block_start_adjoining_margins,
                        collapsed_end_margin: dom_state.collapsed_end_margin,
                        previous_child_page_end: None,
                        float_run: dom_state.float_run,
                        previous_break_after: dom_state.previous_break_after,
                    },
                })
            } else {
                None
            };
            let float_shape_count_before = self
                .float_contexts
                .last()
                .map(|context| context.shapes.len())
                .unwrap_or(0);

            if is_flow_child {
                if !self_collapsing_child {
                    dom_state.seen_flow_child.record_in_flow_child();
                    // The child has been entered as the first in-flow block
                    // source. Its own line selection owns the propagated
                    // pseudo, so later siblings must not restart it.
                    dom_state.first_formatted_line.consume_next_formatted_line();
                }
                if dom_state.trim_block_start_adjoining_margins && !self_collapsing_child {
                    dom_state.trim_block_start_adjoining_margins = false;
                }
            } else if !matches!(child_style.position, Position::Absolute | Position::Fixed) {
                // An absolutely/fixed-positioned source child is removed from
                // normal flow, so it cannot separate the adjacent in-flow
                // siblings' vertical margins. Preserve the prior in-flow
                // bottom-margin candidate until the next in-flow block has
                // consumed the collapsed set.
                // <https://www.w3.org/TR/CSS22/visuren.html#absolute-positioning>
                // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
                dom_state.previous_flow_bottom_margin = None;
            }

            let child_uses_block_layout = matches!(
                element_layout_kind(child_element, &child_style),
                ElementLayoutKind::BlockFlow
            );
            // The DOM fallback traversal is the normal route for ordinary
            // HTML documents. Its physical top-to-bottom cursor must not
            // become the block cursor for a vertical containing flow: that
            // flow advances through a local horizontal block track.
            //
            // Keep the physical inline origin and the horizontal block track
            // as separate inputs. The child is laid out against the current
            // track, then its used physical border-box span advances the next
            // sibling in the parent's logical block direction.
            // <https://www.w3.org/TR/css-writing-modes-4/#orthogonal-flows>
            // <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
            let principal_vertical_placement = (is_flow_child
                && style.writing_mode.has_vertical_lines())
            .then(|| {
                OrthogonalBlockPlacement::new(
                    style.writing_mode,
                    style.used_direction(),
                    vertical_child_inline_origin
                        .expect("a vertical parent has a fixed physical inline origin"),
                    PageInlineSpan::from_edges(self.content_left, self.content_right),
                    LogicalInlineContentSize::new(content_box_pt(
                        self.current_content_logical_inline_size(),
                    )),
                )
            })
            .flatten();
            let advances_principal_fragmentainers = self.active_fragmentainer_kind()
                == FragmentainerKind::Page
                && self.element_supplies_document_principal_flow(element);
            let principal_flow_page_index = self.pages.len();
            let principal_flow_paint_checkpoint =
                principal_vertical_placement.map(|_| self.current_page.paint_checkpoint());
            if let Some(placement) = principal_vertical_placement {
                debug_assert_eq!(
                    placement.logical_inline_percentage_basis().points(),
                    self.current_content_logical_inline_size(),
                    "vertical principal-flow child constraints use the page-height inline basis"
                );
            }
            if advances_principal_fragmentainers
                && principal_vertical_placement
                    .is_some_and(|placement| placement.block_track_is_exhausted())
                && self.current_page_has_content()
            {
                self.push_page();
            }
            let source_body_canvas = principal_vertical_placement.is_some()
                && self.principal_flow.is_source_body(child_element);
            let bottom_origin_canvas_child = principal_vertical_placement.is_some()
                && self.element_supplies_document_principal_flow(element)
                && inline_start_side(style.writing_mode, style.used_direction())
                    == PhysicalSide::Bottom;
            let bottom_origin_nested_child = principal_vertical_placement.is_some()
                && !self.element_supplies_document_principal_flow(element)
                && inline_start_side(style.writing_mode, style.used_direction())
                    == PhysicalSide::Bottom;
            let canvas_inline_origin = self.cursor_y;
            if bottom_origin_canvas_child {
                // The legacy block formatter measures available physical Y
                // space from the page top.  Lay out a bottom-origin
                // principal-flow child in that scratch coordinate, then
                // project its completed paint back to the canvas inline
                // start. Its logical block advancement remains the separate
                // horizontal track below.
                // <https://drafts.csswg.org/css-writing-modes-4/#principal-flow>
                self.cursor_y = self.page_top();
            } else if let Some(placement) = principal_vertical_placement
                && !bottom_origin_nested_child
            {
                self.cursor_y = placement.page_inline_origin().points();
            }
            // This snapshot is taken after the parent has resolved sibling
            // margins and clearance, immediately before the child enters its
            // own formatting context. The automatic controller consumes the
            // child's actual normal-flow block advance, not paint bounds or
            // an out-of-flow descendant's extent.
            let automatic_child_flow_start = (is_flow_child
                && traversal_state.has_automatic_block_size_clamp())
            .then_some(self.cursor_y);
            self.last_block_layout_outcome = BlockLayoutOutcome::default();
            let mut descendant_clearance_retry_snapshot = start_margin_without_descendant
                .filter(|margin| (margin - child_style.margin.top).abs() > FLOAT_EPSILON)
                .map(|_| Box::new(self.snapshot()));
            loop {
                if child_style.display.is_block_level() {
                    let captures_direct_out_of_flow_static_position =
                        matches!(child_style.position, Position::Absolute | Position::Fixed)
                            && !child_style.abspos_static_source.is_inline_level();
                    let previous_direct_out_of_flow_static_position = self.absolute_static_position;
                    let previous_block_static_position_y_offset =
                        if captures_direct_out_of_flow_static_position {
                            let previous = self.block_static_position_y_offset;
                            self.block_static_position_y_offset = None;
                            Some(previous)
                        } else {
                            None
                        };
                    if captures_direct_out_of_flow_static_position {
                        let capture = dom_state.out_of_flow_static_capture.unwrap_or_else(|| {
                            StaticPositionCapture {
                                rectangle: self.block_static_position_rectangle_at(
                                    PageTopBlockPosition::new(self.cursor_y),
                                ),
                            }
                        });
                        dom_state.out_of_flow_static_capture = Some(capture);
                        let rectangle = capture.rectangle;
                        self.absolute_static_position = Some(
                            self.absolute_static_position
                                .unwrap_or_else(|| {
                                    AbsoluteStaticPosition::from_page_rect(
                                        self.content_left,
                                        self.content_right,
                                        rectangle.area.top_y(),
                                    )
                                })
                                .with_static_position_rectangle(rectangle),
                        );
                    }
                    if let Some(margin) = inherited_adjoining_start_margin {
                        self.inherited_adjoining_start_margins
                            .push_for(child_element.id, margin);
                    }
                    let previous_direct_block_layout_constraint = self
                        .replace_direct_block_layout_constraint(
                            child_element,
                            principal_vertical_placement,
                        );
                    self.push_ancestor_signature(child_signature.clone());
                    self.with_text_box_line_trim_scope(child_text_box_line_trim, |layout| {
                        layout.layout_element(child_element, &child_style, stylesheets);
                    });
                    self.ancestors.pop();
                    self.restore_direct_block_layout_constraint(
                        previous_direct_block_layout_constraint,
                    );
                    if inherited_adjoining_start_margin.is_some() {
                        self.inherited_adjoining_start_margins
                            .pop_for(child_element.id);
                    }
                    if let Some(previous) = previous_block_static_position_y_offset {
                        self.block_static_position_y_offset = previous;
                    }
                    if captures_direct_out_of_flow_static_position {
                        self.absolute_static_position = previous_direct_out_of_flow_static_position;
                    }
                }
                if child_uses_block_layout
                    && self.last_block_layout_outcome.margin_collapse_boundary
                        == BlockMarginCollapseBoundary::SeparatedByClearance
                    && let (Some(snapshot), Some(retry_margin)) = (
                        descendant_clearance_retry_snapshot.take(),
                        start_margin_without_descendant,
                    )
                {
                    self.restore(*snapshot);
                    child_style.margin.top = retry_margin;
                    preserve_adjusted_block_margins(&mut child_style);
                    self.last_block_layout_outcome = BlockLayoutOutcome::default();
                    continue;
                }
                break;
            }
            self.flush_float_run(&mut dom_state.float_run);
            if let (Some(checkpoint), Some(placement)) = (
                principal_flow_paint_checkpoint,
                principal_vertical_placement,
            ) && self.pages.len() == principal_flow_page_index
            {
                let fragment = self.current_page.take_paint_fragment_since(checkpoint);
                let child_block_end_margin = placement.child_block_end_margin(&child_style);
                let child_margin_box_block_extent = placement.child_margin_box_block_extent(
                    self.last_block_layout_outcome
                        .physical_border_box_inline_span,
                    &child_style,
                );
                let remaining_block_track =
                    placement.track_after_committed_child(child_margin_box_block_extent);
                if self.principal_flow.is_source_body(element) {
                    let active_canvas = self
                        .root_principal_flow_context
                        .active_canvas
                        .as_mut()
                        .expect("a propagated body keeps an active document canvas");
                    debug_assert_eq!(active_canvas.body, Some(element.id));
                    active_canvas.trailing_child_block_margin = layout_pt(child_block_end_margin);
                }
                let translation = if bottom_origin_canvas_child {
                    fragment
                        .bounds()
                        .map(|bounds| {
                            PaintTranslation::new(
                                0.0,
                                self.page_bottom()
                                    + if self.principal_flow.is_source_body(element) {
                                        self.principal_inline_end_inset
                                    } else {
                                        0.0
                                    }
                                    - bounds.y(),
                            )
                        })
                        .unwrap_or_else(PaintTranslation::identity)
                } else if bottom_origin_nested_child {
                    self.last_block_layout_outcome
                        .static_border_box
                        .map(|border_box| {
                            // Nested bottom-origin vertical flow is laid out
                            // once at the physical top scratch origin, then
                            // projected so the child's physical bottom margin
                            // edge meets the containing inline-start edge.
                            // Use principal-box geometry rather than ink
                            // bounds, which can include glyph overflow or be
                            // absent for an empty child.
                            // <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
                            PaintTranslation::new(
                                0.0,
                                placement.page_inline_origin().points() + child_style.margin.top
                                    - border_box.min_y(),
                            )
                        })
                        .unwrap_or_else(PaintTranslation::identity)
                } else {
                    PaintTranslation::identity()
                };
                self.current_page
                    .append_paint_fragment_owned(fragment, translation);
                self.cursor_y = if bottom_origin_canvas_child || bottom_origin_nested_child {
                    canvas_inline_origin
                } else {
                    placement.page_inline_origin().points()
                };
                if !source_body_canvas {
                    self.content_left = remaining_block_track.left_x();
                    self.content_right = remaining_block_track.right_x();
                }
            }
            if self.last_block_layout_outcome.margin_collapse_boundary
                == BlockMarginCollapseBoundary::Adjoining
                && let Some(offset) = adjoining_start_margin_paint_offset
            {
                dom_state.collapsed_start_margin_offset =
                    dom_state.collapsed_start_margin_offset.max(offset);
            }
            let emitted_float = self
                .float_contexts
                .last()
                .is_some_and(|context| context.shapes.len() > float_shape_count_before);
            if emitted_float && let Some(candidate) = adjoining_candidate {
                dom_state.adjoining_float_replay = Some(candidate.arm(self));
            }
            if is_flow_child {
                let child_start_separated_by_clearance = child_uses_block_layout
                    && self.last_block_layout_outcome.margin_collapse_boundary
                        == BlockMarginCollapseBoundary::SeparatedByClearance;
                if child_start_separated_by_clearance {
                    dom_state.adjoining_margin_set_boundary =
                        BlockMarginCollapseBoundary::SeparatedByClearance;
                }
                let child_consumed_bottom_margin = if child_uses_block_layout {
                    self.last_block_layout_outcome.consumed_bottom_margin
                } else {
                    layout_pt(child_style.margin.bottom)
                };
                dom_state.previous_flow_bottom_margin = if trims_self_collapsing_end_margin_set {
                    Some(PendingAdjoiningMargin::from_consumed_margin(layout_pt(0.0)))
                } else if self_collapsing_child {
                    Some(if trimmed_block_start_margin {
                        PendingAdjoiningMargin::from_consumed_margin(layout_pt(0.0))
                    } else if child_start_separated_by_clearance {
                        // CSS2 clearance separates the box's top margin from
                        // its own bottom margin and from its parent. Its
                        // bottom margin may still adjoin a following sibling.
                        PendingAdjoiningMargin::from_consumed_margin(child_consumed_bottom_margin)
                    } else {
                        continued_self_collapsing_margin.unwrap_or_else(|| {
                            PendingAdjoiningMargin::from_consumed_set(
                                self_collapsing_margin_set
                                    .expect("a self-collapsing child has a complete adjoining set"),
                            )
                        })
                    })
                } else {
                    outer_margins_adjoin_block_siblings(child_element, &child_style).then(|| {
                        PendingAdjoiningMargin::from_consumed_margin(child_consumed_bottom_margin)
                    })
                };
                if collapses_with_parent_end && !child_start_separated_by_clearance {
                    let collapsed_margin = if self_collapsing_child
                        && !child_style.display.establishes_block_formatting_context()
                    {
                        dom_state
                            .previous_flow_bottom_margin
                            .map(|pending| {
                                pending.collapsed_with_margin(layout_pt(style.margin.bottom))
                            })
                            .unwrap_or_else(|| {
                                collapse_margins(
                                    child_consumed_bottom_margin,
                                    layout_pt(style.margin.bottom),
                                )
                            })
                    } else {
                        collapse_margins(
                            child_consumed_bottom_margin,
                            layout_pt(style.margin.bottom),
                        )
                    };
                    dom_state.pending_end_margin_collapse = Some(BlockEndMarginCollapse {
                        child_consumed_margin: child_consumed_bottom_margin,
                        collapsed_margin,
                    });
                }
                if zero_height_page_boundary {
                    dom_state.previous_child_page_end = effective_child_page_start;
                } else if let Some(values) = child_page_values {
                    dom_state.previous_child_page_end = Some(values.end);
                }
                if traversal_state.has_automatic_block_size_clamp()
                    && let Some(child_flow_start) = automatic_child_flow_start
                {
                    // The legacy page cursor can include a descendant's
                    // local overflow or omit an authored definite block
                    // extent. Automatic clamp candidates instead consume the
                    // child's resolved normal-flow margin-box contribution.
                    // The static border box is source geometry, before paint
                    // transforms and clipping; use the cursor only for box
                    // kinds that cannot expose one.
                    // <https://drafts.csswg.org/css-overflow-4/#line-clamp-containers>
                    let static_block_contribution = self
                        .last_block_layout_outcome
                        .static_border_box
                        .map(|border_box| {
                            let border_box_block_size =
                                if WritingModeAxes::new(style.writing_mode, style.direction)
                                    .swaps_physical_axes()
                                {
                                    border_box.size.width
                                } else {
                                    border_box.size.height
                                };
                            crate::units::content_box_pt(
                                (border_box_block_size
                                    + child_style.margin.top
                                    + child_style.margin.bottom)
                                    .max(0.0),
                            )
                        })
                        .unwrap_or_else(|| {
                            crate::units::content_box_pt(
                                (child_flow_start - self.cursor_y).max(0.0),
                            )
                        });
                    traversal_state.debit_automatic_block_contribution(static_block_contribution);
                }
                if child_uses_block_layout && child_shares_clamp_context {
                    traversal_state.record_descendant_clamp_line_slots(
                        self.last_block_layout_outcome.clamp_line_slots,
                    );
                    traversal_state
                        .debit_rendered_slots(self.last_block_layout_outcome.clamp_line_slots);
                    if traversal_state.has_active_clamp()
                        && self.last_block_layout_outcome.has_local_continuation_cutoff
                    {
                        traversal_state.mark_local_continuation_cutoff();
                    }
                }
            }
            let replay_preceding_child_for_empty_automatic_cutoff = child_shares_clamp_context
                && self.last_block_layout_outcome.clamp_line_slots == 0
                && self.last_block_layout_outcome.has_local_continuation_cutoff
                && dom_state.automatic_marker_replay_target != Some(dom_state.child_node_index);
            if replay_preceding_child_for_empty_automatic_cutoff
                && let Some(checkpoint) = dom_state.automatic_marker_candidate.take()
            {
                dom_state.restore_automatic_marker_checkpoint(checkpoint, self, traversal_state);
                continue;
            }
            let replay_current_child_as_automatic_marker = traversal_state
                .has_automatic_block_size_clamp()
                && traversal_state.is_exhausted()
                && child_shares_clamp_context
                && has_later_in_flow_child
                && self.last_block_layout_outcome.clamp_line_slots > 0
                && !self.last_block_layout_outcome.has_local_continuation_cutoff
                && dom_state.automatic_marker_replay_target != Some(dom_state.child_node_index);
            if replay_current_child_as_automatic_marker
                && let Some(checkpoint) = automatic_replay_before_child
            {
                dom_state.restore_automatic_marker_checkpoint(checkpoint, self, traversal_state);
                continue;
            }
            if traversal_state.has_automatic_block_size_clamp()
                && child_shares_clamp_context
                && self.last_block_layout_outcome.clamp_line_slots > 0
                && !self.last_block_layout_outcome.has_local_continuation_cutoff
                && dom_state.automatic_marker_replay_target != Some(dom_state.child_node_index)
            {
                dom_state.automatic_marker_candidate = automatic_replay_before_child;
            }
            if dom_state.automatic_marker_replay_target == Some(dom_state.child_node_index) {
                dom_state.automatic_marker_replay_target = None;
            }
            dom_state.avoid_run_candidate = if avoid_run_start_decision.seeds_later_avoid_boundary {
                match (run_start_candidate.take(), child_avoid_block_extent) {
                    (Some(candidate), Some(child_block_extent)) => {
                        Some(candidate.add_block_extent(child_block_extent))
                    }
                    (Some(candidate), None) => Some(candidate),
                    (None, _) => None,
                }
            } else {
                None
            };
            dom_state.previous_break_after = if is_flow_child {
                dom_state.out_of_flow_static_capture = Some(StaticPositionCapture {
                    rectangle: self.block_static_position_rectangle_at(PageTopBlockPosition::new(
                        self.cursor_y,
                    )),
                });
                child_break_context
                    .avoid_after_in(fragmentainer_kind)
                    .unwrap_or(PageBreak::Auto)
            } else {
                PageBreak::Auto
            };
            dom_state.child_node_index += 1;
        }
        self.flush_float_run(&mut dom_state.float_run);
        ChildFlowTraversalOutcome {
            pending_end_margin_collapse: dom_state.pending_end_margin_collapse,
            collapsed_start_margin_offset: dom_state.collapsed_start_margin_offset,
            adjoining_margin_set_boundary: dom_state.adjoining_margin_set_boundary,
            rendered_legend: None,
        }
    }

    /// Construct the auto-height margin-collapse view outside the recursive
    /// DOM child traversal frame. This override is required only when a
    /// definite descendant percentage basis makes an authored auto height
    /// behave specially for CSS margin collapsing.
    #[inline(never)]
    fn dom_auto_height_margin_collapse_style(style: &ComputedStyle) -> Box<ComputedStyle> {
        let mut style = Box::new(style.clone());
        style
            .box_values
            .height
            .replace_with_used(css::ComputedLengthPercentageOrAuto::Auto);
        style
    }

    /// Coalesce a run of explicit zero-height named-page children into the
    /// next nonzero normal-flow sibling's page group.
    ///
    /// The frozen formatting-tree traversal applies the same CSS Paged Media
    /// rule. DOM fallback layout must preserve it too, otherwise each member
    /// of the zero-height run manufactures an empty page:
    /// <https://www.w3.org/TR/css-page-3/#using-named-pages>.
    #[allow(clippy::too_many_arguments)]
    fn dom_coalesced_zero_height_page_start(
        &mut self,
        parent: &Element,
        current_node_index: usize,
        next_element_index: usize,
        sibling_tags: &ElementSiblingSignatureList,
        parent_style: &ComputedStyle,
        stylesheets: &Stylesheets<'_>,
        fallback: Option<String>,
    ) -> Option<String> {
        let mut element_index = next_element_index;
        for node in parent.children.iter().skip(current_node_index + 1) {
            let NodeKind::Element(child) = &node.kind else {
                continue;
            };
            let signature =
                ElementSignature::from_sibling_snapshot(element_index, sibling_tags.clone())
                    .expect("source child must have a cached sibling signature");
            element_index += 1;
            let child_style = self.style_for_layout_element_with_parent_font_metrics(
                child,
                signature,
                stylesheets,
                Some(parent_style),
            );
            if !style_is_in_normal_flow(&child_style) || !child_style.display.is_block_level() {
                continue;
            }
            if !dom_child_is_zero_height_page_boundary(&child_style) {
                let inherited_page_name = self.active_page_value_scope(parent_style);
                return self
                    .dom_page_boundary_values(
                        child,
                        &child_style,
                        stylesheets,
                        inherited_page_name.as_deref(),
                    )
                    .start;
            }
        }
        fallback
    }

    /// Derives the CSS Paged Media start/end page values for a DOM fallback
    /// subtree. This mirrors the frozen formatting-tree path so dynamically
    /// laid-out block flow cannot bypass class-A named-page transitions.
    pub(in crate::layout) fn dom_page_boundary_values(
        &mut self,
        element: &Element,
        style: &ComputedStyle,
        stylesheets: &Stylesheets<'_>,
        inherited_page_name: Option<&str>,
    ) -> ResolvedPageBoundaryValues {
        self.dom_page_boundary_summary(element, style, stylesheets, inherited_page_name)
            .0
    }

    /// Resolve a DOM fallback subtree's page values and the authored sources
    /// that determine whether those values override its parent summary.
    ///
    /// These results are inseparable during the recursive walk: a child's
    /// source decides whether its resolved value replaces the parent.  Keeping
    /// them together avoids re-cascading every descendant once for values and
    /// again for sources at each table row/cell boundary.
    /// <https://www.w3.org/TR/css-page-3/#using-named-pages>
    fn dom_page_boundary_summary(
        &mut self,
        element: &Element,
        style: &ComputedStyle,
        stylesheets: &Stylesheets<'_>,
        inherited_page_name: Option<&str>,
    ) -> (ResolvedPageBoundaryValues, PageBoundaryValues) {
        let cache_key = (element.id, inherited_page_name.map(str::to_string));
        if let Some(summary) = self.dom_page_boundary_summaries.get(&cache_key) {
            return summary.clone();
        }
        let own_page_name = if style.page.is_specified() {
            style
                .page
                .specified_name()
                .map(|name| name.as_str().to_string())
                .or_else(|| inherited_page_name.map(str::to_string))
        } else {
            inherited_page_name.map(str::to_string)
        };
        let mut values = ResolvedPageBoundaryValues {
            start: own_page_name.clone(),
            end: own_page_name.clone(),
        };
        let mut sources = PageBoundaryValues::from_style(style);
        if style.display.is_flex() {
            return (values, sources);
        }
        if style.display.is_table() {
            // DOM fallback traversal does not retain the table's normalized
            // row/cell structure. Rebuild that durable fragment here so its
            // page boundaries match ordinary frozen-box table layout rather
            // than treating table internals as inapplicable DOM children.
            // <https://www.w3.org/TR/css-page-3/#using-named-pages>
            let child_boxes =
                self.build_frozen_child_boxes_with_current_ancestors(element, stylesheets, style);
            let fragment = box_tree::build_frozen_table_fragment(
                element,
                &element_signature(element),
                style,
                &child_boxes,
            );
            let table_summary =
                table::table_page_boundary_summary(&fragment, style, inherited_page_name);
            let summary = (table_summary.resolved, table_summary.sources);
            self.dom_page_boundary_summaries
                .insert(cache_key, summary.clone());
            return summary;
        }
        let siblings = element_sibling_signature_list(element);
        let mut element_index = 0;
        let mut first = None;
        let mut last = None;
        for child in &element.children {
            let NodeKind::Element(child) = &child.kind else {
                continue;
            };
            let signature =
                ElementSignature::from_sibling_snapshot(element_index, siblings.clone())
                    .expect("source child must have a cached sibling signature");
            element_index += 1;
            let child_style = self.style_for_layout_element_with_parent_font_metrics(
                child,
                signature,
                stylesheets,
                Some(style),
            );
            if !style_is_in_normal_flow(&child_style) {
                continue;
            }
            // Only block-level boxes establish a class-A page boundary in
            // this block-flow traversal. Inline replaced elements remain in
            // the surrounding inline formatting context, so their own
            // `page` value must not mask a following block sibling's
            // transition.
            // <https://www.w3.org/TR/css-page-3/#using-named-pages>
            let applies =
                child_style.display.is_block_level() && !child_style.display.is_table_internal();
            let (child_values, child_sources) = if applies {
                self.dom_page_boundary_summary(
                    child,
                    &child_style,
                    stylesheets,
                    own_page_name.as_deref(),
                )
            } else {
                (
                    ResolvedPageBoundaryValues {
                        start: None,
                        end: None,
                    },
                    PageBoundaryValues::inapplicable(),
                )
            };
            first.get_or_insert_with(|| (child_values.clone(), child_sources.clone()));
            last = Some((child_values, child_sources));
        }
        if let Some((first_values, first_sources)) = first.as_ref()
            && first_sources.start.overrides_parent_summary()
        {
            values.start = first_values.start.clone();
            sources.start = first_sources.start.clone();
        }
        if let Some((last_values, last_sources)) = last.as_ref()
            && last_sources.end.overrides_parent_summary()
        {
            values.end = last_values.end.clone();
            sources.end = last_sources.end.clone();
        }
        let summary = (values, sources);
        self.dom_page_boundary_summaries
            .insert(cache_key, summary.clone());
        summary
    }
}

fn dom_child_is_zero_height_page_boundary(style: &ComputedStyle) -> bool {
    style.page.is_specified()
        && style
            .box_values
            .height
            .length_if_no_percent()
            .is_some_and(|height| height.abs() < 0.01)
}