-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
fragment.rs
171 lines (138 loc) · 5.18 KB
/
fragment.rs
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
use std::collections::VecDeque;
use std::ops::{Deref, DerefMut};
use web_sys::{Element, Node};
use super::BSubtree;
use crate::html::NodeRef;
use crate::virtual_dom::Collectable;
/// A Hydration Fragment
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub(crate) struct Fragment(VecDeque<Node>);
impl Deref for Fragment {
type Target = VecDeque<Node>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Fragment {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Fragment {
/// Collects child nodes of an element into a VecDeque.
pub fn collect_children(parent: &Element) -> Self {
let mut fragment = VecDeque::with_capacity(parent.child_nodes().length() as usize);
let mut current_node = parent.first_child();
// This is easier than iterating child nodes at the moment
// as we don't have to downcast iterator values.
while let Some(m) = current_node {
current_node = m.next_sibling();
fragment.push_back(m);
}
Self(fragment)
}
/// Collects nodes for a Component Bundle or a BSuspense.
pub fn collect_between(
collect_from: &mut Fragment,
collect_for: &Collectable,
parent: &Element,
) -> Self {
let is_open_tag = |node: &Node| {
let comment_text = node.text_content().unwrap_or_else(|| "".to_string());
comment_text.starts_with(collect_for.open_start_mark())
&& comment_text.ends_with(collect_for.end_mark())
};
let is_close_tag = |node: &Node| {
let comment_text = node.text_content().unwrap_or_else(|| "".to_string());
comment_text.starts_with(collect_for.close_start_mark())
&& comment_text.ends_with(collect_for.end_mark())
};
// We trim all leading text nodes as it's likely these are whitespaces.
collect_from.trim_start_text_nodes(parent);
let first_node = collect_from
.pop_front()
.unwrap_or_else(|| panic!("expected {} opening tag, found EOF", collect_for.name()));
assert_eq!(
first_node.node_type(),
Node::COMMENT_NODE,
// TODO: improve error message with human readable node type name.
"expected {} start, found node type {}",
collect_for.name(),
first_node.node_type()
);
let mut nodes = VecDeque::new();
if !is_open_tag(&first_node) {
panic!(
"expected {} opening tag, found comment node",
collect_for.name()
);
}
// We remove the opening tag.
parent.remove_child(&first_node).unwrap();
let mut nested_layers = 1;
loop {
let current_node = collect_from.pop_front().unwrap_or_else(|| {
panic!("expected {} closing tag, found EOF", collect_for.name())
});
if current_node.node_type() == Node::COMMENT_NODE {
if is_open_tag(¤t_node) {
// We found another opening tag, we need to increase component counter.
nested_layers += 1;
} else if is_close_tag(¤t_node) {
// We found a closing tag, minus component counter.
nested_layers -= 1;
if nested_layers == 0 {
// We have found the end of the current tag we are collecting, breaking
// the loop.
// We remove the closing tag.
parent.remove_child(¤t_node).unwrap();
break;
}
}
}
nodes.push_back(current_node.clone());
}
Self(nodes)
}
/// Remove child nodes until first non-text node.
pub fn trim_start_text_nodes(&mut self, parent: &Element) {
while let Some(ref m) = self.front().cloned() {
if m.node_type() == Node::TEXT_NODE {
self.pop_front();
parent.remove_child(m).unwrap();
} else {
break;
}
}
}
/// Deeply clones all nodes.
pub fn deep_clone(&self) -> Self {
let nodes = self
.iter()
.map(|m| m.clone_node_with_deep(true).expect("failed to clone node."))
.collect::<VecDeque<_>>();
Self(nodes)
}
// detaches current fragment.
pub fn detach(self, _root: &BSubtree, parent: &Element, parent_to_detach: bool) {
if !parent_to_detach {
for node in self.iter() {
parent
.remove_child(node)
.expect("failed to remove child element");
}
}
}
/// Shift current Fragment into a different position in the dom.
pub fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
for node in self.iter() {
next_parent
.insert_before(node, next_sibling.get().as_ref())
.unwrap();
}
self.front()
.cloned()
.map(NodeRef::new)
.unwrap_or(next_sibling)
}
}