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
use super::{VDiff, VNode};
use crate::html::{AnyScope, NodeRef};
use crate::utils::document;
use cfg_if::cfg_if;
use log::warn;
use std::cmp::PartialEq;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::web::{Element, INode, TextNode};
} else if #[cfg(feature = "web_sys")] {
use web_sys::{Element, Text as TextNode};
}
}
#[derive(Clone, Debug)]
pub struct VText {
pub text: String,
pub reference: Option<TextNode>,
}
impl VText {
pub fn new(text: String) -> Self {
VText {
text,
reference: None,
}
}
}
impl VDiff for VText {
fn detach(&mut self, parent: &Element) {
let node = self
.reference
.take()
.expect("tried to remove not rendered VText from DOM");
if parent.remove_child(&node).is_err() {
warn!("Node not found to remove VText");
}
}
fn apply(
&mut self,
_parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
ancestor: Option<VNode>,
) -> NodeRef {
if let Some(mut ancestor) = ancestor {
if let VNode::VText(mut vtext) = ancestor {
self.reference = vtext.reference.take();
let text_node = self
.reference
.clone()
.expect("Rendered VText nodes should have a ref");
if self.text != vtext.text {
text_node.set_node_value(Some(&self.text));
}
return NodeRef::new(text_node.into());
}
ancestor.detach(parent);
}
let text_node = document().create_text_node(&self.text);
super::insert_node(&text_node, parent, next_sibling.get());
self.reference = Some(text_node.clone());
NodeRef::new(text_node.into())
}
}
impl PartialEq for VText {
fn eq(&self, other: &VText) -> bool {
self.text == other.text
}
}
#[cfg(test)]
mod test {
use crate::html;
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
#[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(all(test, feature = "web_sys"))]
mod layout_tests {
use crate::virtual_dom::layout_tests::{diff_layouts, TestLayout};
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
#[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]);
}
}