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
use crate::engine::ecs::ComponentId;
use crate::engine::ecs::component::Component;
/// The structural/semantic role of a layout node, analogous to the HTML element type.
///
/// Determines the UA-stylesheet default for `display` when `StyleComponent.display` is `None`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ElementType {
// Block-level (default display: block)
Div,
P,
H1,
H2,
H3,
H4,
H5,
H6,
Article,
Section,
Header,
Footer,
Main,
Nav,
Aside,
// Inline (default display: inline)
Span,
A,
Strong,
Em,
Code,
// Special
/// Block + also acts as a LayoutComponent root content node.
Body,
/// Replaced element; intrinsic size from asset.
Img,
// Table (phase 2)
Table,
Thead,
Tbody,
Tr,
Th,
Td,
// Form (phase 3)
Input,
Button,
Textarea,
Select,
/// Generic — no implied display; `StyleComponent` must set `display` explicitly.
#[default]
Element,
}
impl ElementType {
/// The UA-stylesheet default `display` for this element type.
///
/// Returns `None` for `Element` (no default — caller must set `StyleComponent.display`).
pub fn default_display(&self) -> Option<crate::engine::ecs::component::style::Display> {
use crate::engine::ecs::component::style::Display;
match self {
ElementType::Div
| ElementType::P
| ElementType::H1
| ElementType::H2
| ElementType::H3
| ElementType::H4
| ElementType::H5
| ElementType::H6
| ElementType::Article
| ElementType::Section
| ElementType::Header
| ElementType::Footer
| ElementType::Main
| ElementType::Nav
| ElementType::Aside
| ElementType::Body => Some(Display::Block),
ElementType::Span
| ElementType::A
| ElementType::Strong
| ElementType::Em
| ElementType::Code => Some(Display::Inline),
ElementType::Table => Some(Display::Block),
ElementType::Thead | ElementType::Tbody | ElementType::Tr => Some(Display::Block),
ElementType::Th | ElementType::Td => Some(Display::Block),
ElementType::Input | ElementType::Button | ElementType::Select => {
Some(Display::InlineBlock)
}
ElementType::Textarea => Some(Display::Block),
ElementType::Img => Some(Display::InlineBlock),
ElementType::Element => None,
}
}
}
/// Structural/semantic type of a layout node — the HTML-element half of the layout pair.
///
/// Always combined with [`StyleComponent`](crate::engine::ecs::component::StyleComponent)
/// for visual/layout properties and
/// [`LayoutComponent`](crate::engine::ecs::component::LayoutComponent) at the root.
///
/// `element_type` determines the UA-stylesheet default for any CSS property not explicitly
/// set in `StyleComponent` — just like browsers: `div` → block, `span` → inline, etc.
#[derive(Debug, Clone)]
pub struct HtmlElementComponent {
pub element_type: ElementType,
component: Option<ComponentId>,
}
impl HtmlElementComponent {
pub fn new(element_type: ElementType) -> Self {
Self {
element_type,
component: None,
}
}
pub fn div() -> Self {
Self::new(ElementType::Div)
}
pub fn span() -> Self {
Self::new(ElementType::Span)
}
pub fn body() -> Self {
Self::new(ElementType::Body)
}
pub fn header() -> Self {
Self::new(ElementType::Header)
}
pub fn p() -> Self {
Self::new(ElementType::P)
}
}
impl Component for HtmlElementComponent {
fn name(&self) -> &'static str {
"html_element"
}
fn set_id(&mut self, id: ComponentId) {
self.component = Some(id);
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn to_mms_ast(
&self,
_world: &crate::engine::ecs::World,
) -> crate::scripting::ast::ComponentExpression {
use crate::engine::ecs::component::ce_helpers::*;
let ctor = match self.element_type {
ElementType::Div => "div",
ElementType::Span => "span",
ElementType::Body => "body",
ElementType::Header => "header",
ElementType::Footer => "footer",
ElementType::Main => "main",
ElementType::Nav => "nav",
ElementType::Aside => "aside",
ElementType::Section => "section",
ElementType::Article => "article",
ElementType::P => "p",
ElementType::H1 => "h1",
ElementType::H2 => "h2",
ElementType::H3 => "h3",
ElementType::H4 => "h4",
ElementType::H5 => "h5",
ElementType::H6 => "h6",
// Element types without dedicated ctors fall back to bare `HtmlElement {}`,
// which `create_component` resolves to `ElementType::Element`. Most authored
// scenes use the named ctors above; the rest of the enum is reachable via
// future `apply_call` builders or named assignment.
_ => return ce("HtmlElement"),
};
ce_call("HtmlElement", ctor, vec![])
}
}