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
//! This file contains interactions with `web_sys`.
use crate::dom_types;
use crate::dom_types::{AtValue, El, Namespace, Node, Text};

use wasm_bindgen::JsCast;
use web_sys::Document;

/// Convenience function to reduce repetition
fn set_style(el_ws: &web_sys::Node, style: &dom_types::Style) {
    el_ws
        .dyn_ref::<web_sys::Element>()
        .expect("Problem casting Node as Element while setting style")
        .set_attribute("style", &style.to_string())
        .expect("Problem setting style");
}

pub(crate) fn assign_ws_nodes_to_el<Ms>(document: &Document, el: &mut El<Ms>) {
    el.node_ws = Some(make_websys_el(el, document));
    for mut child in &mut el.children {
        assign_ws_nodes(document, &mut child);
    }
}
/// Recursively create `web_sys::Node`s, and place them in the vdom Nodes' fields.
pub(crate) fn assign_ws_nodes<Ms>(document: &Document, node: &mut Node<Ms>)
where
    Ms: 'static,
{
    match node {
        Node::Element(el) => assign_ws_nodes_to_el(document, el),
        Node::Text(text) => {
            text.node_ws = Some(
                document
                    .create_text_node(&text.text)
                    .dyn_into::<web_sys::Node>()
                    .expect("Problem casting Text as Node."),
            );
        }
        Node::Empty => (),
    }
}

fn node_to_element(el_ws: &web_sys::Node) -> Result<&web_sys::Element, &'static str> {
    if let web_sys::Node::ELEMENT_NODE = el_ws.node_type() {
        el_ws
            .dyn_ref::<web_sys::Element>()
            .ok_or("Problem casting Node as Element")
    } else {
        Err("Node isn't Element!")
    }
}

fn set_attr_value(el_ws: &web_sys::Node, at: &dom_types::At, at_value: &AtValue) {
    match at_value {
        AtValue::Some(value) => {
            node_to_element(el_ws)
                .and_then(|element| {
                    element
                        .set_attribute(at.as_str(), value)
                        .map_err(|_| "Problem setting an atrribute.")
                })
                .unwrap_or_else(|err| {
                    crate::error(err);
                });
        }
        AtValue::None => {
            node_to_element(el_ws)
                .and_then(|element| {
                    element
                        .set_attribute(at.as_str(), "")
                        .map_err(|_| "Problem setting an atrribute.")
                })
                .unwrap_or_else(|err| {
                    crate::error(err);
                });
        }
        AtValue::Ignored => {
            node_to_element(el_ws)
                .and_then(|element| {
                    element
                        .remove_attribute(at.as_str())
                        .map_err(|_| "Problem removing an atrribute.")
                })
                .unwrap_or_else(|err| {
                    crate::error(err);
                });
        }
    }
}

/// Create and return a `web_sys` Element from our virtual-dom `El`. The `web_sys`
/// Element is a close analog to JS/DOM elements.
///
/// # References
/// * [`web_sys` Element](https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Element.html)
/// * [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Element\)
/// * See also: [`web_sys` Node](https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Node.html)
pub(crate) fn make_websys_el<Ms>(
    el_vdom: &mut El<Ms>,
    document: &web_sys::Document,
) -> web_sys::Node {
    let tag = el_vdom.tag.as_str();

    let el_ws = match el_vdom.namespace {
        Some(ref ns) => document
            .create_element_ns(Some(ns.as_str()), tag)
            .expect("Problem creating web-sys element with namespace"),
        None => document
            .create_element(tag)
            .expect("Problem creating web-sys element"),
    };

    for (at, attr_value) in &el_vdom.attrs.vals {
        set_attr_value(&el_ws, at, attr_value);
    }
    if let Some(ns) = &el_vdom.namespace {
        el_ws
            .dyn_ref::<web_sys::Element>()
            .expect("Problem casting Node as Element while setting an attribute")
            .set_attribute("xmlns", ns.as_str())
            .expect("Problem setting xlmns attribute");
    }

    // Style is just an attribute in the actual Dom, but is handled specially in our vdom;
    // merge the different parts of style here.
    if el_vdom.style.vals.keys().len() > 0 {
        set_style(&el_ws, &el_vdom.style)
    }

    el_ws.into()
}

