Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / node_4.lucb

#==============================================================================================
#
#   node_4 - DOM::Node: cloning a single node, node documents, editing, dirty flags
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Node.cpp (lines 1357-1885): cloning a single node of each node
#       type, cloneNode(), adopting into another node document, the editable and editing-host
#       predicates, the layout-node and paintable links, get-the-parent, the layout-tree and
#       style dirty flags, the insertion/removing/moving steps, the parent-or-host and
#       flat-tree parents, childNodes and removing all children.
#
#==============================================================================================

## AK/Debug.h UPDATE_LAYOUT_DEBUG (0 in the reference build).
let update_layout_debug: bool = false

# mark: Cloning a single node ===================================================================

## The captures of Node::clone_single_node's for_each_attribute lambda.
struct DomNodeCloneSingleNodeCloneAttribute:
    var document: DomDocument*
    var element_copy: DomElement*
    ## The last attribute's exception: its code, and its message copied into `exception_room`,
    ## since its handler gives the original back when it finishes (luce-base base.md §11.3).
    var maybe_exception: ErrorCode?
    var exception_room: u8[1024]
    var exception_message: str

## Node::clone_single_node's for_each_attribute lambda: clone one attribute into the copy.
func dom_node_clone_single_node_clone_attribute(context: void*?, attr: DomAttr*):
    let captures = (DomNodeCloneSingleNodeCloneAttribute*)(context else trap("no captures"))

    # 1. Let copyAttribute be the result of cloning a single node given attribute, document, and null.
    let copy_attribute = dom_node_clone_single_node(attr.dom_node(), captures.document, none) catch failure:
        captures.maybe_exception = failure.code
        captures.exception_message = copy_into(captures.exception_room, failure.message)
        return

    # 2. Append copyAttribute to copy.
    dom_element_append_attribute_dom_attr(captures.element_copy, as_dom_attr(copy_attribute))

