yew_stdweb/virtual_dom/
vtext.rsuse super::{AttrValue, 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: AttrValue,
pub reference: Option<TextNode>,
}
impl VText {
pub fn new(text: impl Into<AttrValue>) -> Self {
VText {
text: text.into(),
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 {
extern crate self as yew;
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 {
extern crate self as yew;
use crate::html;
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]);
}
}