glory-render 0.7.0

Semantic rendering contracts and reference interpreter for Glory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! Finite composite-widget metadata. Stable names are explicit document keys,
//! independent of NodeId lifetimes and snapshot rekeying.

use crate::{Error, Navigation, Props, Role};

/// Structural form content, fixed for a node's lifetime on HTML adapters.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FieldPart {
    Form,
    Label,
    Fieldset,
    Legend,
}

/// The host captures requests; application policy owns validation and transport.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FormAction {
    Submit,
    Reset,
}

/// Submission metadata is separate from the accessible name. Text input values
/// come from InputProps; Check/Button may supply an explicit successful value.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormControl {
    pub name: String,
    pub value: Option<String>,
}

/// Native links may use their current context or request a separate one.
/// Opening a system browser remains an outer native host service.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LinkOpen {
    #[default]
    Current,
    NewContext,
}

/// The current location in a related set, independent of selection or visits.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Current {
    Page,
    Step,
    Location,
    Date,
}
/// The popup role is one typed value, never an independent boolean flag.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PopupKind {
    Dialog,
    Menu,
    Listbox,
    Tree,
}
impl PopupKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Dialog => "dialog",
            Self::Menu => "menu",
            Self::Listbox => "listbox",
            Self::Tree => "tree",
        }
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MenuItemKind {
    Action,
    Checkbox,
    Radio,
}
/// Finite intrinsic-control refinement. Owner is a named Menu View, also used
/// to keep menu check controls outside any enclosing successful form controls.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MenuItemSemantics {
    pub kind: MenuItemKind,
    pub owner: SemanticId,
}

