Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_or_svg_element.lucb

#==============================================================================================
#
#   html_or_svg_element - HTML::HTMLOrSVGElement<ElementBase>: dataset, nonce, focus and blur
#                         of HTML, SVG and MathML elements
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLOrSVGElement.cpp (lines 1-161) and the inline functions of
#       HTMLOrSVGElement.h. The CRTP base is embedded in HTMLElement, SVGElement and
#       MathMLElement as `html_or_svg_element`, after the Element base and the
#       GlobalEventHandlers mixin, so it sits at the same offset in all three; C++'s
#       `static_cast<ElementBase*>(this)` is a subtraction of that offset (asserted below), as
#       Element's ARIAMixin does it (region r16). The functions keep the C++ template parameter.
#
#==============================================================================================

# mark: The element of the mixin ================================================================

# HTMLOrSVGElement is at one offset in every class that includes it.
assert(memory.offset_of(SvgElement, html_or_svg_element) == memory.offset_of(HtmlElement, html_or_svg_element))
assert(memory.offset_of(MathMlMathMlElement, html_or_svg_element) == memory.offset_of(HtmlElement, html_or_svg_element))

## `static_cast<ElementBase*>(this)`: the element that includes this HTMLOrSVGElement.
func html_or_svg_element_element[TElementBase](this: const HtmlOrSvgElement[TElementBase]*) -> DomElement*:
    return (DomElement*)((u8*)this - memory.offset_of(HtmlElement, html_or_svg_element))

## The HTMLOrSVGElement included in `element`, an element of the same class as the mixin's.
func html_or_svg_element_of[TElementBase](element: DomNode*) -> HtmlOrSvgElement[TElementBase]*:
    return (HtmlOrSvgElement[TElementBase]*)((u8*)element + memory.offset_of(HtmlElement, html_or_svg_element))

# mark: HTMLOrSVGElement ========================================================================

## Ported from HTMLOrSVGElement.cpp:23 Web::HTML::HTMLOrSVGElement::dataset.
# https://html.spec.whatwg.org/multipage/dom.html#dom-dataset-dev
pub func html_or_svg_element_dataset[TElementBase](this: HtmlOrSvgElement[TElementBase]*) -> HtmlDomStringMap*:
    if this.m_dataset == none:
        this.m_dataset = html_dom_string_map_create(html_or_svg_element_element(this))
    return this.m_dataset else trap("unreachable")

## Ported from HTMLOrSVGElement.h:21 Web::HTML::HTMLOrSVGElement::nonce.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
pub func html_or_svg_element_nonce[TElementBase](this: const HtmlOrSvgElement[TElementBase]*) -> ak.String:
    return this.m_cryptographic_nonce

## Ported from HTMLOrSVGElement.h:22 Web::HTML::HTMLOrSVGElement::set_nonce.
pub func html_or_svg_element_set_nonce[TElementBase](this: HtmlOrSvgElement[TElementBase]*, nonce: ak.String):
    this.m_cryptographic_nonce = nonce

## Ported from HTMLOrSVGElement.cpp:32 Web::HTML::HTMLOrSVGElement::focus.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
pub func html_or_svg_element_focus[TElementBase](this: HtmlOrSvgElement[TElementBase]*):
    let element = html_or_svg_element_element(this)

    # 1. If the allow focus steps given this's node document return false, then return.
    if not dom_document_allow_focus(dom_element_document(element)):
        return

    # 2. Run the focusing steps for this.
    html_run_focusing_steps(element.dom_node(), none, HtmlFocusTrigger.script)

    # FIXME: 3. If options["focusVisible"] is true, or does not exist but in an implementation-defined way the user agent determines it would be best to do so, then indicate focus.

    # FIXME: 4. If options["preventScroll"] is false, then scroll a target into view given this, "auto", "center", and "center".

## Ported from HTMLOrSVGElement.cpp:48 Web::HTML::HTMLOrSVGElement::blur.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-blur
pub func html_or_svg_element_blur[TElementBase](this: HtmlOrSvgElement[TElementBase]*):
    # 1. The user agent should run the unfocusing steps given this.
    #    User agents may instead selectively or uniformly do nothing, for usability reasons.
    html_run_unfocusing_steps(html_or_svg_element_element(this).dom_node())

