dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / dom / slottable.lucb
#==============================================================================================
#
# slottable - DOM::SlottableMixin and the slot assignment algorithms
#
# DESCRIPTION:
# Port of LibWeb/DOM/Slottable.cpp (lines 1-281) and the inline functions of
# Slottable.h: the mixin Element and Text carry (name, assigned slot, manual slot
# assignment), "find a slot", "find (flattened) slottables", "assign slottables (for a
# tree)", "assign a slot" and "signal a slot change". A Slottable is the variant
# DomSlottable (an Element or a Text).
#
#==============================================================================================
# mark: SlottableMixin ==========================================================================
## Ported from Slottable.h:26 Web::DOM::SlottableMixin::slottable_as_node (virtual).
pub func dom_slottable_mixin_slottable_as_node_impl(this: DomSlottableMixin*) -> DomNode*:
trap("pure virtual: Web::DOM::SlottableMixin::slottable_as_node")
## Ported from Slottable.cpp:22 Web::DOM::SlottableMixin::visit_edges.
pub func dom_slottable_mixin_visit_edges(this: DomSlottableMixin*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_t(visitor, this.m_assigned_slot)
gc.cell_visitor_visit_t(visitor, this.m_manual_slot_assignment)
## Ported from Slottable.cpp:29 Web::DOM::SlottableMixin::assigned_slot.
# https://dom.spec.whatwg.org/#dom-slotable-assignedslot
pub func dom_slottable_mixin_assigned_slot(this: DomSlottableMixin*) -> HtmlSlotElement*?:
let node = dom_slottable_mixin_slottable_as_node(this)
# The assignedSlot getter steps are to return the result of find a slot given this and with the open flag set.
return dom_find_a_slot(dom_node_as_slottable(node), DomOpenFlag.set)
## Ported from Slottable.h:28 Web::DOM::SlottableMixin::slottable_name.
# Not called `name` to distinguish from `Element::name`.
pub func dom_slottable_mixin_slottable_name(this: DomSlottableMixin*) -> ak.FlyString:
return this.m_name
## Ported from Slottable.h:29 Web::DOM::SlottableMixin::set_slottable_name.
pub func dom_slottable_mixin_set_slottable_name(this: DomSlottableMixin*, name: ak.FlyString):
this.m_name = name
## Ported from Slottable.h:33 Web::DOM::SlottableMixin::assigned_slot_internal.
pub func dom_slottable_mixin_assigned_slot_internal(this: DomSlottableMixin*) -> HtmlSlotElement*?:
return this.m_assigned_slot
## Ported from Slottable.h:34 Web::DOM::SlottableMixin::set_assigned_slot.
pub func dom_slottable_mixin_set_assigned_slot(this: DomSlottableMixin*, assigned_slot: HtmlSlotElement*?):
this.m_assigned_slot = assigned_slot
## Ported from Slottable.h:36 Web::DOM::SlottableMixin::manual_slot_assignment.
pub func dom_slottable_mixin_manual_slot_assignment(this: DomSlottableMixin*) -> HtmlSlotElement*?:
return this.m_manual_slot_assignment
## Ported from Slottable.h:37 Web::DOM::SlottableMixin::set_manual_slot_assignment.
pub func dom_slottable_mixin_set_manual_slot_assignment(this: DomSlottableMixin*, manual_slot_assignment: HtmlSlotElement*?):
this.m_manual_slot_assignment = manual_slot_assignment
# mark: Slottable helpers =======================================================================
## `slottable.visit([](auto& node) { ... })` reaching the SlottableMixin of either alternative.
func dom_slottable_mixin_of(slottable: DomSlottable) -> DomSlottableMixin*:
match slottable:
.dom_element(element): return &element.slottable_mixin
.dom_text(text): return &text.slottable_mixin
## `slottable.visit([](auto& node) { return node.ptr(); })`: the slottable as a node.
func dom_slottable_node(slottable: DomSlottable) -> DomNode*:
match slottable:
.dom_element(element): return element.dom_node()
.dom_text(text): return text.dom_node()
## `Vector<Slottable>::contains_slow(slottable)` over a span.
func dom_slottables_contain(slottables: const DomSlottable[], slottable: DomSlottable) -> bool:
for candidate in slottables:
if candidate == slottable:
return true
return false
# mark: Finding slots and slottables ============================================================
## Ported from Slottable.cpp:37 Web::DOM::assigned_slot_for_node.
pub func dom_assigned_slot_for_node(node: DomNode*) -> HtmlSlotElement*?:
if not dom_node_is_slottable(node):
return none
return dom_slottable_mixin_assigned_slot_internal(dom_slottable_mixin_of(dom_node_as_slottable(node)))
## Ported from Slottable.cpp:48 Web::DOM::is_an_assigned_slottable.
# https://dom.spec.whatwg.org/#slotable-assigned
pub func dom_is_an_assigned_slottable(node: DomNode*) -> bool:
if not dom_node_is_slottable(node):
return false
# A slottable is assigned if its assigned slot is non-null.
return dom_assigned_slot_for_node(node) != none
## Ported from Slottable.cpp:58 Web::DOM::find_a_slot.
# https://dom.spec.whatwg.org/#find-a-slot
pub func dom_find_a_slot(slottable: DomSlottable, open_flag: DomOpenFlag = DomOpenFlag.unset) -> HtmlSlotElement*?:
# 1. If slottable’s parent is null, then return null.
let parent = dom_node_parent_element(dom_slottable_node(slottable)) else return none
# 2. Let shadow be slottable’s parent’s shadow root.
let shadow_root = dom_element_shadow_root(parent)
# 3. If shadow is null, then return null.
let shadow = shadow_root else return none
# 4. If the open flag is set and shadow’s mode is not "open", then return null.
if open_flag == DomOpenFlag.set and dom_shadow_root_mode(shadow) != BindShadowRootMode.open:
return none
# 5. If shadow’s slot assignment is "manual", then return the slot in shadow’s descendants whose manually assigned
# nodes contains slottable, if any; otherwise null.
if dom_shadow_root_slot_assignment(shadow) == BindSlotAssignmentMode.manual:
var slot: HtmlSlotElement*? = none
for node in dom_node_subtree(shadow.dom_node()):
let child = as_if_html_slot_element(node) else continue
if not dom_slottables_contain(html_slot_element_manually_assigned_nodes(child), slottable):
continue
slot = child
break
return slot
# 6. Return the first slot in tree order in shadow’s descendants whose name is slottable’s name, if any; otherwise null.
let slottable_name = dom_slottable_mixin_slottable_name(dom_slottable_mixin_of(slottable))
return dom_shadow_root_first_slot_with_name(shadow, slottable_name)
## Ported from Slottable.cpp:98 Web::DOM::find_slottables.
# https://dom.spec.whatwg.org/#find-slotables
pub func dom_find_slottables(slot: HtmlSlotElement*) -> ak.Vector[DomSlottable]:
# 1. Let result be an empty list.
var result: ak.Vector[DomSlottable]
# 2. Let root be slot’s root.
let root = tree_node_root(slot.dom_node())
# 3. If root is not a shadow root, then return result.
if not dom_node_is_shadow_root(root):
return result
# 4. Let host be root’s host.
let shadow_root = (DomShadowRoot*)root
let host = dom_document_fragment_host(shadow_root.dom_document_fragment())
# 5. If root’s slot assignment is "manual", then:
if dom_shadow_root_slot_assignment(shadow_root) == BindSlotAssignmentMode.manual:
# 1. Let result be « ».
# 2. For each slottable slottable of slot’s manually assigned nodes, if slottable’s parent is host, append slottable to result.
for slottable in html_slot_element_manually_assigned_nodes(slot):
let parent = tree_node_parent(dom_slottable_node(slottable))
if parent == (DomNode*?)host:
ak.vector_append_t(&result, slottable)
# 6. Otherwise, for each slottable child slottable of host, in tree order:
else:
let host_element = host else trap("VERIFY: shadow root host")
for node in dom_node_children(host_element.dom_node()):
if not dom_node_is_slottable(node):
continue
let slottable = dom_node_as_slottable(node)
# 1. Let foundSlot be the result of finding a slot given slottable.
let found_slot = dom_find_a_slot(slottable)
# 2. If foundSlot is slot, then append slottable to result.
if found_slot == slot:
ak.vector_append_t(&result, slottable)
# 7. Return result.
return result
## Ported from Slottable.cpp:149 Web::DOM::find_flattened_slottables.
# https://dom.spec.whatwg.org/#find-flattened-slotables
pub func dom_find_flattened_slottables(slot: HtmlSlotElement*) -> ak.Vector[DomSlottable]:
# 1. Let result be « ».
var result: ak.Vector[DomSlottable]
# 2. If slot’s root is not a shadow root, then return result.
if not dom_node_is_shadow_root(tree_node_root(slot.dom_node())):
return result
# 3. Let slottables be the result of finding slottables given slot.
var slottables = dom_find_slottables(slot)
# 4. If slottables is the empty list, then append each slottable child of slot, in tree order, to slottables.
if ak.vector_is_empty(&slottables):
for node in dom_node_children(slot.dom_node()):
if not dom_node_is_slottable(node):
continue
ak.vector_append_t(&slottables, dom_node_as_slottable(node))
# 5. For each node of slottables:
for node in ak.vector_span(&slottables):
# 1. If node is a slot whose root is a shadow root:
# 1. Let temporaryResult be the result of finding flattened slottables given node.
# 2. Append each slottable in temporaryResult, in order, to result.
# 2. Otherwise, append node to result.
match node:
.dom_text(_):
ak.vector_append_t(&result, node)
.dom_element(maybe_slot):
let slot_element = as_if_html_slot_element(maybe_slot)
var appended = false
if let nested_slot = slot_element:
if dom_node_is_shadow_root(tree_node_root(nested_slot.dom_node())):
var temporary_result = dom_find_flattened_slottables(nested_slot)
ak.vector_extend(&result, &temporary_result)
appended = true
if not appended:
ak.vector_append_t(&result, node)
# 6. Return result.
return result
# mark: Assigning ===============================================================================
## Ported from Slottable.cpp:197 Web::DOM::assign_slottables.
# https://dom.spec.whatwg.org/#assign-slotables
pub func dom_assign_slottables(slot: HtmlSlotElement*):
# 1. Let slottables be the result of finding slottables for slot.
let slottables = dom_find_slottables(slot)
# 2. If slottables and slot’s assigned nodes are not identical, then run signal a slot change for slot.
let assigned_nodes = dom_slot_assigned_nodes_internal(&slot.slot)
var identical = ak.vector_size(&slottables) == assigned_nodes.length
if identical:
for (index, slottable) in ak.vector_span_const(&slottables).indexed():
if slottable != assigned_nodes[index]:
identical = false
break
if not identical:
dom_signal_a_slot_change(slot)
# AD-HOC: Clear the assigned slot for slottables that are no longer assigned to this slot.
# This must happen before setting the new assigned slots to avoid stale references
# during style computation.
for old_slottable in dom_slot_assigned_nodes_internal(&slot.slot):
dom_slottable_mixin_set_assigned_slot(dom_slottable_mixin_of(old_slottable), none)
# ::slotted(...) rules in the slot's shadow scope no longer apply to a slottable that just lost its
# assignment, so its style must be recomputed.
css_invalidation_invalidate_style_after_slottable_assignment_change(old_slottable)
# 4. For each slottable in slottables, set slottable’s assigned slot to slot.
for slottable in ak.vector_span_const(&slottables):
dom_slottable_mixin_set_assigned_slot(dom_slottable_mixin_of(slottable), slot)
# Newly-assigned slottables become subject to ::slotted(...) rules in the slot's shadow scope, so their style
# needs to be recomputed.
css_invalidation_invalidate_style_after_slottable_assignment_change(slottable)
# 3. Set slot’s assigned nodes to slottables.
# NOTE: We do this step last so that we can move the slottables list.
dom_slot_set_assigned_nodes(&slot.slot, slottables)
## for_each_registered_slot's lambda in assign_slottables_for_a_tree.
func dom_assign_slottables_for_a_tree_slot(context: void*?, slot: HtmlSlotElement*):
dom_assign_slottables(slot)
## Ported from Slottable.cpp:234 Web::DOM::assign_slottables_for_a_tree.
# https://dom.spec.whatwg.org/#assign-slotables-for-a-tree
pub func dom_assign_slottables_for_a_tree(root: DomNode*):
# To assign slottables for a tree, given a node root, run assign slottables for each slot of root’s inclusive
# descendants, in tree order.
# OPTIMIZATION: If root is not a shadow root or slot element, there are no slots to process, so return early.
let shadow_root = as_if_dom_shadow_root(root)
if shadow_root == none and not dom_node_is_html_slot_element(root):
return
if let shadow = shadow_root:
dom_shadow_root_for_each_registered_slot(shadow, ak.Function1[HtmlSlotElement*, unit](call = dom_assign_slottables_for_a_tree_slot, context = none))
return
for node in dom_node_inclusive_subtree(root):
if let slot = as_if_html_slot_element(node):
dom_assign_slottables(slot)
## Ported from Slottable.cpp:258 Web::DOM::assign_a_slot.
# https://dom.spec.whatwg.org/#assign-a-slot
pub func dom_assign_a_slot(slottable: DomSlottable):
# 1. Let slot be the result of finding a slot with slottable.
let slot = dom_find_a_slot(slottable)
# 2. If slot is non-null, then run assign slottables for slot.
if let found = slot:
dom_assign_slottables(found)
## Ported from Slottable.cpp:269 Web::DOM::signal_a_slot_change.
# https://dom.spec.whatwg.org/#signal-a-slot-change
pub func dom_signal_a_slot_change(slottable: HtmlSlotElement*):
# 1. Append slot to slot’s relevant agent’s signal slots.
# NB: signal_slots is supposed to be a set, so ensure the slottable is not already there.
# (JS::Object& from a platform object: JS::Object is opaque until phase 3, DESIGN.md §3.6.)
let agent = html_relevant_similar_origin_window_agent((JsObject*)slottable)
if ak.vector_find_first_index(&agent.signal_slots, slottable) == none:
ak.vector_append_t(&agent.signal_slots, slottable)
# 2. Queue a mutation observer microtask.
bind_queue_mutation_observer_microtask()