float-pigment-forest 0.8.2

A node tree implementation for float-pigment-layout.
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
// Tests for `margin` properties in CSS
// Based on CSS Box Model Module Level 3:
// - Margins create space outside element's border
// - Vertical margins collapse between adjacent block elements
// - margin: auto can center elements horizontally
// - Margins can be fixed values or percentages (relative to container width)

use crate::*;

// Case: Fixed margin
// Spec points:
// - margin: 10px applies 10px to all four sides
// - Element is offset from container edge by margin
// In this test:
// - Element: margin=10px, positioned at left=10
// - Note: top margin collapses with parent, so expect_top=0 within parent
#[test]
fn margin_fixed() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 100px;" expect_top="0">
          <div style="width: 10px; height: 10px; margin: 10px;" expect_width="10" expect_height="10" expect_left="10" expect_top="0"></div>
        </div>
    "#
    )
}

// Case: Percentage margin
// Spec points:
// - Percentage margins are relative to container width (including vertical margins)
// In this test:
// - Parent: 100x100px
// - Child: margin=10% = 10px (10% of 100px width)
#[test]
fn margin_percentage() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 100px;" expect_top="0">
            <div style="width: 10px; height: 10px; margin: 10%;" expect_width="10" expect_height="10" expect_left="10" expect_top="0"></div>
        </div>
    "#
    )
}

// Case: margin-left fixed
// Spec points:
// - margin-left only affects left side positioning
// In this test:
// - Element: margin-left=10px, positioned at left=10
#[test]
fn margin_left_fixed() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 100px;">
            <div style="width: 10px; height: 10px; margin-left: 10px;" expect_width="10" expect_height="10" expect_left="10"></div>
        </div>
    "#
    )
}

// Case: margin-right in flex context
// Spec points:
// - In flex layout, margin-right creates space between items
// In this test:
// - Container: flex
// - First child: margin-right=10px
// - Second child: positioned at left=20 (10px width + 10px margin)
#[test]
fn margin_right_fixed() {
    assert_xml!(
        r#"
        <div style="height: 100px; display: flex; width: 100px;">
            <div style="width: 10px; height: 10px; margin-right: 10px" expect_width="10" expect_height="10"></div>
            <div style="width: 10px; height: 10px;" expect_left="20" expect_width="10" expect_height="10"></div>
        </div>
    "#
    )
}

// Case: margin-top between siblings
// Spec points:
// - margin-top creates space above element
// In this test:
// - First child: 10x10px, at top=0
// - Second child: margin-top=10px, at top=20 (10px height + 10px margin)
#[test]
fn margin_top_fixed() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 100px;">
            <div style="width: 10px; height: 10px;"></div>
            <div style="width: 10px; height: 10px; margin-top: 10px;" expect_top="20"></div>
        </div>
    "#
    )
}

// Case: margin-bottom between siblings
// Spec points:
// - margin-bottom creates space below element
// In this test:
// - First child: margin-bottom=20px
// - Second child: positioned at top=30 (10px height + 20px margin)
#[test]
fn margin_bottom_fixed() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 100px;">
            <div style="width: 10px; height: 10px; margin-bottom: 20px;"></div>
            <div style="width: 10px; height: 10px;" expect_top="30"></div>
        </div>
    "#
    )
}

// Case: Margin collapse between siblings (positive margins)
// Spec points:
// - Adjacent vertical margins collapse to the larger value
// In this test:
// - First child: margin-bottom=50px
// - Second child: margin-top=40px
// - Collapsed margin = max(50, 40) = 50px
// - Second child at top = 100 + 50 = 150
#[test]
fn margin_collapse_1() {
    assert_xml!(
        r#"
        <div style="height: 800px;">
          <div style="height: 100px; margin-bottom: 50px;"></div>
          <div style="height: 100px; margin-top: 40px;" expect_top="150"></div>
        </div>
    "#
    )
}

// Case: Margin collapse with empty inline nodes between
// Spec points:
// - Empty inline elements don't prevent margin collapse
// In this test:
// - Same as margin_collapse_1 but with empty inline between siblings
// - Margins still collapse to max(50, 40) = 50px
#[test]
fn margin_collapse_empty_inline_nodes() {
    assert_xml!(
        r#"
        <div style="height: 800px;">
          <div style="height: 100px; margin-bottom: 50px;"></div>
          <div style="display: inline; height: 0;"></div>
          <div style="margin-top: 40px;" expect_top="150"></div>
        </div>
    "#
    )
}

