ironpress 1.4.1

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, LaTeX math, tables, images, custom fonts, and streaming output. No browser, no system dependencies.
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
use crate::parser::css::{AncestorInfo, SelectorContext};
use crate::parser::dom::{DomNode, ElementNode, HtmlTag};
use crate::parser::ttf::TtfFont;
use crate::style::computed::{
    BackgroundOrigin, BackgroundPosition, BackgroundRepeat, BackgroundSize, BoxSizing, Clear,
    ComputedStyle, Display, Float, Overflow, Position, TextAlign, TextOverflow, VerticalAlign,
    Visibility, WhiteSpace, compute_style_with_context,
};
use std::collections::HashMap;

use super::context::{ContainingBlock, LayoutContext, LayoutEnv};
use super::engine::{LayoutBorder, LayoutElement, TextRun, flatten_element};
use super::helpers::{
    BackgroundFields, append_pseudo_inline_run, aspect_ratio_height, build_pseudo_block,
    collects_as_inline_text, has_background_paint, heading_level,
    patch_absolute_children_containing_block, pseudo_is_block_like, push_block_pseudo,
    recurses_as_layout_child, resolve_abs_containing_block, resolve_padding_box_height,
};
use super::inline::{element_is_inline_block, layout_inline_block_group};
use super::paginate::estimate_element_height;
use super::text::{
    TextWrapOptions, apply_text_overflow_ellipsis, collect_text_runs, resolved_line_height_factor,
    wrap_text_runs,
};

