azul-core 0.0.7

Common datatypes used for the Azul document object model, shared across all azul-* crates
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
//! CSS Inheritance and Cascade Tests
//!
//! Tests the computed values cache system (Option C from CSS_INHERITANCE_PROBLEM_REPORT.md)
//! Verifies that:
//! 1. Inherited properties correctly cascade from parent to child
//! 2. Explicit values override inherited values
//! 3. Multiple levels of nesting work correctly
//! 4. Updates to parent styles correctly invalidate children
//! 5. User-agent styles integrate properly with inheritance

use azul_core::{
    dom::{Dom, NodeType},
    prop_cache::CssPropertyOrigin,
    styled_dom::StyledDom,
};
use azul_css::{
    css::Css,
    dynamic_selector::CssPropertyWithConditions,
    props::{
        basic::font::{StyleFontSize, StyleFontWeight},
        property::{CssProperty, CssPropertyType},
    },
};

// Helper macro to create a StyledDom and get the necessary references
macro_rules! setup_test {
    ($dom:expr) => {{
        let mut dom = $dom;
        let styled_dom = StyledDom::create(&mut dom, Css::empty());

        let cache = styled_dom.css_property_cache.ptr.clone();

        (styled_dom, cache)
    }};
}

#[test]
fn test_font_size_inheritance_single_level() {
    // Create a simple parent-child DOM:
    // <div style="font-size: 24px">
    //   <p>Text</p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(24.0),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let (styled_dom, mut cache) = setup_test!(dom);

    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];

    // Compute inherited values
    let changed_nodes = cache.compute_inherited_values(node_hierarchy, node_data);

    println!("Changed nodes: {:?}", changed_nodes);
    println!("Computed values: {:#?}", cache.computed_values);

    // Verify that <p> (child) inherited the font-size from <div> (parent)
    let parent_id = azul_core::dom::NodeId::new(0); // div
    let child_id = azul_core::dom::NodeId::new(1); // p

    // Check computed values for child
    if let Some(child_computed) = cache.computed_values.get(child_id.index()) {
        let Some(prop_with_origin) = child_computed.get(&CssPropertyType::FontSize) else {
            panic!("Child should have FontSize");
        };
        if let CssProperty::FontSize(font_size_value) = &prop_with_origin.property {
            if let Some(font_size) = font_size_value.get_property() {
                let size = font_size.inner.to_pixels_internal(16.0, 16.0); // Default fallback
                assert_eq!(
                    size, 24.0,
                    "Child should inherit parent's font-size of 24px"
                );
            } else {
                panic!("FontSize value should not be None/Auto/Initial/Inherit");
            }
        } else {
            panic!("Child should have computed FontSize property");
        }
    } else {
        panic!("Child should have computed values");
    }
}

#[test]
fn test_font_size_override_not_inherited() {
    // Create a parent-child DOM where child has explicit font-size:
    // <div style="font-size: 24px">
    //   <p style="font-size: 12px">Text</p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(24.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::px(12.0),
                    ))]
                    .into(),
                )
                .with_child(Dom::create_text("Text")),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];
    let changed_nodes = cache.compute_inherited_values(node_hierarchy, node_data);

    println!("Changed nodes: {:?}", changed_nodes);
    println!("Computed values: {:#?}", cache.computed_values);

    let child_id = azul_core::dom::NodeId::new(1); // p

    // Verify that child has its own explicit value, not the inherited one
    if let Some(child_computed) = cache.computed_values.get(child_id.index()) {
        let Some(prop_with_origin) = child_computed.get(&CssPropertyType::FontSize) else {
            panic!("Child should have FontSize");
        };
        if let CssProperty::FontSize(font_size_value) = &prop_with_origin.property {
            if let Some(font_size) = font_size_value.get_property() {
                let size = font_size.inner.to_pixels_internal(16.0, 16.0);
                assert_eq!(
                    size, 12.0,
                    "Child should use its explicit font-size of 12px, not inherit 24px"
                );
            } else {
                panic!("FontSize value should not be None/Auto/Initial/Inherit");
            }
        } else {
            panic!("Child should have computed FontSize property");
        }
    } else {
        panic!("Child should have computed values");
    }
}