// Case: Margin collapse with negative margin
// Spec points:
// - When one margin is negative, subtract it from the positive
// In this test:
// - First child: margin-bottom=50px
// - Second child: margin-top=-40px
// - Net margin = 50 + (-40) = 10px
// - Second child at top = 100 + 10 = 110
#[test]
fn margin_collapse_negative() {
    assert_xml!(
        r#"
        <div style="height: 800px;">
          <div style="height: 100px; margin-bottom: 50px;"></div>
          <div style="height: 100px; margin-top: -40px;" expect_top="110"></div>
        </div>
    "#
    )
}

// Case: Margin collapse with both negative margins
// Spec points:
// - When both margins are negative, use the most negative
// In this test:
// - First child: margin-bottom=-50px
// - Second child: margin-top=-40px
// - Collapsed margin = min(-50, -40) = -50px
// - Second child at top = 100 - 50 = 50
#[test]
fn margin_collapse_negative_maximum() {
    assert_xml!(
        r#"
        <div style="height: 800px;">
          <div style="height: 100px; margin-bottom: -50px;"></div>
          <div style="height: 100px; margin-top: -40px;" expect_top="50"></div>
        </div>
    "#
    )
}

// Case: Margin does not collapse if padding exists
// Spec points:
// - Padding prevents margin collapse between parent and child
// In this test:
// - First child: padding-bottom=10px, margin-bottom=10px
// - Second child: margin-top=10px
// - No collapse because of padding, top = 10 + 10 = 20
#[test]
fn margin_not_collapse_if_padding_exists() {
    assert_xml!(
        r#"
        <div style="height: 800px;">
          <div style="height: 10px; margin-bottom: 10px; box-sizing: border-box; padding-bottom: 10px;" expect_top="0"></div>
          <div style="height: 10px; margin-top: 10px;" expect_top="20"></div>
        </div>
    "#
    )
}

// Case: Margin collapse with complex padding/border structure
// Spec points:
// - Border prevents margin collapse at that edge
// - Padding prevents margin collapse at that edge
// In this test:
// - Parent with padding-top, border-bottom, nested children with margins
// - Margins collapse within the padding but stop at border
#[test]
fn margin_not_collapse_if_padding_exists_2() {
    assert_xml!(
        r#"
        <div style="border: 1px;>
          <div style="padding-top: 30px; border-bottom: 10px; margin-top: 20px; margin-bottom: 20px;" expect_top="20" expect_height="90">
            <div style="margin-top: 10px; margin-bottom: 10px; width: 10px; height: 10px;" expect_top="40"></div>
            <div style="margin-top: 5px; margin-bottom: 10px; width: 10px; height: 10px;" expect_top="60"></div>
          </div>
        </div>
    "#
    )
}

// Case: Margin collapse with min-height (min-height < total content)
// Spec points:
// - min-height allows content to overflow
// - Margins still collapse normally
// In this test:
// - Parent with min-height=30px, children total more than that
// - Margin collapse continues past min-height
#[test]
fn margin_collapse_min_height() {
    assert_xml!(
        r#"
        <div style="width: 100px;" expect_height="120">
            <div style="min-height: 30px;" expect_height="40">
                <div style="height: 20px;" expect_height="20"></div>
                <div style="height: 20px; margin-bottom: 50px;" expect_height="20"></div>
            </div>
            <div style="height: 30px;" expect_height="30" expect_top="90"><div>
        </div>
    "#
    )
}

// Case: Margin collapse with min-height (min-height > total content)
// Spec points:
// - min-height expands element beyond content
// - Margins collapse up to the min-height boundary
// In this test:
// - Parent with min-height=50px > content height
// - Parent expands to accommodate margin-bottom
#[test]
fn margin_collapse_min_height_2() {
    assert_xml!(
        r#"
        <div style="width: 100px;" expect_height="120">
            <div style="min-height: 50px;" expect_height="90">
                <div style="height: 20px;" expect_height="20"></div>
                <div style="height: 20px; margin-bottom: 50px;" expect_height="20"></div>
            </div>
            <div style="height: 30px;" expect_height="30" expect_top="90"><div>
        </div>
    "#
    )
}

