1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod autotest_generated {
5
    use alloc::{string::String, vec, vec::Vec};
6

            
7
    use azul_css::{corety::AzString, css::BoxOrStatic, props::basic::length::SizeMetric};
8

            
9
    use super::*;
10
    use crate::resources::{ImageRef, RawImageFormat};
11

            
12
    // ------------------------------------------------------------------
13
    // Constructors / helpers
14
    // ------------------------------------------------------------------
15

            
16
    fn text_node(s: &str) -> NodeType {
17
        NodeType::Text(BoxOrStatic::heap(AzString::from(s)))
18
    }
19

            
20
    /// A VirtualView is a scroll container BY DEFAULT: `overflow: auto` on
21
    /// both axes from the UA sheet, so app CSS no longer has to opt in (the
22
    /// virtual-size-aware necessity rule keeps bars away until the published
23
    /// `virtual_scroll_size` actually overflows). Opt-outs stay explicit
24
    /// (`overflow: hidden` — map/video); invisible-but-scrollable composes
25
    /// via `scrollbar-width: none`.
26
    #[test]
27
    fn virtual_view_defaults_to_overflow_auto_on_both_axes() {
28
        use azul_css::props::layout::LayoutOverflow;
29
        for pt in [CssPropertyType::OverflowX, CssPropertyType::OverflowY] {
30
            let got = get_ua_property(&NodeType::VirtualView, pt)
31
                .unwrap_or_else(|| panic!("no UA default for VirtualView {pt:?}"));
32
            let ok = matches!(
33
                got,
34
                CssProperty::OverflowX(CssPropertyValue::Exact(LayoutOverflow::Auto))
35
                    | CssProperty::OverflowY(CssPropertyValue::Exact(LayoutOverflow::Auto))
36
            );
37
            assert!(
38
                ok,
39
                "VirtualView {pt:?} UA default is not overflow:auto: {got:?}"
40
            );
41
        }
42
        // And the axis-correct variant is returned for each request.
43
        assert!(matches!(
44
            get_ua_property(&NodeType::VirtualView, CssPropertyType::OverflowX),
45
            Some(CssProperty::OverflowX(_))
46
        ));
47
        assert!(matches!(
48
            get_ua_property(&NodeType::VirtualView, CssPropertyType::OverflowY),
49
            Some(CssProperty::OverflowY(_))
50
        ));
51
    }
52

            
53
    fn icon_node(s: &str) -> NodeType {
54
        NodeType::Icon(BoxOrStatic::heap(AzString::from(s)))
55
    }
56

            
57
    fn image_node() -> NodeType {
58
        NodeType::Image(BoxOrStatic::heap(ImageRef::null_image(
59
            1,
60
            1,
61
            RawImageFormat::RGBA8,
62
            Vec::new(),
63
        )))
64
    }
65

            
66
    /// Broad (not literally exhaustive) sample of `NodeType`, covering every
67
    /// variant that has an arm in `get_ua_property` plus a spread of variants
68
    /// that have none, so the catch-all arms get exercised too.
69
    fn sample_node_types() -> Vec<NodeType> {
70
        use crate::dom::NodeType as NT;
71
        vec![
72
            // matched arms
73
            NT::Html,
74
            NT::Head,
75
            NT::Body,
76
            NT::Div,
77
            NT::P,
78
            NT::Main,
79
            NT::Header,
80
            NT::Footer,
81
            NT::Section,
82
            NT::Article,
83
            NT::Aside,
84
            NT::Nav,
85
            NT::H1,
86
            NT::H2,
87
            NT::H3,
88
            NT::H4,
89
            NT::H5,
90
            NT::H6,
91
            NT::Ul,
92
            NT::Ol,
93
            NT::Li,
94
            NT::Dl,
95
            NT::Dt,
96
            NT::Dd,
97
            NT::Span,
98
            NT::A,
99
            NT::Strong,
100
            NT::Em,
101
            NT::B,
102
            NT::I,
103
            NT::U,
104
            NT::Small,
105
            NT::Code,
106
            NT::Kbd,
107
            NT::Samp,
108
            NT::Sub,
109
            NT::Sup,
110
            NT::Pre,
111
            NT::BlockQuote,
112
            NT::Hr,
113
            NT::Table,
114
            NT::THead,
115
            NT::TBody,
116
            NT::TFoot,
117
            NT::Tr,
118
            NT::Th,
119
            NT::Td,
120
            NT::Caption,
121
            NT::ColGroup,
122
            NT::Col,
123
            NT::Form,
124
            NT::Input,
125
            NT::Button,
126
            NT::Select,
127
            NT::TextArea,
128
            NT::Label,
129
            NT::Title,
130
            NT::Script,
131
            NT::Style,
132
            NT::Link,
133
            NT::Br,
134
            NT::Video,
135
            NT::Audio,
136
            NT::Canvas,
137
            NT::Svg,
138
            NT::VirtualView,
139
            NT::SelectOption,
140
            NT::OptGroup,
141
            NT::Abbr,
142
            NT::Cite,
143
            NT::Del,
144
            NT::Ins,
145
            NT::Mark,
146
            NT::Q,
147
            NT::Dfn,
148
            NT::Var,
149
            NT::Time,
150
            NT::Data,
151
            NT::Wbr,
152
            NT::Bdi,
153
            NT::Bdo,
154
            NT::Rp,
155
            NT::Rt,
156
            NT::Rtc,
157
            NT::Ruby,
158
            NT::FieldSet,
159
            NT::Figure,
160
            NT::FigCaption,
161
            NT::Details,
162
            NT::Summary,
163
            NT::Dialog,
164
            NT::Menu,
165
            NT::Dir,
166
            // unmatched arms (must fall through to the catch-alls)
167
            NT::Address,
168
            NT::Legend,
169
            NT::Output,
170
            NT::Progress,
171
            NT::Meter,
172
            NT::DataList,
173
            NT::MenuItem,
174
            NT::S,
175
            NT::Big,
176
            NT::Acronym,
177
            NT::Object,
178
            NT::Param,
179
            NT::Embed,
180
            NT::Source,
181
            NT::Track,
182
            NT::Map,
183
            NT::Area,
184
            NT::Meta,
185
            NT::Base,
186
            NT::Before,
187
            NT::After,
188
            NT::Marker,
189
            NT::Placeholder,
190
            NT::SvgG,
191
            NT::SvgPath,
192
            NT::SvgRect,
193
            NT::SvgText(AzString::from("svg-text")),
194
            // payload-carrying variants
195
            text_node(""),
196
            text_node("hello"),
197
            icon_node("home"),
198
            image_node(),
199
        ]
200
    }
201

            
202
    fn all_os() -> Vec<OsCondition> {
203
        vec![
204
            OsCondition::Any,
205
            OsCondition::Apple,
206
            OsCondition::MacOS,
207
            OsCondition::IOS,
208
            OsCondition::Linux,
209
            OsCondition::Windows,
210
            OsCondition::Android,
211
            OsCondition::Web,
212
        ]
213
    }
214

            
215
    fn all_themes() -> Vec<ThemeCondition> {
216
        vec![
217
            ThemeCondition::Light,
218
            ThemeCondition::Dark,
219
            ThemeCondition::Custom(AzString::from("neon")),
220
            ThemeCondition::SystemPreferred,
221
        ]
222
    }
223

            
224
    fn ctx(os: OsCondition, theme: ThemeCondition) -> DynamicSelectorContext {
225
        DynamicSelectorContext {
226
            os,
227
            theme,
228
            ..DynamicSelectorContext::default()
229
        }
230
    }
231

            
232
    const CLASSIC_LIGHT_THUMB: ColorU = ColorU {
233
        r: 193,
234
        g: 193,
235
        b: 193,
236
        a: 255,
237
    };
238
    const CLASSIC_LIGHT_TRACK: ColorU = ColorU {
239
        r: 241,
240
        g: 241,
241
        b: 241,
242
        a: 255,
243
    };
244

            
245
    fn custom_color(thumb: ColorU, track: ColorU) -> StyleScrollbarColor {
246
        StyleScrollbarColor::Custom(ScrollbarColorCustom { thumb, track })
247
    }
248

            
249
    /// Extract the `(thumb, track)` pair, panicking if the property is not a
250
    /// `Custom` scrollbar color.
251
    fn unwrap_custom(c: StyleScrollbarColor) -> (ColorU, ColorU) {
252
        match c {
253
            StyleScrollbarColor::Custom(c) => (c.thumb, c.track),
254
            StyleScrollbarColor::Auto => panic!("expected a Custom scrollbar color, got Auto"),
255
        }
256
    }
257

            
258
    fn display_of(nt: &NodeType) -> LayoutDisplay {
259
        match get_ua_property(nt, CssPropertyType::Display) {
260
            Some(CssProperty::Display(CssPropertyValue::Exact(d))) => *d,
261
            other => panic!("{nt:?}: expected an exact display value, got {other:?}"),
262
        }
263
    }
264

            
265
    fn font_size_em(nt: &NodeType) -> f32 {
266
        match get_ua_property(nt, CssPropertyType::FontSize) {
267
            Some(CssProperty::FontSize(CssPropertyValue::Exact(fs))) => {
268
                assert_eq!(
269
                    fs.inner.metric,
270
                    SizeMetric::Em,
271
                    "{nt:?}: font-size must be em-relative"
272
                );
273
                fs.inner.number.get()
274
            }
275
            other => panic!("{nt:?}: expected an exact em font-size, got {other:?}"),
276
        }
277
    }
278

            
279
    // ==================================================================
280
    // get_ua_property — table-wide invariants
281
    // ==================================================================
282

            
283
    /// The single most important invariant of the lookup table: the property
284
    /// that comes back must be *the property that was asked for*. A copy-paste
285
    /// slip in the ~200-arm table (e.g. `(H1, MarginBottom) => &MARGIN_TOP_...`)
286
    /// would silently mis-style elements; nothing else in the codebase checks it.
287
    #[test]
288
    fn returned_property_always_has_the_requested_type() {
289
        for nt in sample_node_types() {
290
            for pt in CssPropertyType::ALL {
291
                if let Some(prop) = get_ua_property(&nt, *pt) {
292
                    assert_eq!(
293
                        prop.get_type(),
294
                        *pt,
295
                        "get_ua_property({nt:?}, {pt:?}) returned a {:?} property",
296
                        prop.get_type()
297
                    );
298
                }
299
            }
300
        }
301
    }
302

            
303
    #[test]
304
    fn full_cross_product_never_panics_and_is_deterministic() {
305
        for nt in sample_node_types() {
306
            for pt in CssPropertyType::ALL {
307
                let a = get_ua_property(&nt, *pt);
308
                let b = get_ua_property(&nt, *pt);
309
                match (a, b) {
310
                    (Some(a), Some(b)) => assert!(
311
                        core::ptr::eq(a, b),
312
                        "{nt:?}/{pt:?}: repeated lookups must hand back the same static"
313
                    ),
314
                    (None, None) => {}
315
                    _ => panic!("{nt:?}/{pt:?}: lookup is not deterministic"),
316
                }
317
            }
318
        }
319
    }
320

            
321
    /// Documented contract: the `(_, Display)` catch-all means *every* node type
322
    /// resolves a display value, so layout never sees a node without one.
323
    #[test]
324
    fn display_resolves_for_every_node_type() {
325
        for nt in sample_node_types() {
326
            assert!(
327
                get_ua_property(&nt, CssPropertyType::Display).is_some(),
328
                "{nt:?} has no default display"
329
            );
330
        }
331
    }
332

            
333
    #[test]
334
    fn unknown_elements_default_to_inline_display() {
335
        // Per CSS spec, unknown/custom elements are inline.
336
        for nt in [NodeType::Address, NodeType::Legend, NodeType::Meter] {
337
            assert_eq!(display_of(&nt), LayoutDisplay::Inline, "{nt:?}");
338
        }
339
    }
340

            
341
    /// SVG shapes are NOT unknown elements: they are painted boxes.
342
    ///
343
    /// `SvgPath` used to be listed above as an example of the inline default,
344
    /// and that default is exactly what stopped an SVG in a DOM from painting -
345
    /// `paint_node_background_and_border` skips inline boxes, because their
346
    /// backgrounds belong to text layout, which knows nothing about SVG
347
    /// geometry. The definition containers stay `display: none`: they define,
348
    /// they do not draw.
349
    #[test]
350
    fn svg_shapes_are_boxes_and_svg_definitions_do_not_draw() {
351
        for nt in [
352
            NodeType::SvgPath,
353
            NodeType::SvgCircle,
354
            NodeType::SvgRect,
355
            NodeType::SvgEllipse,
356
            NodeType::SvgLine,
357
            NodeType::SvgPolygon,
358
            NodeType::SvgPolyline,
359
            NodeType::SvgG,
360
            NodeType::SvgUse,
361
        ] {
362
            assert_eq!(display_of(&nt), LayoutDisplay::Block, "{nt:?} must paint");
363
        }
364
        for nt in [
365
            NodeType::SvgDefs,
366
            NodeType::SvgSymbol,
367
            NodeType::SvgClipPathElement,
368
        ] {
369
            assert_eq!(display_of(&nt), LayoutDisplay::None, "{nt:?} must not draw");
370
        }
371
        // The `<svg>` element itself is a REPLACED element, like `<img>`:
372
        // inline-level, but with a box, so its intrinsic size applies.
373
        assert_eq!(display_of(&NodeType::Svg), LayoutDisplay::InlineBlock);
374
    }
375

            
376
    /// `cursor` is deliberately defined for exactly three node types; anything
377
    /// else must return `None` so the cursor-resolution walk can inherit.
378
    #[test]
379
    fn cursor_default_exists_only_for_button_textarea_and_text() {
380
        for nt in sample_node_types() {
381
            let has_cursor = get_ua_property(&nt, CssPropertyType::Cursor).is_some();
382
            let expected = matches!(
383
                nt,
384
                NodeType::Button | NodeType::TextArea | NodeType::Text(_)
385
            );
386
            assert_eq!(has_cursor, expected, "{nt:?}: unexpected cursor default");
387
        }
388
    }
389

            
390
    // ==================================================================
391
    // get_ua_property — payload-carrying node types (unicode / huge / empty)
392
    // ==================================================================
393

            
394
    #[test]
395
    fn text_node_defaults_are_independent_of_the_payload() {
396
        let huge = "🦀".repeat(100_000);
397
        let payloads: Vec<String> = vec![
398
            String::new(),
399
            "\0".into(),
400
            "\u{202E}\u{200B}\u{FEFF}".into(), // RTL override, ZWSP, BOM
401
            "مرحبا بالعالم".into(),
402
            "🇩🇪👨‍👩‍👧‍👦".into(),
403
            "\u{FFFD}".into(),
404
            huge,
405
        ];
406

            
407
        for p in payloads {
408
            let nt = text_node(&p);
409
            assert_eq!(
410
                display_of(&nt),
411
                LayoutDisplay::Inline,
412
                "text node display must not depend on its content"
413
            );
414
            assert_eq!(
415
                get_ua_property(&nt, CssPropertyType::Cursor),
416
                Some(&CURSOR_TEXT),
417
                "text node cursor must not depend on its content"
418
            );
419
            // Text nodes define no box properties of their own.
420
            assert!(get_ua_property(&nt, CssPropertyType::Width).is_none());
421
            assert!(get_ua_property(&nt, CssPropertyType::Height).is_none());
422
            assert!(get_ua_property(&nt, CssPropertyType::MarginTop).is_none());
423
        }
424
    }
425

            
426
    #[test]
427
    fn icon_and_image_nodes_are_inline_block_regardless_of_payload() {
428
        let huge_name = "x".repeat(50_000);
429
        let names: [&str; 4] = ["", "home", "🏠", huge_name.as_str()];
430
        for name in names {
431
            assert_eq!(
432
                display_of(&icon_node(name)),
433
                LayoutDisplay::InlineBlock,
434
                "icon {name:?}"
435
            );
436
        }
437
        assert_eq!(display_of(&image_node()), LayoutDisplay::InlineBlock);
438
    }
439

            
440
    // ==================================================================
441
    // get_ua_property — specific, load-bearing defaults
442
    // ==================================================================
443

            
444
    /// Regression guard for the 2026-06-02 DIAG revert documented in the table:
445
    /// `(Html, Height) => HEIGHT_100_PERCENT` is commented out on purpose. If it
446
    /// comes back without the jump-table dispatch fix, children wrongly inherit
447
    /// `height: 100%`.
448
    #[test]
449
    fn html_has_no_default_height() {
450
        assert_eq!(
451
            get_ua_property(&NodeType::Html, CssPropertyType::Display),
452
            Some(&DISPLAY_BLOCK)
453
        );
454
        assert!(
455
            get_ua_property(&NodeType::Html, CssPropertyType::Height).is_none(),
456
            "the (Html, Height) arm is intentionally disabled — see the DIAG note"
457
        );
458
    }
459

            
460
    /// `body { margin: 8px }` (Chrome UA), and crucially *no* width/height:
461
    /// giving body a size would break percentage sizing of its children.
462
    #[test]
463
    fn body_has_8px_margins_and_no_intrinsic_size() {
464
        assert_eq!(display_of(&NodeType::Body), LayoutDisplay::Block);
465
        assert_eq!(
466
            get_ua_property(&NodeType::Body, CssPropertyType::MarginTop),
467
            Some(&MARGIN_TOP_8PX)
468
        );
469
        assert_eq!(
470
            get_ua_property(&NodeType::Body, CssPropertyType::MarginBottom),
471
            Some(&MARGIN_BOTTOM_8PX)
472
        );
473
        assert_eq!(
474
            get_ua_property(&NodeType::Body, CssPropertyType::MarginLeft),
475
            Some(&MARGIN_LEFT_8PX)
476
        );
477
        assert_eq!(
478
            get_ua_property(&NodeType::Body, CssPropertyType::MarginRight),
479
            Some(&MARGIN_RIGHT_8PX)
480
        );
481
        assert!(get_ua_property(&NodeType::Body, CssPropertyType::Width).is_none());
482
        assert!(get_ua_property(&NodeType::Body, CssPropertyType::Height).is_none());
483
    }
484

            
485
    /// Block elements must have `width: auto`, not `width: 100%` — the comment in
486
    /// the table calls this out as critical for flexbox (100% defeats flex-grow).
487
    #[test]
488
    fn block_elements_have_no_default_width() {
489
        for nt in [
490
            NodeType::Div,
491
            NodeType::P,
492
            NodeType::Section,
493
            NodeType::Main,
494
            NodeType::VirtualView,
495
        ] {
496
            assert_eq!(display_of(&nt), LayoutDisplay::Block, "{nt:?}");
497
            assert!(
498
                get_ua_property(&nt, CssPropertyType::Width).is_none(),
499
                "{nt:?} must be width:auto so it can flex-grow"
500
            );
501
        }
502
    }
503

            
504
    #[test]
505
    fn div_defines_only_a_display_default() {
506
        for pt in CssPropertyType::ALL {
507
            let got = get_ua_property(&NodeType::Div, *pt);
508
            if *pt == CssPropertyType::Display {
509
                assert!(got.is_some());
510
            } else {
511
                assert!(
512
                    got.is_none(),
513
                    "Div should not define a UA default for {pt:?}"
514
                );
515
            }
516
        }
517
    }
518

            
519
    #[test]
520
    fn metadata_elements_are_display_none() {
521
        for nt in [
522
            NodeType::Head,
523
            NodeType::Title,
524
            NodeType::Script,
525
            NodeType::Style,
526
            NodeType::Link,
527
        ] {
528
            assert_eq!(
529
                display_of(&nt),
530
                LayoutDisplay::None,
531
                "{nt:?} must not render"
532
            );
533
        }
534
    }
535

            
536
    #[test]
537
    fn heading_font_sizes_are_strictly_decreasing() {
538
        let sizes: Vec<f32> = [
539
            NodeType::H1,
540
            NodeType::H2,
541
            NodeType::H3,
542
            NodeType::H4,
543
            NodeType::H5,
544
            NodeType::H6,
545
        ]
546
        .iter()
547
        .map(font_size_em)
548
        .collect();
549

            
550
        // Chrome UA values — also verifies `const_em_fractional(1, 5)` really
551
        // encodes 1.5 (and not 1.05), which the digit-count encoding makes subtle.
552
        let expected = [2.0_f32, 1.5, 1.17, 1.0, 0.83, 0.67];
553
        for (i, (got, want)) in sizes.iter().zip(expected.iter()).enumerate() {
554
            assert!(
555
                (got - want).abs() < 1e-4,
556
                "H{} font-size: got {got}em, want {want}em",
557
                i + 1
558
            );
559
        }
560
        for w in sizes.windows(2) {
561
            assert!(
562
                w[0] > w[1],
563
                "heading font sizes must strictly decrease, got {sizes:?}"
564
            );
565
        }
566
    }
567

            
568
    #[test]
569
    fn headings_are_bold_blocks_that_avoid_page_breaks() {
570
        for nt in [
571
            NodeType::H1,
572
            NodeType::H2,
573
            NodeType::H3,
574
            NodeType::H4,
575
            NodeType::H5,
576
            NodeType::H6,
577
        ] {
578
            assert_eq!(display_of(&nt), LayoutDisplay::Block, "{nt:?}");
579
            assert_eq!(
580
                get_ua_property(&nt, CssPropertyType::FontWeight),
581
                Some(&FONT_WEIGHT_BOLD),
582
                "{nt:?}"
583
            );
584
            assert_eq!(
585
                get_ua_property(&nt, CssPropertyType::BreakInside),
586
                Some(&BREAK_INSIDE_AVOID),
587
                "{nt:?}"
588
            );
589
            assert_eq!(
590
                get_ua_property(&nt, CssPropertyType::BreakAfter),
591
                Some(&BREAK_AFTER_AVOID),
592
                "{nt:?}"
593
            );
594
            // Both margins must exist and be em-relative (they scale with font-size).
595
            for pt in [CssPropertyType::MarginTop, CssPropertyType::MarginBottom] {
596
                assert!(
597
                    get_ua_property(&nt, pt).is_some(),
598
                    "{nt:?} is missing {pt:?}"
599
                );
600
            }
601
        }
602
    }
603

            
604
    /// Tables *can* break across pages; their rows/headers/footers cannot. The
605
    /// table comments say so explicitly, so lock the asymmetry in.
606
    #[test]
607
    fn tables_may_break_across_pages_but_rows_may_not() {
608
        assert!(get_ua_property(&NodeType::Table, CssPropertyType::BreakInside).is_none());
609
        assert!(get_ua_property(&NodeType::TBody, CssPropertyType::BreakInside).is_none());
610
        for nt in [NodeType::THead, NodeType::TFoot, NodeType::Tr] {
611
            assert_eq!(
612
                get_ua_property(&nt, CssPropertyType::BreakInside),
613
                Some(&BREAK_INSIDE_AVOID),
614
                "{nt:?}"
615
            );
616
        }
617
    }
618

            
619
    #[test]
620
    fn table_display_types_are_not_crossed() {
621
        assert_eq!(display_of(&NodeType::Table), LayoutDisplay::Table);
622
        assert_eq!(
623
            display_of(&NodeType::THead),
624
            LayoutDisplay::TableHeaderGroup
625
        );
626
        assert_eq!(display_of(&NodeType::TBody), LayoutDisplay::TableRowGroup);
627
        assert_eq!(
628
            display_of(&NodeType::TFoot),
629
            LayoutDisplay::TableFooterGroup
630
        );
631
        assert_eq!(display_of(&NodeType::Tr), LayoutDisplay::TableRow);
632
        assert_eq!(display_of(&NodeType::Th), LayoutDisplay::TableCell);
633
        assert_eq!(display_of(&NodeType::Td), LayoutDisplay::TableCell);
634
        assert_eq!(display_of(&NodeType::Caption), LayoutDisplay::TableCaption);
635
        assert_eq!(
636
            display_of(&NodeType::ColGroup),
637
            LayoutDisplay::TableColumnGroup
638
        );
639
        assert_eq!(display_of(&NodeType::Col), LayoutDisplay::TableColumn);
640
    }
641

            
642
    #[test]
643
    fn table_cells_have_1px_padding_on_all_four_sides() {
644
        for nt in [NodeType::Th, NodeType::Td] {
645
            assert_eq!(
646
                get_ua_property(&nt, CssPropertyType::PaddingTop),
647
                Some(&PADDING_TOP_1PX),
648
                "{nt:?}"
649
            );
650
            assert_eq!(
651
                get_ua_property(&nt, CssPropertyType::PaddingBottom),
652
                Some(&PADDING_BOTTOM_1PX),
653
                "{nt:?}"
654
            );
655
            assert_eq!(
656
                get_ua_property(&nt, CssPropertyType::PaddingLeft),
657
                Some(&PADDING_LEFT_1PX),
658
                "{nt:?}"
659
            );
660
            assert_eq!(
661
                get_ua_property(&nt, CssPropertyType::PaddingRight),
662
                Some(&PADDING_RIGHT_1PX),
663
                "{nt:?}"
664
            );
665
            assert_eq!(
666
                get_ua_property(&nt, CssPropertyType::VerticalAlign),
667
                Some(&VERTICAL_ALIGN_MIDDLE),
668
                "{nt:?}"
669
            );
670
        }
671
        // Only <th> is centered + bold.
672
        assert_eq!(
673
            get_ua_property(&NodeType::Th, CssPropertyType::TextAlign),
674
            Some(&TEXT_ALIGN_CENTER)
675
        );
676
        assert_eq!(
677
            get_ua_property(&NodeType::Th, CssPropertyType::FontWeight),
678
            Some(&FONT_WEIGHT_BOLD)
679
        );
680
        assert!(get_ua_property(&NodeType::Td, CssPropertyType::TextAlign).is_none());
681
        assert!(get_ua_property(&NodeType::Td, CssPropertyType::FontWeight).is_none());
682
    }
683

            
684
    /// A button's border is symmetric. Crossed sides (e.g. `BorderLeftWidth`
685
    /// answered with the *top* static) would render an asymmetric button, so
686
    /// check that each side carries the value the table promises.
687
    #[test]
688
    fn button_border_is_symmetric_on_all_four_sides() {
689
        let widths = [
690
            (CssPropertyType::BorderTopWidth, &BUTTON_BORDER_TOP_WIDTH),
691
            (
692
                CssPropertyType::BorderBottomWidth,
693
                &BUTTON_BORDER_BOTTOM_WIDTH,
694
            ),
695
            (CssPropertyType::BorderLeftWidth, &BUTTON_BORDER_LEFT_WIDTH),
696
            (
697
                CssPropertyType::BorderRightWidth,
698
                &BUTTON_BORDER_RIGHT_WIDTH,
699
            ),
700
        ];
701
        for (pt, want) in widths {
702
            assert_eq!(get_ua_property(&NodeType::Button, pt), Some(want), "{pt:?}");
703
        }
704

            
705
        let styles = [
706
            (CssPropertyType::BorderTopStyle, &BUTTON_BORDER_TOP_STYLE),
707
            (
708
                CssPropertyType::BorderBottomStyle,
709
                &BUTTON_BORDER_BOTTOM_STYLE,
710
            ),
711
            (CssPropertyType::BorderLeftStyle, &BUTTON_BORDER_LEFT_STYLE),
712
            (
713
                CssPropertyType::BorderRightStyle,
714
                &BUTTON_BORDER_RIGHT_STYLE,
715
            ),
716
        ];
717
        for (pt, want) in styles {
718
            assert_eq!(get_ua_property(&NodeType::Button, pt), Some(want), "{pt:?}");
719
        }
720

            
721
        let colors = [
722
            (CssPropertyType::BorderTopColor, &BUTTON_BORDER_TOP_COLOR),
723
            (
724
                CssPropertyType::BorderBottomColor,
725
                &BUTTON_BORDER_BOTTOM_COLOR,
726
            ),
727
            (CssPropertyType::BorderLeftColor, &BUTTON_BORDER_LEFT_COLOR),
728
            (
729
                CssPropertyType::BorderRightColor,
730
                &BUTTON_BORDER_RIGHT_COLOR,
731
            ),
732
        ];
733
        for (pt, want) in colors {
734
            assert_eq!(get_ua_property(&NodeType::Button, pt), Some(want), "{pt:?}");
735
        }
736

            
737
        assert_eq!(display_of(&NodeType::Button), LayoutDisplay::InlineBlock);
738
        assert_eq!(
739
            get_ua_property(&NodeType::Button, CssPropertyType::Cursor),
740
            Some(&CURSOR_POINTER)
741
        );
742
    }
743

            
744
    /// `<hr>` draws its line from the *border*, not from a height — height must
745
    /// be exactly 0px, and the width exactly 100%.
746
    #[test]
747
    fn hr_line_comes_from_the_border_not_from_height() {
748
        match get_ua_property(&NodeType::Hr, CssPropertyType::Height) {
749
            Some(CssProperty::Height(CssPropertyValue::Exact(LayoutHeight::Px(pv)))) => {
750
                assert_eq!(pv.metric, SizeMetric::Px);
751
                assert!(
752
                    (pv.number.get() - 0.0).abs() < 1e-6,
753
                    "hr height must be 0px"
754
                );
755
            }
756
            other => panic!("hr height: {other:?}"),
757
        }
758
        match get_ua_property(&NodeType::Hr, CssPropertyType::Width) {
759
            Some(CssProperty::Width(CssPropertyValue::Exact(LayoutWidth::Px(pv)))) => {
760
                assert_eq!(pv.metric, SizeMetric::Percent);
761
                assert!(
762
                    (pv.number.get() - 100.0).abs() < 1e-4,
763
                    "hr width must be 100%"
764
                );
765
            }
766
            other => panic!("hr width: {other:?}"),
767
        }
768
        assert_eq!(
769
            get_ua_property(&NodeType::Hr, CssPropertyType::BorderTopStyle),
770
            Some(&BORDER_TOP_STYLE_INSET)
771
        );
772
        assert_eq!(
773
            get_ua_property(&NodeType::Hr, CssPropertyType::BorderTopWidth),
774
            Some(&BORDER_TOP_WIDTH_1PX)
775
        );
776
        assert_eq!(
777
            get_ua_property(&NodeType::Hr, CssPropertyType::BorderTopColor),
778
            Some(&BORDER_TOP_COLOR_GRAY)
779
        );
780
    }
781

            
782
    #[test]
783
    fn list_containers_reset_the_counter_and_reserve_marker_space() {
784
        for (nt, marker) in [
785
            (NodeType::Ul, &LIST_STYLE_TYPE_DISC),
786
            (NodeType::Ol, &LIST_STYLE_TYPE_DECIMAL),
787
        ] {
788
            assert_eq!(display_of(&nt), LayoutDisplay::Block, "{nt:?}");
789
            assert_eq!(
790
                get_ua_property(&nt, CssPropertyType::ListStyleType),
791
                Some(marker),
792
                "{nt:?}"
793
            );
794
            assert_eq!(
795
                get_ua_property(&nt, CssPropertyType::CounterReset),
796
                Some(&COUNTER_RESET_LIST_ITEM),
797
                "{nt:?} must reset the list-item counter"
798
            );
799
            assert_eq!(
800
                get_ua_property(&nt, CssPropertyType::PaddingLeft),
801
                Some(&PADDING_INLINE_START_40PX),
802
                "{nt:?}"
803
            );
804
        }
805
        assert_eq!(display_of(&NodeType::Li), LayoutDisplay::ListItem);
806
    }
807

            
808
    #[test]
809
    fn inline_emphasis_and_link_defaults() {
810
        assert_eq!(
811
            get_ua_property(&NodeType::A, CssPropertyType::TextDecoration),
812
            Some(&TEXT_DECORATION_UNDERLINE)
813
        );
814
        assert_eq!(
815
            get_ua_property(&NodeType::U, CssPropertyType::TextDecoration),
816
            Some(&TEXT_DECORATION_UNDERLINE)
817
        );
818
        assert_eq!(
819
            get_ua_property(&NodeType::Strong, CssPropertyType::FontWeight),
820
            Some(&FONT_WEIGHT_BOLDER)
821
        );
822
        assert_eq!(
823
            get_ua_property(&NodeType::B, CssPropertyType::FontWeight),
824
            Some(&FONT_WEIGHT_BOLDER)
825
        );
826
        // <em>/<i> are italic via font-style, which the UA table does not define.
827
        assert!(get_ua_property(&NodeType::Em, CssPropertyType::FontWeight).is_none());
828
        assert!(get_ua_property(&NodeType::I, CssPropertyType::FontWeight).is_none());
829
    }
830

            
831
    // ==================================================================
832
    // const scrollbar helpers — numeric round-trips / boundaries
833
    // ==================================================================
834

            
835
    #[test]
836
    fn scrollbar_fade_delay_round_trips_every_boundary() {
837
        for ms in [
838
            0_u32,
839
            1,
840
            2,
841
            299,
842
            300,
843
            500,
844
            u32::from(u16::MAX),
845
            i32::MAX as u32,
846
            u32::MAX - 1,
847
            u32::MAX,
848
        ] {
849
            match scrollbar_fade_delay(ms) {
850
                CssProperty::ScrollbarFadeDelay(CssPropertyValue::Exact(d)) => {
851
                    assert_eq!(d.ms, ms, "fade-delay must round-trip losslessly");
852
                }
853
                other => panic!("scrollbar_fade_delay({ms}) built a {other:?}"),
854
            }
855
        }
856
    }
857

            
858
    #[test]
859
    fn scrollbar_fade_duration_round_trips_every_boundary() {
860
        for ms in [
861
            0_u32,
862
            1,
863
            150,
864
            200,
865
            u32::from(u16::MAX),
866
            i32::MAX as u32,
867
            u32::MAX - 1,
868
            u32::MAX,
869
        ] {
870
            match scrollbar_fade_duration(ms) {
871
                CssProperty::ScrollbarFadeDuration(CssPropertyValue::Exact(d)) => {
872
                    assert_eq!(d.ms, ms, "fade-duration must round-trip losslessly");
873
                }
874
                other => panic!("scrollbar_fade_duration({ms}) built a {other:?}"),
875
            }
876
        }
877
    }
878

            
879
    /// `u32::MAX` in a `const` item: if either helper ever grew an arithmetic
880
    /// conversion (ms → ns, ms → seconds), this fails to *compile* rather than
881
    /// silently wrapping in release and panicking in debug.
882
    #[test]
883
    fn scrollbar_fade_helpers_are_const_evaluable_at_u32_max() {
884
        const MAX_DELAY: CssProperty = scrollbar_fade_delay(u32::MAX);
885
        const MAX_DURATION: CssProperty = scrollbar_fade_duration(u32::MAX);
886
        const ZERO_DELAY: CssProperty = scrollbar_fade_delay(0);
887

            
888
        assert_eq!(MAX_DELAY, scrollbar_fade_delay(u32::MAX));
889
        assert_eq!(MAX_DURATION, scrollbar_fade_duration(u32::MAX));
890
        assert_eq!(ZERO_DELAY, scrollbar_fade_delay(0));
891
    }
892

            
893
    /// The two helpers take the same `u32` and differ only in the wrapper type —
894
    /// exactly the shape a copy-paste bug likes. Assert they stay distinct.
895
    #[test]
896
    fn fade_delay_and_fade_duration_produce_distinct_property_types() {
897
        assert_eq!(
898
            scrollbar_fade_delay(42).get_type(),
899
            CssPropertyType::ScrollbarFadeDelay
900
        );
901
        assert_eq!(
902
            scrollbar_fade_duration(42).get_type(),
903
            CssPropertyType::ScrollbarFadeDuration
904
        );
905
        assert_ne!(scrollbar_fade_delay(42), scrollbar_fade_duration(42));
906
    }
907

            
908
    /// A `0` delay means "never fades" (per the `ScrollbarFadeDelay` docs), so it
909
    /// must be stored as a literal zero, not as a sentinel.
910
    #[test]
911
    fn zero_fade_delay_and_duration_are_literal_zero() {
912
        assert_eq!(
913
            scrollbar_fade_delay(0),
914
            CssProperty::ScrollbarFadeDelay(CssPropertyValue::Exact(ScrollbarFadeDelay::ZERO))
915
        );
916
        assert_eq!(
917
            scrollbar_fade_duration(0),
918
            CssProperty::ScrollbarFadeDuration(CssPropertyValue::Exact(
919
                ScrollbarFadeDuration::ZERO
920
            ))
921
        );
922
    }
923

            
924
    #[test]
925
    fn scrollbar_color_never_swaps_thumb_and_track() {
926
        let cases = [
927
            (
928
                ColorU {
929
                    r: 1,
930
                    g: 2,
931
                    b: 3,
932
                    a: 4,
933
                },
934
                ColorU {
935
                    r: 5,
936
                    g: 6,
937
                    b: 7,
938
                    a: 8,
939
                },
940
            ),
941
            (
942
                ColorU {
943
                    r: 0,
944
                    g: 0,
945
                    b: 0,
946
                    a: 0,
947
                },
948
                ColorU {
949
                    r: 255,
950
                    g: 255,
951
                    b: 255,
952
                    a: 255,
953
                },
954
            ),
955
            (
956
                ColorU {
957
                    r: 255,
958
                    g: 255,
959
                    b: 255,
960
                    a: 255,
961
                },
962
                ColorU::TRANSPARENT,
963
            ),
964
            (ColorU::TRANSPARENT, ColorU::TRANSPARENT),
965
        ];
966
        for (thumb, track) in cases {
967
            match scrollbar_color(thumb, track) {
968
                CssProperty::ScrollbarColor(CssPropertyValue::Exact(
969
                    StyleScrollbarColor::Custom(c),
970
                )) => {
971
                    assert_eq!(c.thumb, thumb, "thumb was not preserved");
972
                    assert_eq!(
973
                        c.track, track,
974
                        "track was not preserved (arguments swapped?)"
975
                    );
976
                }
977
                other => panic!("scrollbar_color built a {other:?}"),
978
            }
979
        }
980
    }
981

            
982
    #[test]
983
    fn scrollbar_width_and_visibility_round_trip_every_variant() {
984
        for w in [
985
            LayoutScrollbarWidth::Auto,
986
            LayoutScrollbarWidth::Thin,
987
            LayoutScrollbarWidth::None,
988
        ] {
989
            match scrollbar_width(w) {
990
                CssProperty::ScrollbarWidth(CssPropertyValue::Exact(got)) => assert_eq!(got, w),
991
                other => panic!("scrollbar_width({w:?}) built a {other:?}"),
992
            }
993
        }
994
        for v in [
995
            ScrollbarVisibilityMode::Always,
996
            ScrollbarVisibilityMode::WhenScrolling,
997
            ScrollbarVisibilityMode::Auto,
998
        ] {
999
            match scrollbar_visibility(v) {
                CssProperty::ScrollbarVisibility(CssPropertyValue::Exact(got)) => {
                    assert_eq!(got, v)
                }
                other => panic!("scrollbar_visibility({v:?}) built a {other:?}"),
            }
        }
    }
    // ==================================================================
    // UA_SCROLLBAR_CSS — table shape invariants
    // ==================================================================
    /// `evaluate_ua_scrollbar_css` matches on exactly five property kinds and
    /// silently drops everything else via `_ => {}`. A sixth property added to
    /// the table would therefore never take effect — fail loudly here instead.
    #[test]
    fn table_contains_only_property_kinds_the_evaluator_understands() {
        let understood = [
            CssPropertyType::ScrollbarColor,
            CssPropertyType::ScrollbarWidth,
            CssPropertyType::ScrollbarVisibility,
            CssPropertyType::ScrollbarFadeDelay,
            CssPropertyType::ScrollbarFadeDuration,
        ];
        for (i, entry) in UA_SCROLLBAR_CSS.iter().enumerate() {
            let ty = entry.property.get_type();
            assert!(
                understood.contains(&ty),
                "UA_SCROLLBAR_CSS[{i}] is a {ty:?}, which evaluate_ua_scrollbar_css ignores"
            );
        }
    }
    /// The evaluator only reads `CssPropertyValue::Exact`; an `Auto`/`Inherit`
    /// entry would be skipped without a trace.
    #[test]
    fn every_table_entry_carries_an_exact_value() {
        for (i, entry) in UA_SCROLLBAR_CSS.iter().enumerate() {
            let is_exact = matches!(
                &entry.property,
                CssProperty::ScrollbarColor(CssPropertyValue::Exact(_))
                    | CssProperty::ScrollbarWidth(CssPropertyValue::Exact(_))
                    | CssProperty::ScrollbarVisibility(CssPropertyValue::Exact(_))
                    | CssProperty::ScrollbarFadeDelay(CssPropertyValue::Exact(_))
                    | CssProperty::ScrollbarFadeDuration(CssPropertyValue::Exact(_))
            );
            assert!(
                is_exact,
                "UA_SCROLLBAR_CSS[{i}] is not an Exact value: {:?}",
                entry.property
            );
        }
    }
    /// The documented guarantee ("unconditional fallback entries … guarantee that
    /// every field resolves") plus the ordering rule it depends on: under
    /// first-match-wins, an unconditional entry that is *not* last for its
    /// property type would make every rule after it dead code.
    #[test]
    fn each_property_type_has_exactly_one_unconditional_entry_and_it_is_last() {
        for ty in [
            CssPropertyType::ScrollbarColor,
            CssPropertyType::ScrollbarWidth,
            CssPropertyType::ScrollbarVisibility,
            CssPropertyType::ScrollbarFadeDelay,
            CssPropertyType::ScrollbarFadeDuration,
        ] {
            let of_type: Vec<&CssPropertyWithConditions> = UA_SCROLLBAR_CSS
                .iter()
                .filter(|e| e.property.get_type() == ty)
                .collect();
            assert!(!of_type.is_empty(), "{ty:?} has no entry at all");
            let unconditional: Vec<usize> = of_type
                .iter()
                .enumerate()
                .filter(|(_, e)| e.apply_if.as_slice().is_empty())
                .map(|(i, _)| i)
                .collect();
            assert_eq!(
                unconditional.len(),
                1,
                "{ty:?} must have exactly one unconditional fallback, found {}",
                unconditional.len()
            );
            assert_eq!(
                unconditional[0],
                of_type.len() - 1,
                "{ty:?}: the unconditional fallback must come last, otherwise the {} rule(s) \
                 after it are dead under first-match-wins",
                of_type.len() - 1 - unconditional[0]
            );
        }
    }
    // ==================================================================
    // evaluate_ua_scrollbar_css
    // ==================================================================
    #[test]
    fn default_context_resolves_to_the_classic_light_scrollbar() {
        let r = evaluate_ua_scrollbar_css(&DynamicSelectorContext::default());
        assert_eq!(r.width, LayoutScrollbarWidth::Auto);
        assert_eq!(r.visibility, ScrollbarVisibilityMode::Always);
        assert_eq!(r.fade_delay.ms, 0);
        assert_eq!(r.fade_duration.ms, 0);
        assert_eq!(
            unwrap_custom(r.color),
            (CLASSIC_LIGHT_THUMB, CLASSIC_LIGHT_TRACK)
        );
    }
    #[test]
    fn per_os_and_theme_defaults_are_what_the_table_promises() {
        let cases: Vec<(
            OsCondition,
            ThemeCondition,
            LayoutScrollbarWidth,
            ScrollbarVisibilityMode,
            u32,
            u32,
            StyleScrollbarColor,
        )> = vec![
            (
                OsCondition::MacOS,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                500,
                200,
                custom_color(
                    ColorU {
                        r: 180,
                        g: 180,
                        b: 180,
                        a: 200,
                    },
                    ColorU {
                        r: 40,
                        g: 40,
                        b: 40,
                        a: 80,
                    },
                ),
            ),
            (
                OsCondition::MacOS,
                ThemeCondition::Light,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                500,
                200,
                custom_color(
                    ColorU {
                        r: 80,
                        g: 80,
                        b: 80,
                        a: 200,
                    },
                    ColorU {
                        r: 200,
                        g: 200,
                        b: 200,
                        a: 80,
                    },
                ),
            ),
            (
                OsCondition::Windows,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Auto,
                ScrollbarVisibilityMode::Always,
                0,
                0,
                custom_color(
                    ColorU {
                        r: 110,
                        g: 110,
                        b: 110,
                        a: 255,
                    },
                    ColorU {
                        r: 32,
                        g: 32,
                        b: 32,
                        a: 255,
                    },
                ),
            ),
            (
                OsCondition::Windows,
                ThemeCondition::Light,
                LayoutScrollbarWidth::Auto,
                ScrollbarVisibilityMode::Always,
                0,
                0,
                custom_color(
                    ColorU {
                        r: 130,
                        g: 130,
                        b: 130,
                        a: 255,
                    },
                    ColorU {
                        r: 241,
                        g: 241,
                        b: 241,
                        a: 255,
                    },
                ),
            ),
            (
                OsCondition::IOS,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                500,
                200,
                custom_color(
                    ColorU {
                        r: 255,
                        g: 255,
                        b: 255,
                        a: 100,
                    },
                    ColorU::TRANSPARENT,
                ),
            ),
            (
                OsCondition::IOS,
                ThemeCondition::Light,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                500,
                200,
                custom_color(
                    ColorU {
                        r: 0,
                        g: 0,
                        b: 0,
                        a: 100,
                    },
                    ColorU::TRANSPARENT,
                ),
            ),
            (
                OsCondition::Android,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                300,
                150,
                custom_color(
                    ColorU {
                        r: 255,
                        g: 255,
                        b: 255,
                        a: 77,
                    },
                    ColorU::TRANSPARENT,
                ),
            ),
            (
                OsCondition::Android,
                ThemeCondition::Light,
                LayoutScrollbarWidth::Thin,
                ScrollbarVisibilityMode::WhenScrolling,
                300,
                150,
                custom_color(
                    ColorU {
                        r: 0,
                        g: 0,
                        b: 0,
                        a: 77,
                    },
                    ColorU::TRANSPARENT,
                ),
            ),
            (
                // Linux has no OS-specific colour rule: dark falls through to the
                // generic dark entry.
                OsCondition::Linux,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Auto,
                ScrollbarVisibilityMode::Always,
                0,
                0,
                custom_color(
                    ColorU {
                        r: 100,
                        g: 100,
                        b: 100,
                        a: 255,
                    },
                    ColorU {
                        r: 45,
                        g: 45,
                        b: 45,
                        a: 255,
                    },
                ),
            ),
            (
                OsCondition::Linux,
                ThemeCondition::Light,
                LayoutScrollbarWidth::Auto,
                ScrollbarVisibilityMode::Always,
                0,
                0,
                custom_color(CLASSIC_LIGHT_THUMB, CLASSIC_LIGHT_TRACK),
            ),
            (
                OsCondition::Web,
                ThemeCondition::Dark,
                LayoutScrollbarWidth::Auto,
                ScrollbarVisibilityMode::Always,
                0,
                0,
                custom_color(
                    ColorU {
                        r: 100,
                        g: 100,
                        b: 100,
                        a: 255,
                    },
                    ColorU {
                        r: 45,
                        g: 45,
                        b: 45,
                        a: 255,
                    },
                ),
            ),
        ];
        for (os, theme, width, visibility, delay, duration, color) in cases {
            let r = evaluate_ua_scrollbar_css(&ctx(os, theme.clone()));
            assert_eq!(r.width, width, "{os:?}/{theme:?}: width");
            assert_eq!(r.visibility, visibility, "{os:?}/{theme:?}: visibility");
            assert_eq!(r.fade_delay.ms, delay, "{os:?}/{theme:?}: fade-delay");
            assert_eq!(
                r.fade_duration.ms, duration,
                "{os:?}/{theme:?}: fade-duration"
            );
            assert_eq!(r.color, color, "{os:?}/{theme:?}: color");
        }
    }
    /// `match_theme` compares by equality (except when the *condition* is
    /// `SystemPreferred`), so a context theme of `Custom(..)` / `SystemPreferred`
    /// matches no `@theme` rule at all — every such context must still resolve a
    /// colour, via the unconditional fallback.
    #[test]
    fn unrecognised_context_themes_fall_back_instead_of_failing() {
        for theme in [
            ThemeCondition::Custom(AzString::from("")),
            ThemeCondition::Custom(AzString::from("🎨")),
            ThemeCondition::SystemPreferred,
        ] {
            // OS-conditioned properties still apply — only the theme rules miss.
            let r = evaluate_ua_scrollbar_css(&ctx(OsCondition::MacOS, theme.clone()));
            assert_eq!(r.width, LayoutScrollbarWidth::Thin, "{theme:?}");
            assert_eq!(
                r.visibility,
                ScrollbarVisibilityMode::WhenScrolling,
                "{theme:?}"
            );
            assert_eq!(
                unwrap_custom(r.color),
                (CLASSIC_LIGHT_THUMB, CLASSIC_LIGHT_TRACK),
                "{theme:?}: must fall back to the unconditional colour"
            );
        }
    }
    /// `OsCondition::Apple` is condition-side sugar (it *matches* MacOS/IOS); as a
    /// *context* value it equals neither, so an `Apple` context gets the generic
    /// defaults. `DynamicSelectorContext::from_system_style` never produces it, so
    /// this pins down the (slightly surprising) behaviour rather than blessing it.
    #[test]
    fn apple_as_a_context_os_matches_no_macos_or_ios_rule() {
        let r = evaluate_ua_scrollbar_css(&ctx(OsCondition::Apple, ThemeCondition::Dark));
        assert_eq!(r.width, LayoutScrollbarWidth::Auto);
        assert_eq!(r.visibility, ScrollbarVisibilityMode::Always);
        assert_eq!(r.fade_delay.ms, 0);
        assert_eq!(r.fade_duration.ms, 0);
    }
    /// Overlay scrollbars are a package deal: `thin` ⇔ `when-scrolling` ⇔ a
    /// non-zero fade delay ⇔ a non-zero fade duration. A per-OS rule added to one
    /// group but forgotten in another would produce an overlay scrollbar that
    /// never fades (or a classic one that does).
    #[test]
    fn overlay_scrollbar_fields_stay_consistent_across_every_os_and_theme() {
        for os in all_os() {
            for theme in all_themes() {
                let r = evaluate_ua_scrollbar_css(&ctx(os, theme.clone()));
                let thin = r.width == LayoutScrollbarWidth::Thin;
                let overlay = r.visibility == ScrollbarVisibilityMode::WhenScrolling;
                assert_eq!(
                    thin, overlay,
                    "{os:?}/{theme:?}: thin/when-scrolling disagree"
                );
                assert_eq!(
                    overlay,
                    r.fade_delay.ms > 0,
                    "{os:?}/{theme:?}: an overlay scrollbar needs a fade delay"
                );
                assert_eq!(
                    overlay,
                    r.fade_duration.ms > 0,
                    "{os:?}/{theme:?}: an overlay scrollbar needs a fade duration"
                );
                // The table only ever supplies Custom colours.
                assert!(
                    matches!(r.color, StyleScrollbarColor::Custom(_)),
                    "{os:?}/{theme:?}: colour resolved to Auto"
                );
            }
        }
    }
    /// The evaluator `break`s early once all five fields are filled. Cross-check
    /// it against a straight first-match-wins scan with no early exit: the two
    /// must agree for every context, or the optimisation changed the semantics.
    #[test]
    fn early_break_does_not_change_the_first_match_result() {
        for os in all_os() {
            for theme in all_themes() {
                let c = ctx(os, theme.clone());
                let got = evaluate_ua_scrollbar_css(&c);
                let mut want_color = None;
                let mut want_width = None;
                let mut want_vis = None;
                let mut want_delay = None;
                let mut want_dur = None;
                for entry in UA_SCROLLBAR_CSS.iter().filter(|e| e.matches(&c)) {
                    match &entry.property {
                        CssProperty::ScrollbarColor(CssPropertyValue::Exact(v)) => {
                            if want_color.is_none() {
                                want_color = Some(*v);
                            }
                        }
                        CssProperty::ScrollbarWidth(CssPropertyValue::Exact(v)) => {
                            if want_width.is_none() {
                                want_width = Some(*v);
                            }
                        }
                        CssProperty::ScrollbarVisibility(CssPropertyValue::Exact(v)) => {
                            if want_vis.is_none() {
                                want_vis = Some(*v);
                            }
                        }
                        CssProperty::ScrollbarFadeDelay(CssPropertyValue::Exact(v)) => {
                            if want_delay.is_none() {
                                want_delay = Some(*v);
                            }
                        }
                        CssProperty::ScrollbarFadeDuration(CssPropertyValue::Exact(v)) => {
                            if want_dur.is_none() {
                                want_dur = Some(*v);
                            }
                        }
                        _ => {}
                    }
                }
                let label = alloc::format!("{os:?}/{theme:?}");
                assert_eq!(Some(got.color), want_color, "{label}: color");
                assert_eq!(Some(got.width), want_width, "{label}: width");
                assert_eq!(Some(got.visibility), want_vis, "{label}: visibility");
                assert_eq!(Some(got.fade_delay), want_delay, "{label}: fade-delay");
                assert_eq!(Some(got.fade_duration), want_dur, "{label}: fade-duration");
            }
        }
    }
    /// Degenerate / hostile context values (NaN, infinities, empty and huge
    /// strings) must not panic, and every field must still resolve.
    #[test]
    fn degenerate_context_values_do_not_panic() {
        let hostile = [
            (f32::NAN, f32::NAN),
            (0.0, 0.0),
            (-0.0, -1.0),
            (f32::INFINITY, f32::NEG_INFINITY),
            (f32::MAX, f32::MIN),
            (f32::MIN_POSITIVE, f32::EPSILON),
        ];
        for (w, h) in hostile {
            let c = DynamicSelectorContext {
                os: OsCondition::MacOS,
                theme: ThemeCondition::Dark,
                de_version: u32::MAX,
                viewport_width: w,
                viewport_height: h,
                container_width: h,
                container_height: w,
                language: AzString::from(""),
                ..DynamicSelectorContext::default()
            };
            let r = evaluate_ua_scrollbar_css(&c);
            // macOS/dark rules are OS+theme-only, so viewport garbage cannot
            // perturb them.
            assert_eq!(r.width, LayoutScrollbarWidth::Thin, "viewport {w}x{h}");
            assert_eq!(r.fade_delay.ms, 500, "viewport {w}x{h}");
            assert!(
                matches!(r.color, StyleScrollbarColor::Custom(_)),
                "viewport {w}x{h}"
            );
        }
    }
    #[test]
    fn evaluate_is_deterministic() {
        for os in all_os() {
            for theme in all_themes() {
                let c = ctx(os, theme.clone());
                let a = evaluate_ua_scrollbar_css(&c);
                let b = evaluate_ua_scrollbar_css(&c);
                assert_eq!(a.color, b.color, "{os:?}/{theme:?}");
                assert_eq!(a.width, b.width, "{os:?}/{theme:?}");
                assert_eq!(a.visibility, b.visibility, "{os:?}/{theme:?}");
                assert_eq!(a.fade_delay, b.fade_delay, "{os:?}/{theme:?}");
                assert_eq!(a.fade_duration, b.fade_duration, "{os:?}/{theme:?}");
            }
        }
    }
}
#[cfg(test)]
mod themed_ua_colours {
    //! The UA defaults that are COLOURS resolve per theme through
    //! `get_ua_property_themed`; everything else is unchanged by it.
    use azul_css::{
        css::CssPropertyValue,
        dynamic_selector::{DynamicSelectorContext, OsCondition, ThemeCondition},
        props::property::{CssProperty, CssPropertyType},
    };
    use crate::{
        dom::NodeType,
        ua_css::{get_ua_property, get_ua_property_themed},
    };
14
    fn ctx(theme: ThemeCondition) -> DynamicSelectorContext {
14
        DynamicSelectorContext {
14
            os: OsCondition::MacOS,
14
            theme,
14
            ..DynamicSelectorContext::default()
14
        }
14
    }
2
    fn border_top(p: &CssProperty) -> Option<(u8, u8, u8)> {
2
        match p {
2
            CssProperty::BorderTopColor(CssPropertyValue::Exact(c)) => {
2
                Some((c.inner.r, c.inner.g, c.inner.b))
            }
            _ => None,
        }
2
    }
    #[test]
1
    fn a_button_border_has_a_dark_twin_and_keeps_its_light_value() {
1
        let light = get_ua_property_themed(
1
            &NodeType::Button,
1
            CssPropertyType::BorderTopColor,
1
            Some(&ctx(ThemeCondition::Light)),
        )
1
        .and_then(border_top);
1
        let dark = get_ua_property_themed(
1
            &NodeType::Button,
1
            CssPropertyType::BorderTopColor,
1
            Some(&ctx(ThemeCondition::Dark)),
        )
1
        .and_then(border_top);
1
        assert_eq!(light, Some((200, 200, 200)), "the light border is #c8c8c8");
1
        assert_eq!(dark, Some((90, 90, 90)), "the dark border is #5a5a5a");
1
        assert_ne!(
            light, dark,
            "a dark window must not get the light-mode border"
        );
1
    }
    #[test]
1
    fn every_button_border_edge_and_the_hr_rule_follow_the_theme() {
5
        for (node, prop) in [
1
            (NodeType::Button, CssPropertyType::BorderTopColor),
1
            (NodeType::Button, CssPropertyType::BorderBottomColor),
1
            (NodeType::Button, CssPropertyType::BorderLeftColor),
1
            (NodeType::Button, CssPropertyType::BorderRightColor),
1
            (NodeType::Hr, CssPropertyType::BorderTopColor),
        ] {
5
            let light = get_ua_property_themed(&node, prop, Some(&ctx(ThemeCondition::Light)));
5
            let dark = get_ua_property_themed(&node, prop, Some(&ctx(ThemeCondition::Dark)));
5
            assert!(
5
                light.is_some() && dark.is_some(),
                "{node:?}/{prop:?} must resolve"
            );
5
            assert_ne!(light, dark, "{node:?}/{prop:?}: no dark twin");
        }
1
    }
    #[test]
1
    fn no_context_and_non_colour_properties_are_the_plain_table() {
        // Callers that have no window yet get exactly what they always got.
1
        assert_eq!(
1
            get_ua_property_themed(&NodeType::Button, CssPropertyType::BorderTopColor, None),
1
            get_ua_property(&NodeType::Button, CssPropertyType::BorderTopColor),
        );
        // A non-colour default is untouched by the theme in either mode.
2
        for theme in [ThemeCondition::Light, ThemeCondition::Dark] {
2
            assert_eq!(
2
                get_ua_property_themed(&NodeType::Div, CssPropertyType::Display, Some(&ctx(theme))),
2
                get_ua_property(&NodeType::Div, CssPropertyType::Display),
            );
        }
1
    }
}
/// Theme-chain analysis 2026-09-12, item 3: ONE themed UA table for both
/// cascades, with the inherited text colour in it.
#[cfg(test)]
mod one_themed_ua_table {
    use azul_css::{
        css::CssPropertyValue,
        dynamic_selector::{DynamicSelectorContext, ThemeCondition},
        props::property::{CssProperty, CssPropertyType},
    };
    use crate::ua_css::{get_ua_root_property_themed, UA_PROPERTY_TYPES};
6
    fn ctx(theme: ThemeCondition) -> DynamicSelectorContext {
6
        DynamicSelectorContext {
6
            theme,
6
            ..DynamicSelectorContext::default()
6
        }
6
    }
3
    fn rgb(p: Option<&CssProperty>) -> Option<(u8, u8, u8)> {
3
        match p {
3
            Some(CssProperty::TextColor(CssPropertyValue::Exact(c))) => {
3
                Some((c.inner.r, c.inner.g, c.inner.b))
            }
            _ => None,
        }
3
    }
    #[test]
1
    fn the_root_text_colour_follows_the_theme() {
1
        let light = get_ua_root_property_themed(
1
            CssPropertyType::TextColor,
1
            Some(&ctx(ThemeCondition::Light)),
        );
1
        let dark = get_ua_root_property_themed(
1
            CssPropertyType::TextColor,
1
            Some(&ctx(ThemeCondition::Dark)),
        );
1
        assert_eq!(rgb(light), Some((0, 0, 0)), "light: the CSS initial value");
1
        assert_eq!(
1
            rgb(dark),
            Some((0xe8, 0xe8, 0xe8)),
            "dark: near-white, not pure white"
        );
1
    }
    #[test]
1
    fn no_context_answers_the_light_table() {
        // A DOM no window has adopted yet: the unconditional entry only.
1
        assert_eq!(
1
            rgb(get_ua_root_property_themed(
1
                CssPropertyType::TextColor,
1
                None
            )),
            Some((0, 0, 0))
        );
1
    }
    #[test]
1
    fn the_root_table_defines_nothing_but_the_text_colour() {
4
        for pt in [
1
            CssPropertyType::Display,
1
            CssPropertyType::BorderTopColor,
1
            CssPropertyType::BackgroundContent,
1
            CssPropertyType::FontSize,
        ] {
4
            assert!(
4
                get_ua_root_property_themed(pt, Some(&ctx(ThemeCondition::Dark))).is_none(),
                "{pt:?} is not a document-wide default"
            );
        }
1
    }
    /// Both cascade passes walk this one list; the two per-pass copies it
    /// replaced had each lost entries the other had.
    #[test]
1
    fn the_shared_type_list_covers_both_former_lists() {
9
        for pt in [
            // formerly compact-only
1
            CssPropertyType::BorderBottomColor,
1
            CssPropertyType::BorderLeftColor,
1
            CssPropertyType::BorderRightColor,
1
            CssPropertyType::TextColor,
            // formerly cascaded-only
1
            CssPropertyType::FontFamily,
1
            CssPropertyType::BreakInside,
1
            CssPropertyType::BreakAfter,
            // both
1
            CssPropertyType::Display,
1
            CssPropertyType::Cursor,
        ] {
9
            assert!(
9
                UA_PROPERTY_TYPES.contains(&pt),
                "{pt:?} missing from UA_PROPERTY_TYPES"
            );
        }
58
        let mut sorted: Vec<_> = UA_PROPERTY_TYPES.iter().map(|p| *p as u16).collect();
1
        sorted.sort_unstable();
1
        sorted.dedup();
1
        assert_eq!(
1
            sorted.len(),
1
            UA_PROPERTY_TYPES.len(),
            "a duplicated type would push a UA entry twice"
        );
1
    }
}
/// Editing hosts preserve what the user types: `white-space: pre-wrap` (a
/// trailing space keeps its advance, a doubled space stays two) and
/// `overflow-wrap: break-word`. The element's own row still wins
/// (`<pre contenteditable>` stays `pre`), and a non-editable `<div>` gets
/// neither.
#[test]
1
fn editing_hosts_default_to_pre_wrap_and_break_word() {
    use azul_css::props::style::{StyleOverflowWrap, StyleWhiteSpace};
    use crate::dom::NodeData;
3
    let ws = |p: Option<&CssProperty>| match p {
3
        Some(CssProperty::WhiteSpace(v)) => v.get_property().copied(),
1
        _ => None,
4
    };
1
    let ow = |p: Option<&CssProperty>| match p {
1
        Some(CssProperty::OverflowWrap(v)) => v.get_property().copied(),
        _ => None,
1
    };
1
    let mut host = NodeData::create_node(NodeType::Div);
1
    host.set_contenteditable(true);
1
    assert_eq!(
1
        ws(get_ua_default(&host, false, CssPropertyType::WhiteSpace, None)),
        Some(StyleWhiteSpace::PreWrap)
    );
1
    assert_eq!(
1
        ow(get_ua_default(&host, false, CssPropertyType::OverflowWrap, None)),
        Some(StyleOverflowWrap::BreakWord)
    );
1
    let plain = NodeData::create_node(NodeType::Div);
1
    assert_eq!(ws(get_ua_default(&plain, false, CssPropertyType::WhiteSpace, None)), None);
1
    let mut pre_host = NodeData::create_node(NodeType::Pre);
1
    pre_host.set_contenteditable(true);
1
    assert_eq!(
1
        ws(get_ua_default(&pre_host, false, CssPropertyType::WhiteSpace, None)),
        Some(StyleWhiteSpace::Pre),
        "the element's own UA row wins over the editing-host default"
    );
1
    assert_eq!(
1
        ws(get_ua_default(&NodeData::create_node(NodeType::TextArea), false, CssPropertyType::WhiteSpace, None)),
        Some(StyleWhiteSpace::PreWrap)
    );
1
}