html

    A fast, lossless HTML parser with source offsets, CSS selectors, and an optional mutable tree

    html
    parser
    dom
    css
    selector
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    18 hours ago
    Downloads
    12

    No dependencies

    #justjavac/html

    coverage

    Fast, lossless HTML parsing with UTF-16 source offsets, lazy entity decoding, CSS selectors, and an optional mutable tree. Supports native, js, wasm, and wasm-gc.

    #Install

    moon add justjavac/html

    #Quick Start

    ///|
    test "parse and query HTML" {
    let doc = @html.parse(
    "<head><title>Hi &amp; bye</title><link rel=canonical href=\"/\"></head>",
    )
    inspect(doc.find("head > title").unwrap().text(), content="Hi & bye")
    assert_eq(
    doc.find("link[rel=canonical]").unwrap().get_attr("href"),
    Some("/"),
    )
    let title = doc.find("title").unwrap()
    assert_eq(doc.position(title.start()), { line: 1, column: 7, })
    inspect(title.stringify(), content="<title>Hi &amp; bye</title>")
    }

    #Mutable Trees

    Import justjavac/html/mutable for attribute, text, and child edits. Mutable nodes preserve untouched source and rebuild only edited branches.

    This package follows htmlparser2-style recovery, not the WHATWG tree-construction algorithm. Do not use it as an HTML sanitizer.

    Stringify

    pub trait Stringify {
    fn stringify(Self) -> String
    }

    Values that can be serialized back to their exact original HTML source.

    SelectorError

    pub suberror SelectorError {
    InvalidSelector(String)
    }

    A malformed or unsupported CSS selector.

    Comment

    pub struct Comment {
    // private fields
    }

    A normal or bogus comment.
    impl Eq for Comment

    Comment::document

    fn Comment::document(self : Comment) -> Document

    Comment::end

    fn Comment::end(self : Comment) -> Int

    Comment::equal

    #deprecated("Use the Eq trait directly")
    fn Comment::equal(self : Comment, other : Comment) -> Bool

    Comment::not_equal

    #deprecated("Use the Eq trait directly")
    fn Comment::not_equal(x : Comment, y : Comment) -> Bool

    Comment::parent

    fn Comment::parent(self : Comment) -> Parent

    Comment::start

    fn Comment::start(self : Comment) -> Int

    Comment::stringify

    fn Comment::stringify(self : Comment) -> String

    Comment::to_repr

    #deprecated("Use the Debug trait directly")
    fn Comment::to_repr(self : Comment) ->
    Repr

    Comment::value

    fn Comment::value(self : Comment) -> String

    Directive

    pub struct Directive {
    // private fields
    }

    A doctype declaration.
    impl Eq for Directive

    Directive::document

    fn Directive::document(self : Directive) -> Document

    Directive::end

    fn Directive::end(self : Directive) -> Int

    Directive::equal

    #deprecated("Use the Eq trait directly")
    fn Directive::equal(self : Directive, other : Directive) -> Bool

    Directive::not_equal

    #deprecated("Use the Eq trait directly")
    fn Directive::not_equal(x : Directive, y : Directive) -> Bool

    Directive::parent

    fn Directive::parent(self : Directive) -> Parent

    Directive::start

    fn Directive::start(self : Directive) -> Int

    Directive::stringify

    fn Directive::stringify(self : Directive) -> String

    Directive::to_repr

    #deprecated("Use the Debug trait directly")
    fn Directive::to_repr(self : Directive) ->
    Repr

    Directive::value

    fn Directive::value(self : Directive) -> String

    Document

    pub struct Document {
    // private fields
    }

    An HTML document produced by parse.
    impl Eq for Document

    Document::all

    fn Document::all(self : Document, selector : String) -> Array[Element] raise SelectorError

    Find all matching elements in source order.

    Document::all_with

    fn Document::all_with(self : Document, selector : Selector) -> Array[Element]

    Find all elements using a selector compiled by Selector::parse.

    Document::children

    fn Document::children(self : Document) -> Array[Node]

    Top-level nodes in source order.

    Document::end

    fn Document::end(self : Document) -> Int

    Document::equal

    #deprecated("Use the Eq trait directly")
    fn Document::equal(self : Document, other : Document) -> Bool

    Document::find

    fn Document::find(self : Document, selector : String) -> Element? raise SelectorError

    Find the first matching element, or return None.

    Document::find_with

    fn Document::find_with(self : Document, selector : Selector) -> Element?

    Find the first element using a selector compiled by Selector::parse.

    Document::not_equal

    #deprecated("Use the Eq trait directly")
    fn Document::not_equal(x : Document, y : Document) -> Bool

    Document::position

    fn Document::position(self : Document, offset : Int) -> Position

    Convert a UTF-16 source offset to a one-based line and column.

    Document::source

    fn Document::source(self : Document) -> String

    The exact source from which this document was parsed.

    Document::start

    fn Document::start(_self : Document) -> Int

    Document::stringify

    fn Document::stringify(self : Document) -> String

    Document::text

    fn Document::text(self : Document) -> String

    The decoded text of all text nodes in document order.

    Document::to_repr

    #deprecated("Use the Debug trait directly")
    fn Document::to_repr(self : Document) ->
    Repr

    Element

    pub struct Element {
    // private fields
    }

    An element handle. Handles are cheap views into the document's compact tree.
    impl Eq for Element

    Element::all

    fn Element::all(self : Element, selector : String) -> Array[Element] raise SelectorError

    Element::all_with

    fn Element::all_with(self : Element, selector : Selector) -> Array[Element]

    Find all descendants using a precompiled selector.

    Element::attributes

    fn Element::attributes(self : Element) -> Map[String, String]

    Return an independent map of lowercase attribute names and decoded values.

    Element::children

    fn Element::children(self : Element) -> Array[Node]

    Element::closest

    fn Element::closest(self : Element, selector : String) -> Element? raise SelectorError

    Return this element or its nearest matching ancestor.

    Element::closest_with

    fn Element::closest_with(self : Element, selector : Selector) -> Element?

    Return this element or its nearest ancestor matching a precompiled selector.

    Element::document

    fn Element::document(self : Element) -> Document

    Element::end

    fn Element::end(self : Element) -> Int

    Element::equal

    #deprecated("Use the Eq trait directly")
    fn Element::equal(self : Element, other : Element) -> Bool

    Element::find

    fn Element::find(self : Element, selector : String) -> Element? raise SelectorError

    Element::find_with

    fn Element::find_with(self : Element, selector : Selector) -> Element?

    Find the first descendant using a precompiled selector.

    Element::get_attr

    fn Element::get_attr(self : Element, name : String) -> String?

    Read an attribute without distinguishing its ASCII letter case.

    Element::has_attr

    fn Element::has_attr(self : Element, name : String) -> Bool

    Element::html

    fn Element::html(self : Element) -> String

    The element's inner HTML exactly as written.

    Element::matches

    fn Element::matches(self : Element, selector : String) -> Bool raise SelectorError

    Element::matches_with

    fn Element::matches_with(self : Element, selector : Selector) -> Bool

    Match this element against a precompiled selector.

    Element::name

    fn Element::name(self : Element) -> String

    Element::not_equal

    #deprecated("Use the Eq trait directly")
    fn Element::not_equal(x : Element, y : Element) -> Bool

    Element::parent

    fn Element::parent(self : Element) -> Parent

    Element::start

    fn Element::start(self : Element) -> Int

    Element::stringify

    fn Element::stringify(self : Element) -> String

    Element::text

    fn Element::text(self : Element) -> String

    The decoded text of all descendant text nodes.

    Element::to_repr

    #deprecated("Use the Debug trait directly")
    fn Element::to_repr(self : Element) ->
    Repr

    Node

    pub(all) enum Node {
    Element(Element)
    Text(Text)
    Comment(Comment)
    Directive(Directive)
    } derive(Eq,
    Debug
    )

    A child node in an HTML document.
    impl Stringify for Node

    Node::document

    fn Node::document(self : Node) -> Document

    Node::end

    fn Node::end(self : Node) -> Int

    Node::equal

    #deprecated("Use derived trait implementations directly")
    fn Node::equal(Node, Node) -> Bool

    Node::not_equal

    #deprecated("Use derived trait implementations directly")
    fn Node::not_equal(x : Node, y : Node) -> Bool

    Node::parent

    fn Node::parent(self : Node) -> Parent

    Node::start

    fn Node::start(self : Node) -> Int

    Node::stringify

    fn Node::stringify(self : Node) -> String

    Node::to_repr

    #deprecated("Use derived trait implementations directly")
    fn Node::to_repr(Node) ->
    Repr

    Parent

    pub(all) enum Parent {
    Document(Document)
    Element(Element)
    } derive(Eq,
    Debug
    )

    The parent of a top-level or nested node.

    Parent::equal

    #deprecated("Use derived trait implementations directly")
    fn Parent::equal(Parent, Parent) -> Bool

    Parent::not_equal

    #deprecated("Use derived trait implementations directly")
    fn Parent::not_equal(x : Parent, y : Parent) -> Bool

    Parent::to_repr

    #deprecated("Use derived trait implementations directly")
    fn Parent::to_repr(Parent) ->
    Repr

    Position

    pub(all) struct Position {
    line : Int
    column : Int
    } derive(Eq,
    Debug
    )

    A one-based line and column in the original UTF-16 source.

    Position::equal

    #deprecated("Use derived trait implementations directly")
    fn Position::equal(Position, Position) -> Bool

    Position::not_equal

    #deprecated("Use derived trait implementations directly")
    fn Position::not_equal(x : Position, y : Position) -> Bool

    Position::to_repr

    #deprecated("Use derived trait implementations directly")
    fn Position::to_repr(Position) ->
    Repr

    Selector

    pub struct Selector {
    // private fields
    }

    A compiled CSS selector list.

    Selector::parse

    fn Selector::parse(input : String) -> Selector raise SelectorError

    Compile a selector once for repeated matching.

    Text

    pub struct Text {
    // private fields
    }

    A text node, including foreign-content CDATA sections.
    impl Stringify for Text
    impl Eq for Text

    Text::document

    fn Text::document(self : Text) -> Document

    Text::end

    fn Text::end(self : Text) -> Int

    Text::equal

    #deprecated("Use the Eq trait directly")
    fn Text::equal(self : Text, other : Text) -> Bool

    Text::not_equal

    #deprecated("Use the Eq trait directly")
    fn Text::not_equal(x : Text, y : Text) -> Bool

    Text::parent

    fn Text::parent(self : Text) -> Parent

    Text::start

    fn Text::start(self : Text) -> Int

    Text::stringify

    fn Text::stringify(self : Text) -> String

    Text::to_repr

    #deprecated("Use the Debug trait directly")
    fn Text::to_repr(self : Text) ->
    Repr

    Text::value

    fn Text::value(self : Text) -> String

    parse

    fn parse(source : String) -> Document

    Parse HTML without raising on malformed markup. Recovery follows htmlparser2, not the WHATWG browser tree-construction algorithm.

    stringify

    fn[T : Stringify] stringify(target : T) -> String