html_ast/elements/
mod.rs

1mod display;
2
3use crate::node::HtmlNode;
4use std::{
5    borrow::Cow,
6    collections::{BTreeMap, BTreeSet},
7    fmt::{Display, Formatter},
8};
9
10/// An HTML element.
11#[derive(Clone, PartialEq, Eq)]
12pub struct HtmlElement {
13    tag: String,
14    id: String,
15    classes: BTreeSet<String>,
16    attributes: BTreeMap<String, String>,
17    children: Vec<HtmlNode>,
18}
19
20impl Default for HtmlElement {
21    fn default() -> Self {
22        Self {
23            tag: "html".to_string(),
24            id: "".to_string(),
25            classes: BTreeSet::default(),
26            attributes: Default::default(),
27            children: vec![],
28        }
29    }
30}
31
32impl HtmlElement {
33    /// Create a new HTML element with the given tag.
34    pub fn new<S: ToString>(tag: S) -> Self {
35        Self { tag: tag.to_string(), ..Default::default() }
36    }
37    pub fn set_tag<S: ToString>(&mut self, tag: S) {
38        self.tag = tag.to_string();
39    }
40    pub fn get_id(&self) -> &str {
41        &self.id
42    }
43    pub fn set_id<S: ToString>(&mut self, id: S) {
44        self.id = id.to_string();
45    }
46    pub fn with_id<S: ToString>(self, id: S) -> Self {
47        Self { id: id.to_string(), ..self }
48    }
49    pub fn get_classes(&self) -> &BTreeSet<String> {
50        &self.classes
51    }
52    pub fn mut_classes(&mut self) -> &mut BTreeSet<String> {
53        &mut self.classes
54    }
55    pub fn add_class<S: ToString>(&mut self, class: S) {
56        self.classes.insert(class.to_string());
57    }
58    pub fn with_class<S: ToString>(mut self, class: S) -> Self {
59        self.add_class(class);
60        self
61    }
62    pub fn get_attributes(&self) -> &BTreeMap<String, String> {
63        &self.attributes
64    }
65    pub fn mut_attributes(&mut self) -> &mut BTreeMap<String, String> {
66        &mut self.attributes
67    }
68    pub fn add_attribute<K: ToString, V: ToString>(&mut self, key: K, value: V) {
69        self.attributes.insert(key.to_string(), value.to_string());
70    }
71    pub fn with_attribute<K: ToString, V: ToString>(mut self, key: K, value: V) -> Self {
72        self.add_attribute(key, value);
73        self
74    }
75    pub fn get_children(&self) -> &[HtmlNode] {
76        &self.children
77    }
78    pub fn mut_children(&mut self) -> &mut Vec<HtmlNode> {
79        &mut self.children
80    }
81    pub fn add_child<H: Into<HtmlNode>>(&mut self, child: H) {
82        self.children.push(child.into());
83    }
84    pub fn with_child<H: Into<HtmlNode>>(mut self, child: H) -> Self {
85        self.add_child(child);
86        self
87    }
88    /// Add a section of HTML text that does not need to be transferred
89    pub fn add_safe_text(&mut self, text: Cow<'static, str>) {
90        self.children.push(HtmlNode::Text(text))
91    }
92    /// Add a section of HTML text and make righteousness
93    pub fn add_text<S: AsRef<str>>(&mut self, text: S) {
94        let txt = text.as_ref();
95        let mut text = String::with_capacity(txt.len());
96        for c in txt.chars() {
97            match c {
98                '<' => text.push_str("&lt;"),
99                '>' => text.push_str("&gt;"),
100                '&' => text.push_str("&amp;"),
101                '"' => text.push_str("&quot;"),
102                '\'' => text.push_str("&#39;"),
103                _ => text.push(c),
104            }
105        }
106        self.children.push(HtmlNode::Text(Cow::Owned(text)));
107    }
108    pub fn with_safe_text(mut self, text: Cow<'static, str>) -> Self {
109        self.add_safe_text(text);
110        self
111    }
112    pub fn with_text<S: AsRef<str>>(mut self, text: S) -> Self {
113        self.add_text(text);
114        self
115    }
116}