#![allow(dead_code)]
use std::collections::BTreeMap;
use crate::visitor::NodeContext;
use crate::visitor::NodeType;
use std::borrow::Cow;
#[inline]
pub fn build_node_context<'a>(
node_type: NodeType,
tag_name: &'a str,
attributes: &'a BTreeMap<String, String>,
depth: usize,
index_in_parent: usize,
parent_tag: Option<&'a str>,
is_inline: bool,
) -> NodeContext<'a> {
NodeContext::with_borrowed_attributes(
node_type,
Cow::Borrowed(tag_name),
attributes,
depth,
index_in_parent,
parent_tag.map(Cow::Borrowed),
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.as_deref(), Some("body"));
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());
}
}