azul-layout 0.0.9

Layout solver + font and image loader the Azul GUI framework
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
//! Diagnostic tests for the table cell width bug.
//!
//! Bug: `<td><span><a>text</a></span></td>` gets 0 min-content width
//! in a 3-cell table row, but works fine in a 2-cell table and for
//! `<td><a>text</a></td>` (single inline wrapper).
//!
//! These tests do NOT fix anything. They dump the layout tree structure,
//! cache state, and measure results to pinpoint the root cause.

use azul_core::{
    dom::{Dom, DomId, DomNodeId, FormattingContext, IdOrClass, NodeId, NodeType},
    geom::LogicalSize,
    resources::RendererResources,
    styled_dom::{NodeHierarchyItemId, StyledDom},
};
use azul_layout::{
    callbacks::ExternalSystemCallbacks,
    solver3::layout_tree::{AnonymousBoxType, LayoutTree},
    window::LayoutWindow,
    window_state::FullWindowState,
};
use rust_fontconfig::FcFontCache;

fn create_layout_window() -> LayoutWindow {
    let font_cache = FcFontCache::build();
    LayoutWindow::new(font_cache).unwrap()
}

fn create_window_state(width: f32, height: f32) -> FullWindowState {
    let mut window_state = FullWindowState::default();
    window_state.size.dimensions = LogicalSize::new(width, height);
    window_state
}

fn layout_dom(dom: Dom, css_str: &str, width: f32, height: f32) -> LayoutWindow {
    let (css, _) = azul_css::parser2::new_from_str(css_str);
    let mut dom = dom;
    let styled_dom = StyledDom::create(&mut dom, css);

    let mut layout_window = create_layout_window();
    let window_state = create_window_state(width, height);
    let renderer_resources = RendererResources::default();
    let system_callbacks = ExternalSystemCallbacks::rust_internal();
    let mut debug_messages = Some(Vec::new());

    layout_window
        .layout_and_generate_display_list(
            styled_dom,
            &window_state,
            &renderer_resources,
            &system_callbacks,
            &mut debug_messages,
        )
        .unwrap();

    layout_window
}

fn node_id(index: usize) -> DomNodeId {
    DomNodeId {
        dom: DomId::ROOT_ID,
        node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(index))),
    }
}

/// Helper: dump the full layout tree structure for a LayoutWindow.
/// Prints node index, DOM ID, formatting context, anonymous type,
/// parent, children, and used_size for every node.
fn dump_layout_tree(lw: &LayoutWindow, label: &str) {
    let tree = lw.layout_cache.tree.as_ref().expect("layout tree missing");
    let positions = &lw.layout_cache.calculated_positions;

    eprintln!("\n============================================================");
    eprintln!("=== LAYOUT TREE DUMP: {} ===", label);
    eprintln!("Total nodes: {}", tree.nodes.len());
    eprintln!("============================================================");

    for i in 0..tree.nodes.len() {
        let hot = &tree.nodes[i];
        let cold = &tree.cold[i];
        let warm = &tree.warm[i];
        let children = tree.children(i);

        let dom_id_str = match hot.dom_node_id {
            Some(id) => format!("dom={}", id.index()),
            None => "ANON".to_string(),
        };

        let fc_str = format!("{:?}", hot.formatting_context);

        let anon_str = match cold.anonymous_type {
            Some(at) => format!(" anon={:?}", at),
            None => String::new(),
        };

        let parent_str = match hot.parent {
            Some(p) => format!("{}", p),
            None => "ROOT".to_string(),
        };

        let size_str = match hot.used_size {
            Some(s) => format!("w={:.1} h={:.1}", s.width, s.height),
            None => "NO SIZE".to_string(),
        };

        let pos_str = match positions.get(i) {
            Some(p) => format!("x={:.1} y={:.1}", p.x, p.y),
            None => "NO POS".to_string(),
        };

        let overflow_str = match &warm.overflow_content_size {
            Some(s) => format!(" overflow=({:.1},{:.1})", s.width, s.height),
            None => String::new(),
        };

        let display_str = format!("{:?}", warm.computed_style.display);

        eprintln!(
            "  [{:>2}] {} fc={:<30} parent={:<5} children={:?} {} {} display={}{}{}",
            i,
            dom_id_str,
            fc_str,
            parent_str,
            children,
            size_str,
            pos_str,
            display_str,
            anon_str,
            overflow_str,
        );
    }
    eprintln!();
}

