1use std::cell::RefCell;
 2use std::collections::HashSet;
 3use std::sync::OnceLock;
 4
 5use html5ever::Attribute;
 6
 7/// Returns a [`HashSet`] containing the HTML elements that are inline by default.
 8///
 9/// [MDN: List of "inline" elements](https://yari-demos.prod.mdn.mozit.cloud/en-US/docs/Web/HTML/Inline_elements)
10fn inline_elements() -> &'static HashSet<&'static str> {
11    static INLINE_ELEMENTS: OnceLock<HashSet<&str>> = OnceLock::new();
12    INLINE_ELEMENTS.get_or_init(|| {
13        HashSet::from_iter([
14            "a", "abbr", "acronym", "audio", "b", "bdi", "bdo", "big", "br", "button", "canvas",
15            "cite", "code", "data", "datalist", "del", "dfn", "em", "embed", "i", "iframe", "img",
16            "input", "ins", "kbd", "label", "map", "mark", "meter", "noscript", "object", "output",
17            "picture", "progress", "q", "ruby", "s", "samp", "script", "select", "slot", "small",
18            "span", "strong", "sub", "sup", "svg", "template", "textarea", "time", "tt", "u",
19            "var", "video", "wbr",
20        ])
21    })
22}
23
24#[derive(Debug, Clone)]
25pub struct HtmlElement {
26    tag: String,
27    pub(crate) attrs: RefCell<Vec<Attribute>>,
28}
29
30impl HtmlElement {
31    pub fn new(tag: String, attrs: RefCell<Vec<Attribute>>) -> Self {
32        Self { tag, attrs }
33    }
34
35    pub fn tag(&self) -> &str {
36        &self.tag
37    }
38
39    /// Returns whether this [`HtmlElement`] is an inline element.
40    pub fn is_inline(&self) -> bool {
41        inline_elements().contains(self.tag.as_str())
42    }
43
44    /// Returns the attribute with the specified name.
45    pub fn attr(&self, name: &str) -> Option<String> {
46        self.attrs
47            .borrow()
48            .iter()
49            .find(|attr| attr.name.local.to_string() == name)
50            .map(|attr| attr.value.to_string())
51    }
52
53    /// Returns the list of classes on this [`HtmlElement`].
54    pub fn classes(&self) -> Vec<String> {
55        self.attrs
56            .borrow()
57            .iter()
58            .find(|attr| attr.name.local.to_string() == "class")
59            .map(|attr| {
60                attr.value
61                    .split(' ')
62                    .map(|class| class.trim().to_string())
63                    .collect::<Vec<_>>()
64            })
65            .unwrap_or_default()
66    }
67
68    /// Returns whether this [`HtmlElement`] has the specified class.
69    pub fn has_class(&self, class: &str) -> bool {
70        self.has_any_classes(&[class])
71    }
72
73    /// Returns whether this [`HtmlElement`] has any of the specified classes.
74    pub fn has_any_classes(&self, classes: &[&str]) -> bool {
75        self.attrs.borrow().iter().any(|attr| {
76            attr.name.local.to_string() == "class"
77                && attr
78                    .value
79                    .split(' ')
80                    .any(|class| classes.contains(&class.trim()))
81        })
82    }
83}