/// Lay out a `display: block` or `display: inline-block` element.
///
/// Returns `true` when the layout completed via the mixed-block-children
/// early-exit path (page-break-after already emitted), meaning the caller
/// should return immediately without further post-processing.
#[allow(clippy::too_many_arguments)]
pub(crate) fn layout_block_element(
    el: &ElementNode,
    style: &mut ComputedStyle,
    ctx: &LayoutContext,
    output: &mut Vec<LayoutElement>,
    ancestors: &[AncestorInfo],
    child_ancestors: &[AncestorInfo],
    positioned_depth: usize,
    before_style: Option<ComputedStyle>,
    after_style: Option<ComputedStyle>,
    env: &mut LayoutEnv,
) -> bool {
    let available_width = ctx.available_width();
    let available_height = ctx.available_height();
    let abs_containing_block = ctx.containing_block;
    // Compute effective block width considering CSS width/max-width/min-width.
    // Block elements without explicit width shrink by their horizontal margins.
    let margin_h = style.margin.left + style.margin.right;
    let mut block_w = available_width;
    if let Some(pct) = style.percentage_sizing.width {
        // Percentage width resolves against the actual layout parent width,
        // not the style-time parent width stored in style.width.
        block_w = (pct / 100.0 * available_width).min(available_width);
    } else if let Some(w) = style.width {
        block_w = w.min(available_width);
    } else if margin_h > 0.0 {
        block_w = (available_width - margin_h).max(0.0);
    }
    if let Some(pct) = style.percentage_sizing.max_width {
        block_w = block_w.min(pct / 100.0 * available_width);
    } else if let Some(mw) = style.max_width {
        block_w = block_w.min(mw);
    }
    if let Some(pct) = style.percentage_sizing.min_width {
        block_w = block_w.max(pct / 100.0 * available_width);
    } else if let Some(mw) = style.min_width {
        block_w = block_w.max(mw);
    }

    // Compute effective height considering CSS height/min-height/max-height
    let mut effective_height = style.height;
    if effective_height.is_none() {
        if let Some(pct) = style.percentage_sizing.height {
            // Resolve percentage height against containing block if available
            if let Some(cb) = abs_containing_block {
                effective_height = Some(pct / 100.0 * cb.height);
            }
        }
    }
    if let Some(min_h) = style.min_height {
        effective_height = Some(effective_height.map_or(min_h, |h| h.max(min_h)));
    }
    if let Some(max_h) = style.max_height {
        effective_height = effective_height.map(|h| h.min(max_h));
    }

    // Compute margin auto offset for horizontal centering
    let has_explicit_width = style.width.is_some()
        || style.max_width.is_some()
        || style.min_width.is_some()
        || style.percentage_sizing.width.is_some();
    let auto_offset_left = if has_explicit_width && block_w < available_width {
        if style.margin_left_auto && style.margin_right_auto {
            (available_width - block_w) / 2.0
        } else if style.margin_left_auto {
            available_width - block_w
        } else {
            style.margin.left
        }
    } else {
        style.margin.left
    };

    // Adjust for box-sizing: border-box
    // When border-box, the specified width includes padding and border,
    // so the content area is width minus padding and border.
    let inner_width = if style.box_sizing == BoxSizing::BorderBox {
        block_w - style.padding.left - style.padding.right - style.border.horizontal_width()
    } else {
        block_w - style.padding.left - style.padding.right
    };
    let inner_width = inner_width.max(0.0);

    // Resolve percentage border-radius against element dimensions
    if let Some(pct) = style.border_radius_pct {
        let dim = if let Some(h) = effective_height {
            block_w.min(h)
        } else {
            block_w
        };
        style.border_radius = dim * pct / 100.0;
    }

    let style = &*style;

    let ib_ctx = ctx.with_parent(inner_width, ctx.parent.content_height, style.font_size);

    let positioned_container =
        style.position == Position::Relative || style.position == Position::Absolute;
    let make_containing_block = |padding_box_height: f32| {
        if positioned_container {
            let cb_width = if style.box_sizing == BoxSizing::BorderBox {
                block_w - style.border.horizontal_width()
            } else {
                block_w + style.padding.left + style.padding.right
            };
            Some(ContainingBlock {
                x: style.left.unwrap_or(0.0)
                    + auto_offset_left
                    + style.border.left.width
                    + style.padding.left,
                width: cb_width,
                height: padding_box_height,
                depth: positioned_depth,
            })
        } else {
            None
        }
    };

    // Emit block-level ::before pseudo-element.
    let before_is_abs = before_style
        .as_ref()
        .is_some_and(|s| s.position == Position::Absolute);
    let after_is_abs = after_style
        .as_ref()
        .is_some_and(|s| s.position == Position::Absolute);
    if let Some(ref ps) = before_style {
        if pseudo_is_block_like(ps) && !before_is_abs {
            output.push(build_pseudo_block(
                ps,
                el,
                inner_width,
                env.fonts,
                None,
                positioned_depth,
                env.counter_state,
            ));
        }
    }

    // When the element has absolute pseudo-elements, skip inline text
    // collection. The wrapper path will handle all children via
    // flatten_element, avoiding double-rendering of text.
    let skip_inline_collection = positioned_container && (before_is_abs || after_is_abs);

    // Collect inline content as text runs, splitting at math elements.
    // When a math span is encountered, flush accumulated text runs as a
    // TextBlock, emit a MathBlock, then continue collecting.
    let mut runs = Vec::new();
    if !skip_inline_collection {
        append_pseudo_inline_run(
            &mut runs,
            before_style.as_ref(),
            el,
            env.fonts,
            env.counter_state,
        );
    }

    // Helper closure: flush accumulated runs as a TextBlock
    let flush_runs = |runs: &mut Vec<TextRun>,
                      inner_width: f32,
                      style: &ComputedStyle,
                      available_width: f32,
                      block_w: f32,
                      effective_height: Option<f32>,
                      auto_offset_left: f32,
                      el: &ElementNode,
                      output: &mut Vec<LayoutElement>,
                      fonts: &HashMap<String, TtfFont>| {
        if runs.is_empty() {
            return;
        }
        let wrap_width = if style.white_space == WhiteSpace::NoWrap {
            f32::MAX
        } else {
            inner_width
        };
        let lines = wrap_text_runs(
            std::mem::take(runs),
            TextWrapOptions::new(
                wrap_width,
                style.font_size,
                resolved_line_height_factor(style, fonts),
                style.overflow_wrap,
            ),
            fonts,
        );
        if lines.is_empty() {
            return;
        }
        // For inline-block without explicit width, shrink-to-fit
        let render_w = if style.display == Display::InlineBlock
            && style.width.is_none()
            && style.percentage_sizing.width.is_none()
        {
            let max_line_w: f32 = lines
                .iter()
                .map(|l| {
                    l.runs
                        .iter()
                        .map(|r| {
                            crate::fonts::str_width(&r.text, r.font_size, &r.font_family, r.bold)
                        })
                        .sum::<f32>()
                })
                .fold(0.0f32, f32::max);
            let shrink_w = max_line_w
                + style.padding.left
                + style.padding.right
                + style.border.horizontal_width();
            shrink_w.min(block_w)
        } else {
            block_w
        };

        let bg = style
            .background_color
            .map(|c: crate::types::Color| c.to_f32_rgba());
        let explicit_width = if render_w < available_width
            || style.min_width.is_some()
            || style.display == Display::InlineBlock
        {
            Some(render_w)
        } else {
            None
        };
        let BackgroundFields {
            gradient: background_gradient,
            radial_gradient: background_radial_gradient,
            svg: background_svg,
            blur_radius: background_blur_radius,
            size: background_size,
            position: background_position,
            repeat: background_repeat,
            origin: background_origin,
        } = BackgroundFields::from_style(style);
        output.push(LayoutElement::TextBlock {
            lines,
            margin_top: style.margin.top,
            margin_bottom: 0.0,
            text_align: style.text_align,
            background_color: bg,
            padding_top: style.padding.top,
            padding_bottom: style.padding.bottom,
            padding_left: style.padding.left,
            padding_right: style.padding.right,
            border: LayoutBorder::from_computed(&style.border),
            block_width: explicit_width,
            block_height: effective_height,
            opacity: style.opacity,
            float: style.float,
            clear: style.clear,
            position: style.position,
            offset_top: style.top.unwrap_or(0.0),
            offset_left: style.left.unwrap_or(0.0) + auto_offset_left,
            offset_bottom: style.bottom.unwrap_or(0.0),
            offset_right: style.right.unwrap_or(0.0),
            containing_block: None,
            box_shadow: style.box_shadow,
            visible: style.visibility == Visibility::Visible,
            clip_rect: None,
            transform: style.transform,
            border_radius: style.border_radius,
            outline_width: style.outline_width,
            outline_color: style.outline_color.map(|c| c.to_f32_rgb()),
            text_indent: style.text_indent,
            letter_spacing: style.letter_spacing,
            word_spacing: style.word_spacing,
            vertical_align: style.vertical_align,
            background_gradient,
            background_radial_gradient,
            background_svg,
            background_blur_radius,
            background_size,
            background_position,
            background_repeat,
            background_origin,
            z_index: style.z_index,
            repeat_on_each_page: false,
            positioned_depth,
            heading_level: heading_level(el.tag),
            clip_children_count: 0,
        });
    };

    // Check if any child is a math element — if so, split at boundaries
    let has_math_children = el.children.iter().any(|c| {
        if let DomNode::Element(child) = c {
            child.attributes.contains_key("data-math")
        } else {
            false
        }
    });

    // Check if this block has visual properties AND block children.
    // When true, inline text is collected separately and block children
    // are processed via the needs_wrapper path for correct nesting.
    let early_has_visual = has_background_paint(style)
        || style.border.has_any()
        || style.border_radius > 0.0
        || style.box_shadow.is_some();
    // Limit nesting depth to prevent stack overflow on deeply nested HTML.
    // Beyond depth 40, fall back to flat text collection instead of Containers.
    let nesting_depth = ancestors.len();
    let has_block_kids_for_wrapper = nesting_depth < 40
        && early_has_visual
        && el.children.iter().any(|c| {
            matches!(c, DomNode::Element(e)
                if (e.tag.is_block() || e.tag == HtmlTag::Svg)
                    && !collects_as_inline_text(e.tag))
        });

    if has_math_children {
        // Split mode: interleave TextBlocks and MathBlocks
        for child in &el.children {
            match child {
                DomNode::Element(child_el) if child_el.attributes.contains_key("data-math") => {
                    // Flush accumulated text runs before math
                    flush_runs(
                        &mut runs,
                        inner_width,
                        style,
                        available_width,
                        block_w,
                        effective_height,
                        auto_offset_left,
                        el,
                        output,
                        env.fonts,
                    );
                    // Emit math block
                    let tex = child_el.attributes.get("data-math").unwrap();
                    let child_classes = child_el.class_list();
                    let is_display = child_classes.contains(&"math-display");
                    let ast = crate::parser::math::parse_math(tex);
                    let math_layout =
                        crate::layout::math::layout_math(&ast, style.font_size, is_display);
                    output.push(LayoutElement::MathBlock {
                        layout: math_layout,
                        display: is_display,
                        margin_top: 0.0,
                        margin_bottom: 0.0,
                    });
                }
                _ => {
                    // Collect text from this child
                    collect_text_runs(
                        std::slice::from_ref(child),
                        style,
                        &mut runs,
                        None,
                        env.rules,
                        env.fonts,
                        child_ancestors,
                    );
                }
            }
        }
        // Flush remaining text runs after math
        flush_runs(
            &mut runs,
            inner_width,
            style,
            available_width,
            block_w,
            effective_height,
            auto_offset_left,
            el,
            output,
            env.fonts,
        );
    } else {
        // Check if children contain block-level elements that have their own
        // margins (e.g. <p>, <h1>-<h6>, <ul>, <ol>, <blockquote>).
        // These need individual layout via flatten_element to preserve
        // their margins. Generic containers (<div>) are not included to
        // avoid expensive recursion on deeply nested structures.
        fn has_own_margins(tag: HtmlTag) -> bool {
            matches!(
                tag,
                HtmlTag::P
                    | HtmlTag::H1
                    | HtmlTag::H2
                    | HtmlTag::H3
                    | HtmlTag::H4
                    | HtmlTag::H5
                    | HtmlTag::H6
                    | HtmlTag::Ul
                    | HtmlTag::Ol
                    | HtmlTag::Li
                    | HtmlTag::Blockquote
                    | HtmlTag::Pre
                    | HtmlTag::Hr
                    | HtmlTag::Dl
                    | HtmlTag::Dt
                    | HtmlTag::Dd
                    | HtmlTag::Figure
                    | HtmlTag::Table
            )
        }
        let parent_has_visual = has_background_paint(style)
            || style.border.has_any()
            || style.border_radius > 0.0
            || style.box_shadow.is_some();
        // Check early if this positioned container has absolute children.
        // When true, skip the has_block_children fast path so we use the
        // Container/wrapper path instead, preserving the containing block.
        let early_has_abs_children = positioned_container
            && el.children.iter().any(|c| {
                if let DomNode::Element(e) = c {
                    // Quick inline style check
                    let s = e.style_attr().unwrap_or("");
                    if s.contains("absolute") {
                        return true;
                    }
                    // Check stylesheet rules
                    let cls = e.class_list();
                    let cls_refs: Vec<&str> = cls.iter().map(|s| s.as_ref()).collect();
                    let cs = compute_style_with_context(
                        e.tag,
                        e.style_attr(),
                        style,
                        env.rules,
                        e.tag_name(),
                        &cls_refs,
                        e.id(),
                        &e.attributes,
                        &SelectorContext::default(),
                    );
                    cs.position == Position::Absolute
                } else {
                    false
                }
            });
        let has_abs_pseudo_early = positioned_container && (before_is_abs || after_is_abs);
        let has_block_children = !parent_has_visual
            && !early_has_abs_children
            && !has_abs_pseudo_early
            && el.children.iter().any(|c| {
                matches!(c, DomNode::Element(e)
                    if (has_own_margins(e.tag)
                        || (e.tag.is_block() && !collects_as_inline_text(e.tag)))
                        && !element_is_inline_block(
                            e, style, env.rules, child_ancestors, 0, 0, &[]))
            });

        if skip_inline_collection {
            // All content will be handled by the wrapper path below.
            // Don't collect inline text — the <p> children will be
            // processed via flatten_element in the Container wrapper.
        } else if has_block_children {
            // For visual containers (border, background), emit a wrapper
            // TextBlock first, then a pullback spacer so children render
            // inside the wrapper's padding area.
            let wrapper_output_idx = output.len();
            if parent_has_visual {
                let bg = style
                    .background_color
                    .map(|c: crate::types::Color| c.to_f32_rgba());
                let BackgroundFields {
                    gradient: bg_grad,
                    radial_gradient: bg_rgrad,
                    svg: bg_svg,
                    blur_radius: bg_blur,
                    size: bg_size,
                    position: bg_pos,
                    repeat: bg_repeat,
                    origin: bg_origin,
                } = BackgroundFields::from_style(style);
                // Wrapper height will be patched after children are processed.
                let wrapper_h = effective_height.map_or(0.0, |h| {
                    resolve_padding_box_height(
                        0.0,
                        Some(h),
                        style.padding.top,
                        style.padding.bottom,
                        style.border.vertical_width(),
                        style.box_sizing,
                    )
                });
                output.push(LayoutElement::TextBlock {
                    lines: Vec::new(),
                    margin_top: style.margin.top,
                    margin_bottom: 0.0,
                    text_align: style.text_align,
                    background_color: bg,
                    padding_top: 0.0,
                    padding_bottom: 0.0,
                    padding_left: style.padding.left,
                    padding_right: style.padding.right,
                    border: LayoutBorder::from_computed(&style.border),
                    block_width: Some(block_w),
                    block_height: effective_height.map(|_| wrapper_h),
                    opacity: style.opacity,
                    float: style.float,
                    clear: style.clear,
                    position: style.position,
                    offset_top: style.top.unwrap_or(0.0),
                    offset_left: style.left.unwrap_or(0.0) + auto_offset_left,
                    offset_bottom: style.bottom.unwrap_or(0.0),
                    offset_right: style.right.unwrap_or(0.0),
                    containing_block: None,
                    clip_children_count: 0,
                    box_shadow: style.box_shadow,
                    visible: style.visibility == Visibility::Visible,
                    clip_rect: if style.overflow == Overflow::Hidden {
                        Some((0.0, 0.0, block_w, wrapper_h))
                    } else {
                        None
                    },
                    transform: style.transform,
                    border_radius: style.border_radius,
                    outline_width: style.outline_width,
                    outline_color: style.outline_color.map(|c| c.to_f32_rgb()),
                    text_indent: 0.0,
                    letter_spacing: 0.0,
                    word_spacing: 0.0,
                    vertical_align: VerticalAlign::Baseline,
                    background_gradient: bg_grad,
                    background_radial_gradient: bg_rgrad,
                    background_svg: bg_svg,
                    background_blur_radius: bg_blur,
                    background_size: bg_size,
                    background_position: bg_pos,
                    background_repeat: bg_repeat,
                    background_origin: bg_origin,
                    z_index: style.z_index,
                    repeat_on_each_page: false,
                    positioned_depth,
                    heading_level: None,
                });
                // Pullback spacer
                let pullback = if effective_height.is_some() && wrapper_h > 0.0 {
                    wrapper_h - style.padding.top
                } else {
                    0.0
                };
                if pullback > 0.0 {
                    output.push(LayoutElement::TextBlock {
                        lines: Vec::new(),
                        margin_top: -pullback,
                        margin_bottom: 0.0,
                        text_align: TextAlign::Left,
                        background_color: None,
                        padding_top: 0.0,
                        padding_bottom: 0.0,
                        padding_left: style.padding.left,
                        padding_right: style.padding.right,
                        border: LayoutBorder::default(),
                        block_width: None,
                        block_height: None,
                        opacity: 1.0,
                        float: Float::None,
                        clear: Clear::None,
                        position: Position::Static,
                        offset_top: 0.0,
                        offset_left: 0.0,
                        offset_bottom: 0.0,
                        offset_right: 0.0,
                        containing_block: None,
                        clip_children_count: 0,
                        box_shadow: None,
                        visible: true,
                        clip_rect: None,
                        transform: None,
                        border_radius: 0.0,
                        outline_width: 0.0,
                        outline_color: None,
                        text_indent: 0.0,
                        letter_spacing: 0.0,
                        word_spacing: 0.0,
                        vertical_align: VerticalAlign::Baseline,
                        background_gradient: None,
                        background_radial_gradient: None,
                        background_svg: None,
                        background_blur_radius: 0.0,
                        background_size: BackgroundSize::Auto,
                        background_position: BackgroundPosition::default(),
                        background_repeat: BackgroundRepeat::Repeat,
                        background_origin: BackgroundOrigin::Padding,
                        z_index: 0,
                        repeat_on_each_page: false,
                        positioned_depth: 0,
                        heading_level: None,
                    });
                }
            }

            // Mixed inline + block children: split at block boundaries.
            let mut block_child_buf: Vec<LayoutElement> = Vec::new();
            let target: &mut Vec<LayoutElement> = if parent_has_visual {
                &mut block_child_buf
            } else {
                output
            };
            for child in &el.children {
                match child {
                    DomNode::Text(_) => {
                        collect_text_runs(
                            std::slice::from_ref(child),
                            style,
                            &mut runs,
                            None,
                            env.rules,
                            env.fonts,
                            child_ancestors,
                        );
                    }
                    DomNode::Element(child_el)
                        if (child_el.tag.is_block() || child_el.tag == HtmlTag::Svg)
                            && !collects_as_inline_text(child_el.tag) =>
                    {
                        // Flush inline runs before block child
                        flush_runs(
                            &mut runs,
                            inner_width,
                            style,
                            available_width,
                            block_w,
                            effective_height,
                            auto_offset_left,
                            el,
                            target,
                            env.fonts,
                        );
                        // Recurse into block child
                        let n_children = el
                            .children
                            .iter()
                            .filter(|c| matches!(c, DomNode::Element(_)))
                            .count();
                        flatten_element(
                            child_el,
                            style,
                            &ctx.with_parent(inner_width, Some(available_height), style.font_size)
                                .with_containing_block(None),
                            target,
                            None,
                            child_ancestors,
                            positioned_depth,
                            0,
                            n_children,
                            &[],
                            env,
                        );
                    }
                    DomNode::Element(_) => {
                        // Inline element: collect as text runs
                        collect_text_runs(
                            std::slice::from_ref(child),
                            style,
                            &mut runs,
                            None,
                            env.rules,
                            env.fonts,
                            child_ancestors,
                        );
                    }
                }
            }
            // Flush remaining inline runs after the last block child
            flush_runs(
                &mut runs,
                inner_width,
                style,
                available_width,
                block_w,
                effective_height,
                auto_offset_left,
                el,
                target,
                env.fonts,
            );
            // For visual containers, propagate parent padding to children
            // so they render inside the padded area.
            if parent_has_visual {
                if style.padding.left > 0.0 || style.padding.right > 0.0 {
                    for elem in &mut block_child_buf {
                        if let LayoutElement::TextBlock {
                            padding_left,
                            padding_right,
                            ..
                        } = elem
                        {
                            *padding_left += style.padding.left;
                            *padding_right += style.padding.right;
                        }
                    }
                }
                output.extend(block_child_buf);

                // Patch wrapper block_height to cover all children
                if effective_height.is_none() {
                    let children_total_h: f32 = output[wrapper_output_idx + 1..]
                        .iter()
                        .map(estimate_element_height)
                        .sum();
                    let patched_h = style.padding.top
                        + children_total_h
                        + style.padding.bottom
                        + style.border.vertical_width();
                    if let Some(LayoutElement::TextBlock { block_height, .. }) =
                        output.get_mut(wrapper_output_idx)
                    {
                        *block_height = Some(patched_h);
                    }
                }
            }
            // Add bottom spacer for visual containers
            if parent_has_visual {
                let bottom_space =
                    style.padding.bottom + style.border.vertical_width() + style.margin.bottom;
                if bottom_space > 0.0 {
                    output.push(LayoutElement::TextBlock {
                        lines: Vec::new(),
                        margin_top: bottom_space,
                        margin_bottom: 0.0,
                        text_align: TextAlign::Left,
                        background_color: None,
                        padding_top: 0.0,
                        padding_bottom: 0.0,
                        padding_left: 0.0,
                        padding_right: 0.0,
                        border: LayoutBorder::default(),
                        block_width: None,
                        block_height: None,
                        opacity: 1.0,
                        float: Float::None,
                        clear: Clear::None,
                        position: Position::Static,
                        offset_top: 0.0,
                        offset_left: 0.0,
                        offset_bottom: 0.0,
                        offset_right: 0.0,
                        containing_block: None,
                        clip_children_count: 0,
                        box_shadow: None,
                        visible: true,
                        clip_rect: None,
                        transform: None,
                        border_radius: 0.0,
                        outline_width: 0.0,
                        outline_color: None,
                        text_indent: 0.0,
                        letter_spacing: 0.0,
                        word_spacing: 0.0,
                        vertical_align: VerticalAlign::Baseline,
                        background_gradient: None,
                        background_radial_gradient: None,
                        background_svg: None,
                        background_blur_radius: 0.0,
                        background_size: BackgroundSize::Auto,
                        background_position: BackgroundPosition::default(),
                        background_repeat: BackgroundRepeat::Repeat,
                        background_origin: BackgroundOrigin::Padding,
                        z_index: 0,
                        repeat_on_each_page: false,
                        positioned_depth: 0,
                        heading_level: None,
                    });
                }
            }
            // Emit absolute-positioned ::before / ::after pseudo-elements
            if positioned_container && (before_is_abs || after_is_abs) {
                // Compute containing block from children height
                let children_h: f32 = output[wrapper_output_idx..]
                    .iter()
                    .map(estimate_element_height)
                    .sum();
                let pseudo_cb = Some(ContainingBlock {
                    x: 0.0,
                    width: block_w,
                    height: children_h,
                    depth: positioned_depth,
                });
                if before_is_abs {
                    push_block_pseudo(
                        output,
                        before_style.as_ref(),
                        el,
                        inner_width,
                        env.fonts,
                        pseudo_cb,
                        positioned_depth,
                        env.counter_state,
                    );
                }
                if after_is_abs {
                    push_block_pseudo(
                        output,
                        after_style.as_ref(),
                        el,
                        inner_width,
                        env.fonts,
                        pseudo_cb,
                        positioned_depth,
                        env.counter_state,
                    );
                }
            }

            if style.page_break_after {
                output.push(LayoutElement::PageBreak);
            }
            return true;
        } else if has_block_kids_for_wrapper {
            // Only collect inline children's text — block children will
            // be handled by the needs_wrapper path via flatten_element.
            for child in &el.children {
                match child {
                    DomNode::Text(_) => {
                        collect_text_runs(
                            std::slice::from_ref(child),
                            style,
                            &mut runs,
                            None,
                            env.rules,
                            env.fonts,
                            child_ancestors,
                        );
                    }
                    DomNode::Element(child_el) if collects_as_inline_text(child_el.tag) => {
                        collect_text_runs(
                            std::slice::from_ref(child),
                            style,
                            &mut runs,
                            None,
                            env.rules,
                            env.fonts,
                            child_ancestors,
                        );
                    }
                    _ => {} // Block children handled by needs_wrapper
                }
            }
        } else {
            collect_text_runs(
                &el.children,
                style,
                &mut runs,
                None,
                env.rules,
                env.fonts,
                child_ancestors,
            );
        }
    }
    if !skip_inline_collection {
        append_pseudo_inline_run(
            &mut runs,
            after_style.as_ref(),
            el,
            env.fonts,
            env.counter_state,
        );
    }

    let had_inline_runs = runs.iter().any(|r| !r.text.trim().is_empty()) || has_math_children;
    let mut cb_info = None;

    // has_block_kids_for_wrapper is computed earlier (before has_math_children).
    let mut saved_inline_element: Option<LayoutElement> = None;

    if !runs.is_empty() {
        // When white-space: nowrap, prevent wrapping by using a huge width
        let wrap_width = if style.white_space == WhiteSpace::NoWrap {
            f32::MAX
        } else {
            inner_width
        };
        let mut lines = wrap_text_runs(
            runs,
            TextWrapOptions::new(
                wrap_width,
                style.font_size,
                resolved_line_height_factor(style, env.fonts),
                style.overflow_wrap,
            ),
            env.fonts,
        );

        // Apply text-overflow: ellipsis when overflow is hidden, white-space
        // is nowrap, and we have a fixed width.
        if style.text_overflow == TextOverflow::Ellipsis
            && style.overflow == Overflow::Hidden
            && style.white_space == WhiteSpace::NoWrap
            && style.width.is_some()
        {
            apply_text_overflow_ellipsis(&mut lines, inner_width, env.fonts);
        }

        let bg = style
            .background_color
            .map(|c: crate::types::Color| c.to_f32_rgba());

        let explicit_width = if block_w < available_width || style.min_width.is_some() {
            Some(block_w)
        } else {
            None
        };

        // Compute clip rect before moving lines
        let clip_rect = if style.overflow == Overflow::Hidden {
            let text_height: f32 = lines.iter().map(|l| l.height).sum();
            let total_h = resolve_padding_box_height(
                text_height,
                effective_height,
                style.padding.top,
                style.padding.bottom,
                style.border.vertical_width(),
                style.box_sizing,
            );
            Some((0.0, 0.0, block_w, total_h))
        } else {
            None
        };
        let BackgroundFields {
            gradient: background_gradient,
            radial_gradient: background_radial_gradient,
            svg: background_svg,
            blur_radius: background_blur_radius,
            size: background_size,
            position: background_position,
            repeat: background_repeat,
            origin: background_origin,
        } = BackgroundFields::from_style(style);
        let text_height: f32 = lines.iter().map(|l| l.height).sum();
        let total_h = resolve_padding_box_height(
            text_height,
            effective_height,
            style.padding.top,
            style.padding.bottom,
            style.border.vertical_width(),
            style.box_sizing,
        );
        cb_info = make_containing_block(total_h);

        // Resolve containing block and offsets for absolute elements
        let (elem_cb, resolved_top, resolved_left) = resolve_abs_containing_block(
            style,
            abs_containing_block,
            total_h,
            explicit_width.unwrap_or(block_w),
        );

        // When this block has visual properties AND block children,
        // save the inline text for inclusion inside the wrapper instead
        // of emitting it directly.  The wrapper path will use it.
        let inline_tb = LayoutElement::TextBlock {
            lines,
            margin_top: if has_block_kids_for_wrapper {
                0.0
            } else {
                style.margin.top
            },
            margin_bottom: if has_block_kids_for_wrapper {
                0.0
            } else {
                style.margin.bottom
            },
            text_align: style.text_align,
            background_color: if has_block_kids_for_wrapper { None } else { bg },
            padding_top: if has_block_kids_for_wrapper {
                0.0
            } else {
                style.padding.top
            },
            padding_bottom: style.padding.bottom,
            padding_left: style.padding.left,
            padding_right: style.padding.right,
            border: LayoutBorder::from_computed(&style.border),
            block_width: explicit_width,
            block_height: effective_height.map(|_| total_h),
            opacity: style.opacity,
            float: style.float,
            clear: style.clear,
            position: style.position,
            offset_top: resolved_top,
            offset_left: resolved_left + auto_offset_left,
            offset_bottom: style.bottom.unwrap_or(0.0),
            offset_right: style.right.unwrap_or(0.0),
            containing_block: elem_cb,
            box_shadow: style.box_shadow,
            visible: style.visibility == Visibility::Visible,
            clip_rect,
            transform: style.transform,
            border_radius: style.border_radius,
            outline_width: style.outline_width,
            outline_color: style.outline_color.map(|c| c.to_f32_rgb()),
            text_indent: style.text_indent,
            letter_spacing: style.letter_spacing,
            word_spacing: style.word_spacing,
            vertical_align: style.vertical_align,
            background_gradient,
            background_radial_gradient,
            background_svg,
            background_blur_radius,
            background_size,
            background_position,
            background_repeat,
            background_origin,
            z_index: style.z_index,
            repeat_on_each_page: false,
            positioned_depth,
            heading_level: heading_level(el.tag),
            clip_children_count: 0,
        };
        // Compute needs_wrapper early so we know whether to push the
        // TextBlock or save it for the Container wrapper path.
        let early_has_visual_for_wrapper = has_background_paint(style)
            || style.border.has_any()
            || style.border_radius > 0.0
            || style.box_shadow.is_some();
        let early_needs_wrapper = early_has_visual_for_wrapper
            || style.aspect_ratio.is_some()
            || style.height.is_some()
            || (positioned_container && (before_is_abs || after_is_abs))
            || skip_inline_collection;
        let early_no_inline = !had_inline_runs;

        if has_block_kids_for_wrapper {
            saved_inline_element = Some(inline_tb);
        } else if early_no_inline && early_needs_wrapper {
            // Don't push empty TextBlock — the wrapper path will
            // create a Container with the correct block_width.
            saved_inline_element = Some(inline_tb);
        } else {
            output.push(inline_tb);
        }
        push_block_pseudo(
            output,
            before_style.as_ref(),
            el,
            inner_width,
            env.fonts,
            cb_info,
            positioned_depth,
            env.counter_state,
        );
    }

    // Also process block children recursively, using inner_width
    // so children respect the parent's padding boundaries.
    let child_el_count = el
        .children
        .iter()
        .filter(|c| matches!(c, DomNode::Element(_)))
        .count();

    // If no inline content but the element has visual properties (background,
    // gradient, border, border-radius), emit a wrapper TextBlock so the visuals
    // are rendered.  Children are then pulled back inside via a negative-margin
    // spacer (same technique as flex column containers).
    // NB: check before runs is moved into wrap_text_runs above.
    let has_visual = has_background_paint(style)
        || style.border.has_any()
        || style.border_radius > 0.0
        || style.box_shadow.is_some();
    // A positioned container (position: relative/absolute) needs the
    // Container element to establish a containing block for absolute children.
    let has_abs_children = positioned_container
        && el.children.iter().any(|c| {
            if let DomNode::Element(e) = c {
                let cls = e.class_list();
                let cls_refs: Vec<&str> = cls.iter().map(|s| s.as_ref()).collect();
                let child_style = compute_style_with_context(
                    e.tag,
                    e.style_attr(),
                    style,
                    env.rules,
                    e.tag_name(),
                    &cls_refs,
                    e.id(),
                    &e.attributes,
                    &SelectorContext::default(),
                );
                child_style.position == Position::Absolute
            } else {
                false
            }
        });
    let needs_wrapper = has_visual
        || style.aspect_ratio.is_some()
        || style.height.is_some()
        || (positioned_container && (before_is_abs || after_is_abs))
        || has_abs_children;
    let no_inline_content = !had_inline_runs;

    let has_abs_pseudo = positioned_container && (before_is_abs || after_is_abs);
    if (no_inline_content || has_block_kids_for_wrapper || has_abs_pseudo)
        && needs_wrapper
        && nesting_depth < 40
    {
        // Pre-flatten children to measure total height.
        // If there's saved inline content, include it as the first child.
        let mut child_elements: Vec<LayoutElement> = Vec::new();
        if let Some(inline_el) = saved_inline_element.take() {
            child_elements.push(inline_el);
        }
        let mut child_el_idx = 0;
        let mut ib_group_wrapper: Vec<&ElementNode> = Vec::new();
        for child in &el.children {
            if let DomNode::Element(child_el) = child {
                if recurses_as_layout_child(child_el.tag)
                    && element_is_inline_block(
                        child_el,
                        style,
                        env.rules,
                        child_ancestors,
                        child_el_idx,
                        child_el_count,
                        &[],
                    )
                {
                    ib_group_wrapper.push(child_el);
                } else {
                    // Flush any pending inline-block group
                    if !ib_group_wrapper.is_empty() {
                        #[allow(clippy::drain_collect)]
                        let taken: Vec<&ElementNode> = ib_group_wrapper.drain(..).collect();
                        layout_inline_block_group(
                            &taken,
                            style,
                            &ib_ctx,
                            &mut child_elements,
                            env.rules,
                            child_ancestors,
                            env.fonts,
                        );
                    }
                    if recurses_as_layout_child(child_el.tag) {
                        let child_cb = if effective_height.is_some() {
                            Some(ContainingBlock {
                                x: 0.0,
                                width: inner_width,
                                height: effective_height.unwrap_or(0.0),
                                depth: positioned_depth,
                            })
                        } else {
                            None
                        };
                        flatten_element(
                            child_el,
                            style,
                            &ctx.with_parent(inner_width, Some(available_height), style.font_size)
                                .with_containing_block(child_cb),
                            &mut child_elements,
                            None,
                            child_ancestors,
                            positioned_depth,
                            child_el_idx,
                            child_el_count,
                            &[],
                            env,
                        );
                    }
                }
                child_el_idx += 1;
            }
        }
        // Flush remaining inline-block group
        if !ib_group_wrapper.is_empty() {
            #[allow(clippy::drain_collect)]
            let taken: Vec<&ElementNode> = ib_group_wrapper.drain(..).collect();
            layout_inline_block_group(
                &taken,
                style,
                &ib_ctx,
                &mut child_elements,
                env.rules,
                child_ancestors,
                env.fonts,
            );
        }
        // Measure children total height
        let children_h: f32 = child_elements.iter().map(estimate_element_height).sum();
        let mut container_h = resolve_padding_box_height(
            children_h,
            effective_height,
            style.padding.top,
            style.padding.bottom,
            style.border.vertical_width(),
            style.box_sizing,
        );
        if effective_height.is_none()
            && let Some(aspect_h) = aspect_ratio_height(block_w, style)
        {
            container_h = container_h.max(aspect_h);
        }
        cb_info = make_containing_block(container_h);

        // Add absolute-positioned ::before pseudo-element as a Container child.
        if let Some(ref ps) = before_style {
            if pseudo_is_block_like(ps) && ps.position == Position::Absolute {
                child_elements.push(build_pseudo_block(
                    ps,
                    el,
                    inner_width,
                    env.fonts,
                    cb_info,
                    positioned_depth,
                    env.counter_state,
                ));
            }
        }
        // Add absolute-positioned ::after pseudo-element as a Container child.
        if let Some(ref ps) = after_style {
            if pseudo_is_block_like(ps) && ps.position == Position::Absolute {
                child_elements.push(build_pseudo_block(
                    ps,
                    el,
                    inner_width,
                    env.fonts,
                    cb_info,
                    positioned_depth,
                    env.counter_state,
                ));
            }
        }

        // Patch absolute children with the now-known containing block,
        // and resolve bottom/right offsets into top/left.
        if let Some(cb) = cb_info {
            patch_absolute_children_containing_block(&mut child_elements, cb);
        }

        let bg = style
            .background_color
            .map(|c: crate::types::Color| c.to_f32_rgba());
        let BackgroundFields {
            gradient: background_gradient,
            radial_gradient: background_radial_gradient,
            svg: background_svg,
            blur_radius: background_blur_radius,
            size: background_size,
            position: background_position,
            repeat: background_repeat,
            origin: background_origin,
        } = BackgroundFields::from_style(style);
        // Resolve containing block and offsets for absolute elements
        let (_wrapper_cb, wrapper_top, wrapper_left) =
            resolve_abs_containing_block(style, abs_containing_block, container_h, block_w);
        // Emit a Container element with true parent-child nesting.
        // The renderer draws background/border, then renders children inside.
        output.push(LayoutElement::Container {
            children: child_elements,
            background_color: bg,
            border: LayoutBorder::from_computed(&style.border),
            border_radius: style.border_radius,
            padding_top: style.padding.top,
            padding_bottom: style.padding.bottom,
            padding_left: style.padding.left,
            padding_right: style.padding.right,
            margin_top: style.margin.top,
            margin_bottom: style.margin.bottom,
            block_width: Some(block_w),
            block_height: if effective_height.is_some() || style.aspect_ratio.is_some() {
                Some(container_h)
            } else {
                None
            },
            opacity: style.opacity,
            float: style.float,
            position: style.position,
            offset_top: wrapper_top,
            offset_left: wrapper_left + auto_offset_left,
            overflow: style.overflow,
            transform: style.transform,
            box_shadow: style.box_shadow,
            background_gradient,
            background_radial_gradient,
            background_svg,
            background_blur_radius,
            background_size,
            background_position,
            background_repeat,
            background_origin,
            z_index: style.z_index,
        });
    } else {
        if no_inline_content {
            push_block_pseudo(
                output,
                before_style.as_ref(),
                el,
                inner_width,
                env.fonts,
                cb_info,
                positioned_depth,
                env.counter_state,
            );
        }
        // Compute cb_info for positioned containers in the non-wrapper path
        // so that absolute children get a containing block.
        if cb_info.is_none() && positioned_container {
            let h = effective_height.unwrap_or(0.0);
            cb_info = make_containing_block(h);
        }
        let mut child_el_idx = 0;
        let mut ib_group: Vec<&ElementNode> = Vec::new();
        for child in &el.children {
            if let DomNode::Element(child_el) = child {
                if recurses_as_layout_child(child_el.tag)
                    && element_is_inline_block(
                        child_el,
                        style,
                        env.rules,
                        child_ancestors,
                        child_el_idx,
                        child_el_count,
                        &[],
                    )
                {
                    ib_group.push(child_el);
                } else {
                    // Flush any pending inline-block group
                    if !ib_group.is_empty() {
                        #[allow(clippy::drain_collect)]
                        let taken: Vec<&ElementNode> = ib_group.drain(..).collect();
                        layout_inline_block_group(
                            &taken,
                            style,
                            &ib_ctx,
                            output,
                            env.rules,
                            child_ancestors,
                            env.fonts,
                        );
                    }
                    if recurses_as_layout_child(child_el.tag) {
                        flatten_element(
                            child_el,
                            style,
                            &ctx.with_parent(inner_width, Some(available_height), style.font_size)
                                .with_containing_block(cb_info),
                            output,
                            None,
                            child_ancestors,
                            positioned_depth,
                            child_el_idx,
                            child_el_count,
                            &[],
                            env,
                        );
                    }
                }
                child_el_idx += 1;
            }
        }
        // Flush remaining inline-block group
        if !ib_group.is_empty() {
            #[allow(clippy::drain_collect)]
            let taken: Vec<&ElementNode> = ib_group.drain(..).collect();
            layout_inline_block_group(
                &taken,
                style,
                &ib_ctx,
                output,
                env.rules,
                child_ancestors,
                env.fonts,
            );
        }
    }

    // Emit block-level ::after pseudo-element (inside block path)
    push_block_pseudo(
        output,
        after_style.as_ref(),
        el,
        inner_width,
        env.fonts,
        cb_info,
        positioned_depth,
        env.counter_state,
    );
    false
}