/// Helper: print which DOM node IDs map to which layout indices.
fn dump_dom_to_layout(lw: &LayoutWindow, label: &str) {
    let tree = lw.layout_cache.tree.as_ref().expect("layout tree missing");
    eprintln!("--- DOM -> Layout mapping ({}) ---", label);
    let mut entries: Vec<_> = tree.dom_to_layout.iter().collect();
    entries.sort_by_key(|(k, _)| k.index());
    for (dom_id, layout_indices) in entries {
        eprintln!("  dom[{}] -> layout{:?}", dom_id.index(), layout_indices);
    }
    eprintln!();
}

// ============================================================================
// TEST 1: Full layout tree dump for the failing 3-cell case
// ============================================================================
#[test]
fn diag_dump_3cell_layout_tree() {
    eprintln!("\n########## DIAGNOSTIC: 3-CELL NESTED INLINE TREE DUMP ##########");

    let dom = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("Hacker News"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("new | past | comments | ask | show"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("login"))
                                )
                        )
                )
        );

    let lw = layout_dom(dom, "", 800.0, 600.0);
    dump_layout_tree(&lw, "3-cell: <td><span><a>text</a></span></td>");
    dump_dom_to_layout(&lw, "3-cell");

    // Print the final rects for all DOM nodes
    eprintln!("--- Final DOM node rects ---");
    for i in 0..20 {
        let id = node_id(i);
        if let Some(rect) = lw.get_node_layout_rect(id) {
            eprintln!(
                "  dom_node[{}]: x={:.1} y={:.1} w={:.1} h={:.1}",
                i, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height
            );
        }
    }

    // Count how many layout tree nodes have 0 content width
    let tree = lw.layout_cache.tree.as_ref().unwrap();
    let mut zero_width_cells = Vec::new();
    for i in 0..tree.nodes.len() {
        if matches!(tree.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree.nodes[i].used_size.map(|s| s.width).unwrap_or(0.0);
            if w < 5.0 {
                zero_width_cells.push((i, w));
            }
        }
    }
    eprintln!("\nTable cells with near-zero width: {:?}", zero_width_cells);
    eprintln!("EXPECTED: no table cells should have near-zero width");
}

