Sign in

    mizchi/crater/html does not have a README file

    Document

    pub(all) struct Document {
    root : Element
    stylesheets : Array[String]
    stylesheet_links : Array[String]
    }

    Parsed HTML document with extracted resources

    Element

    pub(all) struct Element {
    tag : String
    id : String?
    classes : Array[String]
    style : String?
    attributes : Map[String, String]
    children : Array[Node]
    }

    HTML Element node

    FormattingElement

    pub(all) enum FormattingElement {
    Element(Element)
    Marker
    }

    Formatting element marker or element

    InsertionMode

    pub(all) enum InsertionMode {
    Initial
    BeforeHtml
    BeforeHead
    InHead
    InHeadNoscript
    AfterHead
    InBody
    Text
    InTable
    InTableText
    InCaption
    InColumnGroup
    InTableBody
    InRow
    InCell
    InSelect
    InSelectInTable
    AfterBody
    InFrameset
    AfterFrameset
    AfterAfterBody
    AfterAfterFrameset
    }

    Insertion mode for tree construction

    Node

    pub(all) enum Node {
    Element(Element)
    Text(String)
    }

    HTML Node (Element or Text)

    StreamingTokenizer

    pub(all) struct StreamingTokenizer {
    buffer : StringBuilder
    cached_str : String
    cache_valid : Bool
    pos : Int
    finished : Bool
    }

    Streaming tokenizer that processes HTML incrementally

    StreamingTokenizer::compact

    fn StreamingTokenizer::compact(self : StreamingTokenizer) -> Unit

    Compact the buffer by removing already-processed data Call this periodically to free memory

    StreamingTokenizer::feed

    fn StreamingTokenizer::feed(self : StreamingTokenizer, chunk : String) -> Unit

    Add a chunk of HTML to the buffer

    StreamingTokenizer::finish

    fn StreamingTokenizer::finish(self : StreamingTokenizer) -> Unit

    Signal that no more input will be provided

    StreamingTokenizer::new

    StreamingTokenizer::next_token

    fn StreamingTokenizer::next_token(self : StreamingTokenizer) -> Token?

    Get the next token, or None if more data is needed

    StreamingTreeBuilder

    pub(all) struct StreamingTreeBuilder {
    tokenizer : StreamingTokenizer
    insertion_mode : InsertionMode
    original_insertion_mode : InsertionMode?
    open_elements : Array[Element]
    active_formatting : Array[FormattingElement]
    foster_parenting : Bool
    frameset_ok : Bool
    head_element : Element?
    pending_table_chars : Array[Char]
    pending_text : StringBuilder
    pending_tokens : Array[Token]
    root : Element?
    }

    Streaming tree builder that processes HTML incrementally

    StreamingTreeBuilder::feed

    fn StreamingTreeBuilder::feed(self : StreamingTreeBuilder, chunk : String) -> Unit

    Feed a chunk of HTML

    StreamingTreeBuilder::finish

    Signal end of input and get the result

    StreamingTreeBuilder::new

    Token

    pub(all) enum Token {
    Doctype(String)
    StartTag(String, Map[String, String], Bool)
    EndTag(String)
    Character(Char)
    Characters(String)
    Comment(String)
    EOF
    }

    HTML Token types

    Tokenizer

    pub(all) struct Tokenizer {
    input : String
    pos : Int
    len : Int
    }

    Tokenizer state

    Tokenizer::collect_all

    fn Tokenizer::collect_all(self : Tokenizer) -> Array[Token]

    Collect all tokens (for debugging/testing)

    Tokenizer::new

    fn Tokenizer::new(input : String) -> Tokenizer

    Tokenizer::next_token

    fn Tokenizer::next_token(self : Tokenizer) -> Token

    Get the next token

    TreeBuilder

    pub(all) struct TreeBuilder {
    tokenizer : Tokenizer
    insertion_mode : InsertionMode
    original_insertion_mode : InsertionMode?
    open_elements : Array[Element]
    active_formatting : Array[FormattingElement]
    foster_parenting : Bool
    frameset_ok : Bool
    head_element : Element?
    pending_table_chars : Array[Char]
    pending_text : StringBuilder
    pending_tokens : Array[Token]
    }

    Tree Builder state machine

    TreeBuilder::build

    fn TreeBuilder::build(self : TreeBuilder) -> Element

    Run the tree construction algorithm for fragment parsing Starts in InBody mode with pre-created html/head/body structure

    TreeBuilder::build_document

    fn TreeBuilder::build_document(self : TreeBuilder) -> Element

    Run the tree construction algorithm for full document parsing Starts in Initial mode and builds structure from tokens

    TreeBuilder::new

    fn TreeBuilder::new(tokenizer : Tokenizer) -> TreeBuilder

    assign_synthetic_ids

    fn assign_synthetic_ids(doc : Document) -> Document

    Assign synthetic IDs to all elements in a document This ensures every element has a unique ID for layout/AOM integration
    fn extract_stylesheet_links(html : String) -> Array[String]

    Extract stylesheet links from HTML without full DOM parsing This is a lightweight alternative to parse_document for just getting CSS links

    parse

    fn parse(html : String) -> Element?

    Parse HTML string into a document

    parse_document

    fn parse_document(html : String) -> Document

    Parse HTML and extract stylesheets

    parse_document_streaming

    fn parse_document_streaming(html : String, chunk_size : Int) -> Document

    Parse HTML document using streaming

    parse_document_v2

    fn parse_document_v2(html : String) -> Element

    Parse full HTML document using document parsing mode

    parse_fragment

    fn parse_fragment(html : String) -> Element

    Parse HTML with a wrapper div if needed

    parse_fragment_v2

    fn parse_fragment_v2(html : String) -> Element

    Parse HTML fragment using TreeBuilder (HTML5 compliant)