MoonBit HTML parser and sanitizer ported from JustHTML.
Dependencies
moon add bobzhang/html_parserimport {
"bobzhang/html_parser"
}///|
test "readme parse fragment example" {
let doc = @html_parser.parse_fragment(
"<p class='intro'>Hello <b>MoonBit</b></p>",
)
assert_eq(
doc.to_html(pretty=false),
"<p class=\"intro\">Hello <b>MoonBit</b></p>",
)
assert_eq(doc.to_text(separator="", strip=false), "Hello MoonBit")
assert_eq(doc.to_markdown(), "Hello **MoonBit**")
assert_eq(doc.query("p.intro").length(), 1)
let tokens = @html_parser.tokenize("<p>Hello</p>").tokens
assert_eq(tokens.length(), 4)
}///|
test "readme parse bytes example" {
let bytes = @utf8.encode("<meta charset=utf-8><p>\u{20AC}</p>")
let doc = @html_parser.parse_bytes(bytes)
guard doc.encoding is Some("utf-8") else { fail("expected utf-8 encoding") }
assert_eq(doc.to_text(separator="", strip=false), "\u{20AC}")
}///|
test "readme sanitize dom example" {
let doc = @html_parser.parse(
"<!DOCTYPE html><!--x--><p onclick=alert(1)>ok</p><script>alert(1)</script>",
sanitize=false,
)
let clean = @html_parser.sanitize_dom(
doc.root,
policy=@html_parser.default_sanitization_policy(),
)
assert_eq(@html_parser.to_html(clean, pretty=false), "<p>ok</p>")
let fragment = @html_parser.parse_fragment(
"<p onclick=alert(1)>ok</p><script>alert(1)</script>",
sanitize=true,
)
assert_eq(fragment.to_html(pretty=false), "<p>ok</p>")
}///|
test "readme CLI reader example" {
let paths : Array[String] = []
let result = @html_parser.run_cli_with_reader(["-", "--format", "text"], fn(
path,
) {
paths.push(path)
@utf8.encode("<p>Hello <b>MoonBit</b></p>")
})
@test.assert_eq(paths, ["-"])
assert_eq(result.exit_code, 0)
assert_eq(result.stdout, "Hello MoonBit\n")
}moon run --target native --release --build-only cmd/mainprintf '<p>Hello <b>MoonBit</b></p>' \
| _build/native/release/build/bobzhang/html_parser/cmd/main/main.exe - --format textmoon run --target native scripts/check_scrut_cli.mbtxmoon run --target native examples/cmd/htmlfmt -- "<article><p>Hello <b>MoonBit</b></p></article>"moon run --target native scripts/check_ci.mbtx --skip-without-credentialsfn parse(html : StringView, sanitize? : Bool, collect_errors? : Bool, strict? : Bool, scripting_enabled? : Bool, xml_coercion? : Bool, track_node_locations? : Bool) -> ParsedHtml raise HtmlErrorfn parse_bytes(input : BytesView, encoding? : String, sanitize? : Bool, collect_errors? : Bool, strict? : Bool, scripting_enabled? : Bool, xml_coercion? : Bool, track_node_locations? : Bool) -> ParsedHtml raise HtmlErrorfn parse_fragment(html : StringView, context? : FragmentContext, sanitize? : Bool, collect_errors? : Bool, strict? : Bool, scripting_enabled? : Bool, xml_coercion? : Bool, track_node_locations? : Bool) -> ParsedHtml raise HtmlErrorfn to_html(node : Node, pretty? : Bool, indent_size? : Int, context? : HtmlContext, quote? : Char) -> String raise HtmlErrorfn to_text(node : Node, separator? : String, strip? : Bool, separator_blocks_only? : Bool) -> StringMoonBit HTML parser and sanitizer ported from JustHTML.
Dependencies