// ============================================================================
// TEST 2: Compare 2-cell (works) vs 3-cell (fails)
// ============================================================================
#[test]
fn diag_compare_2cell_vs_3cell() {
    eprintln!("\n########## DIAGNOSTIC: 2-CELL vs 3-CELL COMPARISON ##########");

    // 2-cell case: both cells have <span><a>text</a></span>
    // This should work per the bug report
    let dom_2cell = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("Hacker News"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("login"))
                                )
                        )
                )
        );

    let lw2 = layout_dom(dom_2cell, "", 800.0, 600.0);
    dump_layout_tree(&lw2, "2-cell: both nested inline");

    eprintln!("--- 2-cell DOM node rects ---");
    for i in 0..15 {
        let id = node_id(i);
        if let Some(rect) = lw2.get_node_layout_rect(id) {
            eprintln!("  dom[{}]: w={:.1} h={:.1}", i, rect.size.width, rect.size.height);
        }
    }

    // 3-cell case: cells 1 and 3 have <span><a>text</a></span>,
    // cell 2 has <span>direct text</span>
    let dom_3cell = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("Hacker News"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("new | past | comments"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("login"))
                                )
                        )
                )
        );

    let lw3 = layout_dom(dom_3cell, "", 800.0, 600.0);
    dump_layout_tree(&lw3, "3-cell: mixed (nested/direct/nested)");

    eprintln!("--- 3-cell DOM node rects ---");
    for i in 0..20 {
        let id = node_id(i);
        if let Some(rect) = lw3.get_node_layout_rect(id) {
            eprintln!("  dom[{}]: w={:.1} h={:.1}", i, rect.size.width, rect.size.height);
        }
    }

    // Compare cell widths
    let tree2 = lw2.layout_cache.tree.as_ref().unwrap();
    let tree3 = lw3.layout_cache.tree.as_ref().unwrap();

    eprintln!("\n--- Cell width comparison ---");
    for i in 0..tree2.nodes.len() {
        if matches!(tree2.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree2.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  2-cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree2.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }
    for i in 0..tree3.nodes.len() {
        if matches!(tree3.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree3.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  3-cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree3.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }
}

// ============================================================================
// TEST 3: Does removing cell 2 (direct-text cell) fix the bug?
// This tests if the issue is specifically about mixing direct-text and
// nested-inline cells in the same row.
// ============================================================================
#[test]
fn diag_3cell_all_nested_inline() {
    eprintln!("\n########## DIAGNOSTIC: 3-CELL ALL NESTED INLINE ##########");

    // 3 cells, ALL with <span><a>text</a></span> (no direct text)
    let dom = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("Hacker News"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("new | past | comments"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("login"))
                                )
                        )
                )
        );

    let lw = layout_dom(dom, "", 800.0, 600.0);
    dump_layout_tree(&lw, "3-cell: ALL nested inline");

    let tree = lw.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Cell widths (all nested inline) ---");
    let mut cell_widths = Vec::new();
    for i in 0..tree.nodes.len() {
        if matches!(tree.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            cell_widths.push((i, w));
            eprintln!("  cell layout[{}]: w={:.1}", i, w);
        }
    }

    let zero_cells: Vec<_> = cell_widths.iter().filter(|(_, w)| *w < 5.0).collect();
    if zero_cells.is_empty() {
        eprintln!("RESULT: All 3 nested-inline cells have proper width!");
        eprintln!("  -> Bug is specific to MIXING direct-text and nested-inline cells");
    } else {
        eprintln!("RESULT: {} cells still have zero width: {:?}", zero_cells.len(), zero_cells);
        eprintln!("  -> Bug is NOT about mixing, it's about nested inlines in general with 3 cells");
    }
}

// ============================================================================
// TEST 4: Does cell ORDER matter? Put the direct-text cell first vs last.
// ============================================================================
#[test]
fn diag_cell_order_matters() {
    eprintln!("\n########## DIAGNOSTIC: CELL ORDER ##########");

    // Order A: direct-text FIRST, then two nested-inline
    let dom_a = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("direct text first"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested second"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested third"))
                                )
                        )
                )
        );

    let lw_a = layout_dom(dom_a, "", 800.0, 600.0);
    let tree_a = lw_a.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Order A: [direct, nested, nested] ---");
    for i in 0..tree_a.nodes.len() {
        if matches!(tree_a.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree_a.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree_a.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }

    // Order B: direct-text LAST
    let dom_b = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested first"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested second"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("direct text last"))
                        )
                )
        );

    let lw_b = layout_dom(dom_b, "", 800.0, 600.0);
    let tree_b = lw_b.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Order B: [nested, nested, direct] ---");
    for i in 0..tree_b.nodes.len() {
        if matches!(tree_b.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree_b.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree_b.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }

    // Order C: direct-text MIDDLE (the original failing case order)
    let dom_c = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested first"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("direct text middle"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested last"))
                                )
                        )
                )
        );

    let lw_c = layout_dom(dom_c, "", 800.0, 600.0);
    let tree_c = lw_c.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Order C: [nested, direct, nested] (original bug) ---");
    for i in 0..tree_c.nodes.len() {
        if matches!(tree_c.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree_c.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree_c.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }
}

