Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_slot_element.lucb

#==============================================================================================
#
#   html_slot_element - HTML::HTMLSlotElement: <slot>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLSlotElement.cpp (lines 1-176) and HTMLSlotElement.h: the
#       constructor (which moves the mixin tables to HTMLSlotElement's, as C++'s vptrs move,
#       and gives the DOM::Slot mixin its table), initialize, visit_edges, assignedNodes and
#       assignedElements, manual assignment, and the name attribute's change steps.
#
#==============================================================================================

## Ported from HTMLSlotElement.cpp:20 Web::HTML::HTMLSlotElement::HTMLSlotElement.
pub func html_slot_element_construct(this: HtmlSlotElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLSlotElement's tables; the DOM::Slot base gets its table.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_slot_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_slot_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_slot_element_animatable_vtable
    element.global_event_handlers.vtable = &html_slot_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_slot_element_form_associated_element_vtable
    this.slot.vtable = &html_slot_element_slot_vtable
    this.slot.class_id = this.cell().class_id

## Ported from HTMLSlotElement.cpp:27 Web::HTML::HTMLSlotElement::initialize (override).
pub func html_slot_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_slot_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLSlotElement.cpp:33 Web::HTML::HTMLSlotElement::visit_edges (override).
pub func html_slot_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlSlotElement*)base
    html_element_visit_edges_impl(base, visitor)
    dom_slot_visit_edges(&this.slot, visitor)

    for node in ak.vector_span(&this.m_manually_assigned_nodes):
        gc.cell_visitor_visit_t(visitor, dom_slottable_node(node))

## Ported from HTMLSlotElement.cpp:43 Web::HTML::HTMLSlotElement::assigned_nodes.
# https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignednodes
pub func html_slot_element_assigned_nodes(this: HtmlSlotElement*, options: HtmlAssignedNodesOptions) -> ak.Vector[DomNode*?]:
    # 1. If options["flatten"] is false, then return this's assigned nodes.
    if not options.flatten:
        var assigned_nodes: ak.Vector[DomNode*?]
        let nodes = dom_slot_assigned_nodes_internal(&this.slot)
        ak.vector_ensure_capacity(&assigned_nodes, nodes.length)

        for node in nodes:
            ak.vector_append_t(&assigned_nodes, dom_slottable_node(node))

        return assigned_nodes

    # 2. Return the result of finding flattened slottables with this.
    # FIXME: Make this a lot less awkward!
    var nodes = dom_find_flattened_slottables(this)
    var assigned_nodes: ak.Vector[DomNode*?]
    ak.vector_ensure_capacity(&assigned_nodes, ak.vector_size(&nodes))
    for node in ak.vector_span(&nodes):
        ak.vector_append_t(&assigned_nodes, dom_slottable_node(node))
    return assigned_nodes

## Ported from HTMLSlotElement.cpp:73 Web::HTML::HTMLSlotElement::assigned_elements.
# https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assignedelements
pub func html_slot_element_assigned_elements(this: HtmlSlotElement*, options: HtmlAssignedNodesOptions) -> ak.Vector[DomElement*?]:
    # 1. If options["flatten"] is false, then return this's assigned nodes, filtered to contain only Element nodes.
    if not options.flatten:
        var assigned_nodes: ak.Vector[DomElement*?]

        for node in dom_slot_assigned_nodes_internal(&this.slot):
            match node:
                .dom_element(element): ak.vector_append_t(&assigned_nodes, element)
                .dom_text(_): continue

        return assigned_nodes

    # 2. Return the result of finding flattened slottables with this, filtered to contain only Element nodes.
    var result = dom_find_flattened_slottables(this)
    var assigned_nodes: ak.Vector[DomElement*?]
    for node in ak.vector_span(&result):
        match node:
            .dom_element(element): ak.vector_append_t(&assigned_nodes, element)
            .dom_text(_): continue
    return assigned_nodes

