html-cat 0.1.0

HTML5 parser: tokenizer + tree builder producing a Document tree of Element/Text/Comment nodes. No mut, no Rc/Arc, no interior mutability, no panics, exhaustive matches. First sub-crate of a Servo-replacement webview runtime targeting Tauri.
//! Tokenizer output: the atoms the tree builder consumes.

use crate::attr::Attributes;
use crate::span::Span;

/// One token produced by [`tokenize`].
///
/// [`tokenize`]: crate::tokenizer::tokenize
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Token {
    /// `<!DOCTYPE name PUBLIC? SYSTEM? >`.
    Doctype {
        /// The root element name (typically `"html"`).
        name: String,
        /// The optional PUBLIC identifier.
        public_id: Option<String>,
        /// The optional SYSTEM identifier.
        system_id: Option<String>,
        /// Source span.
        span: Span,
    },
    /// An opening tag.  Self-closing flag set for `<br />` and friends.
    StartTag {
        /// Lower-cased tag name.
        name: String,
        /// Attributes in source order.
        attributes: Attributes,
        /// True iff the source wrote `/>`.
        self_closing: bool,
        /// Source span.
        span: Span,
    },
    /// A closing tag (`</p>`).
    EndTag {
        /// Lower-cased tag name.
        name: String,
        /// Source span.
        span: Span,
    },
    /// A run of text.  Entities have already been decoded.
    Text {
        /// Decoded text content.
        content: String,
        /// Source span.
        span: Span,
    },
    /// A `<!-- comment -->`.
    Comment {
        /// Comment text (between `<!--` and `-->`).
        text: String,
        /// Source span.
        span: Span,
    },
    /// End of input.
    Eof {
        /// Source span (synthetic; points past the last byte).
        span: Span,
    },
}

impl Token {
    /// The source span of the token.
    #[must_use]
    pub fn span(&self) -> Span {
        match self {
            Self::Doctype { span, .. }
            | Self::StartTag { span, .. }
            | Self::EndTag { span, .. }
            | Self::Text { span, .. }
            | Self::Comment { span, .. }
            | Self::Eof { span } => *span,
        }
    }
}