/// Similar to `attach_el_and_children`, but for text nodes
pub fn attach_text_node(text: &mut Text, parent: &web_sys::Node) {
    let node_ws = text.node_ws.take().expect("Missing websys node for Text");
    parent
        .append_child(&node_ws)
        .expect("Problem appending text node");
    text.node_ws.replace(node_ws);
}

/// Similar to `attach_el_and_children`, but without attaching the elemnt. Useful for
/// patching, where we want to insert the element at a specific place.
pub fn attach_children<Ms>(el_vdom: &mut El<Ms>) {
    let el_ws = el_vdom
        .node_ws
        .as_ref()
        .expect("Missing websys el in attach_children");
    // appending the its children to the el_ws
    for child in &mut el_vdom.children {
        match child {
            // Raise the active level once per recursion.
            Node::Element(child_el) => attach_el_and_children(child_el, el_ws),
            Node::Text(child_text) => attach_text_node(child_text, el_ws),
            Node::Empty => (),
        }
    }
}

/// Attaches the element, and all children, recursively. Only run this when creating a fresh vdom node, since
/// it performs a rerender of the el and all children; eg a potentially-expensive op.
/// This is where rendering occurs.
pub fn attach_el_and_children<Ms>(el_vdom: &mut El<Ms>, parent: &web_sys::Node) {
    // No parent means we're operating on the top-level element; append it to the main div.
    // This is how we call this function externally, ie not through recursion.
    let el_ws = el_vdom
        .node_ws
        .as_ref()
        .expect("Missing websys el in attach_el_and_children");

    // Append the element

    // todo: This error can occur with raw html elements, but am unsure of the cause.
    if parent.append_child(el_ws).is_err() {
        crate::error("Minor problem with html element (append)");
    }

    // appending the its children to the el_ws
    for child in &mut el_vdom.children {
        match child {
            // Raise the active level once per recursion.
            Node::Element(child_el) => attach_el_and_children(child_el, el_ws),
            Node::Text(child_text) => attach_text_node(child_text, el_ws),
            Node::Empty => (),
        }
    }

    // Note: Call `set_default_element_state` after child appending,
    // otherwise it breaks autofocus in Firefox
    set_default_element_state(el_ws, el_vdom);

    // Perform side-effects specified for mounting.
    if let Some(mount_actions) = &mut el_vdom.hooks.did_mount {
        (mount_actions.actions)(el_ws);
        //        if let Some(message) = mount_actions.message.clone() {
        //            app.update(message);
        //        }
    }
}

fn set_default_element_state<Ms>(el_ws: &web_sys::Node, el_vdom: &El<Ms>) {
    // @TODO handle also other Auto* attributes?
    // Set focus because of attribute "autofocus"
    if let Some(at_value) = el_vdom.attrs.vals.get(&dom_types::At::AutoFocus) {
        match at_value {
            AtValue::Some(_) | AtValue::None => el_ws
                .dyn_ref::<web_sys::HtmlElement>()
                .expect("Problem casting Node as HtmlElement while focusing")
                .focus()
                .expect("Problem focusing to an element."),
            AtValue::Ignored => (),
        }
    }

    // We set Textarea's initial value through non-standard attribute "value", so we have to simulate
    // the standard way (i.e. `<textarea>A Value</textarea>`)
    if let Some(textarea) = el_ws.dyn_ref::<web_sys::HtmlTextAreaElement>() {
        if let Some(AtValue::Some(value)) = el_vdom.attrs.vals.get(&dom_types::At::Value) {
            textarea.set_value(value);
        }
    }
}

/// Recursively remove all children.
pub fn _remove_children(el: &web_sys::Node) {
    while let Some(child) = el.last_child() {
        el.remove_child(&child).expect("Problem removing child");
    }
}

