Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / dom / child_node.lucb

#==============================================================================================
#
#   child_node - DOM::ChildNode: before(), after(), replaceWith() and remove()
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/ChildNode.h (lines 1-171), the ChildNode mixin of Element,
#       CharacterData and DocumentType. The C++ is a CRTP helper template with no state whose
#       body only uses Node, so it becomes functions on Node (DESIGN.md §2.5): the callers pass
#       their node, `dom_child_node_before(element.dom_node(), nodes)`.
#
#==============================================================================================

## Ported from ChildNode.h:19 Web::DOM::ChildNode::before.
# https://dom.spec.whatwg.org/#dom-childnode-before
pub func dom_child_node_before(this: DomNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
    let node = this

    # 1. Let parent be this’s parent.
    # 2. If parent is null, then return.
    let parent = tree_node_parent(node) else return

    # 3. Let viablePreviousSibling be this’s first preceding sibling not in nodes; otherwise null.
    var viable_previous_sibling = dom_child_node_viable_previous_sibling_for_insertion(this, nodes)

    # 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
    let node_to_insert = try dom_convert_nodes_to_single_node(nodes, dom_node_document(node))

    # 5. If viablePreviousSibling is null, then set it to parent’s first child; otherwise to viablePreviousSibling’s next sibling.
    if let sibling = viable_previous_sibling:
        viable_previous_sibling = tree_node_next_sibling(sibling)
    else:
        viable_previous_sibling = tree_node_first_child(parent)

    # 6. Pre-insert node into parent before viablePreviousSibling.
    _ = try dom_node_pre_insert(parent, node_to_insert, viable_previous_sibling)

## Ported from ChildNode.h:49 Web::DOM::ChildNode::after.
# https://dom.spec.whatwg.org/#dom-childnode-after
pub func dom_child_node_after(this: DomNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
    let node = this

    # 1. Let parent be this’s parent.
    # 2. If parent is null, then return.
    let parent = tree_node_parent(node) else return

    # 3. Let viableNextSibling be this’s first following sibling not in nodes; otherwise null.
    let viable_next_sibling = dom_child_node_viable_next_sibling_for_insertion(this, nodes)

    # 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
    let node_to_insert = try dom_convert_nodes_to_single_node(nodes, dom_node_document(node))

    # 5. Pre-insert node into parent before viableNextSibling.
    _ = try dom_node_pre_insert(parent, node_to_insert, viable_next_sibling)

## Ported from ChildNode.h:73 Web::DOM::ChildNode::replace_with.
# https://dom.spec.whatwg.org/#dom-childnode-replacewith
pub func dom_child_node_replace_with(this: DomNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> !:
    let node = this

    # 1. Let parent be this’s parent.
    # 2. If parent is null, then return.
    let parent = tree_node_parent(node) else return

    # 3. Let viableNextSibling be this’s first following sibling not in nodes; otherwise null.
    let viable_next_sibling = dom_child_node_viable_next_sibling_for_insertion(this, nodes)

    # 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
    let node_to_insert = try dom_convert_nodes_to_single_node(nodes, dom_node_document(node))

    # 5. If this’s parent is parent, replace this with node within parent.
    # Note: This could have been inserted into node.
    if tree_node_parent(node) == parent:
        _ = try dom_node_replace_child(parent, node_to_insert, node)
        return

    # 6. Otherwise, pre-insert node into parent before viableNextSibling.
    _ = try dom_node_pre_insert(parent, node_to_insert, viable_next_sibling)

## Ported from ChildNode.h:104 Web::DOM::ChildNode::remove_binding.
# https://dom.spec.whatwg.org/#dom-childnode-remove
pub func dom_child_node_remove_binding(this: DomNode*):
    let node = this

    # 1. If this’s parent is null, then return.
    if tree_node_parent(node) == none:
        return

    # 2. Remove this.
    dom_node_remove(node)

## Whether `sibling` is one of the nodes (not strings) of `nodes`.
func dom_child_node_nodes_contain(nodes: ak.Vector[DomNodeOrUtf16String], sibling: DomNode*) -> bool:
    for node_or_string in ak.vector_span_const(&nodes):
        match node_or_string:
            .dom_node(node_in_vector):
                if node_in_vector == sibling:
                    return true
            .utf16_string(_):
                continue
    return false

## Ported from ChildNode.h:120 Web::DOM::ChildNode::viable_previous_sibling_for_insertion.
pub func dom_child_node_viable_previous_sibling_for_insertion(this: DomNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> DomNode*?:
    let node = this

    var sibling = tree_node_previous_sibling(node)
    while let current = sibling:
        let contained_in_nodes = dom_child_node_nodes_contain(nodes, current)

        if not contained_in_nodes:
            return current
        sibling = tree_node_previous_sibling(current)

    return none

## Ported from ChildNode.h:145 Web::DOM::ChildNode::viable_next_sibling_for_insertion.
pub func dom_child_node_viable_next_sibling_for_insertion(this: DomNode*, nodes: ak.Vector[DomNodeOrUtf16String]) -> DomNode*?:
    let node = this

    var sibling = tree_node_next_sibling(node)
    while let current = sibling:
        let contained_in_nodes = dom_child_node_nodes_contain(nodes, current)

        if not contained_in_nodes:
            return current
        sibling = tree_node_next_sibling(current)

    return none