Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / dom / live_node_list.lucb

#==============================================================================================
#
#   live_node_list - DOM::LiveNodeList: a NodeList that filters a node's children or descendants
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/LiveNodeList.cpp (lines 1-96). Like HTMLCollection, it does no
#       caching: every query walks the tree.
#
#==============================================================================================

## Ported from LiveNodeList.cpp:17 Web::DOM::LiveNodeList::create.
pub func dom_live_node_list_create(realm: Realm*, root: DomNode*, scope: DomLiveNodeListScope, filter: ak.Function1[DomNode*, bool]) -> DomNodeList*:
    return realm_create_dom_live_node_list(realm, realm, root, scope, filter).dom_node_list()

## Ported from LiveNodeList.cpp:22 Web::DOM::LiveNodeList::LiveNodeList.
pub func dom_live_node_list_construct(this: DomLiveNodeList*, realm: Realm*, root: DomNode*, scope: DomLiveNodeListScope, filter: ak.Function1[DomNode*, bool]):
    dom_node_list_construct(this.dom_node_list(), realm)
    dom_live_node_list_init_fields(this)
    this.m_root = root
    this.m_filter = filter
    this.m_scope = scope

## Ported from LiveNodeList.cpp:32 Web::DOM::LiveNodeList::visit_edges (override).
pub func dom_live_node_list_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (DomLiveNodeList*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_root)

## `m_filter(node)`.
func dom_live_node_list_matches(this: DomLiveNodeList*, node: DomNode*) -> bool:
    let call = this.m_filter.call else trap("VERIFY: filter")
    return call(this.m_filter.context, node)

## Ported from LiveNodeList.cpp:38 Web::DOM::LiveNodeList::collection.
pub func dom_live_node_list_collection(this: DomLiveNodeList*) -> ak.Vector[DomNode*?]:
    var nodes: ak.Vector[DomNode*?]
    if this.m_scope == DomLiveNodeListScope.descendants:
        for node in dom_node_subtree(this.m_root):
            if dom_live_node_list_matches(this, node):
                ak.vector_append_t(&nodes, node)
    else:
        for node in dom_node_children(this.m_root):
            if dom_live_node_list_matches(this, node):
                ak.vector_append_t(&nodes, node)
    return nodes

## Ported from LiveNodeList.cpp:57 Web::DOM::LiveNodeList::first_matching.
pub func dom_live_node_list_first_matching(this: DomLiveNodeList*, filter: ak.Function1[DomNode*, bool]) -> DomNode*?:
    let call = filter.call else trap("VERIFY: filter")
    var matched_node: DomNode*? = none
    if this.m_scope == DomLiveNodeListScope.descendants:
        for node in dom_node_subtree(this.m_root):
            if dom_live_node_list_matches(this, node) and call(filter.context, node):
                matched_node = node
                break
    else:
        for node in dom_node_children(this.m_root):
            if dom_live_node_list_matches(this, node) and call(filter.context, node):
                matched_node = node
                break
    return matched_node

## Ported from LiveNodeList.cpp:81 Web::DOM::LiveNodeList::length (override).
# https://dom.spec.whatwg.org/#dom-nodelist-length
pub func dom_live_node_list_length_impl(base: DomNodeList*) -> u32:
    let nodes = dom_live_node_list_collection((DomLiveNodeList*)base)
    return (u32)ak.vector_size(&nodes)

## Ported from LiveNodeList.cpp:87 Web::DOM::LiveNodeList::item (override).
# https://dom.spec.whatwg.org/#dom-nodelist-item
pub func dom_live_node_list_item_impl(base: DomNodeList*, index: u32) -> DomNode*?:
    # The item(index) method must return the indexth node in the collection. If there is no indexth node in the collection, then the method must return null.
    var nodes = dom_live_node_list_collection((DomLiveNodeList*)base)
    if (usize)index >= ak.vector_size(&nodes):
        return none
    return *ak.vector_at(&nodes, (usize)index)