moon_html

Tolerant HTML parsing for real-world pages.

moon_html.html

struct HtmlAttr#

One attribute; duplicate names keep the first occurrence.

name: string

Attribute name, lowercased.

value: string

Attribute value with entities decoded.

struct ElemData#
tag: string

Lowercase tag name, or "" for a text node.

text: string

Text content, for a text node.

attrs: [HtmlAttr]

Attributes in source order; a duplicate name keeps the first.

parent: int

Arena index of the enclosing element.

first_child: int

Arena index of the first child, or -1.

last_child: int

Arena index of the last child, or -1.

next_sibling: int

Arena index of the next sibling, or -1.

struct Doc#

A parsed document: node 0 is the synthetic root.

nodes: [ElemData]

The arena backing the document; index 0 is the synthetic root.

struct HtmlNode#

A handle to one element in a document's arena.

nodes: [ElemData]

The document's arena, shared with every other handle into it.

index: int

Arena index of the element this handle names.

fn decode_entities(text: string) -> string#

Decodes the common named and numeric entities; unknown entities pass through literally.

  • text: Raw markup text to decode.
fn parse(source: string) -> Doc#

Parses source tolerantly; never errors.

  • source: HTML text to parse; malformed markup is recovered rather than rejected.
use moon_html.html

fn main() {
  let doc = html.parse("<main><a class='item' href='/moon'>moon</a></main>")
  let link = doc.select("main > a.item")[0]
  println(link.text())               // moon
  println(link.attr("href").unwrap()) // /moon
}
impl Doc#
fn select(self: Doc, sel: string) -> [HtmlNode]#

All elements matching sel (see selector.mn), in document order.

  • sel: CSS selector to match against the whole document.
fn root(self: Doc) -> HtmlNode#

The document root as a node (its descendants are the whole tree).

impl HtmlNode#
fn select(self: HtmlNode, sel: string) -> [HtmlNode]#

Matching descendants of this node, in document order.

  • sel: CSS selector matched against this node’s descendants.
fn attr(self: HtmlNode, name: string) -> Optional<string>#

The attribute's decoded value, or none.

  • name: Attribute name, matched lowercased.
fn tag(self: HtmlNode) -> string#

The element's lowercase tag name ("" for text nodes).

fn text(self: HtmlNode) -> string#

Concatenated descendant text.

fn html(self: HtmlNode) -> string#

Inner HTML (reserialized from the tree; script/style content and text are emitted as parsed).

moon_html.selector

struct Simple#

One simple predicate inside a compound.

kind: string

What the predicate tests: an id, a class, or an attribute.

name: string

Identifier, class name, or attribute name the predicate names.

value: string

Value the attribute must have, where the predicate carries one.

struct Compound#

A compound plus the combinator that links it to the compound on its right ("" for the rightmost, " " descendant, ">" child).

tag: string

Tag name the compound requires, or "" for any.

simples: [Simple]

The id, class, and attribute predicates that must all hold.

combinator: string

How this compound links to the one on its right: "" rightmost, " " descendant, ">" child.

fn select_from(nodes: [ElemData], root: int, sel: string) -> [HtmlNode]#

All descendants of root matching selector sel, in document order.

  • nodes: The document's node arena.
  • root: Arena index the results must descend from.
  • sel: CSS selector, comma groups included.