// ============================================================================
// TEST 5: Cache depth analysis - count how deep the tree goes below each
// cell and check if the cache clearing depth (2 levels) is sufficient.
// ============================================================================
#[test]
fn diag_cache_depth_analysis() {
    eprintln!("\n########## DIAGNOSTIC: CACHE DEPTH ANALYSIS ##########");
    eprintln!("The cache clearing in measure_cell_content_width() only clears");
    eprintln!("cell + children + grandchildren (2 levels below cell).");
    eprintln!("If the tree is deeper, stale cache entries survive.");

    // Case 1: <td>text</td> - depth 1 (text node is child of td)
    let dom1 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("plain text"))
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("more plain"))
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("yet more"))
                )
        );
    let lw1 = layout_dom(dom1, "", 800.0, 600.0);
    let tree1 = lw1.layout_cache.tree.as_ref().unwrap();

    eprintln!("\n--- Case 1: <td>text</td> (3 cells) ---");
    print_subtree_depth(tree1, "3-cell plain text");

    // Case 2: <td><a>text</a></td> - depth 2 (a > text)
    let dom2 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::A)
                                .with_child(Dom::create_text("link one"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::A)
                                .with_child(Dom::create_text("link two"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::A)
                                .with_child(Dom::create_text("link three"))
                        )
                )
        );
    let lw2 = layout_dom(dom2, "", 800.0, 600.0);
    let tree2 = lw2.layout_cache.tree.as_ref().unwrap();

    eprintln!("\n--- Case 2: <td><a>text</a></td> (3 cells) ---");
    print_subtree_depth(tree2, "3-cell single inline");

    // Case 3: <td><span><a>text</a></span></td> - depth 3 (span > a > text)
    // PLUS possibly anonymous IFC wrappers making it depth 4+
    let dom3 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested one"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested two"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested three"))
                                )
                        )
                )
        );
    let lw3 = layout_dom(dom3, "", 800.0, 600.0);
    let tree3 = lw3.layout_cache.tree.as_ref().unwrap();

    eprintln!("\n--- Case 3: <td><span><a>text</a></span></td> (3 cells) ---");
    print_subtree_depth(tree3, "3-cell nested inline");

    // Full tree dump for case 3 to see anonymous nodes
    dump_layout_tree(&lw3, "Case 3: nested inline 3-cell");
}

/// Recursively compute the depth of the subtree below each table cell.
fn compute_depth(tree: &LayoutTree, node: usize) -> usize {
    let children = tree.children(node);
    if children.is_empty() {
        return 0;
    }
    children.iter().map(|&c| 1 + compute_depth(tree, c)).max().unwrap_or(0)
}

/// Print subtree depth for all table cells in the tree.
fn print_subtree_depth(tree: &LayoutTree, label: &str) {
    eprintln!("  Subtree depths below table cells ({}):", label);
    for i in 0..tree.nodes.len() {
        if matches!(tree.nodes[i].formatting_context, FormattingContext::TableCell) {
            let depth = compute_depth(tree, i);
            let w = tree.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            let dom_id = tree.nodes[i].dom_node_id.map(|n| n.index());
            eprintln!(
                "    cell layout[{}] (dom={:?}): depth={}, w={:.1} {}",
                i, dom_id, depth, w,
                if w < 5.0 { " <<< ZERO WIDTH" } else { "" }
            );

            // Print the full child tree below this cell
            print_children_recursive(tree, i, 3);
        }
    }
}