/// Update the attributes, style, text, and events of an element. Does not
/// process children, and assumes the tag is the same. Assume we've identfied
/// the most-correct pairing between new and old.
pub fn patch_el_details<Ms>(old: &mut El<Ms>, new: &mut El<Ms>, old_el_ws: &web_sys::Node) {
    // Perform side-effects specified for updating
    if let Some(update_actions) = &mut new.hooks.did_update {
        (update_actions.actions)(old_el_ws) // todo
    }

    if old.attrs != new.attrs {
        for (key, new_val) in &new.attrs.vals {
            match old.attrs.vals.get(key) {
                Some(old_val) => {
                    // The value's different
                    if old_val != new_val {
                        set_attr_value(old_el_ws, key, new_val);
                    }
                }
                None => {
                    set_attr_value(old_el_ws, key, new_val);
                }
            }

            // We handle value in the vdom using attributes, but the DOM needs
            // to use set_value or set_checked.
            match key {
                dom_types::At::Value => match new_val {
                    AtValue::Some(new_val) => crate::util::set_value(old_el_ws, new_val),
                    AtValue::None | AtValue::Ignored => crate::util::set_value(old_el_ws, ""),
                },
                dom_types::At::Checked => match new_val {
                    AtValue::Some(_) | AtValue::None => crate::util::set_checked(old_el_ws, true),
                    AtValue::Ignored => crate::util::set_checked(old_el_ws, false),
                },
                _ => Ok(()),
            }
            .unwrap_or_else(|err| {
                crate::error(err);
            })
        }
        // Remove attributes that aren't in the new vdom.
        for name in old.attrs.vals.keys() {
            if new.attrs.vals.get(name).is_none() {
                // todo get to the bottom of this
                match old_el_ws.dyn_ref::<web_sys::Element>() {
                    Some(el) => el
                        .remove_attribute(name.as_str())
                        .expect("Removing an attribute"),
                    None => {
                        crate::error("Minor error on html element (setting attrs)");
                    }
                }
            }
        }
    }

    // Patch style.
    if old.style != new.style {
        // We can't patch each part of style; rewrite the whole attribute.
        set_style(old_el_ws, &new.style)
    }
}

/// Convenience function used in event handling: Convert an event target
/// to an input element; eg so you can take its value.
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")
}

/// See [`to_input`](fn.to_input.html)
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")
}

/// See [`to_input`](fn.to_input.html)
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")
}

/// See [`to_input`](fn.to_input.html)
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")
}

/// Convert a `web_sys::Event` to a `web_sys::KeyboardEvent`. Useful for extracting
/// info like which key has been pressed, which is not available with normal Events.
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")
}

/// See `to_kbevent`
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")
}