#[test]
fn test_font_weight_inheritance_multi_level() {
    // Create a three-level hierarchy:
    // <div style="font-weight: bold">
    //   <p>
    //     <span>Text</span>
    //   </p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_weight(
                StyleFontWeight::Bold,
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_child(Dom::create_node(NodeType::Span).with_child(Dom::create_text("Text"))),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];
    let changed_nodes = cache.compute_inherited_values(node_hierarchy, node_data);

    println!("Changed nodes: {:?}", changed_nodes);
    println!("Computed values: {:#?}", cache.computed_values);

    let div_id = azul_core::dom::NodeId::new(0); // div
    let p_id = azul_core::dom::NodeId::new(1); // p
    let span_id = azul_core::dom::NodeId::new(2); // span

    // Verify that both <p> and <span> inherited font-weight: bold
    for (node_id, node_name) in &[(p_id, "p"), (span_id, "span")] {
        if let Some(computed) = cache.computed_values.get(node_id.index()) {
            let Some(prop_with_origin) = computed.get(&CssPropertyType::FontWeight) else {
                panic!("{} should have FontWeight", node_name);
            };
            if let CssProperty::FontWeight(font_weight_value) = &prop_with_origin.property {
                if let Some(font_weight) = font_weight_value.get_property() {
                    assert_eq!(
                        *font_weight,
                        StyleFontWeight::Bold,
                        "{} should inherit font-weight: bold from ancestor div",
                        node_name
                    );
                } else {
                    panic!(
                        "{} FontWeight value should not be None/Auto/Initial/Inherit",
                        node_name
                    );
                }
            } else {
                panic!("{} should have computed FontWeight property", node_name);
            }
        } else {
            panic!("{} should have computed values", node_name);
        }
    }
}

#[test]
fn test_mixed_inherited_and_explicit_properties() {
    // Test cascade priority:
    // <div style="font-size: 20px; font-weight: bold">
    //   <p style="font-size: 16px">
    //     Text (should have font-size: 16px, font-weight: bold)
    //   </p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![
                CssPropertyWithConditions::simple(CssProperty::font_size(StyleFontSize::px(20.0))),
                CssPropertyWithConditions::simple(CssProperty::font_weight(StyleFontWeight::Bold)),
            ]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::px(16.0),
                    ))]
                    .into(),
                )
                .with_child(Dom::create_text("Text")),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];
    cache.compute_inherited_values(node_hierarchy, node_data);

    let p_id = azul_core::dom::NodeId::new(1); // p

    if let Some(p_computed) = cache.computed_values.get(p_id.index()) {
        // Check font-size (explicit)
        let Some(prop_with_origin) = p_computed.get(&CssPropertyType::FontSize) else {
            panic!("p should have computed FontSize");
        };
        if let CssProperty::FontSize(font_size_value) = &prop_with_origin.property {
            if let Some(font_size) = font_size_value.get_property() {
                let size = font_size.inner.to_pixels_internal(16.0, 16.0);
                assert_eq!(size, 16.0, "p should have explicit font-size: 16px");
            }
        } else {
            panic!("p FontSize should be CssProperty::FontSize variant");
        }

        // Check font-weight (inherited)
        let Some(prop_with_origin) = p_computed.get(&CssPropertyType::FontWeight) else {
            panic!("p should have FontWeight");
        };
        if let CssProperty::FontWeight(font_weight_value) = &prop_with_origin.property {
            if let Some(font_weight) = font_weight_value.get_property() {
                assert_eq!(
                    *font_weight,
                    StyleFontWeight::Bold,
                    "p should inherit font-weight: bold from div"
                );
            }
        } else {
            panic!("p should have computed FontWeight inherited from parent");
        }
    } else {
        panic!("p should have computed values");
    }
}

#[test]
fn test_non_inheritable_property_not_inherited() {
    // Test that non-inheritable properties (like width) are NOT inherited:
    // <div style="width: 200px">
    //   <p>Text</p>  <!-- should NOT inherit width -->
    // </div>

    use azul_css::{
        css::CssPropertyValue,
        props::{basic::pixel::PixelValue, layout::dimensions::LayoutWidth},
    };

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::Width(
                CssPropertyValue::Exact(LayoutWidth::Px(PixelValue::px(200.0))),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];
    cache.compute_inherited_values(node_hierarchy, node_data);

    let p_id = azul_core::dom::NodeId::new(1); // p

    if let Some(p_computed) = cache.computed_values.get(p_id.index()) {
        // Width should NOT be inherited from parent (200px)
        // Test the ORIGIN of the property, not just its value
        if let Some(prop_with_origin) = p_computed.get(&CssPropertyType::Width) {
            // The KEY test: Width should have origin Own (from UA CSS), NOT Inherited
            assert_eq!(
                prop_with_origin.origin,
                CssPropertyOrigin::Own,
                "Width origin should be Own (from UA CSS), not Inherited from parent. This proves \
                 that non-inheritable properties are not inherited."
            );
        }
        // If there's no Width at all, that's also fine (no UA CSS for this element)
    }
}

