Skip to main content

oak_html/ast/
mod.rs

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