///|
pub(all) enum NodeKind {
Document
Fragment
Element
Text
Comment
Doctype
}///|
test "readme dom constructors" {
let p = @dom.element("p", attrs={ "class": Some("intro") }, children=[
@dom.text("hi"),
])
debug_inspect(
(p.kind(), p.name(), p.attrs(), p.children().length()),
content=(
#|(Element, "p", { "class": Some("intro") }, 1)
),
)
}///|
test "readme dom doctype" {
// The `data` field carries the doctype's declared name (default
// "html"); the `name` field carries the structural label
// "#doctype".
let dt = @dom.doctype()
debug_inspect(
(dt.kind(), dt.name(), dt.data),
content=(
#|(Doctype, "#doctype", "html")
),
)
}///|
test "readme dom append and remove" {
let parent = @dom.element("ul")
let a = @dom.element("li", children=[@dom.text("one")])
let b = @dom.element("li", children=[@dom.text("two")])
parent.append_child(a)
parent.append_child(b)
parent.remove_child(a)
// Use kind/name pairs instead of dumping every node, which would
// include parent back-pointers.
debug_inspect(
parent.children().map(c => (c.kind(), c.name(), c.text())),
content=(
#|[(Element, "li", "two")]
),
)
}///|
test "readme dom text concatenation" {
let node = @dom.element("p", children=[
@dom.text("Hello "),
@dom.element("b", children=[@dom.text("MoonBit")]),
@dom.text("!"),
])
inspect(
node.text(),
content=(
#|Hello MoonBit!
),
)
}///|
test "readme dom clone shallow vs deep" {
let original = @dom.element("p", children=[@dom.text("hi")])
debug_inspect(
(
original.clone_node().children().length(),
original.clone_node(deep=true).children().length(),
),
content=(
#|(0, 1)
),
)
}pub(all) struct Node {
kind : NodeKind
name : String
ns : String?
attrs : Map[String, String?]
data : String
public_id : String?
system_id : String?
force_quirks : Bool
parsed_from_source : Bool
source_start_tag : String?
source_end_tag : String?
sanitize_escape_only : Bool
origin_offset : Int?
origin_line : Int?
origin_col : Int?
parent : Node?
children : Array[Node]
template_contents : Node?
} derive(Debug)Install
Download zipMoonBit HTML parser and sanitizer ported from JustHTML.
Dependencies