use std::collections::BTreeMap;
use crate::visitor::NodeContext;
use crate::visitor::NodeType;
#[allow(dead_code)]
#[inline]
pub fn build_node_context(
node_type: NodeType,
tag_name: &str,
attributes: &BTreeMap<String, String>,
depth: usize,
index_in_parent: usize,
parent_tag: Option<&str>,
is_inline: bool,
) -> NodeContext {
NodeContext {
node_type,
tag_name: tag_name.to_string(),
attributes: attributes.clone(),
depth,
index_in_parent,
parent_tag: parent_tag.map(String::from),
is_inline,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_node_context() {
let mut attrs = BTreeMap::new();
attrs.insert("id".to_string(), "main".to_string());
attrs.insert("class".to_string(), "container".to_string());
let ctx = build_node_context(NodeType::Div, "div", &attrs, 2, 3, Some("body"), false);
assert_eq!(ctx.node_type, NodeType::Div);
assert_eq!(ctx.tag_name, "div");
assert_eq!(ctx.depth, 2);
assert_eq!(ctx.index_in_parent, 3);
assert_eq!(ctx.parent_tag, Some("body".to_string()));
assert!(!ctx.is_inline);
assert_eq!(ctx.attributes.len(), 2);
assert_eq!(ctx.attributes.get("id"), Some(&"main".to_string()));
}
#[test]
fn test_build_node_context_no_parent() {
let attrs = BTreeMap::new();
let ctx = build_node_context(NodeType::Html, "html", &attrs, 0, 0, None, false);
assert_eq!(ctx.node_type, NodeType::Html);
assert_eq!(ctx.parent_tag, None);
assert!(ctx.attributes.is_empty());
}
}