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
//! This module contains the implementation of a virtual element node `VTag`.

use super::{Attributes, Classes, Listener, Listeners, Patch, Reform, VDiff, VNode};
use crate::html::{Component, Scope};
use log::warn;
use std::borrow::Cow;
use std::cmp::PartialEq;
use std::collections::HashSet;
use std::fmt;
use stdweb::unstable::TryFrom;
use stdweb::web::html_element::InputElement;
use stdweb::web::html_element::TextAreaElement;
use stdweb::web::{document, Element, EventListenerHandle, IElement, INode, Node};
#[allow(unused_imports)]
use stdweb::{_js_impl, js};

/// SVG namespace string used for creating svg elements
pub const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";

/// Default namespace for html elements
pub const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";

/// A type for a virtual
/// [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element)
/// representation.
pub struct VTag<COMP: Component> {
    /// A tag of the element.
    tag: Cow<'static, str>,
    /// A reference to the `Element`.
    pub reference: Option<Element>,
    /// List of attached listeners.
    pub listeners: Listeners<COMP>,
    /// List of attributes.
    pub attributes: Attributes,
    /// The list of children nodes. Which also could have own children.
    pub childs: Vec<VNode<COMP>>,
    /// List of attached classes.
    pub classes: Classes,
    /// Contains a value of an
    /// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
    pub value: Option<String>,
    /// Contains
    /// [kind](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types)
    /// value of an `InputElement`.
    pub kind: Option<String>,
    /// Represents `checked` attribute of
    /// [input](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-checked).
    /// It exists to override standard behavior of `checked` attribute, because
    /// in original HTML it sets `defaultChecked` value of `InputElement`, but for reactive
    /// frameworks it's more useful to control `checked` value of an `InputElement`.
    pub checked: bool,
    /// _Service field_. Keeps handler for attached listeners
    /// to have an opportunity to drop them later.
    captured: Vec<EventListenerHandle>,
}