## Ported from HTMLOrSVGElement.cpp:57 Web::HTML::HTMLOrSVGElement::attribute_changed.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
pub func html_or_svg_element_attribute_changed[TElementBase](this: HtmlOrSvgElement[TElementBase]*, local_name: ak.FlyString, arg1: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    # 1. If element does not include HTMLOrSVGElement, then return.
    # 2. If localName is not nonce or namespace is not null, then return.
    if not ak.fly_string_eq_fly_string(&local_name, &html_attribute_names_nonce) or namespace_ != none:
        return

    # 3. If value is null, then set element's [[CryptographicNonce]] to the empty string.
    if let present_value = value:
        # 4. Otherwise, set element's [[CryptographicNonce]] to value.
        this.m_cryptographic_nonce = present_value
    else:
        this.m_cryptographic_nonce = ak.string_new()

## Ported from HTMLOrSVGElement.cpp:77 Web::HTML::HTMLOrSVGElement::cloned.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
pub func html_or_svg_element_cloned[TElementBase](this: const HtmlOrSvgElement[TElementBase]*, copy: DomNode*, arg1: bool) -> !:
    # The cloning steps for elements that include HTMLOrSVGElement given node, copy, and subtree
    # are to set copy's [[CryptographicNonce]] to node's [[CryptographicNonce]].
    html_or_svg_element_of[TElementBase](copy).m_cryptographic_nonce = this.m_cryptographic_nonce

## `candidates.remove_first_matching([&element](auto const& other) { return other.ptr() == &element; })`.
func html_or_svg_element_is_element(context: void*?, other: const (DomElement*)*) -> bool:
    let element = (DomElement*)(context else trap("no element"))
    return *other == element

## Ported from HTMLOrSVGElement.cpp:87 Web::HTML::HTMLOrSVGElement::inserted.
# https://html.spec.whatwg.org/multipage/urls-and-fetching.html#dom-noncedelement-nonce
pub func html_or_svg_element_inserted[TElementBase](this: HtmlOrSvgElement[TElementBase]*):
    # Whenever an element including HTMLOrSVGElement becomes browsing-context connected, the user
    # agent must execute the following steps on the element:
    let element = html_or_svg_element_element(this)

    # "A node becomes browsing-context connected when the insertion steps are invoked with it as the argument
    # and it is now browsing-context connected."
    # https://html.spec.whatwg.org/multipage/infrastructure.html#becomes-browsing-context-connected
    if not dom_node_is_browsing_context_connected(dom_node_shadow_including_root(element.dom_node())):
        return

    # 1. Let CSP list be element's shadow-including root's policy container's CSP list.
    let csp_list = dom_document_policy_container(dom_node_document(dom_node_shadow_including_root(element.dom_node()))).csp_list

    # 2. If CSP list contains a header-delivered Content Security Policy, and element has a
    #    nonce content attribute whose value is not the empty string, then:
    if content_security_policy_policy_list_contains_header_delivered_policy(csp_list) and dom_element_has_attribute(element, html_attribute_names_nonce):
        # 2.1. Let nonce be element's [[CryptographicNonce]].
        let nonce = this.m_cryptographic_nonce

        # 2.2. Set an attribute value for element using "nonce" and the empty string.
        dom_element_set_attribute_value(element, html_attribute_names_nonce, ak.string_new())

        # 2.3. Set element's [[CryptographicNonce]] to nonce.
        this.m_cryptographic_nonce = nonce

    # https://html.spec.whatwg.org/multipage/interaction.html#the-autofocus-attribute
    if dom_element_has_attribute(element, html_attribute_names_autofocus):
        # When an element with the autofocus attribute specified is inserted into a document, run the following steps:

        # FIXME: 1. If the user has indicated (for example, by starting to type in a form control) that they do not
        #           wish focus to be changed, then optionally return.

        # 2. Let target be the element's node document.
        let target = dom_element_document(element)

        # 3. If target is not fully active, then return.
        if not dom_document_is_fully_active(target):
            return

        # 4. If target's active sandboxing flag set has the sandboxed automatic features browsing
        #    context flag, then return.
        if html_has_flag(dom_document_active_sandboxing_flag_set(target), HtmlSandboxingFlagSet.sandboxed_automatic_features):
            return

        # 5. If the allow focus steps given target return false, then return.
        if not dom_document_allow_focus(target):
            return

        # 6. Let topDocument be target's node navigable's top-level traversable's active document.
        let navigable = dom_document_navigable(target) else trap("VERIFY: navigable of a fully active document")
        let top_level_traversable = html_navigable_top_level_traversable(navigable) else trap("VERIFY: top-level traversable")
        let top_document = html_navigable_active_document(top_level_traversable.html_navigable()) else trap("VERIFY: active document")

        # 7. If topDocument's autofocus processed flag is false, then remove the element from topDocument's autofocus
        #    candidates, and append the element to topDocument's autofocus candidates.
        if not dom_document_autofocus_processed_flag(top_document):
            let candidates = dom_document_autofocus_candidates(top_document)
            _ = ak.vector_remove_first_matching(candidates, ak.Function1[const (DomElement*)*, bool](call = html_or_svg_element_is_element, context = element))
            ak.vector_append_t(candidates, element)

## Ported from HTMLOrSVGElement.cpp:152 Web::HTML::HTMLOrSVGElement::visit_edges.
pub func html_or_svg_element_visit_edges[TElementBase](this: HtmlOrSvgElement[TElementBase]*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t[HtmlDomStringMap](visitor, this.m_dataset)