Skip to main content

oak_html/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// HTML element types.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum HtmlElementType {
7    /// Opening tag bracket `<`.
8    TagOpen,
9    /// Closing tag bracket `>`.
10    TagClose,
11    /// Opening tag with slash `</`.
12    TagSlashOpen,
13    /// Self-closing tag slash `/>`.
14    TagSelfClose,
15    /// Tag name.
16    TagName,
17    /// Attribute name.
18    AttributeName,
19    /// Attribute value.
20    AttributeValue,
21    /// Attribute node.
22    Attribute,
23    /// Text content.
24    Text,
25    /// Comment node.
26    Comment,
27    /// Equal sign.
28    Equal,
29    /// Quote.
30    Quote,
31    /// Doctype declaration.
32    Doctype,
33    /// CDATA section.
34    CData,
35    /// Processing instruction.
36    ProcessingInstruction,
37    /// Entity reference.
38    EntityRef,
39    /// Character reference.
40    CharRef,
41    /// Whitespace.
42    Whitespace,
43    /// Newline.
44    Newline,
45    /// Document root.
46    Document,
47    /// HTML element.
48    Element,
49    /// End of file.
50    Eof,
51    /// Error node.
52    Error,
53}
54
55impl ElementType for HtmlElementType {
56    type Role = UniversalElementRole;
57
58    fn role(&self) -> Self::Role {
59        match self {
60            Self::Document => UniversalElementRole::Root,
61            Self::Element => UniversalElementRole::Container,
62            Self::Attribute => UniversalElementRole::Attribute,
63            _ => UniversalElementRole::None,
64        }
65    }
66}
67
68impl From<crate::lexer::token_type::HtmlTokenType> for HtmlElementType {
69    fn from(token: crate::lexer::token_type::HtmlTokenType) -> Self {
70        unsafe { std::mem::transmute(token) }
71    }
72}