## Ported from Node.cpp:1358 Web::DOM::Node::clone_single_node.
# https://dom.spec.whatwg.org/#clone-a-single-node
pub func dom_node_clone_single_node(this: DomNode*, document: DomDocument*, fallback_registry: HtmlCustomElementRegistry*?) -> DomNode*!:
    # To clone a single node given a node node, document document, and null or a CustomElementRegistry object fallbackRegistry:
    let realm = bind_platform_object_realm(this.bind_platform_object())

    # 1. Let copy be null.
    var copy: DomNode*? = none

    # 2. If node is an element:
    if let element = as_if_dom_element(this):
        # 1. Let registry be node’s custom element registry.
        var registry = dom_element_custom_element_registry(element)

        # 2. If registry is null, then set registry to fallbackRegistry.
        if registry == none:
            registry = fallback_registry

        # 3. If registry is a global custom element registry, then set registry to document’s effective global custom
        #    element registry.
        if html_is_a_global_custom_element_registry(registry):
            registry = dom_document_effective_global_custom_element_registry(document)

        # 4. Set copy to the result of creating an element, given document, node’s local name, node’s namespace,
        #    node’s namespace prefix, node’s is value, false, and registry.
        let element_copy = try dom_create_element(document, dom_element_local_name(element), dom_element_namespace_uri(element), dom_element_prefix(element), dom_element_is_value(element), false, .html_custom_element_registry(registry))

        # 5. For each attribute of node’s attribute list:
        var clone_attribute = DomNodeCloneSingleNodeCloneAttribute(document = document, element_copy = element_copy)
        dom_element_for_each_attribute_function1(element, ak.Function1[DomAttr*, unit](call = dom_node_clone_single_node_clone_attribute, context = &clone_attribute))

        if let code = clone_attribute.maybe_exception:
            error(code, clone_attribute.exception_message)

        copy = element_copy.dom_node()

    # 3. Otherwise, set copy to a node that implements the same interfaces as node, and fulfills these additional
    #    requirements, switching on the interface node implements:
    else:
        if dom_node_is_document(this):
            # -> Document
            let document_ = as_dom_document(this)
            let document_copy = match dom_document_document_type(document_):
                .xml => dom_xml_document_create(realm, dom_document_url(document_)).dom_document()
                .html => html_document_create(realm, dom_document_url(document_)).dom_document()
                _ => dom_document_create(realm, dom_document_url(document_))

            # 1. Set copy’s encoding, content type, URL, origin, type, mode, and allow declarative shadow roots to
            #    those of node.
            dom_document_set_encoding(document_copy, dom_document_encoding(document_))
            dom_document_set_content_type(document_copy, dom_document_content_type(document_))
            dom_document_set_url(document_copy, dom_document_url(document_))
            dom_document_set_origin(document_copy, dom_document_origin(document_))
            dom_document_set_document_type(document_copy, dom_document_document_type(document_))
            dom_document_set_quirks_mode(document_copy, dom_document_mode(document_))
            dom_document_set_allow_declarative_shadow_roots(document_copy, dom_document_allow_declarative_shadow_roots(document_))

            # 2. If node’s custom element registry’s is scoped is true, then set copy’s custom element registry to
            #    node’s custom element registry.
            if let registry = dom_document_custom_element_registry(document_):
                if html_custom_element_registry_is_scoped(registry):
                    dom_document_set_custom_element_registry(document_copy, registry)

            copy = document_copy.dom_node()
        elif dom_node_is_document_type(this):
            # -> DocumentType
            let document_type = as_dom_document_type(this)
            let document_type_copy = realm_create_dom_document_type(realm, document)

            # Set copy’s name, public ID, and system ID to those of node.
            dom_document_type_set_name(document_type_copy, dom_document_type_name(document_type))
            dom_document_type_set_public_id(document_type_copy, dom_document_type_public_id(document_type))
            dom_document_type_set_system_id(document_type_copy, dom_document_type_system_id(document_type))
            copy = document_type_copy.dom_node()
        elif dom_node_is_attribute(this):
            # -> Attr
            # Set copy’s namespace, namespace prefix, local name, and value to those of node.
            let attr = as_dom_attr(this)
            copy = dom_attr_clone(attr, document).dom_node()
        elif dom_node_is_text(this):
            # -> Text
            let text = as_dom_text(this)

            # Set copy’s data to that of node.
            if dom_node_type(this) == DomNodeType.text_node:
                copy = realm_create_dom_text_utf16_string(realm, document, dom_character_data_data(text.dom_character_data())).dom_node()
            elif dom_node_type(this) == DomNodeType.cdata_section_node:
                copy = realm_create_dom_cdata_section(realm, document, dom_character_data_data(text.dom_character_data())).dom_node()
            else:
                trap("VERIFY_NOT_REACHED")
        elif dom_node_is_comment(this):
            # -> Comment
            let comment = as_dom_comment(this)

            # Set copy’s data to that of node.
            let comment_copy = realm_create_dom_comment(realm, document, dom_character_data_data(comment.dom_character_data()))
            copy = comment_copy.dom_node()
        elif is_dom_processing_instruction(this):
            # -> ProcessingInstruction
            let processing_instruction = as_dom_processing_instruction(this)

            # Set copy’s target and data to those of node.
            let processing_instruction_copy = realm_create_dom_processing_instruction(realm, document, dom_character_data_data(processing_instruction.dom_character_data()), dom_processing_instruction_target(processing_instruction))
            copy = processing_instruction_copy.dom_node()
        # -> Otherwise
        #    Do nothing.
        elif is_dom_document_fragment(this):
            copy = realm_create_dom_document_fragment(realm, document).dom_node()
        else:
            ak.dbgln(f"Missing code for cloning a '{ak.string_view_text(gc.cell_class_name(this.cell()))}' node. Please add it to Node::clone_single_node()")
            trap("VERIFY_NOT_REACHED")

    # 4. Assert: copy is a node.
    let copy_node = copy else trap("VERIFY: copy")

    # 5. If node is a document, then set document to copy.
    let document_to_use = (DomDocument*)copy_node if dom_node_is_document(this) else document

    # 6. Set copy’s node document to document.
    dom_node_set_document_dom_document(copy_node, document_to_use)

    # 7. Return copy.
    return copy_node

## Ported from Node.cpp:1509 Web::DOM::Node::clone_node_binding.
# https://dom.spec.whatwg.org/#dom-node-clonenode
pub func dom_node_clone_node_binding(this: DomNode*, subtree: bool) -> DomNode*!:
    # 1. If this is a shadow root, then throw a "NotSupportedError" DOMException.
    if is_dom_shadow_root(this):
        return try idl_throw(.idl_dom_exception(idl_not_supported_error_create(bind_platform_object_realm(this.bind_platform_object()), ak.utf16_string_from_str("Cannot clone shadow root"))))

    # 2. Return the result of cloning a node given this with subtree set to subtree.
    return try dom_node_clone_node(this, none, subtree)