#[test]
fn test_update_invalidation() {
    // Test that updating a parent's property correctly returns changed children:
    // <div style="font-size: 20px">
    //   <p>Child 1</p>
    //   <p>Child 2</p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_children(
            vec![
                Dom::create_node(NodeType::P).with_child(Dom::create_text("Child 1")),
                Dom::create_node(NodeType::P).with_child(Dom::create_text("Child 2")),
            ]
            .into(),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];

    // Clear the cache to test first computation (StyledDom::new already computed once)
    cache.computed_values.iter_mut().for_each(|m| m.clear());
    cache.dependency_chains.iter_mut().for_each(|m| m.clear());

    // First computation
    let changed_nodes_1 = cache.compute_inherited_values(node_hierarchy, node_data);

    println!("First computation changed nodes: {:?}", changed_nodes_1);

    // All nodes should be marked as changed on first computation
    assert!(
        changed_nodes_1.len() >= 3,
        "All nodes should change on first computation"
    );

    // Second computation without changes should return empty list
    let changed_nodes_2 = cache.compute_inherited_values(node_hierarchy, node_data);

    println!("Second computation changed nodes: {:?}", changed_nodes_2);

    assert!(
        changed_nodes_2.is_empty(),
        "No nodes should change when recomputing with same values"
    );
}

#[test]
fn test_deeply_nested_inheritance() {
    // Test inheritance through many levels:
    // <div style="font-weight: bold">
    //   <section>
    //     <article>
    //       <p>
    //         <span>Deep text</span>
    //       </p>
    //     </article>
    //   </section>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_weight(
                StyleFontWeight::Bold,
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::Section).with_child(
            Dom::create_node(NodeType::Article).with_child(
                Dom::create_node(NodeType::P).with_child(
                    Dom::create_node(NodeType::Span).with_child(Dom::create_text("Deep text")),
                ),
            ),
        ));

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];
    cache.compute_inherited_values(node_hierarchy, node_data);

    println!("Node hierarchy: {:#?}", node_hierarchy);
    println!("Computed values: {:#?}", cache.computed_values);

    // Verify all descendants inherited font-weight: bold
    let span_id = azul_core::dom::NodeId::new(4); // The deepest span

    let Some(span_computed) = cache.computed_values.get(span_id.index()) else {
        panic!("Deeply nested span should have computed values");
    };

    let Some(prop_with_origin) = span_computed.get(&CssPropertyType::FontWeight) else {
        panic!("Deeply nested span should have inherited FontWeight");
    };
    let CssProperty::FontWeight(font_weight_value) = &prop_with_origin.property else {
        panic!("FontWeight should be CssProperty::FontWeight variant");
    };

    let Some(font_weight) = font_weight_value.get_property() else {
        panic!("FontWeight should have explicit value");
    };

    assert_eq!(
        *font_weight,
        StyleFontWeight::Bold,
        "Deeply nested span should inherit font-weight: bold"
    );
}

#[test]
fn test_em_unit_inheritance_basic() {
    // Test that 'em' units are resolved relative to the current element's font-size
    // <div style="font-size: 16px">
    //   <p style="font-size: 2em">  <!-- Should be 32px -->
    //     Text
    //   </p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(16.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::em(2.0),
                    ))]
                    .into(),
                )
                .with_child(Dom::create_text("Text")),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];

    cache.compute_inherited_values(node_hierarchy, node_data);

    let p_id = azul_core::dom::NodeId::new(1); // p

    let Some(p_computed) = cache.computed_values.get(p_id.index()) else {
        panic!("p should have computed values");
    };

    let Some(prop_with_origin) = p_computed.get(&CssPropertyType::FontSize) else {
        panic!("p should have computed FontSize property");
    };
    let CssProperty::FontSize(font_size_value) = &prop_with_origin.property else {
        panic!("FontSize should be CssProperty::FontSize variant");
    };

    let Some(font_size) = font_size_value.get_property() else {
        panic!("FontSize value should not be None/Auto/Initial/Inherit");
    };

    // 2em relative to parent's 16px = 32px
    let size = font_size.inner.to_pixels_internal(16.0, 16.0); // Parent's font-size is 16px
    assert_eq!(
        size, 32.0,
        "p with font-size: 2em should compute to 32px (2 * 16px)"
    );
}