## Ported from HTMLSlotElement.cpp:98 Web::HTML::HTMLSlotElement::assign.
# https://html.spec.whatwg.org/multipage/scripting.html#dom-slot-assign
pub func html_slot_element_assign(this: HtmlSlotElement*, nodes: ak.Vector[HtmlSlotElementSlottableHandle]):
    # 1. For each node of this's manually assigned nodes, set node's manual slot assignment to null.
    for node in ak.vector_span(&this.m_manually_assigned_nodes):
        dom_slottable_mixin_set_manual_slot_assignment(dom_slottable_mixin_of(node), none)

    # 2. Let nodesSet be a new ordered set.
    var nodes_set: ak.Vector[DomSlottable]

    # 3. For each node of nodes:
    for node_handle in ak.vector_span_const(&nodes):
        let slottable = match node_handle:
            .dom_element(element) => DomSlottable.dom_element(element else trap("a slottable element"))
            .dom_text(text) => DomSlottable.dom_text(text else trap("a slottable text"))
        let node = dom_slottable_mixin_of(slottable)

        # 1. If node's manual slot assignment refers to a slot, then remove node from that slot's manually assigned nodes.
        if dom_slottable_mixin_manual_slot_assignment(node) != none:
            html_slot_element_remove_manually_assigned_node(this, slottable)

        # 2. Set node's manual slot assignment to this.
        dom_slottable_mixin_set_manual_slot_assignment(node, this)

        # 3. Append node to nodesSet.
        ak.vector_append_t(&nodes_set, slottable)

    # 4. Set this's manually assigned nodes to nodesSet.
    this.m_manually_assigned_nodes = nodes_set

    # 5. Run assign slottables for a tree for this's root.
    dom_assign_slottables_for_a_tree(tree_node_root(this.dom_node()))

## `m_manually_assigned_nodes.remove_all_matching(slottable == manually_assigned_node)`.
func html_slot_element_remove_manually_assigned_node(this: HtmlSlotElement*, slottable: DomSlottable):
    var index: usize = 0
    while index < ak.vector_size(&this.m_manually_assigned_nodes):
        if *ak.vector_at(&this.m_manually_assigned_nodes, index) == slottable:
            ak.vector_remove(&this.m_manually_assigned_nodes, index)
        else:
            index += 1

## Ported from HTMLSlotElement.h:38 Web::HTML::HTMLSlotElement::manually_assigned_nodes.
pub func html_slot_element_manually_assigned_nodes(this: HtmlSlotElement*) -> const DomSlottable[]:
    return ak.vector_span_const(&this.m_manually_assigned_nodes)

## `Optional<String> == Optional<String>`: both absent, or both present and equal.
func html_slot_element_optional_strings_equal(a: ak.String?, b: ak.String?) -> bool:
    if let a_value = a:
        if let b_value = b:
            return ak.string_equals(&a_value, &b_value)
        return false
    return b == none

## `Optional<String> == String {}`: present and empty.
func html_slot_element_optional_string_is_empty(value: ak.String?) -> bool:
    let string = value else return false
    return ak.string_is_empty(&string)

## Ported from HTMLSlotElement.cpp:137 Web::HTML::HTMLSlotElement::attribute_changed (override).
# https://dom.spec.whatwg.org/#ref-for-concept-element-attributes-change-ext
pub func html_slot_element_attribute_changed_impl(base: DomElement*, local_name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlSlotElement*)base
    html_element_attribute_changed_impl(base, local_name, old_value, value, namespace_)

    # 1. If element is a slot, localName is name, and namespace is null, then:
    if ak.fly_string_eq_fly_string(&local_name, &html_attribute_names_name) and namespace_ == none:
        # 1. If value is oldValue, then return.
        if html_slot_element_optional_strings_equal(value, old_value):
            return

        # 2. If value is null and oldValue is the empty string, then return.
        if value == none and html_slot_element_optional_string_is_empty(old_value):
            return

        # 3. If value is the empty string and oldValue is null, then return.
        if html_slot_element_optional_string_is_empty(value) and old_value == none:
            return

        # OPTIMIZATION: Update the slot registry before changing the name.
        let shadow_root = as_if_dom_shadow_root(tree_node_root(base.dom_node()))
        if let root = shadow_root:
            dom_shadow_root_unregister_slot(root, this)

        # 4. If value is null or the empty string, then set element’s name to the empty string.
        if let new_value = value:
            # 5. Otherwise, set element’s name to value.
            dom_slot_set_slot_name(&this.slot, ak.fly_string_from_string(new_value))
        else:
            dom_slot_set_slot_name(&this.slot, ak.fly_string_new())

        # OPTIMIZATION: Register the slot with its new name.
        if let root = shadow_root:
            dom_shadow_root_register_slot(root, this)

        # 6. Run assign slottables for a tree with element’s root.
        dom_assign_slottables_for_a_tree(tree_node_root(base.dom_node()))

## Ported from HTMLSlotElement.h:43 Web::HTML::HTMLSlotElement::is_html_slot_element (override).
pub func html_slot_element_is_html_slot_element_impl(base: DomNode*) -> bool:
    return true