/// Print children recursively with indentation.
fn print_children_recursive(tree: &LayoutTree, node: usize, indent: usize) {
    let children = tree.children(node);
    for &child in children {
        let hot = &tree.nodes[child];
        let cold = &tree.cold[child];
        let prefix = " ".repeat(indent);
        let dom_str = match hot.dom_node_id {
            Some(id) => format!("dom={}", id.index()),
            None => "ANON".to_string(),
        };
        let anon_str = match cold.anonymous_type {
            Some(at) => format!(" ({:?})", at),
            None => String::new(),
        };
        let w = hot.used_size.map(|s| s.width).unwrap_or(-1.0);
        eprintln!(
            "{}[{}] {} fc={:?}{} w={:.1}",
            prefix, child, dom_str, hot.formatting_context, anon_str, w
        );
        print_children_recursive(tree, child, indent + 2);
    }
}

// ============================================================================
// TEST 6: Does adding more text to nested-inline cells change anything?
// If it's a cache issue, longer text might still get 0 width.
// ============================================================================
#[test]
fn diag_longer_text_nested_inline() {
    eprintln!("\n########## DIAGNOSTIC: LONGER TEXT IN NESTED INLINE ##########");

    let dom = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text(
                                            "This is a very long text string that should definitely have significant width"
                                        ))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(Dom::create_text("short"))
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text(
                                            "Another very long text that should be wide"
                                        ))
                                )
                        )
                )
        );

    let lw = layout_dom(dom, "", 800.0, 600.0);
    let tree = lw.layout_cache.tree.as_ref().unwrap();

    eprintln!("--- Cell widths with long nested text ---");
    for i in 0..tree.nodes.len() {
        if matches!(tree.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = tree.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell layout[{}]: w={:.1} (dom={:?})", i, w,
                tree.nodes[i].dom_node_id.map(|n| n.index()));
        }
    }

    eprintln!("\nIf long-text cells STILL get 0 width, it confirms cache/measurement bug.");
    eprintln!("If they get proper width, the bug is text-length dependent (unlikely).");
}

// ============================================================================
// TEST 7: Nesting depth isolation - does depth 2 work but depth 3 fail?
// ============================================================================
#[test]
fn diag_nesting_depth_isolation() {
    eprintln!("\n########## DIAGNOSTIC: NESTING DEPTH ISOLATION ##########");

    // Depth 1: <td>text</td>
    let dom_d1 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(Dom::create_text("AAA")))
                .with_child(Dom::create_node(NodeType::Td).with_child(Dom::create_text("BBB")))
                .with_child(Dom::create_node(NodeType::Td).with_child(Dom::create_text("CCC")))
        );
    let lw1 = layout_dom(dom_d1, "", 800.0, 600.0);
    let t1 = lw1.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Depth 1: <td>text</td> ---");
    for i in 0..t1.nodes.len() {
        if matches!(t1.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = t1.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell[{}]: w={:.1}", i, w);
        }
    }

    // Depth 2: <td><a>text</a></td>
    let dom_d2 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::A).with_child(Dom::create_text("AAA"))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::A).with_child(Dom::create_text("BBB"))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::A).with_child(Dom::create_text("CCC"))))
        );
    let lw2 = layout_dom(dom_d2, "", 800.0, 600.0);
    let t2 = lw2.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Depth 2: <td><a>text</a></td> ---");
    for i in 0..t2.nodes.len() {
        if matches!(t2.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = t2.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell[{}]: w={:.1}", i, w);
        }
    }

    // Depth 3: <td><span><a>text</a></span></td>
    let dom_d3 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("AAA")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("BBB")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("CCC")))))
        );
    let lw3 = layout_dom(dom_d3, "", 800.0, 600.0);
    let t3 = lw3.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Depth 3: <td><span><a>text</a></span></td> ---");
    for i in 0..t3.nodes.len() {
        if matches!(t3.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = t3.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell[{}]: w={:.1}", i, w);
        }
    }

    // Depth 4: <td><div><span><a>text</a></span></div></td>
    let dom_d4 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_div().with_child(
                        Dom::create_node(NodeType::Span).with_child(
                            Dom::create_node(NodeType::A).with_child(Dom::create_text("AAA"))))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_div().with_child(
                        Dom::create_node(NodeType::Span).with_child(
                            Dom::create_node(NodeType::A).with_child(Dom::create_text("BBB"))))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_div().with_child(
                        Dom::create_node(NodeType::Span).with_child(
                            Dom::create_node(NodeType::A).with_child(Dom::create_text("CCC"))))))
        );
    let lw4 = layout_dom(dom_d4, "", 800.0, 600.0);
    let t4 = lw4.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Depth 4: <td><div><span><a>text</a></span></div></td> ---");
    for i in 0..t4.nodes.len() {
        if matches!(t4.nodes[i].formatting_context, FormattingContext::TableCell) {
            let w = t4.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0);
            eprintln!("  cell[{}]: w={:.1}", i, w);
        }
    }
}