#[test]
fn test_em_unit_cascading_multiplication() {
    // Test that 'em' units cascade properly through multiple levels
    // Each level multiplies by the parent's computed font-size
    // <div style="font-size: 10px">
    //   <p style="font-size: 2em">       <!-- 20px = 10 * 2 -->
    //     <span style="font-size: 1.5em"><!-- 30px = 20 * 1.5 -->
    //       Text
    //     </span>
    //   </p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(10.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::em(2.0),
                    ))]
                    .into(),
                )
                .with_child(
                    Dom::create_node(NodeType::Span)
                        .with_css_props(
                            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                                StyleFontSize::em(1.5),
                            ))]
                            .into(),
                        )
                        .with_child(Dom::create_text("Text")),
                ),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];

    cache.compute_inherited_values(node_hierarchy, node_data);

    let p_id = azul_core::dom::NodeId::new(1); // p
    let span_id = azul_core::dom::NodeId::new(2); // span

    // Check p: 2em * 10px = 20px
    let Some(p_computed) = cache.computed_values.get(p_id.index()) else {
        panic!("p should have computed values");
    };

    let Some(prop_with_origin) = p_computed.get(&CssPropertyType::FontSize) else {
        panic!("p should have computed FontSize");
    };
    let CssProperty::FontSize(p_font_size) = &prop_with_origin.property else {
        panic!("FontSize should be CssProperty::FontSize variant");
    };

    let Some(p_size_val) = p_font_size.get_property() else {
        panic!("p FontSize should have value");
    };

    let p_size = p_size_val.inner.to_pixels_internal(10.0, 10.0); // Parent (div) is 10px
    assert_eq!(p_size, 20.0, "p should be 20px (2em * 10px)");

    // Check span: 1.5em * 20px = 30px
    let Some(span_computed) = cache.computed_values.get(span_id.index()) else {
        panic!("span should have computed values");
    };

    let Some(prop_with_origin) = span_computed.get(&CssPropertyType::FontSize) else {
        panic!("span should have computed FontSize");
    };

    let CssProperty::FontSize(span_font_size) = &prop_with_origin.property else {
        panic!("span property should be FontSize");
    };

    let Some(span_size_val) = span_font_size.get_property() else {
        panic!("span FontSize should have value");
    };

    let span_size = span_size_val.inner.to_pixels_internal(20.0, 20.0); // Parent (p) is 20px
    assert_eq!(span_size, 30.0, "span should be 30px (1.5em * 20px)");
}

