Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / document_or_shadow_root.lucb

#==============================================================================================
#
#   document_or_shadow_root - the DocumentOrShadowRoot mixin's activeElement and getAnimations
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/DocumentOrShadowRoot.h (lines 1-90). The C++ templates take a
#       Document or a ShadowRoot (`template<DocumentOrShadowRoot T>`); a Luce generic body
#       cannot reach either's members, and every member they use is Node's, so these take
#       the node.
#
#==============================================================================================

## Ported from DocumentOrShadowRoot.h:23 Web::DOM::calculate_active_element.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-documentorshadowroot-activeelement
pub func dom_calculate_active_element(self_: DomNode*) -> DomElement*?:
    let document = dom_node_document(self_)

    # 1. Let candidate be this's node document's focused area's DOM anchor.
    var candidate = dom_document_focused_area(document)

    # AD-HOC: null focused_area indicates "viewport focus".
    # https://html.spec.whatwg.org/multipage/interaction.html#focusable-area
    # If the focusable area is the viewport of a Document that has a non-null browsing context and is not inert then
    # the DOM anchor is the document for which the viewport was created.
    if candidate == none and dom_document_browsing_context(document) != none and not dom_node_is_inert(document.dom_node()):
        candidate = document.dom_node()

    # 2. Set candidate to the result of retargeting candidate against this.
    var retargeted: DomEventTarget*? = none
    if let candidate_node = candidate:
        retargeted = dom_retarget(candidate_node.dom_event_target(), self_.dom_event_target())
    else:
        retargeted = dom_retarget(none, self_.dom_event_target())
    candidate = none
    if let target = retargeted:
        candidate = as_dom_node(target)

    # 3. If candidate's root is not this, then return null.
    let candidate_node = candidate else return none
    if tree_node_root(candidate_node) != self_:
        return none

    # 4. If candidate is not a Document object, then return candidate.
    if not is_dom_document(candidate_node):
        return as_dom_element(candidate_node)

    let candidate_document = as_if_dom_document(candidate_node) else trap("unreachable")

    # 5. If candidate has a body element, then return that body element.
    if let body = dom_document_body(candidate_document):
        return body.dom_element()

    # 6. If candidate's document element is non-null, then return that document element.
    if let document_element = dom_document_document_element(candidate_document):
        return document_element

    # 7. Return null.
    return none

## calculate_get_animations's for_each_child_of_type_fallible<Element> callback.
func dom_calculate_get_animations_child(context: void*?, child: DomNode*) -> ak.IterationDecision!:
    let relevant_animations = (ak.Vector[AnimationsAnimation*]*)(context else trap("no captures"))
    let element = as_dom_element(child)
    let animations = try animations_animatable_get_animations_internal(&element.animatable, AnimationsAnimatableGetAnimationsSorted.no, AnimationsGetAnimationsOptions(subtree = true, pseudo_element = none))
    for animation in ak.vector_span_const(&animations):
        ak.vector_append_t(relevant_animations, animation)
    return ak.IterationDecision.continue_

## calculate_get_animations's quick_sort comparator: the composite order of the animations' keyframe effects.
func dom_calculate_get_animations_less_than(context: void*?, a: const (AnimationsAnimation*)*, b: const (AnimationsAnimation*)*) -> bool:
    _ = context
    let a_effect = as_animations_keyframe_effect(animations_animation_effect(*a) else trap("a null effect"))
    let b_effect = as_animations_keyframe_effect(animations_animation_effect(*b) else trap("a null effect"))
    return animations_keyframe_effect_composite_order(a_effect, b_effect) < 0

## Ported from DocumentOrShadowRoot.h:62 Web::DOM::calculate_get_animations.
# https://drafts.csswg.org/web-animations-1/#dom-documentorshadowroot-getanimations
pub func dom_calculate_get_animations(self_: DomNode*) -> ak.Vector[AnimationsAnimation*]!:
    # Returns the set of relevant animations for a subtree for the document or shadow root on which this
    # method is called.
    var relevant_animations: ak.Vector[AnimationsAnimation*]
    try dom_node_for_each_child_of_type_fallible(self_, &dom_element_class, dom_calculate_get_animations_child, &relevant_animations)

    # The returned list is sorted using the composite order described for the associated animations of
    # effects in ยง 5.4.2 The effect stack.
    let less_than = ak.Function2[const (AnimationsAnimation*)*, const (AnimationsAnimation*)*, bool](call = dom_calculate_get_animations_less_than, context = none)
    ak.quick_sort_t_collection_t_less_than(ak.vector_span(&relevant_animations), less_than)

    # Calling this method triggers a style change event for the document. As a result, the returned list
    # reflects the state after applying any pending style changes to animation such as changes to
    # animation-related style properties that have yet to be processed.
    # FIXME: Implement this.

    return relevant_animations