#![allow(dead_code)]
use std::collections::BTreeMap;
use crate::visitor::NodeContext;
use crate::visitor::NodeType;
use std::borrow::Cow;
pub struct NodeContextParts<'a> {
pub node_type: NodeType,
pub tag_name: &'a str,
pub attributes: &'a BTreeMap<String, String>,
pub depth: usize,
pub index_in_parent: usize,
pub parent_tag: Option<&'a str>,
pub is_inline: bool,
}
#[inline]
pub fn build_node_context(parts: NodeContextParts<'_>) -> NodeContext<'_> {
NodeContext::with_borrowed_attributes(
parts.node_type,
Cow::Borrowed(parts.tag_name),
parts.attributes,
parts.depth,
parts.index_in_parent,
parts.parent_tag.map(Cow::Borrowed),
parts.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(NodeContextParts {
node_type: NodeType::Div,
tag_name: "div",
attributes: &attrs,
depth: 2,
index_in_parent: 3,
parent_tag: Some("body"),
is_inline: 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(NodeContextParts {
node_type: NodeType::Html,
tag_name: "html",
attributes: &attrs,
depth: 0,
index_in_parent: 0,
parent_tag: None,
is_inline: false,
});
assert_eq!(ctx.node_type, NodeType::Html);
assert_eq!(ctx.parent_tag, None);
assert!(ctx.attributes().is_empty());
}
}