#@dom

    The DOM data type and its constructors. Every node the parser produces — and every node the serializer consumes — is a @dom.Node.

    The examples below are mbt check blocks and run as part of moon test dom.

    #Node kinds

    ///|
    pub(all) enum NodeKind {
    Document
    Fragment
    Element
    Text
    Comment
    Doctype
    }

    #Constructors

    Each kind has its own constructor; they all return a Node.

    ///|
    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")
    ),
    )
    }

    #Tree mutation

    Mutation is in-place: append_child, insert_before, remove_child, and replace_child all update the parent links and the children array.

    ///|
    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")]
    ),
    )
    }

    #Walking the tree

    ///|
    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!
    ),
    )
    }

    #Cloning

    clone_node() makes a shallow copy by default — children are dropped. deep=true mirrors the entire subtree.

    ///|
    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)
    ),
    )
    }

    Node

    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
    )

    DOM node used for documents, fragments, elements, text, comments, and doctypes.

    Nodes are created with helpers such as document, fragment, element, text, comment, and doctype.

    Node::append_child

    fn Node::append_child(self : Node, child : Node) -> Unit

    Append child to this node.

    The child is detached from any existing parent first. Non-container nodes and cycle-producing appends are ignored.

    Node::attrs

    fn Node::attrs(self : Node) -> Map[String, String?]

    Return a copy of this node's attributes.

    Node::children

    fn Node::children(self : Node) -> Array[Node]

    Return a copy of this node's child list.

    The returned array is detached from the node, but the child nodes themselves are the same node objects.

    Node::clone_node

    fn Node::clone_node(self : Node, deep? : Bool, override_attrs? : Map[String, String?]) -> Node

    Clone this node.

    deep=true recursively clones descendants. override_attrs replaces the cloned node's attributes, which is useful for transform operations.

    Node::data

    fn Node::data(self : Node) -> String

    Return this text, comment, or doctype node's data payload.

    Node::flatten_template_contents

    fn Node::flatten_template_contents(self : Node) -> Unit

    Move an HTML template's contents back into its regular child list, for operations that hoist or escape an element's children (sanitizer unwrap and escape handling, transform unwrap). Bypasses append_child, which would redirect into the contents fragment again.

    Node::has_child_nodes

    fn Node::has_child_nodes(self : Node) -> Bool

    Test whether this node has any child nodes.

    Node::insert_before

    fn Node::insert_before(self : Node, child : Node, before : Node?) -> Unit

    Insert child before before, or append when before is absent.

    If before is not a current child, the child is appended. Invalid insertions are ignored in the same way as append_child.

    Node::kind

    fn Node::kind(self : Node) -> NodeKind

    Return this node's kind.

    Node::name

    fn Node::name(self : Node) -> String

    Return this node's name.

    Node::namespace_uri

    fn Node::namespace_uri(self : Node) -> String?

    Return this element's namespace URI, if any.

    Node::origin_col

    fn Node::origin_col(self : Node) -> Int?

    Return this node's original 1-based source column, if known.

    Node::origin_line

    fn Node::origin_line(self : Node) -> Int?

    Return this node's original 1-based source line, if known.

    Node::origin_location

    fn Node::origin_location(self : Node) -> (Int, Int)?

    Return this node's original (line, column) source location, if known.

    Node::origin_offset

    fn Node::origin_offset(self : Node) -> Int?

    Return this node's original source offset, if known.

    Node::parent

    fn Node::parent(self : Node) -> Node?

    Return this node's parent, if any.

    Node::remove_child

    fn Node::remove_child(self : Node, child : Node) -> Unit

    Remove child from this node if it is a current child.

    Node::replace_child

    fn Node::replace_child(self : Node, new_child : Node, old_child : Node) -> Node?

    Replace old_child with new_child.

    Returns the removed child when replacement succeeds, or None when old_child is not a current child or the replacement would create a cycle.

    Node::template_content

    fn Node::template_content(self : Node) -> Node?

    An HTML template element's contents document fragment, when the template has one. Template children live in this fragment rather than in the regular child list, mirroring the HTML template contents model.

    Node::text

    fn Node::text(self : Node) -> String

    Return this node's descendant text with no separator and no trimming.

    Node::to_repr

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn Node::to_repr(Node) ->
    Repr

    NodeKind

    pub(all) enum NodeKind {
    Document
    Fragment
    Element
    Text
    Comment
    Doctype
    } derive(Eq,
    Debug
    )

    Kind of DOM node represented by Node.

    NodeKind::equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn NodeKind::equal(NodeKind, NodeKind) -> Bool

    NodeKind::not_equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn NodeKind::not_equal(x : NodeKind, y : NodeKind) -> Bool

    NodeKind::to_repr

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn NodeKind::to_repr(NodeKind) ->
    Repr

    comment

    fn comment(data : StringView) -> Node

    Create a comment node.

    doctype

    fn doctype(name? : String, public_id? : String, system_id? : String, force_quirks? : Bool) -> Node

    Create a doctype node.

    document

    fn document(children? : Array[Node]) -> Node

    Create a document node with optional children.

    element

    fn element(name : StringView, attrs? : Map[String, String?], children? : Array[Node], ns? : String) -> Node

    Create an element node.

    Namespace aliases html, svg, and mathml are normalized for serializer and sanitizer behavior. Child nodes are attached in order.

    fragment

    fn fragment(children? : Array[Node]) -> Node

    Create a document-fragment node with optional children.

    text

    fn text(data : StringView) -> Node

    Create a text node.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    Ā© 2026 mooncakes.io