// Case: Margin collapse with max-height
// Spec points:
// - max-height=0 clips the element's height but overflow exists
// - Margins collapse from the visible edge
// In this test:
// - max-height=0 clips first container
// - Second container positioned at top=0 due to collapse
#[test]
fn margin_collapse_max_height() {
    assert_xml!(
        r#"
            <div style="width: 100px;" expect_height="30">
                <div style="max-height: 0px" expect_height="0">
                    <div style="height: 20px;" expect_height="20"></div>
                    <div style="height: 20px; margin-bottom: 20px;" expect_height="20"></div>
                </div>
                <div style="height: 30px;" expect_height="30" expect_top="0"></div>
            </div>
        "#
    )
}

// Case: Margin collapse with max-height and external margin
// Spec points:
// - Margin on clipped element still participates in collapse
// In this test:
// - First element: max-height=0, margin-bottom=10px, child margin-bottom=50px
// - Collapsed margin = max(10, 50) = 50px... but collapses with next sibling
// - Second child at top=10 after collapse
#[test]
fn margin_collapse_max_height_2() {
    assert_xml!(
        r#"
            <div expect_height="40">
                <div style="max-height: 0px; margin-bottom: 10px;" expect_height="0">
                    <div style="height: 20px;" expect_height="20"></div>
                    <div style="height: 20px; margin-bottom: 50px;" expect_height="20"></div>
                </div>
                <div style="height: 30px;" expect_top="10" expect_height="30"></div>
            </div>
        "#
    )
}

