Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / tree_node_2.lucb

#==============================================================================================
#
#   tree_node_2 - TreeNode<T>: the subtree, child, sibling and ancestor traversals and
#                 visit_edges
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/TreeNode.h (lines 220-477): the template members that walk the
#       tree with a callback or look for a node of a class, and visit_edges. Conventions as in
#       tree_node_1: the T itself through HasTreeNode[T], a ClassInfo for a class template
#       argument, a TraversalCallback[T] or IterationCallback[T] for a lambda.
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Subtree traversals ======================================================================

## The lambda for_each_in_inclusive_subtree_of_type<U> wraps around its callback: calls it for the
## nodes that are a U only.
struct TreeNodeOfTypeCallback[T: HasTreeNode[T] & gc.IsCell, C: TraversalCallback[T]]: TraversalCallback[T]:
    var info: const ak.ClassInfo*
    var callback: C*

    func call(node: T*) -> TraversalDecision:
        if tree_node_is_of_class(node, self.info):
            return self.callback.call(node)
        return TraversalDecision.continue_

## Ported from Libraries/LibWeb/TreeNode.h:222 Web::TreeNode::for_each_in_inclusive_subtree.
pub func tree_node_for_each_in_inclusive_subtree_const[T: HasTreeNode[T], C: TraversalCallback[T]](this: const T*, callback: C*) -> TraversalDecision:
    return traverse_preorder[T, C]((T*)this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:228 Web::TreeNode::for_each_in_inclusive_subtree.
pub func tree_node_for_each_in_inclusive_subtree[T: HasTreeNode[T], C: TraversalCallback[T]](this: T*, callback: C*) -> TraversalDecision:
    return traverse_preorder[T, C](this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:234 Web::TreeNode::for_each_in_inclusive_subtree_of_type.
pub func tree_node_for_each_in_inclusive_subtree_of_type[T: HasTreeNode[T] & gc.IsCell, C: TraversalCallback[T]](this: T*, class_info: const ak.ClassInfo*, callback: C*) -> TraversalDecision:
    var wrapper = TreeNodeOfTypeCallback[T, C](info = class_info, callback = callback)
    return tree_node_for_each_in_inclusive_subtree(this, &wrapper)

## Ported from Libraries/LibWeb/TreeNode.h:244 Web::TreeNode::for_each_in_inclusive_subtree_of_type.
pub func tree_node_for_each_in_inclusive_subtree_of_type_const[T: HasTreeNode[T] & gc.IsCell, C: TraversalCallback[T]](this: const T*, class_info: const ak.ClassInfo*, callback: C*) -> TraversalDecision:
    return tree_node_for_each_in_inclusive_subtree_of_type((T*)this, class_info, callback)

## Ported from Libraries/LibWeb/TreeNode.h:254 Web::TreeNode::for_each_in_subtree.
pub func tree_node_for_each_in_subtree_const[T: HasTreeNode[T], C: TraversalCallback[T]](this: const T*, callback: C*) -> TraversalDecision:
    return tree_node_for_each_in_subtree((T*)this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:264 Web::TreeNode::for_each_in_subtree.
pub func tree_node_for_each_in_subtree[T: HasTreeNode[T], C: TraversalCallback[T]](this: T*, callback: C*) -> TraversalDecision:
    var child = this.tree().m_first_child
    while let at = child:
        if tree_node_for_each_in_inclusive_subtree(at, callback) == TraversalDecision.break_:
            return TraversalDecision.break_
        child = at.tree().m_next_sibling
    return TraversalDecision.continue_

## Ported from Libraries/LibWeb/TreeNode.h:274 Web::TreeNode::for_each_in_subtree_of_type.
pub func tree_node_for_each_in_subtree_of_type[T: HasTreeNode[T] & gc.IsCell, C: TraversalCallback[T]](this: T*, class_info: const ak.ClassInfo*, callback: C*) -> TraversalDecision:
    var child = this.tree().m_first_child
    while let at = child:
        if tree_node_for_each_in_inclusive_subtree_of_type(at, class_info, callback) == TraversalDecision.break_:
            return TraversalDecision.break_
        child = at.tree().m_next_sibling
    return TraversalDecision.continue_

## Ported from Libraries/LibWeb/TreeNode.h:284 Web::TreeNode::for_each_in_subtree_of_type.
pub func tree_node_for_each_in_subtree_of_type_const[T: HasTreeNode[T] & gc.IsCell, C: TraversalCallback[T]](this: const T*, class_info: const ak.ClassInfo*, callback: C*) -> TraversalDecision:
    return tree_node_for_each_in_subtree_of_type((T*)this, class_info, callback)

# mark: Children and siblings of a type =========================================================

## Ported from Libraries/LibWeb/TreeNode.h:294 Web::TreeNode::for_each_child.
pub func tree_node_for_each_child_const[T: HasTreeNode[T], C: IterationCallback[T]](this: const T*, callback: C*):
    tree_node_for_each_child((T*)this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:300 Web::TreeNode::for_each_child.
pub func tree_node_for_each_child[T: HasTreeNode[T], C: IterationCallback[T]](this: T*, callback: C*):
    var node = this.tree().m_first_child
    while let at = node:
        if callback.call(at) == ak.IterationDecision.break_:
            return
        node = at.tree().m_next_sibling

## Ported from Libraries/LibWeb/TreeNode.h:309 Web::TreeNode::for_each_child_of_type.
pub func tree_node_for_each_child_of_type[T: HasTreeNode[T] & gc.IsCell, C: IterationCallback[T]](this: T*, class_info: const ak.ClassInfo*, callback: C*):
    var node = this.tree().m_first_child
    while let at = node:
        if tree_node_is_of_class(at, class_info):
            if callback.call(at) == ak.IterationDecision.break_:
                return
        node = at.tree().m_next_sibling

## Ported from Libraries/LibWeb/TreeNode.h:320 Web::TreeNode::for_each_child_of_type.
pub func tree_node_for_each_child_of_type_const[T: HasTreeNode[T] & gc.IsCell, C: IterationCallback[T]](this: const T*, class_info: const ak.ClassInfo*, callback: C*):
    tree_node_for_each_child_of_type((T*)this, class_info, callback)

## Ported from Libraries/LibWeb/TreeNode.h:326 Web::TreeNode::next_sibling_of_type.
pub func tree_node_next_sibling_of_type_const[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> const T*?:
    return tree_node_next_sibling_of_type((T*)this, class_info)

## Ported from Libraries/LibWeb/TreeNode.h:332 Web::TreeNode::next_sibling_of_type.
pub func tree_node_next_sibling_of_type[T: HasTreeNode[T] & gc.IsCell](this: T*, class_info: const ak.ClassInfo*) -> T*?:
    var sibling = this.tree().m_next_sibling
    while let at = sibling:
        if tree_node_is_of_class(at, class_info):
            return at
        sibling = at.tree().m_next_sibling
    return none

## Ported from Libraries/LibWeb/TreeNode.h:342 Web::TreeNode::previous_sibling_of_type.
pub func tree_node_previous_sibling_of_type_const[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> const T*?:
    return tree_node_previous_sibling_of_type((T*)this, class_info)

## Ported from Libraries/LibWeb/TreeNode.h:348 Web::TreeNode::previous_sibling_of_type.
pub func tree_node_previous_sibling_of_type[T: HasTreeNode[T] & gc.IsCell](this: T*, class_info: const ak.ClassInfo*) -> T*?:
    var sibling = this.tree().m_previous_sibling
    while let at = sibling:
        if tree_node_is_of_class(at, class_info):
            return at
        sibling = at.tree().m_previous_sibling
    return none

## Ported from Libraries/LibWeb/TreeNode.h:358 Web::TreeNode::has_child_of_type.
pub func tree_node_has_child_of_type[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> bool:
    return tree_node_first_child_of_type_const(this, class_info) != none

## Ported from Libraries/LibWeb/TreeNode.h:364 Web::TreeNode::first_child_of_type.
pub func tree_node_first_child_of_type_const[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> const T*?:
    return tree_node_first_child_of_type((T*)this, class_info)

## Ported from Libraries/LibWeb/TreeNode.h:370 Web::TreeNode::last_child_of_type.
pub func tree_node_last_child_of_type_const[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> const T*?:
    return tree_node_last_child_of_type((T*)this, class_info)

## Ported from Libraries/LibWeb/TreeNode.h:376 Web::TreeNode::first_child_of_type.
pub func tree_node_first_child_of_type[T: HasTreeNode[T] & gc.IsCell](this: T*, class_info: const ak.ClassInfo*) -> T*?:
    var child = this.tree().m_first_child
    while let at = child:
        if tree_node_is_of_class(at, class_info):
            return at
        child = at.tree().m_next_sibling
    return none

## Ported from Libraries/LibWeb/TreeNode.h:386 Web::TreeNode::last_child_of_type.
pub func tree_node_last_child_of_type[T: HasTreeNode[T] & gc.IsCell](this: T*, class_info: const ak.ClassInfo*) -> T*?:
    var child = this.tree().m_last_child
    while let at = child:
        if tree_node_is_of_class(at, class_info):
            return at
        child = at.tree().m_previous_sibling
    return none

# mark: Ancestors ===============================================================================

## Ported from Libraries/LibWeb/TreeNode.h:396 Web::TreeNode::first_ancestor_of_type.
pub func tree_node_first_ancestor_of_type_const[T: HasTreeNode[T] & gc.IsCell](this: const T*, class_info: const ak.ClassInfo*) -> const T*?:
    return tree_node_first_ancestor_of_type((T*)this, class_info)

## Ported from Libraries/LibWeb/TreeNode.h:402 Web::TreeNode::first_ancestor_of_type.
pub func tree_node_first_ancestor_of_type[T: HasTreeNode[T] & gc.IsCell](this: T*, class_info: const ak.ClassInfo*) -> T*?:
    var ancestor = this.tree().m_parent
    while let at = ancestor:
        if tree_node_is_of_class(at, class_info):
            return at
        ancestor = at.tree().m_parent
    return none

## Ported from Libraries/LibWeb/TreeNode.h:412 Web::TreeNode::for_each_ancestor.
pub func tree_node_for_each_ancestor_const[T: HasTreeNode[T], C: IterationCallback[T]](this: const T*, callback: C*):
    tree_node_for_each_ancestor((T*)this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:418 Web::TreeNode::for_each_ancestor.
pub func tree_node_for_each_ancestor[T: HasTreeNode[T], C: IterationCallback[T]](this: T*, callback: C*):
    var ancestor = this.tree().m_parent
    while let at = ancestor:
        if callback.call(at) == ak.IterationDecision.break_:
            break
        ancestor = at.tree().m_parent

## Ported from Libraries/LibWeb/TreeNode.h:427 Web::TreeNode::for_each_inclusive_ancestor.
pub func tree_node_for_each_inclusive_ancestor_const[T: HasTreeNode[T], C: IterationCallback[T]](this: const T*, callback: C*):
    tree_node_for_each_inclusive_ancestor((T*)this, callback)

## Ported from Libraries/LibWeb/TreeNode.h:433 Web::TreeNode::for_each_inclusive_ancestor.
pub func tree_node_for_each_inclusive_ancestor[T: HasTreeNode[T], C: IterationCallback[T]](this: T*, callback: C*):
    var ancestor: T*? = this
    while let at = ancestor:
        if callback.call(at) == ak.IterationDecision.break_:
            break
        ancestor = at.tree().m_parent

## Ported from Libraries/LibWeb/TreeNode.h:442 Web::TreeNode::for_each_inclusive_ancestor_of_type.
pub func tree_node_for_each_inclusive_ancestor_of_type_const[T: HasTreeNode[T] & gc.IsCell, C: IterationCallback[T]](this: const T*, class_info: const ak.ClassInfo*, callback: C*):
    tree_node_for_each_inclusive_ancestor_of_type((T*)this, class_info, callback)

## Ported from Libraries/LibWeb/TreeNode.h:448 Web::TreeNode::for_each_inclusive_ancestor_of_type.
pub func tree_node_for_each_inclusive_ancestor_of_type[T: HasTreeNode[T] & gc.IsCell, C: IterationCallback[T]](this: T*, class_info: const ak.ClassInfo*, callback: C*):
    var ancestor: T*? = this
    while let at = ancestor:
        if tree_node_is_of_class(at, class_info):
            if callback.call(at) == ak.IterationDecision.break_:
                return
        ancestor = at.tree().m_parent

# mark: visit_edges =============================================================================

## Ported from Libraries/LibWeb/TreeNode.h:463 Web::TreeNode::visit_edges.
pub func tree_node_visit_edges[T: HasTreeNode[T] & gc.IsCell](this: T*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t[T](visitor, this.tree().m_parent)
    gc.cell_visitor_visit_t[T](visitor, this.tree().m_first_child)
    gc.cell_visitor_visit_t[T](visitor, this.tree().m_last_child)
    gc.cell_visitor_visit_t[T](visitor, this.tree().m_next_sibling)
    gc.cell_visitor_visit_t[T](visitor, this.tree().m_previous_sibling)