// ============================================================================
// TEST 8: Does the number of cells matter? Compare 2 vs 3 vs 4 cells
// with the SAME nesting depth.
// ============================================================================
#[test]
fn diag_cell_count_matters() {
    eprintln!("\n########## DIAGNOSTIC: CELL COUNT (all nested inline) ##########");

    // 1 cell
    let dom1 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell1")))))
        );
    let lw1 = layout_dom(dom1, "", 800.0, 600.0);
    let t1 = lw1.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- 1 cell ---");
    for i in 0..t1.nodes.len() {
        if matches!(t1.nodes[i].formatting_context, FormattingContext::TableCell) {
            eprintln!("  cell[{}]: w={:.1}", i, t1.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0));
        }
    }

    // 2 cells
    let dom2 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell1")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell2")))))
        );
    let lw2 = layout_dom(dom2, "", 800.0, 600.0);
    let t2 = lw2.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- 2 cells ---");
    for i in 0..t2.nodes.len() {
        if matches!(t2.nodes[i].formatting_context, FormattingContext::TableCell) {
            eprintln!("  cell[{}]: w={:.1}", i, t2.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0));
        }
    }

    // 3 cells
    let dom3 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell1")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell2")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell3")))))
        );
    let lw3 = layout_dom(dom3, "", 800.0, 600.0);
    let t3 = lw3.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- 3 cells ---");
    for i in 0..t3.nodes.len() {
        if matches!(t3.nodes[i].formatting_context, FormattingContext::TableCell) {
            eprintln!("  cell[{}]: w={:.1}", i, t3.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0));
        }
    }

    // 4 cells
    let dom4 = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell1")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell2")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell3")))))
                .with_child(Dom::create_node(NodeType::Td).with_child(
                    Dom::create_node(NodeType::Span).with_child(
                        Dom::create_node(NodeType::A).with_child(Dom::create_text("cell4")))))
        );
    let lw4 = layout_dom(dom4, "", 800.0, 600.0);
    let t4 = lw4.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- 4 cells ---");
    for i in 0..t4.nodes.len() {
        if matches!(t4.nodes[i].formatting_context, FormattingContext::TableCell) {
            eprintln!("  cell[{}]: w={:.1}", i, t4.nodes[i].used_size.map(|s| s.width).unwrap_or(-1.0));
        }
    }
}

