Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / node_operations.lucb

#==============================================================================================
#
#   node_operations - DOM: converting nodes into a node
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/NodeOperations.cpp (lines 1-46): the "convert nodes into a node"
#       algorithm behind prepend(), append(), before(), after() and friends.
#
#==============================================================================================

## NodeOperations.cpp's potentially_convert_string_to_text_node lambda (it captures `document`).
# 1. Replace each string of nodes with a new Text node whose data is the string and node document is document.
func dom_convert_nodes_to_single_node_potentially_convert_string_to_text_node(document: DomDocument*, node: DomNodeOrUtf16String) -> DomNode*:
    match node:
        .dom_node(root):
            return root else trap("null GC::Root<Node>")
        .utf16_string(string):
            return realm_create_dom_text_utf16_string(bind_platform_object_realm(document.bind_platform_object()), document, string).dom_node()

## Ported from NodeOperations.cpp:19 Web::DOM::convert_nodes_to_single_node.
# https://dom.spec.whatwg.org/#convert-nodes-into-a-node
pub func dom_convert_nodes_to_single_node(nodes: ak.Vector[DomNodeOrUtf16String], document: DomDocument*) -> DomNode*!:
    # 1. Replace each string of nodes with a new Text node whose data is the string and node document is document.
    # (dom_convert_nodes_to_single_node_potentially_convert_string_to_text_node)

    # 2. If nodes’s size is 1, then return nodes[0].
    if ak.vector_size(&nodes) == 1:
        return dom_convert_nodes_to_single_node_potentially_convert_string_to_text_node(document, *ak.vector_first_const(&nodes))

    # 3. Let fragment be a new DocumentFragment node whose node document is document.
    let fragment = realm_create_dom_document_fragment(bind_platform_object_realm(document.bind_platform_object()), document)

    # 4. For each node of nodes: append node to fragment.
    for unconverted_node in ak.vector_span_const(&nodes):
        let node = dom_convert_nodes_to_single_node_potentially_convert_string_to_text_node(document, unconverted_node)
        _ = try dom_node_append_child(fragment.dom_node(), node)

    # 5. Return fragment.
    return fragment.dom_node()