forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rs
79 lines (71 loc) · 2.97 KB
/
utils.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
use web_sys::{Element, Node};
/// Insert a concrete [Node] into the DOM
pub(super) fn insert_node(node: &Node, parent: &Element, next_sibling: Option<&Node>) {
match next_sibling {
Some(next_sibling) => parent
.insert_before(node, Some(next_sibling))
.unwrap_or_else(|err| {
// Log normally, so we can inspect the nodes in console
gloo::console::error!(
"failed to insert node before next sibling",
err,
parent,
next_sibling,
node
);
// Log via tracing for consistency
tracing::error!("failed to insert node before next sibling");
// Panic to short-curcuit and fail
panic!("failed to insert node before next sibling")
}),
None => parent.append_child(node).expect("failed to append child"),
};
}
#[cfg(all(test, target_arch = "wasm32", verbose_tests))]
macro_rules! test_log {
($fmt:literal, $($arg:expr),* $(,)?) => {
::wasm_bindgen_test::console_log!(concat!("\t ", $fmt), $($arg),*);
};
}
#[cfg(not(all(test, target_arch = "wasm32", verbose_tests)))]
macro_rules! test_log {
($fmt:literal, $($arg:expr),* $(,)?) => {
// Only type-check the format expression, do not run any side effects
let _ = || { std::format_args!(concat!("\t ", $fmt), $($arg),*); };
};
}
/// Log an operation during tests for debugging purposes
/// Set RUSTFLAGS="--cfg verbose_tests" environment variable to activate.
pub(super) use test_log;
#[cfg(feature = "hydration")]
mod feat_hydration {
use std::borrow::Cow;
use wasm_bindgen::JsCast;
use web_sys::Element;
use super::*;
pub(in crate::dom_bundle) fn node_type_str(node: &Node) -> Cow<'static, str> {
match node.node_type() {
Node::ELEMENT_NODE => {
let tag = node
.dyn_ref::<Element>()
.map(|m| m.tag_name().to_lowercase())
.unwrap_or_else(|| "unknown".to_owned());
format!("{} element node", tag).into()
}
Node::ATTRIBUTE_NODE => "attribute node".into(),
Node::TEXT_NODE => "text node".into(),
Node::CDATA_SECTION_NODE => "cdata section node".into(),
Node::ENTITY_REFERENCE_NODE => "entity reference node".into(),
Node::ENTITY_NODE => "entity node".into(),
Node::PROCESSING_INSTRUCTION_NODE => "processing instruction node".into(),
Node::COMMENT_NODE => "comment node".into(),
Node::DOCUMENT_NODE => "document node".into(),
Node::DOCUMENT_TYPE_NODE => "document type node".into(),
Node::DOCUMENT_FRAGMENT_NODE => "document fragment node".into(),
Node::NOTATION_NODE => "notation node".into(),
_ => "unknown node".into(),
}
}
}
#[cfg(feature = "hydration")]
pub(super) use feat_hydration::*;