The fastest MoonBit HTML parser: ~240 µs for a 157 KB page on native, 20–440x faster than other MoonBit parsers. Lossless, with source offsets, CSS selectors, and an optional mutable tree
moon add justjavac/html///|
test "parse and query HTML" {
let doc = @html.parse(
"<head><title>Hi & 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 & bye</title>")
}pub trait Stringify {
fn stringify(Self) -> String
}pub suberror SelectorError {
InvalidSelector(String)
}pub struct Comment {
// private fields
}pub struct Directive {
// private fields
}pub struct Document {
// private fields
}let doc = @html.parse("<p>Hello & goodbye</p>")
doc.find("p").unwrap().text() // "Hello & goodbye"doc.find("main > p.intro")pub struct Element {
// private fields
}pub struct Selector {
// private fields
}let content = @html.Selector::parse("div.content").unwrap()
doc.find_with(content) // like doc.find("div.content"), without re-parsingpub struct Text {
// private fields
}let doc = @html.parse("<ul><li>one<li>two</ul>")
doc.all("li").length() // 2Install
Download zipThe fastest MoonBit HTML parser: ~240 µs for a 157 KB page on native, 20–440x faster than other MoonBit parsers. Lossless, with source offsets, CSS selectors, and an optional mutable tree