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
use wasm_bindgen::JsCast;
use crate::dom_types;
fn set_attr_shim(el_ws: &web_sys::Node, name: &str, val: &str) {
let mut set_special = false;
if name == "checked" {
let input_el = el_ws.dyn_ref::<web_sys::HtmlInputElement>();
if let Some(el) = input_el {
match val {
"true" => {
el.set_checked(true);
}
"false" => {
el.set_checked(false);
}
_ => (),
}
set_special = true;
}
}
else if name == "autofocus" {
if let Some(input) = el_ws.dyn_ref::<web_sys::HtmlInputElement>() {
match val {
"true" => {
input.set_autofocus(true);
}
"false" => {
input.set_autofocus(false);
}
_ => (),
}
set_special = true;
}
if let Some(input) = el_ws.dyn_ref::<web_sys::HtmlTextAreaElement>() {
match val {
"true" => {
input.set_autofocus(true);
}
"false" => {
input.set_autofocus(false);
}
_ => (),
}
set_special = true;
}
if let Some(input) = el_ws.dyn_ref::<web_sys::HtmlSelectElement>() {
match val {
"true" => {
input.set_autofocus(true);
}
"false" => {
input.set_autofocus(false);
}
_ => (),
}
set_special = true;
}
if let Some(input) = el_ws.dyn_ref::<web_sys::HtmlButtonElement>() {
match val {
"true" => {
input.set_autofocus(true);
}
"false" => {
input.set_autofocus(false);
}
_ => (),
}
set_special = true;
}
}
if !set_special {
el_ws.dyn_ref::<web_sys::Element>()
.expect("Problem casting Node as Element")
.set_attribute(name, val)
.expect("Problem setting an atrribute.");
}
}
pub fn make_websys_el<Ms: Clone>(
el_vdom: &mut dom_types::El<Ms>,
document: &web_sys::Document,
) -> web_sys::Node {
let tag = el_vdom.tag.as_str();
if el_vdom.raw_html {
let el_ws = document
.create_element(tag)
.expect("Problem creating web-sys El");
el_ws.set_inner_html(&el_vdom.text.clone().expect("Missing text on raw HTML element"));
return el_ws.into()
}
if let Some(text) = &el_vdom.text {
return document.create_text_node(&text).into();
}
let el_ws = match el_vdom.namespace {
Some(ref ns) => document
.create_element_ns(Some(ns.as_str()), tag)
.expect("Problem creating web-sys El"),
None => {
document
.create_element(tag)
.expect("Problem creating web-sys El")
}
};
for (name, val) in &el_vdom.attrs.vals {
set_attr_shim(&el_ws, name, val);
}
if el_vdom.style.vals.keys().len() > 0 {
el_ws.dyn_ref::<web_sys::Element>()
.expect("Problem casting Node as Element")
.set_attribute("style", &el_vdom.style.to_string())
.expect("Problem setting style");
}
el_ws.into()
}
pub fn attach_els<Ms: Clone>(el_vdom: &mut dom_types::El<Ms>, parent: &web_sys::Node) {
let el_ws = el_vdom.el_ws.take().expect("Missing websys el");
parent.append_child(&el_ws).unwrap();
if let Some(mount_actions) = &mut el_vdom.did_mount {
mount_actions(&el_ws)
}
for child in &mut el_vdom.children {
attach_els(child, &el_ws)
}
el_vdom.el_ws.replace(el_ws);
}
pub fn _remove_children(el: &web_sys::Node) {
while let Some(child) = el.last_child() {
el.remove_child(&child).unwrap();
}
}
pub fn patch_el_details<Ms: Clone>(
old: &mut dom_types::El<Ms>,
new: &mut dom_types::El<Ms>,
old_el_ws: &web_sys::Node,
) {
if let Some(update_actions) = &mut old.did_update {
update_actions(old_el_ws)
}
if old.attrs != new.attrs {
for (key, new_val) in &new.attrs.vals {
match old.attrs.vals.get(key) {
Some(old_val) => {
if old_val != new_val {
set_attr_shim(&old_el_ws, key, new_val);
}
}
None => set_attr_shim(&old_el_ws, key, new_val),
}
}
for name in old.attrs.vals.keys() {
if new.attrs.vals.get(name).is_none() {
old_el_ws.dyn_ref::<web_sys::Element>()
.expect("Problem casting Node as Element")
.remove_attribute(name)
.expect("Removing an attribute");
}
}
}
if old.style != new.style {
old_el_ws.dyn_ref::<web_sys::Element>()
.expect("Problem casting Node as Element")
.set_attribute("style", &new.style.to_string())
.expect("Setting style");
}
if old.text != new.text {
if new.raw_html {
old_el_ws.dyn_ref::<web_sys::Element>()
.expect("Problem casting Node as Element")
.set_inner_html(&new.text.clone().unwrap_or_default())
} else {
match new.text.clone() {
Some(text) => old_el_ws.set_text_content(Some(&text)),
None => old_el_ws.set_text_content(None)
};
}
}
}
pub fn to_input(target: &web_sys::EventTarget) -> &web_sys::HtmlInputElement {
target
.dyn_ref::<web_sys::HtmlInputElement>()
.expect("Unable to cast as an input element")
}
pub fn to_textarea(target: &web_sys::EventTarget) -> &web_sys::HtmlTextAreaElement {
target
.dyn_ref::<web_sys::HtmlTextAreaElement>()
.expect("Unable to cast as a textarea element")
}
pub fn to_select(target: &web_sys::EventTarget) -> &web_sys::HtmlSelectElement {
target
.dyn_ref::<web_sys::HtmlSelectElement>()
.expect("Unable to cast as a select element")
}
pub fn to_html_el(target: &web_sys::EventTarget) -> &web_sys::HtmlElement {
target
.dyn_ref::<web_sys::HtmlElement>()
.expect("Unable to cast as an HTML element")
}
pub fn to_kbevent(event: &web_sys::Event) -> &web_sys::KeyboardEvent {
event
.dyn_ref::<web_sys::KeyboardEvent>()
.expect("Unable to cast as a keyboard event")
}
pub fn to_mouse_event(event: &web_sys::Event) -> &web_sys::MouseEvent {
event
.dyn_ref::<web_sys::MouseEvent>()
.expect("Unable to cast as a mouse event")
}