Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / popover_target_attributes.lucb

#==============================================================================================
#
#   popover_target_attributes - HTML::PopoverTargetAttributes: popovertarget on buttons
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/PopoverTargetAttributes.cpp (lines 1-115) and the inline functions
#       of PopoverTargetAttributes.h: the mixin of HTMLInputElement and HTMLButtonElement that
#       keeps the explicitly set popover target element, resets it when the popovertarget
#       attribute changes, visits it, finds the popover target element and runs the popover
#       target attribute activation behavior. The mixin has no table: it is found through
#       ClassInfo's mixin offsets (as_if_popover_target_attributes). Added by region r24/r25,
#       whose elements are its only users (it was a closure stub).
#
#       Copyright (c) 2024, Nathan van der Kamp <nbvdkamp@gmail.com>
#       Copyright (c) 2024, Tim Ledbetter <tim.ledbetter@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from PopoverTargetAttributes.h:20 Web::HTML::PopoverTargetAttributes::popover_target_element.
pub func html_popover_target_attributes_popover_target_element(this: HtmlPopoverTargetAttributes*) -> DomElement*?:
    return this.m_popover_target_element

## Ported from PopoverTargetAttributes.h:22 Web::HTML::PopoverTargetAttributes::set_popover_target_element.
pub func html_popover_target_attributes_set_popover_target_element(this: HtmlPopoverTargetAttributes*, value: DomElement*?):
    this.m_popover_target_element = value

## Ported from PopoverTargetAttributes.cpp:17 Web::HTML::PopoverTargetAttributes::associated_attribute_changed.
pub func html_popover_target_attributes_associated_attribute_changed(this: HtmlPopoverTargetAttributes*, name: ak.FlyString, value: ak.String?, namespace_: ak.FlyString?):
    _ = value
    # From: https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributess
    # For element reflected targets only: the following attribute change steps, given element, localName, oldValue, value, and namespace,
    # are used to synchronize between the content attribute and the IDL attribute:

    # 1. If localName is not attr or namespace is not null, then return.
    if not ak.fly_string_eq_fly_string(&name, &html_attribute_names_popovertarget) or namespace_ != none:
        return

    # 2. Set element's explicitly set attr-elements to null.
    this.m_popover_target_element = none

## Ported from PopoverTargetAttributes.cpp:31 Web::HTML::PopoverTargetAttributes::visit_edges.
pub func html_popover_target_attributes_visit_edges(this: HtmlPopoverTargetAttributes*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t(visitor, this.m_popover_target_element)

## Ported from PopoverTargetAttributes.cpp:38 Web::HTML::PopoverTargetAttributes::popover_target_activation_behaviour.
# https://html.spec.whatwg.org/multipage/popover.html#popover-target-attribute-activation-behavior
# https://whatpr.org/html/9457/popover.html#popover-target-attribute-activation-behavior
pub func html_popover_target_attributes_popover_target_activation_behaviour(node: DomNode*, event_target: DomNode*):
    # To run the popover target attribute activation behavior given a Node node and a Node eventTarget:

    # 1. Let popover be node's popover target element.
    # 2. If popover is null, then return.
    let popover = html_popover_target_attributes_get_the_popover_target_element(node) else return

    # 3. If eventTarget is a shadow-including inclusive descendant of popover and popover is a shadow-including descendant of node, then return.
    if dom_node_is_shadow_including_inclusive_descendant_of(event_target, popover.dom_node()) and dom_node_is_shadow_including_descendant_of(popover.dom_node(), node):
        return

    let popovertargetaction = dom_element_get_attribute_value(as_dom_element(node), html_attribute_names_popovertargetaction)

    # 4. If node's popovertargetaction attribute is in the show state and popover's popover visibility state is showing, then return.
    if ak.string_equals_ignoring_ascii_case_string_view(&popovertargetaction, ak.sv("show")) and html_element_popover_visibility_state(popover) == HtmlElementPopoverVisibilityState.showing:
        return

    # 5. If node's popovertargetaction attribute is in the hide state and popover's popover visibility state is hidden, then return.
    if ak.string_equals_ignoring_ascii_case_string_view(&popovertargetaction, ak.sv("hide")) and html_element_popover_visibility_state(popover) == HtmlElementPopoverVisibilityState.hidden:
        return

    # 6. If popover's popover visibility state is showing, then run the hide popover algorithm given popover, true, true, false, false, and node.
    if html_element_popover_visibility_state(popover) == HtmlElementPopoverVisibilityState.showing:
        html_element_hide_popover(popover, HtmlFocusPreviousElement.yes, HtmlFireEvents.yes, HtmlThrowExceptions.no, HtmlIgnoreDomState.no, as_html_element(node)) else trap("MUST: hide_popover")

    # 7. Otherwise, if popover's popover visibility state is hidden and the result of running check popover validity given popover, false, false, null, and false is true, then run show popover given popover, false, and node.
    elif html_element_popover_visibility_state(popover) == HtmlElementPopoverVisibilityState.hidden and (html_element_check_popover_validity(popover, HtmlExpectedToBeShowing.no, HtmlThrowExceptions.no, none, HtmlIgnoreDomState.no) else trap("MUST: check_popover_validity")):
        html_element_show_popover(popover, HtmlThrowExceptions.no, as_html_element(node)) else trap("MUST: show_popover")

## Ported from PopoverTargetAttributes.cpp:73 Web::HTML::PopoverTargetAttributes::get_the_popover_target_element.
# https://html.spec.whatwg.org/multipage/popover.html#popover-target-element
pub func html_popover_target_attributes_get_the_popover_target_element(node: DomNode*) -> HtmlElement*?:
    # To get the popover target element given a Node node, perform the following steps. They return an HTML element or null.

    let form_associated_element = as_if_form_associated_element(node) else return none

    # 1. If node is not a button, then return null.
    if not html_form_associated_element_is_button(form_associated_element):
        return none

    # 2. If node is disabled, then return null.
    if not html_form_associated_element_enabled(form_associated_element):
        return none

    # 3. If node has a form owner and node is a submit button, then return null.
    if html_form_associated_element_form(form_associated_element) != none and html_form_associated_element_is_submit_button(form_associated_element):
        return none

    # 4. Let popoverElement be the result of running node's get the popovertarget-associated element.
    let popover_invoker_element = as_if_popover_target_attributes(node) else return none
    let associated_element = dom_element_get_the_attribute_associated_element(as_html_element(node).dom_element(), html_attribute_names_popovertarget, popover_invoker_element.m_popover_target_element)
    var popover_element: HtmlElement*? = none
    if let element = associated_element:
        popover_element = as_if_html_element(element)

    # 5. If popoverElement is null, then return null.
    let popover = popover_element else return none

    # 6. If popoverElement's popover attribute is in the No Popover state, then return null.
    if html_element_popover(popover) == none:
        return none

    # 7. Return popoverElement.
    return popover