yew/dom_bundle/
utils.rs
1#[cfg(all(test, target_arch = "wasm32", verbose_tests))]
2macro_rules! test_log {
3 ($fmt:literal, $($arg:expr),* $(,)?) => {
4 ::wasm_bindgen_test::console_log!(concat!("\t ", $fmt), $($arg),*);
5 };
6}
7#[cfg(not(all(test, target_arch = "wasm32", verbose_tests)))]
8macro_rules! test_log {
9 ($fmt:literal, $($arg:expr),* $(,)?) => {
10 let _ = || { std::format_args!(concat!("\t ", $fmt), $($arg),*); };
12 };
13}
14pub(super) use test_log;
17
18#[cfg(feature = "hydration")]
19mod feat_hydration {
20 use std::borrow::Cow;
21
22 use wasm_bindgen::JsCast;
23 use web_sys::{Element, Node};
24
25 pub(in crate::dom_bundle) fn node_type_str(node: &Node) -> Cow<'static, str> {
26 match node.node_type() {
27 Node::ELEMENT_NODE => {
28 let tag = node
29 .dyn_ref::<Element>()
30 .map(|m| m.tag_name().to_lowercase())
31 .unwrap_or_else(|| "unknown".to_owned());
32
33 format!("{tag} element node").into()
34 }
35 Node::ATTRIBUTE_NODE => "attribute node".into(),
36 Node::TEXT_NODE => "text node".into(),
37 Node::CDATA_SECTION_NODE => "cdata section node".into(),
38 Node::ENTITY_REFERENCE_NODE => "entity reference node".into(),
39 Node::ENTITY_NODE => "entity node".into(),
40 Node::PROCESSING_INSTRUCTION_NODE => "processing instruction node".into(),
41 Node::COMMENT_NODE => "comment node".into(),
42 Node::DOCUMENT_NODE => "document node".into(),
43 Node::DOCUMENT_TYPE_NODE => "document type node".into(),
44 Node::DOCUMENT_FRAGMENT_NODE => "document fragment node".into(),
45 Node::NOTATION_NODE => "notation node".into(),
46 _ => "unknown node".into(),
47 }
48 }
49}
50
51#[cfg(feature = "hydration")]
52pub(super) use feat_hydration::*;
53#[cfg(test)]
54#[allow(unused_imports)]
56pub(super) use tests::*;
57
58#[cfg(test)]
59mod tests {
60 #![allow(dead_code)]
61
62 use gloo::utils::document;
63 use web_sys::Element;
64
65 use crate::dom_bundle::{BSubtree, DomSlot};
66 use crate::html::AnyScope;
67 use crate::virtual_dom::vtag::SVG_NAMESPACE;
68
69 pub fn setup_parent() -> (BSubtree, AnyScope, Element) {
70 let scope = AnyScope::test();
71 let parent = document().create_element("div").unwrap();
72 let root = BSubtree::create_root(&parent);
73
74 document().body().unwrap().append_child(&parent).unwrap();
75
76 (root, scope, parent)
77 }
78
79 pub fn setup_parent_svg() -> (BSubtree, AnyScope, Element) {
80 let scope = AnyScope::test();
81 let parent = document()
82 .create_element_ns(Some(SVG_NAMESPACE), "svg")
83 .unwrap();
84 let root = BSubtree::create_root(&parent);
85
86 document().body().unwrap().append_child(&parent).unwrap();
87
88 (root, scope, parent)
89 }
90
91 pub const SIBLING_CONTENT: &str = "END";
92
93 pub(crate) fn setup_parent_and_sibling() -> (BSubtree, AnyScope, Element, DomSlot) {
94 let scope = AnyScope::test();
95 let parent = document().create_element("div").unwrap();
96 let root = BSubtree::create_root(&parent);
97
98 document().body().unwrap().append_child(&parent).unwrap();
99
100 let end = document().create_text_node(SIBLING_CONTENT);
101 parent.append_child(&end).unwrap();
102 let sibling = DomSlot::at(end.into());
103
104 (root, scope, parent, sibling)
105 }
106}