Skip to main content

oak_html/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// HTML attribute consisting of a name, an optional value, and its source span.
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7pub struct Attribute {
8    /// The name of the attribute (e.g., "class").
9    pub name: String,
10    /// The optional value of the attribute (e.g., "container").
11    pub value: Option<String>,
12    /// The range in the source code where this attribute is defined.
13    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
14    pub span: Range<usize>,
15}
16
17/// Represents different types of nodes in an HTML document.
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19#[derive(Clone, Debug, PartialEq, Eq, Hash)]
20pub enum HtmlNode {
21    /// An HTML element with a tag, attributes, and children.
22    Element(Element),
23    /// Plain text content.
24    Text(Text),
25    /// An HTML comment.
26    Comment(String),
27}
28
29/// An HTML element consisting of a tag name, attributes, children nodes, and its source span.
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31#[derive(Clone, Debug, PartialEq, Eq, Hash)]
32pub struct Element {
33    /// The name of the HTML tag (e.g., "div").
34    pub tag_name: String,
35    /// A list of attributes for this element.
36    pub attributes: Vec<Attribute>,
37    /// The child nodes contained within this element.
38    pub children: Vec<HtmlNode>,
39    /// The range in the source code where this element is defined.
40    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
41    pub span: Range<usize>,
42}
43
44/// Represents plain text content within an HTML document.
45#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
46#[derive(Clone, Debug, PartialEq, Eq, Hash)]
47pub struct Text {
48    /// The text content.
49    pub content: String,
50    /// The range in the source code where this text is located.
51    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
52    pub span: Range<usize>,
53}
54
55/// The root node of an HTML document, containing a sequence of top-level nodes.
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57#[derive(Clone, Debug, PartialEq, Eq, Hash)]
58pub struct HtmlDocument {
59    /// The top-level nodes in the document.
60    pub nodes: Vec<HtmlNode>,
61}