# mark: Node documents ==========================================================================

## Ported from Node.cpp:1519 Web::DOM::Node::set_document(Badge<Document>, Document&).
pub func dom_node_set_document_document(this: DomNode*, document: DomDocument*):
    dom_node_set_document_dom_document(this, document)

## Ported from Node.cpp:1524 Web::DOM::Node::set_document(Badge<NamedNodeMap>, Document&).
pub func dom_node_set_document_named_node_map(this: DomNode*, document: DomDocument*):
    dom_node_set_document_dom_document(this, document)

## Ported from Node.cpp:1529 Web::DOM::Node::set_document(Document&).
pub func dom_node_set_document_dom_document(this: DomNode*, document: DomDocument*):
    if this.m_document == document:
        return

    let node_needs_style_update = dom_node_needs_style_update(this)
    let subtree_needs_style_update = dom_node_entire_subtree_needs_style_update(this)
    let descendants_need_style_update = dom_node_child_needs_style_update(this)
    this.m_document = document
    if node_needs_style_update:
        # NOTE: We unset and reset the "needs style update" flag here.
        #       This ensures that there's a pending style update in the new document
        #       that will eventually assign some style to this node if needed.
        dom_node_set_needs_style_update(this, false)
        dom_node_set_needs_style_update(this, true)

    if subtree_needs_style_update or descendants_need_style_update:
        # These broader dirty flags stay set across adoption, but the new ancestor chain has never seen them
        # propagate. Re-mark ancestors so style update still descends into this adopted subtree.
        var ancestor = dom_node_parent_or_shadow_host(this)
        while let parent_node = ancestor:
            let ancestor_node = parent_node.dom_node()
            if ancestor_node.m_child_needs_style_update:
                break
            ancestor_node.m_child_needs_style_update = true
            ancestor = dom_node_parent_or_shadow_host(ancestor_node)

# mark: Editing =================================================================================

## `parent() && parent()->m_in_editable_subtree`.
func dom_node_parent_is_in_editable_subtree(this: DomNode*) -> bool:
    let parent = tree_node_parent(this) else return false
    return parent.m_in_editable_subtree

## Ported from Node.cpp:1557 Web::DOM::Node::recompute_editable_subtree_flag.
pub func dom_node_recompute_editable_subtree_flag(this: DomNode*):
    var new_value = false
    if dom_node_is_document(this):
        new_value = dom_document_design_mode_enabled_state(as_dom_document(this))
    elif let html_element = as_if_html_element(this):
        let state = html_element_content_editable_state(html_element)
        if state == HtmlContentEditableState.true_ or state == HtmlContentEditableState.plaintext_only:
            new_value = true
        elif state == HtmlContentEditableState.false_:
            new_value = false
        else:
            new_value = dom_node_parent_is_in_editable_subtree(this)
    else:
        new_value = dom_node_parent_is_in_editable_subtree(this)
    this.m_in_editable_subtree = new_value

## Ported from Node.cpp:1577 Web::DOM::Node::is_editable.
# https://w3c.github.io/editing/docs/execCommand/#editable
pub func dom_node_is_editable(this: DomNode*) -> bool:
    # Something is editable if it is a node; it is not an editing host;
    if dom_node_is_editing_host(this):
        return false

    # it does not have a contenteditable attribute set to the false state;
    let html_element = as_if_html_element(this)
    if let element = html_element:
        if html_element_content_editable_state(element) == HtmlContentEditableState.false_:
            return false

    # its parent is an editing host or editable;
    if not dom_node_parent_is_in_editable_subtree(this):
        return false

    # https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees
    # When a node is inert:
    # - If it is editable, the node behaves as if it were non-editable.
    if dom_node_is_inert(this):
        return false

    # and either it is an HTML element,
    if html_element != none:
        return true

    # or it is an svg or math element,
    if is_svg_element(this) or is_math_ml_math_ml_element(this):
        return true

    # or it is not an Element and its parent is an HTML element.
    let parent = tree_node_parent(this) else return false
    return not is_dom_element(this) and is_html_element(parent)