/// Host-managed top layer. Presence declares a focus scope; `hidden` controls
/// whether it participates in the stack. Relations resolve only live names.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LayerFocus {
    #[default]
    Manage,
    Preserve,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Layer {
    pub sheet: Option<crate::SheetConfig>,
    pub modal: bool,
    pub escape: bool,
    pub outside: bool,
    pub focus: LayerFocus,
    pub placement: LayerPlacement,
    pub anchor: Option<SemanticId>,
    pub initial_focus: Option<SemanticId>,
    pub return_focus: Option<SemanticId>,
}
impl Default for Layer {
    fn default() -> Self {
        Self {
            sheet: None,
            modal: true,
            escape: true,
            outside: true,
            focus: LayerFocus::Manage,
            placement: LayerPlacement::Center,
            anchor: None,
            initial_focus: None,
            return_focus: None,
        }
    }
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum LayerPlacement {
    #[default]
    Center,
    Left,
    Right,
    Top,
    Bottom,
    Anchor(crate::AnchorPlacement),
    /// A context request in viewport CSS pixels, belonging to `Layer::anchor`.
    ContextPoint(crate::ContextPoint),
    Viewport(crate::ViewportPlacement),
}
impl LayerPlacement {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Center => "center",
            Self::Left => "left",
            Self::Right => "right",
            Self::Top => "top",
            Self::Bottom => "bottom",
            Self::Anchor(_) => "anchor",
            Self::ContextPoint(_) => "context-point",
            Self::Viewport(_) => "viewport",
        }
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DismissReason {
    Gesture,
    Escape,
    Outside,
    /// The declared anchor is no longer a live visible semantic box.
    AnchorLost,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SemanticId(String);
impl SemanticId {
    /// Names are bounded ASCII tokens. Applications must choose distinct widget
    /// namespaces when multiple Apps share one host document.
    pub fn new(value: impl Into<String>) -> Result<Self, Error> {
        let value = value.into();
        if value.is_empty()
            || value.len() > 256
            || !value
                .bytes()
                .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
        {
            return Err(Error::InvalidProps(
                "semantic id must be a bounded ASCII token",
            ));
        }
        Ok(Self(value))
    }
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Orientation {
    Horizontal,
    Vertical,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Direction {
    #[default]
    Ltr,
    Rtl,
}
impl Orientation {
    pub fn key_with_direction(
        self,
        direction: Direction,
        input: crate::KeyboardInput<'_>,
    ) -> Option<Navigation> {
        let command = self.key(input)?;
        Some(match (self, direction, input.key, command) {
            (Self::Horizontal, Direction::Rtl, "ArrowLeft" | "ArrowRight", Navigation::Next) => {
                Navigation::Previous
            }
            (
                Self::Horizontal,
                Direction::Rtl,
                "ArrowLeft" | "ArrowRight",
                Navigation::Previous,
            ) => Navigation::Next,
            _ => command,
        })
    }
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Horizontal => "horizontal",
            Self::Vertical => "vertical",
        }
    }
    /// Shared direction mapping. Tab, editing keys, modified/composing keys and
    /// the perpendicular arrows remain host-native. Repeats may navigate.
    pub fn key(self, input: crate::KeyboardInput<'_>) -> Option<Navigation> {
        if input.composing
            || input.editable
            || input.control
            || input.meta
            || input.alt
            || input.shift
        {
            return None;
        }
        match (self, input.key) {
            (Self::Horizontal, "ArrowLeft") | (Self::Vertical, "ArrowUp") => {
                Some(Navigation::Previous)
            }
            (Self::Horizontal, "ArrowRight") | (Self::Vertical, "ArrowDown") => {
                Some(Navigation::Next)
            }
            (_, "Home") => Some(Navigation::First),
            (_, "End") => Some(Navigation::Last),
            _ => None,
        }
    }
}

/// Optional and finite: ordinary nodes allocate none of this metadata. Named
/// relations may refer forward or to an unmounted target; adapters resolve only
/// live names in their document. Removing a target retains no owning reference.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Semantics {
    /// Explicit inherited text direction/language on an ordinary boxed View.
    pub environment: Option<crate::Environment>,
    /// A focus-only boundary. It has no role, top-layer paint or inert backdrop.
    pub focus_scope: Option<crate::FocusScopeConfig>,
    /// Nonphysical source read from the accepted semantic tree only when a
    /// typed clipboard action is admitted. It is not duplicated into HTML.
    pub clipboard: Option<crate::ClipboardText>,
    /// Present only on a structural column header; actions remain intrinsic buttons.
    pub sort: Option<crate::SortDirection>,
    /// Replace ordinary host ValueChanged with one bounded post-edit path.
    pub input_edit: Option<crate::InputEditCapture>,
    /// Finite paint-only motion on an inert, independently boxed View.
    pub translation: Option<crate::TranslationMotion>,
    /// Finite single-line input hints; no automatic code or credential retrieval.
    pub input_assist: Option<crate::InputAssist>,
    /// Optional bounded presentation hint on Input, never its accessible name,
    /// stored value, default value or composition state.
    pub placeholder: Option<String>,
    /// Optional intrinsic-height presentation with immediate logical hiding.
    pub collapse: Option<crate::CollapseConfig>,
    /// Optional finite opacity/transform presence; logical availability is immediate.
    pub presence: Option<Box<crate::PresenceConfig>>,
    /// Immediate primary-pointer capture, in viewport CSS pixels.
    pub drag_capture: Option<crate::DragCapture>,
    /// Axis intent capture preserves orthogonal native panning.
    pub swipe_capture: Option<crate::SwipeCapture>,
    /// An explicit context region. Touch long-press remains host-native.
    pub context_menu: Option<crate::ContextEditPolicy>,
    /// Opted-in free tag entry policy; retains the Input's intrinsic textbox.
    pub tags_input: Option<crate::TagSeparators>,
    pub progress: Option<crate::ProgressValue>,
    pub splitter: Option<crate::RangeValue>,
    pub slider: Option<crate::SliderValue>,
    pub spinbutton: Option<crate::SpinValue>,
    /// Opt-in keyboard commands only while this viewport itself owns focus.
    pub scroll_keyboard: Option<crate::ScrollAxes>,
    /// An application-owned pending state on a boxed semantic node.
    pub busy: Option<bool>,
    pub current: Option<Current>,
    pub separator_axis: Option<Orientation>,
    pub link_open: Option<LinkOpen>,
    /// Capture activation as an application request before default navigation.
    pub link_request: bool,
    pub document_part: Option<crate::DocumentPart>,
    pub table: Option<crate::TableMetadata>,
    pub form_action: Option<FormAction>,
    pub form_control: Option<FormControl>,
    pub field_part: Option<FieldPart>,
    pub label_for: Option<SemanticId>,
    pub error_message: Option<SemanticId>,
    pub required: bool,
    pub readonly: bool,
    pub invalid: bool,
    pub id: Option<SemanticId>,
    pub controls: Option<SemanticId>,
    pub labelled_by: Option<SemanticId>,
    pub described_by: Option<SemanticId>,
    pub popup: Option<PopupKind>,
    pub combobox: Option<crate::ComboboxKind>,
    pub option: Option<crate::OptionSemantics>,
    pub active_descendant: Option<SemanticId>,
    pub listbox_multiple: bool,
    pub menu_item: Option<MenuItemSemantics>,
    /// Single-line search editor controlling its nearest named Menu. Matching,
    /// highlight and activation remain owned by the component.
    pub menu_search: Option<SemanticId>,
    /// An intrinsic action trigger belonging to a structural Menubar.
    pub menubar_item: bool,
    pub layer: Option<Layer>,
    /// Passive grip hit area; the nearest live sheet owns pointer gestures.
    pub sheet_grip: bool,
    pub expanded: Option<bool>,
    pub selected: Option<bool>,
    /// A toggle button's application-owned state; independent from disclosure.
    pub pressed: Option<bool>,
    /// Refine a Button's intrinsic interaction to a tab, retaining activation.
    pub tab: bool,
    /// Keep a control focusable, while suppressing activation (e.g. the sole
    /// expanded header in an accordion that cannot collapse completely).
    pub disabled: bool,
    /// Hide a View subtree without destroying its state or removing its names.
    pub hidden: bool,
    /// Retain a View's content and layout while suppressing subtree interaction
    /// and accessibility exposure. This is not a scroll lock or a modal scope.
    pub inert: bool,
    /// Explicit collection boundary, independent of virtual-list navigation.
    pub navigation: Option<Orientation>,
    /// A two-dimensional physical Button navigation boundary, independent of
    /// the linear collection navigation protocol.
    pub grid_navigation: Option<Direction>,
    /// A Grid that permits more than one selected cell.
    pub grid_multiple: bool,
    pub tree_navigation: Option<crate::TreeNavigation>,
    pub tree_item: Option<crate::TreeItem>,
    /// A checkable tree item, independent from list selection and Check props.
    pub tree_checked: Option<crate::TreeCheckState>,
    pub navigation_item: bool,
    pub navigation_panel: Option<crate::NavigationPanel>,
    /// Native radio association; not an accessible name or a submission value.
    /// HTML uses the same attribute for native grouping and successful names.
    pub check_group: Option<SemanticId>,
    pub navigation_direction: Direction,
    /// Opt in to bounded character search on the collection's mounted members.
    pub typeahead: bool,
}
impl Semantics {
    pub(crate) fn validate(&self, d: crate::DescriptorView<'_>) -> Result<(), Error> {
        if let Some(placeholder) = &self.placeholder {
            let Props::Input(input) = d.props else {
                return Err(Error::InvalidProps("placeholder requires an Input"));
            };
            crate::validate_input_placeholder(placeholder, input.multiline)?;
        }
        if let Some(environment) = &self.environment {
            if !matches!(d.props, Props::View) {
                return Err(Error::InvalidProps("environment metadata belongs to View"));
            }
            if self.navigation.is_some() && self.navigation_direction != environment.direction
                || self
                    .grid_navigation
                    .is_some_and(|direction| direction != environment.direction)
                || self
                    .tree_navigation
                    .is_some_and(|tree| tree.direction != environment.direction)
            {
                return Err(Error::InvalidProps(
                    "environment and navigation directions disagree",
                ));
            }
        }
        if self.focus_scope.is_some()
            && (!matches!(d.props, Props::View)
                || d.focus != crate::FocusMode::Programmatic
                || !matches!(d.role, None | Some(Role::Group))
                || self.layer.is_some())
        {
            return Err(Error::InvalidProps(
                "FocusScope requires an independent programmatic View",
            ));
        }
        if self.tree_checked.is_some()
            && (d.role != Some(Role::TreeItem)
                || self.tree_item.is_none()
                || self.selected.is_some())
        {
            return Err(Error::InvalidProps(
                "tree checked state requires a TreeItem with coordinates, without selected state",
            ));
        }
        if self.tree_navigation.is_some()
            && (d.role != Some(Role::Tree)
                || !matches!(d.props, Props::View)
                || self.navigation.is_some()
                || self.grid_navigation.is_some()
                || self.navigation_item
                || self.tree_item.is_some()
                || self.typeahead)
        {
            return Err(Error::InvalidProps(
                "tree navigation requires an independent Tree View",
            ));
        }
        if let Some(item) = self.tree_item {
            item.validate()?;
            if d.role != Some(Role::TreeItem)
                || !matches!(d.props, Props::View)
                || self.navigation.is_some()
                || self.grid_navigation.is_some()
                || self.navigation_item
                || self.table.is_some()
                || self.typeahead
            {
                return Err(Error::InvalidProps(
                    "tree coordinates require an independent TreeItem View",
                ));
            }
        }
        if self.clipboard.is_some()
            && (!matches!(d.props, Props::Button(_))
                || self.form_action.is_some()
                || self.tab
                || self.menu_item.is_some())
        {
            return Err(Error::InvalidProps(
                "clipboard source requires an independent intrinsic Button",
            ));
        }
        if self.grid_multiple && (d.role != Some(Role::Grid) || !matches!(d.props, Props::View)) {
            return Err(Error::InvalidProps(
                "multiple grid selection requires a Grid View",
            ));
        }
        if self.grid_navigation.is_some()
            && (d.role != Some(Role::Grid)
                || !matches!(d.props, Props::View)
                || self.navigation.is_some()
                || self.navigation_item
                || self.typeahead)
        {
            return Err(Error::InvalidProps(
                "grid navigation requires an independent Grid View",
            ));
        }
        if let Some(capture) = self.input_edit {
            capture.validate()?;
            if !matches!(d.props,Props::Input(p) if !p.multiline&&p.value.len()<=capture.max_bytes)
                || self.tags_input.is_some()
                || self.combobox.is_some()
            {
                return Err(Error::InvalidProps(
                    "input edit capture requires a bounded single-line Input or SpinButton",
                ));
            }
        }
        if let Some(translation) = self.translation {
            translation.validate()?;
            if !matches!(d.props, Props::View)
                || !self.inert
                || d.role.is_some()
                || d.name.is_some()
                || d.focus != crate::FocusMode::Auto
                || self.collapse.is_some()
                || self.presence.is_some()
                || self.layer.is_some()
                || self.drag_capture.is_some()
                || self.document_part.is_some()
                || self.field_part.is_some()
            {
                return Err(Error::InvalidProps(
                    "translation requires an independent inert unnamed View without role, focus, layer, collapse or drag capture",
                ));
            }
        }
        if self.input_assist.is_some() && !matches!(d.props, Props::Input(p) if !p.multiline) {
            return Err(Error::InvalidProps(
                "input assist requires a single-line Input",
            ));
        }
        if let Some(presence) = &self.presence {
            presence.validate()?;
            presence.validate_style(d.style)?;
            if !matches!(d.props, Props::View)
                || self.layer.is_some()
                || self.collapse.is_some()
                || self.translation.is_some()
                || self.document_part.is_some()
                || self.field_part.is_some()
                || self.hidden == presence.open
                || self.inert == presence.open
            {
                return Err(Error::InvalidProps(
                    "presence requires an independent View and hidden/inert opposite to open",
                ));
            }
        }
        if let Some(collapse) = &self.collapse {
            collapse.validate()?;
            collapse.validate_style(d.style)?;
            if !matches!(d.props, Props::View)
                || self.presence.is_some()
                || self.layer.is_some()
                || self.document_part.is_some()
                || self.field_part.is_some()
                || self.hidden == collapse.open
                || self.inert == collapse.open
            {
                return Err(Error::InvalidProps(
                    "collapse requires an independent View and hidden/inert opposite to open",
                ));
            }
        }
        if self.swipe_capture.is_some()
            && (!matches!(d.props, Props::View)
                || d.role.is_some_and(|role| role != Role::Group)
                || self.document_part.is_some()
                || self.field_part.is_some()
                || self.drag_capture.is_some()
                || self.slider.is_some()
                || self.splitter.is_some()
                || self.sheet_grip
                || self.layer.is_some()
                || self.collapse.is_some()
                || self.presence.is_some())
        {
            return Err(Error::InvalidProps(
                "swipe capture requires an independent boxed View",
            ));
        }
        if self.drag_capture.is_some()
            && (!matches!(d.props, Props::View)
                || self.slider.is_some()
                || self.splitter.is_some()
                || self.sheet_grip)
        {
            return Err(Error::InvalidProps(
                "press drag capture requires an independent boxed View",
            ));
        }
        if self.drag_capture == Some(crate::DragCapture::PressFocus)
            && d.focus == crate::FocusMode::Auto
        {
            return Err(Error::InvalidProps(
                "focus capture requires an explicit View focus mode",
            ));
        }
        if d.role == Some(Role::Menubar)
            && (self.id.is_none()
                || self.navigation != Some(Orientation::Horizontal)
                || d.focus != crate::FocusMode::Auto
                || self.layer.is_some())
        {
            return Err(Error::InvalidProps(
                "menubar requires a named horizontal container with roving child focus",
            ));
        }
        if self.menubar_item
            && (!matches!(d.props, Props::Button(_))
                || !self
                    .menu_item
                    .as_ref()
                    .is_some_and(|item| item.kind == MenuItemKind::Action)
                || self.popup != Some(PopupKind::Menu)
                || self.controls.is_none()
                || self.id.is_none()
                || !self.navigation_item
                || d.focus == crate::FocusMode::Auto)
        {
            return Err(Error::InvalidProps(
                "menubar root requires an intrinsic menuitem popup button with explicit roving focus",
            ));
        }
        if self.context_menu.is_some()
            && (!matches!(d.props, Props::View)
                || d.effective_role() != Some(Role::Group)
                || self.id.is_none()
                || self.layer.is_some()
                || self.popup.is_some()
                || self.expanded.is_some()
                || self.navigation.is_some())
        {
            return Err(Error::InvalidProps(
                "context policy requires an independent named group region",
            ));
        }
        if self.tags_input.is_some()
            && (!matches!(d.props,crate::Props::Input(input) if !input.multiline)
                || self.combobox.is_some()
                || self.popup.is_some()
                || self.controls.is_some()
                || self.active_descendant.is_some()
                || self.form_control.is_some())
        {
            return Err(Error::InvalidProps(
                "tags policy requires an independent single-line search input",
            ));
        }
        if self.sheet_grip
            && (!matches!(d.props, Props::View)
                || d.role.is_some()
                || d.focus != crate::FocusMode::Auto
                || self.layer.is_some()
                || self.navigation.is_some())
        {
            return Err(Error::InvalidProps(
                "sheet grip requires a passive generic view",
            ));
        }
        if let Some(panel) = self.navigation_panel {
            let valid = match panel {
                crate::NavigationPanel::Trigger => {
                    matches!(d.props, Props::Button(_))
                        && self.navigation_item
                        && self.id.is_some()
                        && self.controls.is_some()
                        && self.controls != self.id
                        && self.expanded.is_some()
                        && self.popup.is_none()
                        && self.menu_item.is_none()
                        && !self.tab
                        && self.pressed.is_none()
                        && self.form_action.is_none()
                }
                crate::NavigationPanel::Content => {
                    matches!(d.props, Props::View)
                        && d.role == Some(Role::Group)
                        && d.focus == crate::FocusMode::Auto
                        && self.id.is_some()
                        && self.navigation == Some(Orientation::Vertical)
                        && self.menu_item.is_none()
                        && self.popup.is_none()
                        && self.field_part.is_none()
                        && self.document_part.is_none()
                        && self.scroll_keyboard.is_none()
                }
            };
            if !valid {
                return Err(Error::InvalidProps(
                    "navigation panels require an independent disclosure button or physical group content",
                ));
            }
        }
        if self.inert && !matches!(d.props, Props::View) {
            return Err(Error::InvalidProps("inert regions require a view"));
        }
        self.validate_choice(d)?;
        self.validate_menu_search(d)?;
        if let Some(progress) = &self.progress {
            progress.validate()?;
            if d.role != Some(Role::ProgressBar)
                || !matches!(d.props, Props::View)
                || self.navigation.is_some()
                || self.layer.is_some()
                || self.field_part.is_some()
                || self.document_part.is_some()
                || self.splitter.is_some()
                || self.menu_item.is_some()
                || self.popup.is_some()
                || self.pressed.is_some()
                || self.expanded.is_some()
                || self.scroll_keyboard.is_some()
            {
                return Err(Error::InvalidProps(
                    "progress metadata requires an independent progress bar view",
                ));
            }
        }
        if let Some(spin) = &self.spinbutton {
            spin.validate()?;
            if !matches!(d.props, Props::Input(p) if !p.multiline && matches!(p.purpose, crate::Purpose::Text | crate::Purpose::Number))
                || self.combobox.is_some()
                || self.tags_input.is_some()
                || self.popup.is_some()
                || self.navigation.is_some()
                || self.option.is_some()
                || self.slider.is_some()
                || self.splitter.is_some()
            {
                return Err(Error::InvalidProps(
                    "spinbutton requires an independent single-line numeric text input",
                ));
            }
            if (self.disabled || self.readonly || !d.props.enabled())
                && (spin.can_increase || spin.can_decrease)
            {
                return Err(Error::InvalidProps(
                    "disabled or readonly spinbuttons cannot advertise adjustment",
                ));
            }
            if spin.value.is_some_and(|v| {
                spin.min.is_some_and(|min| v < min) || spin.max.is_some_and(|max| v > max)
            }) && !self.invalid
            {
                return Err(Error::InvalidProps(
                    "outside spinbutton coordinates require invalid metadata",
                ));
            }
        }
        if let Some(slider) = &self.slider {
            slider.validate()?;
            if slider.range.min == slider.range.max && !self.readonly {
                return Err(Error::InvalidProps(
                    "a fixed slider interval requires readonly semantics",
                ));
            }
            if !matches!(d.props, Props::View)
                || d.role != Some(Role::Slider)
                || d.focus == crate::FocusMode::Auto
                || self.splitter.is_some()
                || self.progress.is_some()
                || self.separator_axis.is_some()
                || self.navigation.is_some()
                || self.navigation_item
                || self.layer.is_some()
                || self.field_part.is_some()
                || self.document_part.is_some()
                || self.tab
                || self.pressed.is_some()
                || self.expanded.is_some()
                || self.popup.is_some()
                || self.scroll_keyboard.is_some()
                || self.menu_item.is_some()
                || self.combobox.is_some()
                || self.option.is_some()
            {
                return Err(Error::InvalidProps(
                    "slider requires an independent focused range view",
                ));
            }
        }
        if let Some(range) = self.splitter {
            range.validate()?;
            if !matches!(d.props, Props::View)
                || d.role != Some(Role::Separator)
                || d.focus == crate::FocusMode::Auto
                || self.separator_axis.is_none()
                || self.controls.is_none()
                || self.controls == self.id
                || self.navigation.is_some()
            {
                return Err(Error::InvalidProps(
                    "splitter requires a focused separator, orientation and distinct controlled pane",
                ));
            }
        }
        if self.scroll_keyboard.is_some()
            && (!matches!(d.props, Props::View)
                || self.navigation.is_some()
                || self.navigation_item
                || self.layer.is_some()
                || self.field_part.is_some()
                || self.document_part.is_some()
                || d.role == Some(Role::Separator))
        {
            return Err(Error::InvalidProps(
                "scroll keyboard requires an independent viewport view",
            ));
        }
        if let Some(item) = &self.menu_item {
            let valid = match (&d.props, item.kind) {
                (Props::Button(_), MenuItemKind::Action) => {
                    !self.tab && self.pressed.is_none() && self.form_action.is_none()
                }
                (Props::Check(check), MenuItemKind::Checkbox) => matches!(
                    check.mode,
                    crate::CheckMode::Checkbox | crate::CheckMode::Mixed
                ),
                (Props::Check(check), MenuItemKind::Radio) => {
                    check.mode == crate::CheckMode::Radio && self.check_group.is_some()
                }
                _ => false,
            };
            if !valid
                || self.form_control.is_some()
                || self.id.as_ref() == Some(&item.owner)
                || self.popup.is_some_and(|popup| popup != PopupKind::Menu)
            {
                return Err(Error::InvalidProps(
                    "menu item requires a matching intrinsic control and distinct menu owner",
                ));
            }
        }
        if self.current.is_some()
            && !matches!(d.props, Props::Link(_) | Props::Button(_) | Props::Text(_))
        {
            return Err(Error::InvalidProps("current requires link, button or text"));
        }
        if (self.typeahead || self.navigation_direction != Direction::Ltr)
            && self.navigation.is_none()
            && self.combobox != Some(crate::ComboboxKind::Select)
        {
            return Err(Error::InvalidProps(
                "direction and typeahead require a navigation boundary",
            ));
        }
        if self.separator_axis.is_some() && d.role != Some(Role::Separator) {
            return Err(Error::InvalidProps(
                "separator orientation requires a separator role",
            ));
        }
        if (self.link_open.is_some() || self.link_request) && !matches!(d.props, Props::Link(_)) {
            return Err(Error::InvalidProps("link context requires a link"));
        }
        if let Some(table) = &self.table {
            table.validate(d)?;
        }
        if self.sort.is_some()
            && (!matches!(d.props, Props::View)
                || !(self.document_part == Some(crate::DocumentPart::ColumnHeader)
                    || d.role == Some(Role::ColumnHeader)))
        {
            return Err(Error::InvalidProps(
                "sort direction requires a structural column header",
            ));
        }
        if self.document_part.is_some()
            && (!matches!(d.props, Props::View)
                || d.role.is_some()
                || d.focus != crate::FocusMode::Auto
                || self.field_part.is_some()
                || self.layer.is_some()
                || self.navigation.is_some())
        {
            return Err(Error::InvalidProps(
                "document parts require an unfocused structural view",
            ));
        }
        if let Some(group) = &self.check_group
            && (!matches!(&d.props, Props::Check(check) if check.mode == crate::CheckMode::Radio)
                || self
                    .form_control
                    .as_ref()
                    .is_some_and(|control| control.name != group.as_str()))
        {
            return Err(Error::InvalidProps(
                "radio association requires a radio and matching successful-control name",
            ));
        }
        if self.form_action.is_some()
            && (!matches!(d.props, Props::Button(_))
                || self.tab
                || self.popup.is_some()
                || self.expanded.is_some()
                || self.pressed.is_some())
        {
            return Err(Error::InvalidProps("form actions require ordinary buttons"));
        }
        if let Some(control) = &self.form_control
            && (!matches!(
                d.props,
                Props::Input(_) | Props::Select(_) | Props::Check(_) | Props::Button(_)
            ) && d.role != Some(Role::Slider)
                || control.name.len() > 256
                || control.name.contains(['\0', '\r', '\n'])
                || control
                    .value
                    .as_ref()
                    .is_some_and(|value| value.contains(['\0', '\r']))
                || control.value.is_some() && matches!(d.props, Props::Input(_) | Props::Select(_)))
        {
            return Err(Error::InvalidProps("invalid successful-control metadata"));
        }
        if self.field_part.is_some()
            && (!matches!(d.props, Props::View)
                || d.role.is_some()
                || d.focus != crate::FocusMode::Auto
                || self.layer.is_some()
                || self.navigation.is_some())
        {
            return Err(Error::InvalidProps(
                "form parts require an unfocused structural view",
            ));
        }
        if self.label_for.is_some() && self.field_part != Some(FieldPart::Label) {
            return Err(Error::InvalidProps(
                "label association requires a label part",
            ));
        }
        if self.label_for.is_some() && self.label_for == self.id {
            return Err(Error::InvalidProps("a label cannot target itself"));
        }
        if self.readonly && matches!(d.props, Props::Select(_)) {
            return Err(Error::Unsupported("native select has no readonly state"));
        }
        let input_field = self.combobox.is_some()
            || matches!(
                d.props,
                Props::Input(_) | Props::Select(_) | Props::Check(_) | Props::RichEditor(_)
            )
            || matches!(d.role, Some(Role::RadioGroup | Role::Slider));
        if (self.required && !input_field)
            || (self.readonly && !input_field && d.role != Some(Role::Grid))
            || ((self.invalid || self.error_message.is_some())
                && !input_field
                && self.field_part != Some(FieldPart::Fieldset))
        {
            return Err(Error::InvalidProps(
                "field states require an input or check",
            ));
        }
        if self.popup.is_some()
            && ((!matches!(d.props, Props::Button(_)) && self.combobox.is_none()) || self.tab)
        {
            return Err(Error::InvalidProps(
                "popup triggers require a non-tab button",
            ));
        }
        if self.pressed.is_some()
            && (!matches!(d.props, Props::Button(_)) || self.tab || self.popup.is_some())
        {
            return Err(Error::InvalidProps(
                "pressed requires a non-tab toggle button",
            ));
        }
        if let Some(layer) = &self.layer {
            if let LayerPlacement::Viewport(placement) = layer.placement {
                placement.validate()?;
                if d.role != Some(Role::Dialog) || layer.anchor.is_some() || layer.sheet.is_some() {
                    return Err(Error::InvalidProps(
                        "viewport positioning requires an unanchored dialog",
                    ));
                }
            }
            if let LayerPlacement::Anchor(placement) = layer.placement {
                placement.validate()?;
            }
            if let LayerPlacement::ContextPoint(point) = layer.placement {
                point
                    .validate()
                    .map_err(|_| Error::InvalidProps("context layer point must be finite"))?;
                if d.role != Some(Role::Menu) {
                    return Err(Error::InvalidProps(
                        "context point positioning requires a menu",
                    ));
                }
            }
            if let Some(sheet) = &layer.sheet {
                sheet.validate()?;
                if d.style
                    .position
                    .is_some_and(|p| p != crate::style::Position::Fixed)
                {
                    return Err(Error::InvalidProps("sheet owns fixed viewport positioning"));
                }
                if d.role != Some(Role::Dialog)
                    || !crate::sheet::edge(layer.placement)
                    || layer.focus != LayerFocus::Manage
                    || layer.anchor.is_some()
                {
                    return Err(Error::InvalidProps(
                        "sheet requires an edge dialog with managed focus",
                    ));
                }
            }
            if !matches!(d.props, Props::View)
                || !matches!(
                    d.role,
                    Some(
                        Role::Dialog
                            | Role::AlertDialog
                            | Role::Menu
                            | Role::Tooltip
                            | Role::Listbox
                            | Role::Tree
                            | Role::Group
                    )
                )
                || self.id.is_none()
                || (!matches!(d.role, Some(Role::Tooltip | Role::Listbox))
                    && d.focus == crate::FocusMode::Auto
                    && d.role != Some(Role::Group))
                || (layer.focus == LayerFocus::Preserve
                    && (layer.modal
                        || layer.initial_focus.is_some()
                        || layer.return_focus.is_some()))
                || (d.role == Some(Role::Tooltip)
                    && (layer.focus != LayerFocus::Preserve
                        || d.focus != crate::FocusMode::Auto
                        || self.navigation.is_some()
                        || self.popup.is_some()
                        || self.expanded.is_some()))
                || (matches!(d.role, Some(Role::Dialog | Role::AlertDialog))
                    && self.navigation.is_some())
                || (d.role == Some(Role::AlertDialog)
                    && (!layer.modal
                        || layer.focus != LayerFocus::Manage
                        || layer.initial_focus.is_none()))
                || (d.role == Some(Role::Listbox)
                    && (layer.modal
                        || layer.focus != LayerFocus::Preserve
                        || d.focus != crate::FocusMode::Auto
                        || self.navigation != Some(Orientation::Vertical)))
                || (d.role == Some(Role::Tree)
                    && (layer.modal
                        || !matches!(layer.focus, LayerFocus::Manage | LayerFocus::Preserve)
                        || self.tree_navigation.is_none()
                        || d.focus == crate::FocusMode::Auto))
                || (d.role == Some(Role::Menu)
                    && (layer.modal
                        || layer.focus != LayerFocus::Manage
                        || self.navigation != Some(Orientation::Vertical)))
                || (d.role == Some(Role::Group)
                    && (self.navigation_panel != Some(crate::NavigationPanel::Content)
                        || layer.modal
                        || layer.focus != LayerFocus::Preserve
                        || d.focus != crate::FocusMode::Auto))
            {
                return Err(Error::InvalidProps(
                    "layers require a valid named dialog, alert decision, menu or passive tooltip focus policy",
                ));
            }
            if matches!(
                layer.placement,
                LayerPlacement::Anchor(_) | LayerPlacement::ContextPoint(_)
            ) != layer.anchor.is_some()
                || layer
                    .anchor
                    .as_ref()
                    .is_some_and(|id| Some(id) == self.id.as_ref())
            {
                return Err(Error::InvalidProps(
                    "anchored layers require a distinct anchor",
                ));
            }
        }
        if matches!(d.props, Props::Group) {
            return Err(Error::InvalidProps(
                "group has no composite widget metadata",
            ));
        }
        if (self.tab
            || (self.expanded.is_some() && d.role != Some(Role::TreeItem))
            || (self.disabled
                && self.splitter.is_none()
                && self.slider.is_none()
                && self.context_menu.is_none()
                && self.drag_capture.is_none()
                && self.swipe_capture.is_none()
                && d.role != Some(Role::Menubar)
                && !matches!(d.role, Some(Role::Tree | Role::TreeItem))
                && !matches!(self.field_part, Some(FieldPart::Fieldset | FieldPart::Form))))
            && !matches!(d.props, Props::Button(_))
            && self.combobox.is_none()
        {
            return Err(Error::InvalidProps(
                "composite trigger metadata requires a button",
            ));
        }
        if self.navigation_item
            && !matches!(&d.props, Props::Button(_) | Props::Link(_))
            && !matches!(&d.props, Props::Check(check) if check.mode == crate::CheckMode::Radio)
            && self.menu_item.is_none()
        {
            return Err(Error::InvalidProps(
                "navigation items require a button, link or radio",
            ));
        }
        if self.selected.is_some()
            && !self.tab
            && self.option.is_none()
            && d.role != Some(Role::GridCell)
            && d.role != Some(Role::TreeItem)
            || self.tab && (self.expanded.is_some() || self.selected.is_none())
        {
            return Err(Error::InvalidProps(
                "selected belongs to tabs; tabs cannot be disclosures",
            ));
        }
        if self.navigation.is_some() && !matches!(d.props, Props::View) {
            return Err(Error::InvalidProps(
                "hidden panels and navigation boundaries require a view",
            ));
        }
        if self.hidden && !matches!(d.props, Props::View) && self.option.is_none() {
            return Err(Error::InvalidProps(
                "hidden content requires a view or intrinsic option",
            ));
        }
        if self.labelled_by.is_some()
            && (matches!(&d.props, Props::Text(p) if !matches!(p.kind, crate::TextKind::Heading(_)))
                || matches!(&d.props, Props::Image(p) if p.alternative.is_none())
                || matches!(d.props, Props::Graphic(p) if p.alternative.is_none()))
        {
            return Err(Error::InvalidProps(
                "this node cannot have a naming relation",
            ));
        }
        if self.controls.is_some() && self.id == self.controls {
            return Err(Error::InvalidProps("a control cannot control itself"));
        }
        if self.labelled_by.is_some() && self.id == self.labelled_by {
            return Err(Error::InvalidProps("a node cannot label itself"));
        }
        if self.navigation.is_some()
            && !matches!(
                d.role,
                None | Some(
                    Role::Group
                        | Role::TabList
                        | Role::RadioGroup
                        | Role::Toolbar
                        | Role::Menu
                        | Role::Menubar
                        | Role::Listbox
                )
            )
        {
            return Err(Error::InvalidProps(
                "navigation requires a generic group or tab list",
            ));
        }
        Ok(())
    }
}