// ============================================================================
// TEST 9: ROOT CAUSE CONFIRMATION
//
// The bug: In layout_bfc(), max_cross_size (which becomes overflow_size.width)
// is computed from child used_size.width. For inline children like <span>,
// used_size.width = 0 (CSS 2.2 ss 10.3.1: width does not apply to inline
// non-replaced elements). But the actual content extent lives in the child's
// overflow_content_size.
//
// This means: cell.overflow_size.width = 0 for any cell whose BFC has
// inline-only children (e.g. <td><span>...</span></td>). When
// measure_cell_content_width reads overflow_content_size.width for the cell,
// it gets 0, so the cell gets 0 min/max-content width.
//
// Proof: Compare overflow_content_size of cells vs their inline children.
// The child <span> has overflow=(87.0, 18.4) but the parent <td>'s BFC
// only sees child used_size.width = 0, so cell overflow=(0.0, 18.4).
// ============================================================================
#[test]
fn diag_root_cause_overflow_propagation() {
    eprintln!("\n########## DIAGNOSTIC: ROOT CAUSE - OVERFLOW PROPAGATION ##########");
    eprintln!("Theory: BFC's max_cross_size uses child.used_size.width for inline");
    eprintln!("children, but inline elements have used_size.width=0 (CSS spec).");
    eprintln!("The actual content width is in the child's overflow_content_size,");
    eprintln!("which is NOT propagated up to the BFC's overflow_size.");

    // Case A: Direct text in cell (works)
    // <td>text</td> -> BFC child is the IFC text run, overflow propagates correctly
    let dom_a = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("direct text"))
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("more text"))
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("third"))
                )
        );
    let lw_a = layout_dom(dom_a, "", 800.0, 600.0);
    let ta = lw_a.layout_cache.tree.as_ref().unwrap();
    eprintln!("\n--- Case A: <td>text</td> (WORKS) ---");
    for i in 0..ta.nodes.len() {
        let warm = &ta.warm[i];
        let hot = &ta.nodes[i];
        let used_w = hot.used_size.map(|s| s.width).unwrap_or(-1.0);
        let overflow_w = warm.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
        if matches!(hot.formatting_context, FormattingContext::TableCell) {
            eprintln!("  CELL[{}]: used_w={:.1}, overflow_w={:.1}  fc={:?}",
                i, used_w, overflow_w, hot.formatting_context);
            // Print its children
            for &c in ta.children(i) {
                let cw = &ta.warm[c];
                let ch = &ta.nodes[c];
                let cu = ch.used_size.map(|s| s.width).unwrap_or(-1.0);
                let co = cw.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
                eprintln!("    child[{}]: used_w={:.1}, overflow_w={:.1}  fc={:?}  dom={:?}",
                    c, cu, co, ch.formatting_context, ch.dom_node_id.map(|n| n.index()));
            }
        }
    }

    // Case B: Nested inline in cell (FAILS)
    // <td><span><a>text</a></span></td>
    let dom_b = Dom::create_node(NodeType::Table)
        .with_child(
            Dom::create_node(NodeType::Tr)
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("nested text"))
                                )
                        )
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(Dom::create_text("direct text"))
                )
                .with_child(
                    Dom::create_node(NodeType::Td)
                        .with_child(
                            Dom::create_node(NodeType::Span)
                                .with_child(
                                    Dom::create_node(NodeType::A)
                                        .with_child(Dom::create_text("also nested"))
                                )
                        )
                )
        );
    let lw_b = layout_dom(dom_b, "", 800.0, 600.0);
    let tb = lw_b.layout_cache.tree.as_ref().unwrap();
    eprintln!("\n--- Case B: <td><span><a>text</a></span></td> (FAILS) ---");
    for i in 0..tb.nodes.len() {
        let warm = &tb.warm[i];
        let hot = &tb.nodes[i];
        let used_w = hot.used_size.map(|s| s.width).unwrap_or(-1.0);
        let overflow_w = warm.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
        if matches!(hot.formatting_context, FormattingContext::TableCell) {
            eprintln!("  CELL[{}]: used_w={:.1}, overflow_w={:.1}  fc={:?}",
                i, used_w, overflow_w, hot.formatting_context);
            // Print its children and grandchildren
            for &c in tb.children(i) {
                let cw = &tb.warm[c];
                let ch = &tb.nodes[c];
                let cu = ch.used_size.map(|s| s.width).unwrap_or(-1.0);
                let co = cw.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
                eprintln!("    child[{}]: used_w={:.1}, overflow_w={:.1}  fc={:?}  dom={:?}",
                    c, cu, co, ch.formatting_context, ch.dom_node_id.map(|n| n.index()));
                for &gc in tb.children(c) {
                    let gw = &tb.warm[gc];
                    let gh = &tb.nodes[gc];
                    let gu = gh.used_size.map(|s| s.width).unwrap_or(-1.0);
                    let go = gw.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
                    eprintln!("      grandchild[{}]: used_w={:.1}, overflow_w={:.1}  fc={:?}  dom={:?}",
                        gc, gu, go, gh.formatting_context, gh.dom_node_id.map(|n| n.index()));
                }
            }
        }
    }

    eprintln!("\n--- ROOT CAUSE SUMMARY ---");
    eprintln!("In Case A, the cell's BFC child is the IFC text content node.");
    eprintln!("Its used_size.width reflects the text width, so max_cross_size is correct.");
    eprintln!("");
    eprintln!("In Case B, the cell's BFC child is <span> (FormattingContext::Inline).");
    eprintln!("Its used_size.width = 0 (CSS 2.2 ss10.3.1: width N/A to inline non-replaced).");
    eprintln!("The BFC computes max_cross_size from used_size.width = 0.");
    eprintln!("The cell's overflow_content_size.width = 0, so measure_cell_content_width = 0.");
    eprintln!("");
    eprintln!("FIX: In layout_bfc(), when computing max_cross_size for inline children,");
    eprintln!("use max(child.used_size.width, child.overflow_content_size.width) instead");
    eprintln!("of just child.used_size.width.");
    eprintln!("");
    eprintln!("Alternatively: in measure_cell_content_width(), walk the subtree to find");
    eprintln!("the deepest overflow_content_size that reflects actual content width.");
}