## Ported from Node.cpp:1611 Web::DOM::Node::is_editing_host.
# https://html.spec.whatwg.org/multipage/interaction.html#editing-host
pub func dom_node_is_editing_host(this: DomNode*) -> bool:
    # NOTE: Both conditions below require this to be an HTML element.
    let html_element = as_if_html_element(this) else return false

    # An editing host is either an HTML element with its contenteditable attribute in the true state or
    # plaintext-only state,
    # AD-HOC: Only return true here if this node is not the child of another editable node or an editing host,
    #         effectively merging this potential editing host with its editing host ancestor. This causes a call to
    #         `::editing_host()` to automatically traverse to the top-most editing host.
    let state = html_element_content_editable_state(html_element)
    if (state == HtmlContentEditableState.true_ or state == HtmlContentEditableState.plaintext_only) and not dom_node_parent_is_in_editable_subtree(this):
        return true

    # or a child HTML element of a Document whose design mode enabled is true.
    let parent = tree_node_parent(this) else return false
    let doc = as_if_dom_document(parent) else return false
    return dom_document_design_mode_enabled_state(doc)

## Ported from Node.cpp:1635 Web::DOM::Node::editing_host.
# https://w3c.github.io/editing/docs/execCommand/#editing-host-of
pub func dom_node_editing_host(this: DomNode*) -> DomNode*?:
    # node itself, if node is an editing host;
    if dom_node_is_editing_host(this):
        return this

    # or the nearest ancestor of node that is an editing host, if node is editable.
    if dom_node_is_editable(this):
        var result: DomNode*? = none
        # for_each_ancestor
        var ancestor = tree_node_parent(this)
        while let ancestor_node = ancestor:
            if dom_node_is_editing_host(ancestor_node):
                result = ancestor_node
                break
            ancestor = tree_node_parent(ancestor_node)
        assert(result != none)
        return result

    # The editing host of node is null if node is neither editable nor an editing host;
    return none

# mark: Layout node and paintable links =========================================================

## Ported from Node.cpp:1659 Web::DOM::Node::set_layout_node.
pub func dom_node_set_layout_node(this: DomNode*, layout_node: LayoutNode*):
    this.m_layout_node = layout_node

## Ported from Node.cpp:1664 Web::DOM::Node::clear_layout_node_and_paintable.
pub func dom_node_clear_layout_node_and_paintable(this: DomNode*):
    this.m_layout_node = none
    this.m_paintable = none

## Ported from Node.cpp:1670 Web::DOM::Node::detach_layout_node.
pub func dom_node_detach_layout_node(this: DomNode*):
    this.m_layout_node = none

## Ported from Node.cpp:1675 Web::DOM::Node::get_parent (override).
pub func dom_node_get_parent_impl(base: DomEventTarget*, arg0: DomEvent*) -> DomEventTarget*?:
    let this = (DomNode*)base
    # A node’s get the parent algorithm, given an event, returns the node’s assigned slot, if node is assigned;
    # otherwise node’s parent.
    if let assigned_slot = dom_assigned_slot_for_node(this):
        return assigned_slot.dom_event_target()

    let parent = tree_node_parent(this) else return none
    return parent.dom_event_target()

# mark: Layout-tree and style dirty flags =======================================================