impl<COMP: Component> VTag<COMP> {
    /// Creates a new `VTag` instance with `tag` name (cannot be changed later in DOM).
    pub fn new<S: Into<Cow<'static, str>>>(tag: S) -> Self {
        VTag {
            tag: tag.into(),
            reference: None,
            classes: Classes::new(),
            attributes: Attributes::new(),
            listeners: Vec::new(),
            captured: Vec::new(),
            childs: Vec::new(),
            value: None,
            kind: None,
            // In HTML node `checked` attribute sets `defaultChecked` parameter,
            // but we use own field to control real `checked` parameter
            checked: false,
        }
    }

    /// Returns tag of an `Element`. In HTML tags are always uppercase.
    pub fn tag(&self) -> &str {
        &self.tag
    }

    /// Add `VNode` child.
    pub fn add_child(&mut self, child: VNode<COMP>) {
        self.childs.push(child);
    }

    /// Add multiple `VNode` children.
    pub fn add_children(&mut self, children: Vec<VNode<COMP>>) {
        for child in children {
            self.childs.push(child);
        }
    }

    /// Adds a single class to this virtual node. Actually it will set by
    /// [Element.classList.add](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList)
    /// call later.
    pub fn add_class(&mut self, class: &str) {
        let class = class.trim();
        if !class.is_empty() {
            self.classes.insert(class.into());
        }
    }

    /// Adds multiple classes to this virtual node. Actually it will set by
    /// [Element.classList.add](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList)
    /// call later.
    pub fn add_classes(&mut self, classes: Vec<&str>) {
        for class in classes {
            let class = class.trim();
            if !class.is_empty() {
                self.classes.insert(class.into());
            }
        }
    }

    /// Add classes to this virtual node. Actually it will set by
    /// [Element.classList.add](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList)
    /// call later.
    pub fn set_classes(&mut self, classes: &str) {
        self.classes = classes.split_whitespace().map(String::from).collect();
    }

    /// Sets `value` for an
    /// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
    pub fn set_value<T: ToString>(&mut self, value: &T) {
        self.value = Some(value.to_string());
    }

    /// Sets `kind` property of an
    /// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
    /// Same as set `type` attribute.
    pub fn set_kind<T: ToString>(&mut self, value: &T) {
        self.kind = Some(value.to_string());
    }

    /// Sets `checked` property of an
    /// [InputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input).
    /// (Not a value of node's attribute).
    pub fn set_checked(&mut self, value: bool) {
        self.checked = value;
    }

    /// Adds attribute to a virtual node. Not every attribute works when
    /// it set as attribute. We use workarounds for:
    /// `class`, `type/kind`, `value` and `checked`.
    pub fn add_attribute<T: ToString>(&mut self, name: &str, value: &T) {
        self.attributes.insert(name.to_owned(), value.to_string());
    }

    /// Adds attributes to a virtual node. Not every attribute works when
    /// it set as attribute. We use workarounds for:
    /// `class`, `type/kind`, `value` and `checked`.
    pub fn add_attributes(&mut self, attrs: Vec<(String, String)>) {
        for (name, value) in attrs {
            self.attributes.insert(name, value);
        }
    }

    /// Adds new listener to the node.
    /// It's boxed because we want to keep it in a single list.
    /// Lates `Listener::attach` called to attach actual listener to a DOM node.
    pub fn add_listener(&mut self, listener: Box<dyn Listener<COMP>>) {
        self.listeners.push(listener);
    }

    /// Adds new listeners to the node.
    /// They are boxed because we want to keep them in a single list.
    /// Lates `Listener::attach` called to attach actual listener to a DOM node.
    pub fn add_listeners(&mut self, listeners: Vec<Box<dyn Listener<COMP>>>) {
        for listener in listeners {
            self.listeners.push(listener);
        }
    }

    /// Compute differences between the ancestor and determine patch changes.
    ///
    /// If there is an ancestor:
    /// - add the classes that are in self but NOT in ancestor.
    /// - remove the classes that are in ancestor but NOT in self.
    /// - items that are the same stay the same.
    ///
    /// Otherwise just add everything.
    fn diff_classes(&mut self, ancestor: &mut Option<Self>) -> Vec<Patch<String, ()>> {
        // TODO: Use generator in order to avoid useless alloc

        let mut changes = Vec::new();
        if let Some(ref ancestor) = ancestor {
            // Only change what is necessary.
            let to_add = self
                .classes
                .difference(&ancestor.classes)
                .map(|class| Patch::Add(class.to_owned(), ()));
            changes.extend(to_add);
            let to_remove = ancestor
                .classes
                .difference(&self.classes)
                .map(|class| Patch::Remove(class.to_owned()));
            changes.extend(to_remove);
        } else {
            // Add everything
            let to_add = self
                .classes
                .iter()
                .map(|class| Patch::Add(class.to_owned(), ()));
            changes.extend(to_add);
        }
        changes
    }

    /// Similar to diff_classes except for attributes.
    ///
    /// This also handles patching of attributes when the keys are equal but
    /// the values are different.
    fn diff_attributes(&mut self, ancestor: &mut Option<Self>) -> Vec<Patch<String, String>> {
        let mut changes = Vec::new();
        if let Some(ref mut ancestor) = ancestor {
            // Only change what is necessary.
            let self_keys = self.attributes.keys().collect::<HashSet<_>>();
            let ancestor_keys = ancestor.attributes.keys().collect::<HashSet<_>>();
            let to_add = self_keys.difference(&ancestor_keys).map(|key| {
                let value = self.attributes.get(*key).expect("attribute of vtag lost");
                Patch::Add(key.to_string(), value.to_string())
            });
            changes.extend(to_add);
            for key in self_keys.intersection(&ancestor_keys) {
                let self_value = self
                    .attributes
                    .get(*key)
                    .expect("attribute of self side lost");
                let ancestor_value = ancestor
                    .attributes
                    .get(*key)
                    .expect("attribute of ancestor side lost");
                if self_value != ancestor_value {
                    let mutator = Patch::Replace(key.to_string(), self_value.to_string());
                    changes.push(mutator);
                }
            }
            let to_remove = ancestor_keys
                .difference(&self_keys)
                .map(|key| Patch::Remove(key.to_string()));
            changes.extend(to_remove);
        } else {
            // Add everything
            for (key, value) in &self.attributes {
                let mutator = Patch::Add(key.to_string(), value.to_string());
                changes.push(mutator);
            }
        }
        changes
    }

    /// Similar to `diff_attributers` except there is only a single `kind`.
    fn diff_kind(&mut self, ancestor: &mut Option<Self>) -> Option<Patch<String, ()>> {
        match (
            &self.kind,
            ancestor.as_mut().and_then(|anc| anc.kind.take()),
        ) {
            (&Some(ref left), Some(ref right)) => {
                if left != right {
                    Some(Patch::Replace(left.to_string(), ()))
                } else {
                    None
                }
            }
            (&Some(ref left), None) => Some(Patch::Add(left.to_string(), ())),
            (&None, Some(right)) => Some(Patch::Remove(right)),
            (&None, None) => None,
        }
    }

    /// Almost identical in spirit to `diff_kind`
    fn diff_value(&mut self, ancestor: &mut Option<Self>) -> Option<Patch<String, ()>> {
        match (
            &self.value,
            ancestor.as_mut().and_then(|anc| anc.value.take()),
        ) {
            (&Some(ref left), Some(ref right)) => {
                if left != right {
                    Some(Patch::Replace(left.to_string(), ()))
                } else {
                    None
                }
            }
            (&Some(ref left), None) => Some(Patch::Add(left.to_string(), ())),
            (&None, Some(right)) => Some(Patch::Remove(right)),
            (&None, None) => None,
        }
    }

    fn apply_diffs(&mut self, element: &Element, ancestor: &mut Option<Self>) {
        // Update parameters
        let changes = self.diff_classes(ancestor);
        for change in changes {
            let list = element.class_list();
            match change {
                Patch::Add(class, _) | Patch::Replace(class, _) => {
                    list.add(&class).expect("can't add a class");
                }
                Patch::Remove(class) => {
                    list.remove(&class).expect("can't remove a class");
                }
            }
        }

        let changes = self.diff_attributes(ancestor);
        for change in changes {
            match change {
                Patch::Add(key, value) | Patch::Replace(key, value) => {
                    set_attribute(element, &key, &value);
                }
                Patch::Remove(key) => {
                    remove_attribute(element, &key);
                }
            }
        }

        // `input` element has extra parameters to control
        // I override behavior of attributes to make it more clear
        // and useful in templates. For example I interpret `checked`
        // attribute as `checked` parameter, not `defaultChecked` as browsers do
        if let Ok(input) = InputElement::try_from(element.clone()) {
            if let Some(change) = self.diff_kind(ancestor) {
                match change {
                    Patch::Add(kind, _) | Patch::Replace(kind, _) => {
                        //https://github.com/koute/stdweb/commit/3b85c941db00b8e3c942624afd50c5929085fb08
                        //input.set_kind(&kind);
                        let input = &input;
                        js! { @(no_return)
                            @{input}.type = @{kind};
                        }
                    }
                    Patch::Remove(_) => {
                        //input.set_kind("");
                        let input = &input;
                        js! { @(no_return)
                            @{input}.type = "";
                        }
                    }
                }
            }

            if let Some(change) = self.diff_value(ancestor) {
                match change {
                    Patch::Add(kind, _) | Patch::Replace(kind, _) => {
                        input.set_raw_value(&kind);
                    }
                    Patch::Remove(_) => {
                        input.set_raw_value("");
                    }
                }
            }

            // IMPORTANT! This parameter has to be set every time
            // to prevent strange behaviour in the browser when the DOM changes
            set_checked(&input, self.checked);
        } else if let Ok(tae) = TextAreaElement::try_from(element.clone()) {
            if let Some(change) = self.diff_value(ancestor) {
                match change {
                    Patch::Add(value, _) | Patch::Replace(value, _) => {
                        tae.set_value(&value);
                    }
                    Patch::Remove(_) => {
                        tae.set_value("");
                    }
                }
            }
        }
    }
}

