Skip to main content

muffy_document/html/
node.rs

1use super::element::Element;
2use alloc::sync::Arc;
3use markup5ever_rcdom::NodeData;
4
5/// A node.
6#[derive(Debug, Eq, PartialEq)]
7pub enum Node {
8    /// An element.
9    Element(Element),
10    /// A text.
11    Text(String),
12}
13
14impl Node {
15    pub(crate) fn from_markup5ever(node: &markup5ever_rcdom::Node) -> Option<Self> {
16        match &node.data {
17            NodeData::Element { name, attrs, .. } => Some(Self::Element(Element::new(
18                name.local.to_string(),
19                attrs
20                    .borrow()
21                    .iter()
22                    .map(|attribute| {
23                        (
24                            attribute.name.local.to_string(),
25                            attribute.value.to_string(),
26                        )
27                    })
28                    .collect(),
29                node.children
30                    .borrow()
31                    .iter()
32                    .flat_map(|node| Self::from_markup5ever(node))
33                    .map(Arc::new)
34                    .collect(),
35            ))),
36            NodeData::Text { contents } => Some(Self::Text(contents.borrow().to_string())),
37            NodeData::Comment { .. }
38            | NodeData::Document
39            | NodeData::Doctype { .. }
40            | NodeData::ProcessingInstruction { .. } => None,
41        }
42    }
43}