-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
braw.rs
219 lines (193 loc) · 6.7 KB
/
braw.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
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
use wasm_bindgen::JsCast;
use web_sys::Element;
use crate::dom_bundle::bnode::BNode;
use crate::dom_bundle::traits::{Reconcilable, ReconcileTarget};
use crate::dom_bundle::utils::insert_node;
use crate::dom_bundle::BSubtree;
use crate::html::AnyScope;
use crate::virtual_dom::VRaw;
use crate::{NodeRef};
#[derive(Debug)]
pub struct BRaw {
reference: NodeRef,
children_count: usize
}
impl BRaw {
fn create_elements(html: &str) -> Vec<Element> {
let div = gloo::utils::document().create_element("div").unwrap();
let html = html.trim();
div.set_inner_html(html);
let children = div.children();
let children = js_sys::Array::from(&children);
let children = children.to_vec();
children
.into_iter()
.map(|it| it.unchecked_into())
.collect::<Vec<_>>()
}
fn detach_bundle(&self, parent: &Element) {
if let Some(node) = self.reference.cast::<Element>() {
let mut next_sibling = node.unchecked_ref::<Element>().next_sibling();
parent
.remove_child(&node)
.expect("failed to remove braw node");
let len = self.children_count - 1;
for _ in 0..len {
if let Some(node) = next_sibling {
next_sibling = node.clone().unchecked_ref::<Element>().next_sibling();
parent
.remove_child(&node)
.expect("failed to remove braw node");
}
}
}
}
}
impl ReconcileTarget for BRaw {
fn detach(self, _root: &BSubtree, parent: &Element, _parent_to_detach: bool) {
self.detach_bundle(parent);
}
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
if let Some(node) = self.reference.cast::<Element>() {
if let Some(parent) = node.parent_node() {
parent.remove_child(&node).unwrap();
}
next_parent
.insert_before(&node, next_sibling.get().as_ref())
.unwrap();
return NodeRef::new(node.into());
}
NodeRef::default()
}
}
impl Reconcilable for VRaw {
type Bundle = BRaw;
fn attach(
self,
_root: &BSubtree,
_parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
let elements = BRaw::create_elements(&self.html);
if elements.is_empty() {
return (
next_sibling.clone(),
BRaw {
reference: next_sibling,
children_count: 0,
},
);
}
let node_ref = NodeRef::default();
let count = elements.len();
let mut iter = elements.into_iter();
let first = iter.next().unwrap();
insert_node(&first, parent, next_sibling.get().as_ref());
node_ref.set(Some(first.into()));
for child in iter {
insert_node(&child, parent, next_sibling.get().as_ref());
}
(
node_ref.clone(),
BRaw {
reference: node_ref,
children_count: count,
},
)
}
fn reconcile_node(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
bundle: &mut BNode,
) -> NodeRef {
// we don't have a way to diff what's changed in the string so we remove the node if it's
// present and reattach it
if let BNode::Raw(raw) = bundle {
raw.detach_bundle(parent)
}
let (node_ref, braw) = self.attach(root, parent_scope, parent, next_sibling);
*bundle = braw.into();
node_ref
}
fn reconcile(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
bundle: &mut Self::Bundle,
) -> NodeRef {
// we don't have a way to diff what's changed in the string so we remove the node and
// reattach it
bundle.detach_bundle(parent);
let (node_ref, braw) = self.attach(root, parent_scope, parent, next_sibling);
*bundle = braw;
node_ref
}
}
#[cfg(target_arch = "wasm32")]
#[cfg(test)]
mod tests {
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use super::*;
use crate::dom_bundle::utils::setup_parent;
use crate::virtual_dom::VNode;
wasm_bindgen_test_configure!(run_in_browser);
#[test]
fn braw_works_one_node() {
let (root, scope, parent) = setup_parent();
const HTML: &str = "<span>text</span>";
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML)
}
#[test]
fn braw_works_no_node() {
let (root, scope, parent) = setup_parent();
const HTML: &str = "";
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML)
}
#[test]
fn braw_works_one_node_nested() {
let (root, scope, parent) = setup_parent();
const HTML: &str = r#"<p>one <a href="https://yew.rs">link</a> more paragraph</p><div>here</div>"#;
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML)
}
#[test]
fn braw_works_multi_top_nodes() {
let (root, scope, parent) = setup_parent();
const HTML: &str = r#"<p>paragraph</p><a href="https://yew.rs">link</a>"#;
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML)
}
#[test]
fn braw_detach_works() {
let (root, scope, parent) = setup_parent();
const HTML: &str = r#"<p>paragraph</p><a href="https://yew.rs">link</a>"#;
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML);
elem.detach(&root, &parent, false);
assert_eq!(parent.inner_html(), "");
}
fn assert_braw(node: &mut BNode) -> &mut BRaw {
if let BNode::Raw(braw) = node {
return braw;
}
panic!("should be braw");
}
}