impl<Ms> From<&web_sys::Element> for El<Ms> {
    /// Create a vdom node from a `web_sys::Element`. Used in creating elements from html
    /// and markdown strings. Includes children, recursively added.
    fn from(ws_el: &web_sys::Element) -> Self {
        // Result of tag_name is all caps, but tag From<String> expects lower.
        // Probably is more pure to match by xlmns attribute instead.
        let mut el = match ws_el.tag_name().to_lowercase().as_ref() {
            "svg" => El::empty_svg(ws_el.tag_name().to_lowercase().into()),
            _ => El::empty(ws_el.tag_name().to_lowercase().into()),
        };

        // Populate attributes
        let mut attrs = dom_types::Attrs::empty();
        ws_el
            .get_attribute_names()
            .for_each(&mut |attr_name, _, _| {
                let attr_name = attr_name
                    .as_string()
                    .expect("problem converting attr to string");
                if let Some(attr_val) = ws_el.get_attribute(&attr_name) {
                    attrs.add(attr_name.into(), &attr_val);
                }
            });
        el.attrs = attrs;

        // todo This is the same list in `shortcuts::element_svg!`.
        // todo: Fix this repetition: Use `/scripts/populate_tags.rs`
        // todo to consolodate these lists.
        let svg_tags = [
            "line",
            "rect",
            "circle",
            "ellipse",
            "polygon",
            "polyline",
            "mesh",
            "path",
            "defs",
            "g",
            "marker",
            "mask",
            "pattern",
            "svg",
            "switch",
            "symbol",
            "unknown",
            "linear_gradient",
            "radial_gradient",
            "mesh_gradient",
            "stop",
            "image",
            "r#use",
            "altGlyph",
            "altGlyphDef",
            "altGlyphItem",
            "glyph",
            "glyphRef",
            "textPath",
            "text",
            "tref",
            "tspan",
            "clipPath",
            "cursor",
            "filter",
            "foreignObject",
            "hathpath",
            "meshPatch",
            "meshRow",
            "view",
            "colorProfile",
            "animage",
            "animateColor",
            "animateMotion",
            "animateTransform",
            "discard",
            "mpath",
            "set",
            "desc",
            "metadata",
            "title",
            "feBlend",
            "feColorMatrix",
            "feComponentTransfer",
            "feComposite",
            "feConvolveMatrix",
            "feDiffuseLighting",
            "feDisplacementMap",
            "feDropShadow",
            "feFlood",
            "feFuncA",
            "feFuncB",
            "feFuncG",
            "feFuncR",
            "feGaussianBlur",
            "feImage",
            "feMerge",
            "feMergeNode",
            "feMorphology",
            "feOffset",
            "feSpecularLighting",
            "feTile",
            "feTurbulence",
            "font",
            "hkern",
            "vkern",
            "hatch",
            "solidcolor",
        ];

        if svg_tags.contains(&ws_el.tag_name().to_lowercase().as_str()) {
            el.namespace = Some(Namespace::Svg);
        }

        if let Some(ns) = ws_el.namespace_uri() {
            // Prevent attaching a `xlmns` attribute to normal HTML elements.
            if ns != "http://www.w3.org/1999/xhtml" {
                el.namespace = Some(ns.into());
            }
        }

        let children = ws_el.child_nodes();
        for i in 0..children.length() {
            let child = children
                .get(i)
                .expect("Can't find child in raw html element.");

            if let Some(child_vdom) = node_from_ws(&child) {
                el.children.push(child_vdom);
            }
        }
        el
    }
}
impl<Ms> From<&web_sys::Element> for Node<Ms> {
    fn from(ws_el: &web_sys::Element) -> Node<Ms> {
        Node::Element(ws_el.into())
    }
}

/// Create a vdom node from a `web_sys::Node`. Used in creating elements from html
/// and markdown strings. Includes children, recursively added.
pub fn node_from_ws<Ms>(node: &web_sys::Node) -> Option<Node<Ms>> {
    match node.node_type() {
        web_sys::Node::ELEMENT_NODE => {
            // Element node
            let ws_el = node
                .dyn_ref::<web_sys::Element>()
                .expect("Problem casting Node as Element");

            // Create the Element
            Some(ws_el.into())
        }
        web_sys::Node::TEXT_NODE => Some(Node::new_text(
            node.text_content().expect("Can't find text"),
        )),
        _ => {
            crate::error("Unexpected node type found from raw html");
            None
        }
    }
}

/// Insert a new node into the specified part of the DOM tree.
pub(crate) fn insert_node(
    node: &web_sys::Node,
    parent: &web_sys::Node,
    next: Option<web_sys::Node>,
) {
    match next {
        Some(n) => {
            parent
                .insert_before(node, Some(&n))
                .expect("Problem inserting node");
        }
        None => {
            parent.append_child(node).expect("Problem inserting node");
        }
    };
}

pub(crate) fn remove_node(node: &web_sys::Node, parent: &web_sys::Node) {
    parent
        .remove_child(node)
        .expect("Problem removing old el_ws when updating to empty");
}

pub(crate) fn replace_child(new: &web_sys::Node, old: &web_sys::Node, parent: &web_sys::Node) {
    parent
        .replace_child(new, old)
        .expect("Problem replacing element");
}