## Ported from Node.cpp:1685 Web::DOM::Node::set_needs_layout_tree_update.
pub func dom_node_set_needs_layout_tree_update(this: DomNode*, value: bool, reason: DomSetNeedsLayoutTreeUpdateReason):
    if this.m_needs_layout_tree_update == value:
        return
    this.m_needs_layout_tree_update = value

    if update_layout_debug:
        if this.m_needs_layout_tree_update:
            # NOTE: We check some conditions here to avoid debug spam in documents that don't do layout.
            let navigable = dom_node_navigable(this)
            var any_ancestor_needs_layout_tree_update = false
            var ancestor = dom_node_flat_tree_parent(this)
            while let parent_node = ancestor:
                if dom_node_needs_layout_tree_update(parent_node.dom_node()):
                    any_ancestor_needs_layout_tree_update = true
                    break
                ancestor = dom_node_flat_tree_parent(parent_node.dom_node())
            if let active_navigable = navigable:
                if not any_ancestor_needs_layout_tree_update and html_navigable_active_document(active_navigable) == dom_node_document(this):
                    let description = dom_node_debug_description(this)
                    ak.dbgln(f"Need tree update ({ak.string_view_text(dom_to_string_dom_set_needs_layout_tree_upda(reason))}): {ak.string_view_text(ak.string_bytes_as_string_view(&description))}")

    # NOTE: If this is a shadow root, we need to propagate the layout tree update to the host.
    if dom_node_is_shadow_root(this):
        let shadow_root = (DomShadowRoot*)this
        if let host = dom_document_fragment_host(shadow_root.dom_document_fragment()):
            dom_node_set_needs_layout_tree_update(host.dom_node(), value, reason)

    if this.m_needs_layout_tree_update:
        dom_document_set_needs_repaint_t[DomNode](dom_node_document(this), InvalidateDisplayList.no)

        var ancestor = dom_node_flat_tree_parent(this)
        while let parent_node = ancestor:
            let ancestor_node = parent_node.dom_node()
            if ancestor_node.m_child_needs_layout_tree_update:
                break
            ancestor_node.m_child_needs_layout_tree_update = true
            ancestor = dom_node_flat_tree_parent(ancestor_node)

        # If this is an element with display: contents, we need to propagate the layout tree update to the parent.
        if let element = as_if_dom_element(this):
            if let computed_properties = dom_element_computed_properties(element):
                let display = css_computed_properties_display(computed_properties)
                if css_display_is_contents(&display):
                    if let parent_element = dom_node_parent_or_shadow_host_element(element.dom_node()):
                        dom_node_set_needs_layout_tree_update(parent_element.dom_node(), true, reason)

        # NB: Propagating layout invalidation, layout is not up to date.
        if let layout_node = dom_node_unsafe_layout_node(this):
            layout_node_set_needs_layout_update(layout_node, DomSetNeedsLayoutReason.layout_tree_update)

            # If the layout node has an anonymous parent, rebuild from the nearest non-anonymous ancestor.
            # FIXME: This is not optimal, and we should figure out how to rebuild a smaller part of the tree.
            if let layout_parent = layout_node_parent(layout_node):
                if layout_node_is_anonymous(layout_parent.layout_node()):
                    var layout_ancestor: LayoutNode*? = layout_parent.layout_node()
                    while let anonymous = layout_ancestor:
                        if not layout_node_is_anonymous(anonymous):
                            break
                        layout_ancestor = none
                        if let next = layout_node_parent(anonymous):
                            layout_ancestor = next.layout_node()
                    if let non_anonymous = layout_ancestor:
                        let dom_node = layout_node_dom_node(non_anonymous) else trap("anonymous-free layout node without a DOM node")
                        dom_node_set_needs_layout_tree_update(dom_node, true, reason)

## Ported from Node.cpp:1750 Web::DOM::Node::set_needs_style_update.
pub func dom_node_set_needs_style_update(this: DomNode*, value: bool):
    if this.m_needs_style_update == value:
        return
    this.m_needs_style_update = value

    if this.m_needs_style_update:
        dom_document_set_needs_repaint_t[DomNode](dom_node_document(this), InvalidateDisplayList.no)

        dom_document_record_style_invalidation(dom_node_document(this))
        var ancestor = dom_node_parent_or_shadow_host(this)
        while let parent_node = ancestor:
            let ancestor_node = parent_node.dom_node()
            if ancestor_node.m_child_needs_style_update:
                break
            ancestor_node.m_child_needs_style_update = true
            ancestor = dom_node_parent_or_shadow_host(ancestor_node)

# mark: Insertion, removing and moving steps ====================================================

## Ported from Node.cpp:1768 Web::DOM::Node::post_connection (virtual).
pub func dom_node_post_connection_impl(this: DomNode*):
    return

## Ported from Node.cpp:1772 Web::DOM::Node::inserted (virtual).
pub func dom_node_inserted_impl(this: DomNode*):
    # NB: The DOM insertion steps visit shadow-including inclusive descendants in tree order,
    #     so by the time we get here, our parent (or host, for shadow roots) has already had
    #     its connected flag updated, and we can just inherit from it.
    if let shadow_root = as_if_dom_shadow_root(this):
        var host_is_connected = false
        if let host = dom_document_fragment_host(shadow_root.dom_document_fragment()):
            host_is_connected = dom_node_is_connected(host.dom_node())
        this.m_is_connected = host_is_connected
    elif let parent = tree_node_parent(this):
        this.m_is_connected = dom_node_is_connected(parent)

    dom_node_recompute_editable_subtree_flag(this)
    dom_node_set_needs_style_update(this, true)

