Skip to main content

oak_html/parser/
element_type.rs

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