impl<COMP: Component> VDiff for VTag<COMP> {
    type Component = COMP;

    /// Remove VTag from parent.
    fn detach(&mut self, parent: &Element) -> Option<Node> {
        let node = self
            .reference
            .take()
            .expect("tried to remove not rendered VTag from DOM");
        let sibling = node.next_sibling();
        if parent.remove_child(&node).is_err() {
            warn!("Node not found to remove VTag");
        }
        sibling
    }

    /// Renders virtual tag over DOM `Element`, but it also compares this with an ancestor `VTag`
    /// to compute what to patch in the actual DOM nodes.
    fn apply(
        &mut self,
        parent: &Element,
        precursor: Option<&Node>,
        ancestor: Option<VNode<Self::Component>>,
        env: &Scope<Self::Component>,
    ) -> Option<Node> {
        assert!(
            self.reference.is_none(),
            "reference is ignored so must not be set"
        );
        let (reform, mut ancestor) = {
            match ancestor {
                Some(VNode::VTag(mut vtag)) => {
                    if self.tag == vtag.tag {
                        // If tags are equal, preserve the reference that already exists.
                        self.reference = vtag.reference.take();
                        (Reform::Keep, Some(vtag))
                    } else {
                        // We have to create a new reference, remove ancestor.
                        let node = vtag.detach(parent);
                        (Reform::Before(node), None)
                    }
                }
                Some(mut vnode) => {
                    // It is not a VTag variant we must remove the ancestor.
                    let node = vnode.detach(parent);
                    (Reform::Before(node), None)
                }
                None => (Reform::Before(None), None),
            }
        };

        // Ensure that `self.reference` exists.
        //
        // This can use the previous reference or create a new one.
        // If we create a new one we must insert it in the correct
        // place, which we use `before` or `precusor` for.
        match reform {
            Reform::Keep => {}
            Reform::Before(before) => {
                let element = if self.tag == "svg"
                    || parent.namespace_uri() == Some(SVG_NAMESPACE.to_string())
                {
                    document()
                        .create_element_ns(SVG_NAMESPACE, &self.tag)
                        .expect("can't create namespaced element for vtag")
                } else {
                    document()
                        .create_element(&self.tag)
                        .expect("can't create element for vtag")
                };

                if let Some(sibling) = before {
                    parent
                        .insert_before(&element, &sibling)
                        .expect("can't insert tag before sibling");
                } else {
                    let precursor = precursor.and_then(|before| before.next_sibling());
                    if let Some(precursor) = precursor {
                        parent
                            .insert_before(&element, &precursor)
                            .expect("can't insert tag before precursor");
                    } else {
                        parent.append_child(&element);
                    }
                }
                self.reference = Some(element);
            }
        }

        let element = self.reference.clone().expect("element expected");

        {
            let mut ancestor_childs = Vec::new();
            if let Some(ref mut a) = ancestor {
                std::mem::swap(&mut ancestor_childs, &mut a.childs);
            }

            self.apply_diffs(&element, &mut ancestor);

            // Every render it removes all listeners and attach it back later
            // TODO Compare references of handler to do listeners update better
            if let Some(mut ancestor) = ancestor {
                for handle in ancestor.captured.drain(..) {
                    handle.remove();
                }
            }

            for mut listener in self.listeners.drain(..) {
                let handle = listener.attach(&element, env.clone());
                self.captured.push(handle);
            }

            // Process children
            // Start with an empty precursor, because it put childs to itself
            let mut precursor = None;
            let mut self_childs = self.childs.iter_mut();
            let mut ancestor_childs = ancestor_childs.drain(..);
            loop {
                match (self_childs.next(), ancestor_childs.next()) {
                    (Some(left), right) => {
                        precursor = left.apply(&element, precursor.as_ref(), right, &env);
                    }
                    (None, Some(ref mut right)) => {
                        right.detach(&element);
                    }
                    (None, None) => break,
                }
            }
        }
        self.reference.as_ref().map(|e| e.as_node().to_owned())
    }
}