## Ported from Node.cpp:1786 Web::DOM::Node::removed_from (virtual).
pub func dom_node_removed_from_impl(this: DomNode*, arg0: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    this.m_is_connected = false
    this.m_in_editable_subtree = false
    this.m_layout_node = none
    this.m_paintable = none

## Ported from Node.cpp:1795 Web::DOM::Node::moved_from (virtual).
# https://dom.spec.whatwg.org/#concept-node-move-ext
pub func dom_node_moved_from_impl(this: DomNode*, arg0: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?):
    dom_node_recompute_editable_subtree_flag(this)

# mark: Parents, hosts and the flat tree ========================================================

## Ported from Node.cpp:1800 Web::DOM::Node::parent_or_shadow_host.
pub func dom_node_parent_or_shadow_host(this: DomNode*) -> DomParentNode*?:
    if is_dom_shadow_root(this):
        let host = dom_document_fragment_host(((DomShadowRoot*)this).dom_document_fragment()) else return none
        return host.dom_parent_node()
    let parent = tree_node_parent(this) else return none
    return as_dom_parent_node(parent)

## Ported from Node.cpp:1807 Web::DOM::Node::parent_or_shadow_host_element.
pub func dom_node_parent_or_shadow_host_element(this: DomNode*) -> DomElement*?:
    if is_dom_shadow_root(this):
        return dom_document_fragment_host(((DomShadowRoot*)this).dom_document_fragment())
    let parent = tree_node_parent(this) else return none
    if is_dom_element(parent):
        return (DomElement*)parent
    if is_dom_shadow_root(parent):
        return dom_document_fragment_host(((DomShadowRoot*)parent).dom_document_fragment())
    return none

## Ported from Node.cpp:1820 Web::DOM::Node::flat_tree_parent.
pub func dom_node_flat_tree_parent(this: DomNode*) -> DomParentNode*?:
    # If we're assigned to a slot, that slot is our flat tree parent.
    if dom_node_is_slottable(this):
        let slottable = match dom_node_as_slottable(this):
            .dom_element(element) => &element.slottable_mixin
            .dom_text(text) => &text.slottable_mixin
        if let slot = dom_slottable_mixin_assigned_slot(slottable):
            return slot.dom_parent_node()

    # Otherwise, this is the parent or shadow host.
    return dom_node_parent_or_shadow_host(this)

## Ported from Node.cpp:1833 Web::DOM::Node::flat_tree_parent_element.
pub func dom_node_flat_tree_parent_element(this: DomNode*) -> DomElement*?:
    var parent = dom_node_flat_tree_parent(this)
    while let parent_node = parent:
        if let element = as_if_dom_element(parent_node):
            return element
        parent = dom_node_flat_tree_parent(parent_node.dom_node())
    return none

## Ported from Node.cpp:1842 Web::DOM::Node::parent_or_shadow_host_node.
pub func dom_node_parent_or_shadow_host_node(this: DomNode*) -> DomNode*?:
    if is_dom_shadow_root(this):
        let host = dom_document_fragment_host(((DomShadowRoot*)this).dom_document_fragment()) else return none
        return host.dom_node()
    return tree_node_parent(this)

## Ported from Node.cpp:1849 Web::DOM::Node::as_slottable.
pub func dom_node_as_slottable(this: DomNode*) -> DomSlottable:
    assert(dom_node_is_slottable(this))

    if dom_node_is_element(this):
        return .dom_element((DomElement*)this)
    return .dom_text((DomText*)this)

# mark: Children ================================================================================

## Node::child_nodes's LiveNodeList filter: every child matches.
func dom_node_child_nodes_filter(context: void*?, node: DomNode*) -> bool:
    return true

## Ported from Node.cpp:1858 Web::DOM::Node::child_nodes.
pub func dom_node_child_nodes(this: DomNode*) -> DomNodeList*:
    if this.m_child_nodes == none:
        this.m_child_nodes = dom_live_node_list_create(bind_platform_object_realm(this.bind_platform_object()), this, DomLiveNodeListScope.children, ak.Function1[DomNode*, bool](call = dom_node_child_nodes_filter, context = none))
    return this.m_child_nodes else trap("unreachable")

## Ported from Node.cpp:1868 Web::DOM::Node::children_as_vector.
pub func dom_node_children_as_vector(this: DomNode*) -> ak.Vector[DomNode*?]:
    var nodes: ak.Vector[DomNode*?]

    # for_each_child
    for child in dom_node_children(this):
        ak.vector_append_t(&nodes, child)

    return nodes

## Ported from Node.cpp:1880 Web::DOM::Node::remove_all_children.
pub func dom_node_remove_all_children(this: DomNode*, suppress_observers: bool = false):
    while let child = tree_node_first_child(this):
        dom_node_remove(child, suppress_observers)