-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
btext.rs
231 lines (197 loc) · 6.47 KB
/
btext.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
220
221
222
223
224
225
226
227
228
229
230
231
//! This module contains the bundle implementation of text [BText].
use gloo::console;
use gloo_utils::document;
use web_sys::{Element, Text as TextNode};
use super::{insert_node, BNode, BSubtree, Reconcilable, ReconcileTarget};
use crate::html::AnyScope;
use crate::virtual_dom::{AttrValue, VText};
use crate::NodeRef;
/// The bundle implementation to [VText]
pub(super) struct BText {
text: AttrValue,
text_node: TextNode,
}
impl ReconcileTarget for BText {
fn detach(self, _root: &BSubtree, parent: &Element, parent_to_detach: bool) {
if !parent_to_detach {
let result = parent.remove_child(&self.text_node);
if result.is_err() {
console::warn!("Node not found to remove VText");
}
}
}
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
let node = &self.text_node;
next_parent
.insert_before(node, next_sibling.get().as_ref())
.unwrap();
NodeRef::new(self.text_node.clone().into())
}
}
impl Reconcilable for VText {
type Bundle = BText;
fn attach(
self,
_root: &BSubtree,
_parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
let Self { text } = self;
let text_node = document().create_text_node(&text);
insert_node(&text_node, parent, next_sibling.get().as_ref());
let node_ref = NodeRef::new(text_node.clone().into());
(node_ref, BText { text, text_node })
}
/// Renders virtual node over existing `TextNode`, but only if value of text has changed.
fn reconcile_node(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
bundle: &mut BNode,
) -> NodeRef {
match bundle {
BNode::Text(btext) => self.reconcile(root, parent_scope, parent, next_sibling, btext),
_ => self.replace(root, parent_scope, parent, next_sibling, bundle),
}
}
fn reconcile(
self,
_root: &BSubtree,
_parent_scope: &AnyScope,
_parent: &Element,
_next_sibling: NodeRef,
btext: &mut Self::Bundle,
) -> NodeRef {
let Self { text } = self;
let ancestor_text = std::mem::replace(&mut btext.text, text);
if btext.text != ancestor_text {
btext.text_node.set_node_value(Some(&btext.text));
}
NodeRef::new(btext.text_node.clone().into())
}
}
impl std::fmt::Debug for BText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BText").field("text", &self.text).finish()
}
}
#[cfg(feature = "hydration")]
mod feat_hydration {
use wasm_bindgen::JsCast;
use web_sys::Node;
use super::*;
use crate::dom_bundle::{Fragment, Hydratable};
impl Hydratable for VText {
fn hydrate(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
fragment: &mut Fragment,
) -> (NodeRef, Self::Bundle) {
if let Some(m) = fragment.front().cloned() {
// better safe than sorry.
if m.node_type() == Node::TEXT_NODE {
if let Ok(m) = m.dyn_into::<TextNode>() {
// pop current node.
fragment.pop_front();
// TODO: It may make sense to assert the text content in the text node
// against the VText when #[cfg(debug_assertions)]
// is true, but this may be complicated.
// We always replace the text value for now.
//
// Please see the next comment for a detailed explanation.
m.set_node_value(Some(self.text.as_ref()));
return (
NodeRef::new(m.clone().into()),
BText {
text: self.text,
text_node: m,
},
);
}
}
}
// If there are multiple text nodes placed back-to-back in SSR, it may be parsed as a
// single text node by browser, hence we need to add extra text nodes here
// if the next node is not a text node. Similarly, the value of the text
// node may be a combination of multiple VText vnodes. So we always need to
// override their values.
self.attach(
root,
parent_scope,
parent,
fragment
.front()
.cloned()
.map(NodeRef::new)
.unwrap_or_default(),
)
}
}
}
#[cfg(test)]
mod test {
extern crate self as yew;
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use crate::html;
#[cfg(feature = "wasm_test")]
wasm_bindgen_test_configure!(run_in_browser);
#[test]
fn text_as_root() {
html! {
"Text Node As Root"
};
html! {
{ "Text Node As Root" }
};
}
}
#[cfg(test)]
mod layout_tests {
extern crate self as yew;
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use crate::html;
use crate::tests::layout_tests::{diff_layouts, TestLayout};
#[cfg(feature = "wasm_test")]
wasm_bindgen_test_configure!(run_in_browser);
#[test]
fn diff() {
let layout1 = TestLayout {
name: "1",
node: html! { "a" },
expected: "a",
};
let layout2 = TestLayout {
name: "2",
node: html! { "b" },
expected: "b",
};
let layout3 = TestLayout {
name: "3",
node: html! {
<>
{"a"}
{"b"}
</>
},
expected: "ab",
};
let layout4 = TestLayout {
name: "4",
node: html! {
<>
{"b"}
{"a"}
</>
},
expected: "ba",
};
diff_layouts(vec![layout1, layout2, layout3, layout4]);
}
}