// Case: Margin collapse across flex boundary
// Spec points:
// - Margins don't collapse across flex container boundaries
// - Margins collapse within block children
// In this test:
// - Flex column container with block children
// - Block children's margins collapse internally
#[test]
fn margin_collapse_cross_flex() {
    assert_xml!(
        r#"
            <div style="margin-top: 100px; display: flex; flex-direction: column" expect_top="0">
                <div style="margin-top: 200px;" expect_top="200">
                    <div style="display: flex; margin-top: 250px; height: 50px;" expect_top="250"></div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (nested case 2)
// Spec points:
// - Block children within flex items can have margin collapse
#[test]
fn margin_collapse_cross_flex_2() {
    assert_xml!(
        r#"
            <div expect_top="0">
                <div style="height: 10px; margin-top: 10px;" expect_top="0"></div>
                <div style="margin-top: 10px;"  expect_top="30">
                    <div style="margin-top: 20px; display: flex; height: 100px;" expect_top="0"></div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (within flex column)
// Spec points:
// - In flex column, margins are preserved (no collapse)
#[test]
fn margin_collapse_cross_flex_3() {
    assert_xml!(
        r#"
            <div style="display: flex; flex-direction: column;" expect_top="0">
                <div style="height: 10px; margin-top: 10px;"  expect_top="10"></div>
                <div style="margin-top: 10px;" expect_top="30">
                    <div style="margin-top: 20px; display:flex; height: 100px;" expect_top="20"></div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (deeply nested)
// Spec points:
// - Complex nesting with flex and block contexts
#[test]
fn margin_collapse_cross_flex_4() {
    assert_xml!(
        r#"
            <div style="display: flex; flex-direction: column;" expect_top="0">
                <div style="display: flex; flex-direction: column; margin-top: 10px;" expect_top="10">
                    <div style="margin-top: 10px" expect_top="10">
                        <div style="margin-top: 80px;" expect_top="90">
                            <div style="margin-top: 90px;" expect_top="0">
                                <div style="display: flex; flex-direction: column; margin-top: 40px; height: 50px;" expect_top="0"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (variation 5)
#[test]
fn margin_collapse_cross_flex_5() {
    assert_xml!(
        r#"
            <div>
                <div style="display: flex; flex-direction: column; margin-top: 10px;" expect_top="0">
                    <div style="margin-top: 10px" expect_top="10">
                        <div style="margin-top: 80px;" expect_top="90">
                            <div style="margin-top: 90px;" expect_top="0">
                                <div style="display: flex; flex-direction: column; margin-top: 40px; height: 50px;" expect_top="0"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (variation 6)
#[test]
fn margin_collapse_cross_flex_6() {
    assert_xml!(
        r#"
            <div expect_top="0">
                <div style="margin-top: 10px" expect_top="0">
                    <div style="display: flex; flex-direction: column; margin-top: 20px; height: 100px;" expect_top="0"></div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse cross flex (variation 7)
#[test]
fn margin_collapse_cross_flex_7() {
    assert_xml!(
        r#"
            <div style="display: flex; flex-direction: column;" expect_top="0">
                <div style="margin-top: 10px" expect_top="10">
                    <div style="display: flex; flex-direction: column; margin-top: 20px; height: 100px;" expect_top="20"></div>
                </div>
            </div>
        "#
    )
}

// Case: Margin collapse between siblings with empty block
// Spec points:
// - Empty block with margins collapses its own margins
// - Then participates in sibling margin collapse
// In this test:
// - Empty div with margin-top=10px, margin-bottom=20px
// - Collapsed own margins = 20px (larger wins)
// - Third sibling at top=30+20=50
#[test]
fn margin_collapse_between_sibling_and_empty_block_1() {
    assert_xml!(
        r#"
            <div style="height: 300px;">
                <div style="height: 30px;" expect_height="30" expect_top="0"></div>
                <div style="margin-top: 10px; margin-bottom: 20px;" expect_height="0"></div>
                <div style="height: 30px;" expect_height="30" expect_top="50"></div>
            </div>
        "#
    )
}

// Case: Margin collapse between siblings with empty block (larger sibling margin)
// Spec points:
// - When sibling has larger margin, it dominates collapse
// In this test:
// - First sibling: margin-bottom=50px
// - Empty: margin-top=10px, margin-bottom=20px
// - Collapsed = max(50, 10, 20) = 50px
// - Third sibling at top=30+50=80
#[test]
fn margin_collapse_between_sibling_and_empty_block_2() {
    assert_xml!(
        r#"
            <div style="height: 300px;">
                <div style="height: 30px; margin-bottom: 50px" expect_height="30" expect_top="0"></div>
                <div style="margin-top: 10px; margin-bottom: 20px;" expect_height="0"></div>
                <div style="height: 30px;" expect_height="30" expect_top="80"></div>
            </div>
        "#
    )
}

// Case: Margin collapse with empty block and following sibling margin
// Spec points:
// - Multiple margins all collapse together
// In this test:
// - First: margin-bottom=200px
// - Empty: margins 10/20
// - Third: margin-top=100px
// - All collapse to max=200px
#[test]
fn margin_collapse_between_sibling_and_empty_block_3() {
    assert_xml!(
        r#"
            <div style="height: 300px;">
                <div style="height: 30px; margin-bottom: 200px" expect_height="30" expect_top="0"></div>
                <div style="margin-top: 10px; margin-bottom: 20px;" expect_height="0" expect_top="230"></div>
                <div style="height: 30px; margin-top: 100px" expect_height="30" expect_top="230"></div>
            </div>
        "#
    )
}

// Case: Margin collapse with larger following sibling
// In this test:
// - First: no margin-bottom
// - Empty: margins 10/20
// - Third: margin-top=100px (dominates)
// - Result: third at top=130
#[test]
fn margin_collapse_between_sibling_and_empty_block_4() {
    assert_xml!(
        r#"
            <div style="height: 300px;">
                <div style="height: 30px;" expect_height="30" expect_top="0"></div>
                <div style="margin-top: 10px; margin-bottom: 20px;" expect_height="0"></div>
                <div style="height: 30px; margin-top: 100px" expect_height="30" expect_top="130"></div>
            </div>
        "#
    )
}

// Case: Margin collapse with text-slot (zero-width joiner)
// Spec points:
// - Empty text slots don't break margin collapse
// In this test:
// - text-slot with len=0 doesn't prevent collapse
// - Margins collapse: max(50, 100) = 100px
#[test]
fn margin_collapse_between_sibling_and_empty_block_5() {
    assert_xml!(
        r#"
            <div style="height: 300px;">
                <div style="height: 30px; margin-bottom: 50px" expect_height="30" expect_top="0"></div>
                <text-slot len="0"></text-slot>
                <div style="height: 30px; margin-top: 100px" expect_height="30" expect_top="130"></div>
            </div>
        "#,
        true
    )
}

// Case: Margin collapse between parent and empty block child
// Spec points:
// - Child margins collapse through parent
// In this test:
// - Parent: margin-top=40px
// - Child: margin-top=10px, margin-bottom=30px (empty)
// - Collapsed into parent margin = max(40, 30) = 40px
// - Parent at top=100+40=140
#[test]
fn margin_collapse_between_parent_and_empty_block_1() {
    assert_xml!(
        r#"
        <div style="height: 100px;"></div>
        <div style="height: 300px; margin-top: 40px;" expect_top="140">
            <div style="margin-top: 10px; margin-bottom: 30px;" expect_height="0" expect_top="0"></div>
        </div>
        "#
    )
}

// Case: Parent margin smaller than child empty block margins
// In this test:
// - Parent: margin-top=20px
// - Child: margin-top=10px, margin-bottom=30px
// - Collapsed: max(20, 30) = 30px
// - Parent at top=100+30=130
#[test]
fn margin_collapse_between_parent_and_empty_block_2() {
    assert_xml!(
        r#"
            <div style="height: 100px;"></div>
            <div style="height: 300px; margin-top: 20px;" expect_top="130">
                <div style="margin-top: 10px; margin-bottom: 30px;" expect_height="0" expect_top="0"></div>
            </div>
        "#
    )
}

// Case: Multiple empty children with margin collapse
// In this test:
// - Two empty children with different margins
// - All collapse together with parent margin
#[test]
fn margin_collapse_between_parent_and_empty_block_3() {
    assert_xml!(
        r#"
            <div style="height: 100px;"></div>
            <div style="height: 300px; margin-top: 20px;" expect_top="160">
                <div style="margin-top: 10px; margin-bottom: 30px;" expect_height="0" expect_top="0"></div>
                <div style="margin-top: 10px; margin-bottom: 60px;" expect_height="0" expect_top="0"></div>
            </div>
        "#
    )
}

// Case: margin: auto for horizontal centering
// Spec points:
// - margin-left: auto and margin-right: auto centers element horizontally
// - Works with absolute positioning when left/right are set
// In this test:
// - Element: position=absolute, left=0, right=100px, width=10px
// - Available space = 0 to (375-100) = 275px, center = (275-10)/2 = 132.5 ≈ 95
// - Wait, the test expects 95... Let me check: (375 - 100 - 10) / 2 = 132.5...
// - Actually with left=0, right=100, space = 275, (275-10)/2 = 132.5... but expect_left=95
// - Maybe right is relative position? Let me preserve original test
#[test]
fn margin_auto() {
    assert_xml!(
        r#"
        <div style="height: 100px; width: 300px;">
          <div style="position: absolute; left: 0px; right: 100px; width: 10px; height: 10px; margin-left: auto; margin-right: auto;" expect_left="95"></div>
        </div>
    "#
    )
}

// Case: Margin with inline elements
// Spec points:
// - Inline element's margin affects containing block
// - margin-bottom on child pushes following siblings
// In this test:
// - Inline container with block child having margin-bottom=100px
// - Following sibling at top=200 (100+100)
#[test]
fn margin_inline() {
    assert_xml!(
        r#"
        <div style="width: 300px;" expect_height="300">
            <div style="display: inline;">
                <div style="margin-bottom: 100px; height: 100px; width: 100px;" expect_height="100"></div>
            </div>
            <div style="margin-top: 20px; height: 100px; width: 100px;" expect_top="200" expect_height="100"></div>
        </div>
    "#
    )
}

// Case: Margin with nested inline elements
// Spec points:
// - Margins collapse through inline element boundaries
#[test]
fn margin_inline_1() {
    assert_xml!(
        r#"
        <div>
            <div >
                <div style="display: inline;">
                    <div style="margin-bottom: 100px; height: 100px; width: 100px;" expect_height="100"></div>
                </div>
            </div>
            <div style="height: 100px" expect_top="200"></div>
        </div>
    "#
    )
}

// Case: Inline element with margin (but inline margins don't apply to block children)
// In this test:
// - Inline has margin-bottom=200px but doesn't affect layout
// - Block child's margin-bottom=100px affects position
#[test]
fn margin_inline_2() {
    assert_xml!(
        r#"
        <div>
            <div >
                <div style="display: inline; margin-bottom: 200px;">
                    <div style="margin-bottom: 100px; height: 100px; width: 100px;" expect_height="100"></div>
                </div>
            </div>
            <div style="height: 100px" expect_top="200"></div>
        </div>
    "#
    )
}

use float_pigment_forest::{convert_node_ref_to_ptr, ChildOperation, Node, StyleSetter};
use float_pigment_layout::{DefLength, LayoutTreeNode, OptionNum, OptionSize, Size};

unsafe fn as_ref<'a>(node: *mut Node) -> &'a Node {
    &*node
}

// Case: Margin on root element
// Spec points:
// - Root element margins apply relative to viewport
// In this test:
// - Root margins collapse with first child margins
#[test]
pub fn margin_root() {
    unsafe {
        let container = as_ref(Node::new_ptr());
        let root = as_ref(Node::new_ptr());
        let child = as_ref(Node::new_ptr());
        child.set_margin_top(DefLength::Points(Len::from_f32(10.)));
        child.set_margin_bottom(DefLength::Points(Len::from_f32(10.)));
        child.set_padding_top(DefLength::Points(Len::from_f32(10.)));
        child.set_padding_bottom(DefLength::Points(Len::from_f32(10.)));
        container.append_child(convert_node_ref_to_ptr(root));
        root.append_child(convert_node_ref_to_ptr(child));
        container.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(root.layout_position().top, 0.);
        assert_eq!(child.layout_position().top, 0.);
        assert_eq!(child.layout_position().height, 20.);
    }
}

// Case: Margin on empty root block element
// Spec points:
// - Computed margins are stored in computed style
#[test]
pub fn margin_root_empty_block() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        root.set_margin_top(DefLength::Points(Len::from_f32(10.)));
        root.set_margin_right(DefLength::Points(Len::from_f32(20.)));
        root.set_margin_bottom(DefLength::Points(Len::from_f32(30.)));
        root.set_margin_left(DefLength::Points(Len::from_f32(40.)));
        root.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(root.layout_node().computed_style().margin.top, 10.);
        assert_eq!(root.layout_node().computed_style().margin.right, 20.);
        assert_eq!(root.layout_node().computed_style().margin.bottom, 30.);
        assert_eq!(root.layout_node().computed_style().margin.left, 40.);
    }
}

// Case: Inline root element with margin on child
// Spec points:
// - Inline element's margin is determined by content
#[test]
pub fn margin_root_3() {
    unsafe {
        let root = as_ref(Node::new_ptr());
        // root.set_margin(DefLength::Points(100.));
        root.set_display(float_pigment_css::typing::Display::Inline);
        let child = as_ref(Node::new_ptr());
        child.set_height(DefLength::Points(Len::from_f32(100.)));
        child.set_margin(DefLength::Points(Len::from_f32(100.)));
        root.append_child(convert_node_ref_to_ptr(child));
        root.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(root.layout_node().computed_style().margin.bottom, 100.);
        assert_eq!(root.layout_position().height, 100.);
        assert_eq!(child.layout_position().height, 100.);
    }
}

// Case: Nested inline elements with margin
// Spec points:
// - Deeply nested inline margin propagation
#[test]
pub fn margin_root_4() {
    unsafe {
        let root = as_ref(Node::new_ptr());

        let child = as_ref(Node::new_ptr());
        child.set_display(float_pigment_css::typing::Display::Inline);

        let child_child = as_ref(Node::new_ptr());
        child_child.set_height(DefLength::Points(Len::from_f32(100.)));
        child_child.set_margin(DefLength::Points(Len::from_f32(100.)));

        root.append_child(convert_node_ref_to_ptr(child));
        child.append_child(convert_node_ref_to_ptr(child_child));

        root.layout(
            OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
            Size::new(Len::from_f32(0.), Len::from_f32(0.)),
        );
        assert_eq!(root.layout_node().computed_style().margin.bottom, 100.);
        assert_eq!(root.layout_position().height, 100.);
        assert_eq!(child.layout_position().height, 100.);
        assert_eq!(child_child.layout_position().height, 100.);
    }
}