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
use super::{VDiff, VNode, VText};
use std::ops::{Deref, DerefMut};
use stdweb::web::{Element, Node};
#[derive(Clone, Debug, PartialEq, Default)]
pub struct VList {
pub children: Vec<VNode>,
elide_placeholder: bool,
}
impl Deref for VList {
type Target = Vec<VNode>;
fn deref(&self) -> &Self::Target {
&self.children
}
}
impl DerefMut for VList {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.children
}
}
impl VList {
pub fn new() -> Self {
Self::default()
}
pub fn new_with_children(children: Vec<VNode>) -> Self {
VList {
children,
elide_placeholder: false,
}
}
pub(crate) fn new_without_placeholder() -> Self {
VList {
children: Vec::new(),
elide_placeholder: true,
}
}
pub fn add_child(&mut self, child: VNode) {
self.children.push(child);
}
}
impl VDiff for VList {
fn detach(&mut self, parent: &Element) -> Option<Node> {
let mut next_sibling = None;
for mut child in self.children.drain(..) {
next_sibling = child.detach(parent);
}
next_sibling
}
fn apply(
&mut self,
parent: &Element,
previous_sibling: Option<&Node>,
ancestor: Option<VNode>,
) -> Option<Node> {
let mut previous_sibling = previous_sibling.cloned();
let mut rights = {
match ancestor {
Some(VNode::VList(vlist)) => {
vlist.children
}
Some(vnode) => {
vec![vnode]
}
None => Vec::new(),
}
};
if self.children.is_empty() && !self.elide_placeholder {
let placeholder = VText::new("".into());
self.children.push(placeholder.into());
}
let mut lefts = self.children.iter_mut();
let mut rights = rights.drain(..);
loop {
match (lefts.next(), rights.next()) {
(Some(left), Some(right)) => {
previous_sibling = left.apply(parent, previous_sibling.as_ref(), Some(right));
}
(Some(left), None) => {
previous_sibling = left.apply(parent, previous_sibling.as_ref(), None);
}
(None, Some(ref mut right)) => {
right.detach(parent);
}
(None, None) => break,
}
}
previous_sibling
}
}