#[test]
fn test_em_on_font_size_refers_to_parent() {
    // CSS Spec: "The exception is when 'em' occurs in the value of the 'font-size'
    // property itself, in which case it refers to the font size of the parent element."
    //
    // <div style="font-size: 20px">
    //   <p style="font-size: 1.5em">  <!-- Should be 30px (1.5 * parent's 20px) -->
    //     <span style="padding: 2em">  <!-- Should be 60px (2 * current element's 30px) -->
    //       Text
    //     </span>
    //   </p>
    // </div>

    use azul_css::{
        css::CssPropertyValue,
        props::{
            basic::{length::SizeMetric, pixel::PixelValue},
            layout::spacing::LayoutPaddingLeft,
        },
    };

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::em(1.5),
                    ))]
                    .into(),
                )
                .with_child(
                    Dom::create_node(NodeType::Span)
                        .with_css_props(
                            vec![CssPropertyWithConditions::simple(CssProperty::PaddingLeft(
                                CssPropertyValue::Exact(LayoutPaddingLeft {
                                    inner: PixelValue::em(2.0),
                                }),
                            ))]
                            .into(),
                        )
                        .with_child(Dom::create_text("Text")),
                ),
        );

    // NOTE: setup_test! already calls StyledDom::new which calls compute_inherited_values
    // So we should NOT call it again, as that would re-process with already-resolved values
    let (styled_dom, cache) = setup_test!(dom);

    let div_id = azul_core::dom::NodeId::new(0); // div
    let p_id = azul_core::dom::NodeId::new(1); // p
    let span_id = azul_core::dom::NodeId::new(2); // span

    // Check div's font-size: should be 20px (Px metric)
    let div_computed = cache
        .computed_values
        .get(div_id.index())
        .expect("div should have computed values");
    let div_font_prop = div_computed
        .get(&CssPropertyType::FontSize)
        .expect("div should have FontSize");
    let CssProperty::FontSize(div_font_size) = &div_font_prop.property else {
        panic!("div property should be FontSize");
    };
    let div_size_val = div_font_size
        .get_property()
        .expect("div FontSize should have value");
    assert_eq!(
        div_size_val.inner.metric,
        SizeMetric::Px,
        "div should have Px metric"
    );
    assert_eq!(
        div_size_val.inner.number.get(),
        20.0,
        "div font-size should be 20px"
    );

    // Check p's font-size: should be resolved to 30px (1.5 * 20px parent)
    let p_computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");
    let p_font_prop = p_computed
        .get(&CssPropertyType::FontSize)
        .expect("p should have FontSize");
    let CssProperty::FontSize(p_font_size) = &p_font_prop.property else {
        panic!("p property should be FontSize");
    };
    let p_size_val = p_font_size
        .get_property()
        .expect("p FontSize should have value");

    // The key test: p's font-size should be RESOLVED to Px, not still in Em
    assert_eq!(
        p_size_val.inner.metric,
        SizeMetric::Px,
        "p font-size should be resolved to Px metric (was {:?})",
        p_size_val.inner.metric
    );
    assert!(
        (p_size_val.inner.number.get() - 30.0).abs() < 0.001,
        "p font-size should be 30px (1.5em * 20px), got {}",
        p_size_val.inner.number.get()
    );

    // Check span's inherited font-size: should inherit 30px from p
    let span_computed = cache
        .computed_values
        .get(span_id.index())
        .expect("span should have computed values");
    let span_font_prop = span_computed
        .get(&CssPropertyType::FontSize)
        .expect("span should have inherited FontSize");
    let CssProperty::FontSize(span_font_size) = &span_font_prop.property else {
        panic!("span property should be FontSize");
    };
    let span_size_val = span_font_size
        .get_property()
        .expect("span FontSize should have value");

    println!(
        "DEBUG: p font-size: metric={:?}, value={}",
        p_size_val.inner.metric,
        p_size_val.inner.number.get()
    );
    println!(
        "DEBUG: span font-size: metric={:?}, value={}",
        span_size_val.inner.metric,
        span_size_val.inner.number.get()
    );
    println!("DEBUG: span origin={:?}", span_font_prop.origin);

    // span should inherit the already-resolved 30px value from p
    assert_eq!(
        span_size_val.inner.metric,
        SizeMetric::Px,
        "span inherited font-size should be Px metric (was {:?})",
        span_size_val.inner.metric
    );
    assert!(
        (span_size_val.inner.number.get() - 30.0).abs() < 0.001,
        "span should inherit font-size: 30px from p, got {}",
        span_size_val.inner.number.get()
    );
}

