Skip to main content

muffy_document/html/
element.rs

1use super::node::Node;
2use alloc::sync::Arc;
3use core::ops::Deref;
4
5/// An element.
6#[derive(Debug, Eq, PartialEq)]
7pub struct Element {
8    name: String,
9    attributes: Vec<(String, String)>,
10    children: Vec<Arc<Node>>,
11}
12
13impl Element {
14    /// Creates an element.
15    pub const fn new(
16        name: String,
17        attributes: Vec<(String, String)>,
18        children: Vec<Arc<Node>>,
19    ) -> Self {
20        Self {
21            name,
22            attributes,
23            children,
24        }
25    }
26
27    /// Returns a name.
28    pub fn name(&self) -> &str {
29        &self.name
30    }
31
32    /// Returns attributes.
33    pub fn attributes(&self) -> impl Iterator<Item = (&str, &str)> {
34        self.attributes
35            .iter()
36            .map(|(key, value)| (key.as_str(), value.as_str()))
37    }
38
39    /// Returns children.
40    pub fn children(&self) -> impl Iterator<Item = &Node> {
41        self.children.iter().map(Deref::deref)
42    }
43}
44
45impl From<Element> for Node {
46    fn from(element: Element) -> Self {
47        Self::Element(element)
48    }
49}