// ============================================================================
// TEST 10: Verify the BFC overflow propagation directly.
// Compare what the BFC sees for direct text vs inline-wrapped text.
// ============================================================================
#[test]
fn diag_bfc_overflow_direct_vs_inline() {
    eprintln!("\n########## DIAGNOSTIC: BFC OVERFLOW DIRECT vs INLINE ##########");

    // Non-table case: just a plain div with inline children
    // This shows the same issue outside tables: BFC overflow doesn't
    // propagate through inline elements.
    let dom_direct = Dom::create_div()
        .with_ids_and_classes(vec![IdOrClass::Class("box".into())].into())
        .with_child(Dom::create_text("Hello World"));

    let css = ".box { width: auto; }";
    let lw1 = layout_dom(dom_direct, css, 800.0, 600.0);
    let t1 = lw1.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Direct text in div ---");
    for i in 0..t1.nodes.len() {
        let w = &t1.warm[i];
        let h = &t1.nodes[i];
        let uw = h.used_size.map(|s| s.width).unwrap_or(-1.0);
        let ow = w.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
        eprintln!("  [{}] fc={:?} used_w={:.1} overflow_w={:.1} dom={:?}",
            i, h.formatting_context, uw, ow, h.dom_node_id.map(|n| n.index()));
    }

    // Inline-wrapped text
    let dom_inline = Dom::create_div()
        .with_ids_and_classes(vec![IdOrClass::Class("box".into())].into())
        .with_child(
            Dom::create_node(NodeType::Span)
                .with_child(
                    Dom::create_node(NodeType::A)
                        .with_child(Dom::create_text("Hello World"))
                )
        );

    let lw2 = layout_dom(dom_inline, css, 800.0, 600.0);
    let t2 = lw2.layout_cache.tree.as_ref().unwrap();
    eprintln!("--- Inline-wrapped text in div ---");
    for i in 0..t2.nodes.len() {
        let w = &t2.warm[i];
        let h = &t2.nodes[i];
        let uw = h.used_size.map(|s| s.width).unwrap_or(-1.0);
        let ow = w.overflow_content_size.map(|s| s.width).unwrap_or(-1.0);
        eprintln!("  [{}] fc={:?} used_w={:.1} overflow_w={:.1} dom={:?}",
            i, h.formatting_context, uw, ow, h.dom_node_id.map(|n| n.index()));
    }
}