WHATWG HTML5 spec-compliant parser for MoonBit
Dependencies
///|
test "basic parsing" {
let doc = @html5.parse(
"<html><head><title>Hello</title></head><body><p>World</p></body></html>",
)
inspect(
doc.dump(),
content=(
#|<html>
#| <head>
#| <title>
#| "Hello"
#| <body>
#| <p>
#| "World"
),
)
}///|
test "error recovery - unclosed tags" {
let doc = @html5.parse("<p>First<p>Second<p>Third")
inspect(
doc.dump(),
content=(
#|<html>
#| <head>
#| <body>
#| <p>
#| "First"
#| <p>
#| "Second"
#| <p>
#| "Third"
),
)
}///|
test "error recovery - misnested tags" {
let doc = @html5.parse("<b><i>Bold and Italic</b> Just Italic</i>")
inspect(
doc.dump(),
content=(
#|<html>
#| <head>
#| <body>
#| <b>
#| <i>
#| "Bold and Italic"
#| <i>
#| " Just Italic"
),
)
}///|
test "serialize to html" {
let doc = @html5.parse("<div class=\"container\"><span>Hello</span></div>")
inspect(
doc.to_html(),
content="<html><head></head><body><div class=\"container\"><span>Hello</span></div></body></html>",
)
}///|
test "parse with errors" {
let (doc, errors) = @html5.parse_with_errors("<p>Test</p attr>")
inspect(errors.length() > 0, content="true")
inspect(
doc.dump(),
content=(
#|<html>
#| <head>
#| <body>
#| <p>
#| "Test"
),
)
}///|
test "tokenization" {
let (tokens, _errors) = @html5.tokenize("<div>Hello</div>")
inspect(
tokens[0],
content="StartTag(name=\"div\", attrs=[], self_closing=false)",
)
inspect(tokens[1], content="Character('H')")
inspect(tokens[2], content="Character('e')")
}///|
test "dom access" {
let doc = @html5.parse("<div id=\"main\"><p class=\"text\">Content</p></div>")
// Get body element
let body_id = doc.body_element
let children = doc.get_children(body_id)
// Get the div
let div_id = children[0]
inspect(doc.get_tag_name(div_id), content="Some(\"div\")")
inspect(doc.get_attribute(div_id, "id"), content="Some(\"main\")")
// Get the p element
let p_id = doc.get_children(div_id)[0]
inspect(doc.get_attribute(p_id, "class"), content="Some(\"text\")")
inspect(doc.get_text_content(p_id), content="Content")
}///|
test "character references" {
let doc = @html5.parse("<p>& < > © © ©</p>")
let p_id = doc.get_children(doc.body_element)[0]
inspect(
doc.get_text_content(p_id),
content="& < > \u{00A9} \u{00A9} \u{00A9}",
)
}///|
test "svg support" {
let doc = @html5.parse(
"<div><svg><circle cx=\"50\" cy=\"50\" r=\"40\"/></svg></div>",
)
inspect(
doc.dump(),
content=(
#|<html>
#| <head>
#| <body>
#| <div>
#| <svg svg>
#| <svg circle>
#| cx="50"
#| cy="50"
#| r="40"
),
)
}WHATWG HTML5 spec-compliant parser for MoonBit
Dependencies