impl<COMP: Component> fmt::Debug for VTag<COMP> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "VTag {{ tag: {} }}", self.tag)
    }
}

/// `stdweb` doesn't have methods to work with attributes now.
/// this is [workaround](https://github.com/koute/stdweb/issues/16#issuecomment-325195854)
fn set_attribute(element: &Element, name: &str, value: &str) {
    js!( @(no_return) @{element}.setAttribute( @{name}, @{value} ); );
}

/// Removes attribute from a element by name.
fn remove_attribute(element: &Element, name: &str) {
    js!( @(no_return) @{element}.removeAttribute( @{name} ); );
}

/// Set `checked` value for the `InputElement`.
fn set_checked(input: &InputElement, value: bool) {
    js!( @(no_return) @{input}.checked = @{value}; );
}

impl<COMP: Component> PartialEq for VTag<COMP> {
    fn eq(&self, other: &VTag<COMP>) -> bool {
        if self.tag != other.tag {
            return false;
        }

        if self.value != other.value {
            return false;
        }

        if self.kind != other.kind {
            return false;
        }

        if self.checked != other.checked {
            return false;
        }

        if self.listeners.len() != other.listeners.len() {
            return false;
        }

        for i in 0..self.listeners.len() {
            let a = &self.listeners[i];
            let b = &other.listeners[i];

            if a.kind() != b.kind() {
                return false;
            }
        }

        if self.attributes != other.attributes {
            return false;
        }

        if self.classes.iter().ne(other.classes.iter()) {
            return false;
        }

        if self.childs.len() != other.childs.len() {
            return false;
        }

        for i in 0..self.childs.len() {
            let a = &self.childs[i];
            let b = &other.childs[i];

            if a != b {
                return false;
            }
        }

        true
    }
}