#[test]
fn test_em_without_ancestor_absolute_unit() {
    // Test that when no ancestor has an absolute font-size, the default is used (16px)
    // <div style="font-size: 2em">  <!-- 2 * 16px (default) = 32px -->
    //   <p>Text</p>  <!-- Inherits 32px -->
    // </div>

    use azul_css::props::basic::length::SizeMetric;

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::em(2.0),
            ))]
            .into(),
        )
        .with_child(Dom::create_node(NodeType::P).with_child(Dom::create_text("Text")));

    // NOTE: setup_test! already calls StyledDom::new which calls compute_inherited_values
    let (styled_dom, cache) = setup_test!(dom);

    let div_id = azul_core::dom::NodeId::new(0); // div
    let p_id = azul_core::dom::NodeId::new(1); // p

    // div should resolve to 2 * 16px (default) = 32px
    let div_computed = cache
        .computed_values
        .get(div_id.index())
        .expect("div should have computed values");
    let div_font_prop = div_computed
        .get(&CssPropertyType::FontSize)
        .expect("div should have FontSize");
    let CssProperty::FontSize(div_font_size) = &div_font_prop.property else {
        panic!("div property should be FontSize");
    };
    let div_size_val = div_font_size
        .get_property()
        .expect("div FontSize should have value");

    // The key test: div's font-size should be RESOLVED to Px
    assert_eq!(
        div_size_val.inner.metric,
        SizeMetric::Px,
        "div font-size should be resolved to Px metric (was {:?})",
        div_size_val.inner.metric
    );
    assert!(
        (div_size_val.inner.number.get() - 32.0).abs() < 0.001,
        "div font-size: 2em without absolute ancestor should be 32px (2 * 16px default), got {}",
        div_size_val.inner.number.get()
    );

    // p should inherit 32px from div
    let p_computed = cache
        .computed_values
        .get(p_id.index())
        .expect("p should have computed values");
    let p_font_prop = p_computed
        .get(&CssPropertyType::FontSize)
        .expect("p should have inherited FontSize");
    let CssProperty::FontSize(p_font_size) = &p_font_prop.property else {
        panic!("p property should be FontSize");
    };
    let p_size_val = p_font_size
        .get_property()
        .expect("p FontSize should have value");

    assert_eq!(
        p_size_val.inner.metric,
        SizeMetric::Px,
        "p inherited font-size should be Px metric (was {:?})",
        p_size_val.inner.metric
    );
    assert!(
        (p_size_val.inner.number.get() - 32.0).abs() < 0.001,
        "p should inherit 32px from parent div, got {}",
        p_size_val.inner.number.get()
    );
}

#[test]
fn test_percentage_font_size_inheritance() {
    // Test that percentage font-sizes work like em (100% = 1em)
    // <div style="font-size: 20px">
    //   <p style="font-size: 150%">  <!-- 150% of 20px = 30px -->
    //     <span style="font-size: 80%">  <!-- 80% of 30px = 24px -->
    //       Text
    //     </span>
    //   </p>
    // </div>

    let dom = Dom::create_div()
        .with_css_props(
            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                StyleFontSize::px(20.0),
            ))]
            .into(),
        )
        .with_child(
            Dom::create_node(NodeType::P)
                .with_css_props(
                    vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                        StyleFontSize::percent(150.0),
                    ))]
                    .into(),
                )
                .with_child(
                    Dom::create_node(NodeType::Span)
                        .with_css_props(
                            vec![CssPropertyWithConditions::simple(CssProperty::font_size(
                                StyleFontSize::percent(80.0),
                            ))]
                            .into(),
                        )
                        .with_child(Dom::create_text("Text")),
                ),
        );

    let (styled_dom, mut cache) = setup_test!(dom);
    let node_hierarchy = &styled_dom.node_hierarchy.as_container().internal[..];
    let node_data = &styled_dom.node_data.as_container().internal[..];

    cache.compute_inherited_values(node_hierarchy, node_data);

    let p_id = azul_core::dom::NodeId::new(1); // p
    let span_id = azul_core::dom::NodeId::new(2); // span

    // p: 150% of 20px = 30px
    let Some(p_computed) = cache.computed_values.get(p_id.index()) else {
        panic!("p should have computed values");
    };

    let Some(prop_with_origin) = p_computed.get(&CssPropertyType::FontSize) else {
        panic!("p should have FontSize");
    };

    let CssProperty::FontSize(p_font_size) = &prop_with_origin.property else {
        panic!("p property should be FontSize");
    };

    let Some(p_size_val) = p_font_size.get_property() else {
        panic!("p FontSize should have value");
    };

    let p_size = p_size_val.inner.to_pixels_internal(20.0, 20.0); // Parent is 20px
    assert_eq!(p_size, 30.0, "p with 150% should be 30px (1.5 * 20px)");

    // span: 80% of 30px = 24px
    let Some(span_computed) = cache.computed_values.get(span_id.index()) else {
        panic!("span should have computed values");
    };

    let Some(prop_with_origin) = span_computed.get(&CssPropertyType::FontSize) else {
        panic!("span should have FontSize");
    };

    let CssProperty::FontSize(span_font_size) = &prop_with_origin.property else {
        panic!("span property should be FontSize");
    };

    let Some(span_size_val) = span_font_size.get_property() else {
        panic!("span FontSize should have value");
    };

    let span_size = span_size_val.inner.to_pixels_internal(30.0, 30.0); // Parent is 30px
    assert_eq!(span_size, 24.0, "span with 80% should be 24px (0.8 * 30px)");
}