Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_anchor_element.lucb

#==============================================================================================
#
#   html_anchor_element - HTML::HTMLAnchorElement: <a>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLAnchorElement.cpp (lines 1-165) and the inlines of
#       HTMLAnchorElement.h: construction (which moves the mixin tables to HTMLAnchorElement's,
#       as C++'s vptrs move, and sets up the HTMLHyperlinkElementUtils base), initialize,
#       visit_edges, attribute_changed (the URL and relList), the activation behavior that
#       follows the hyperlink, the tab index and ARIA role, relList and text, and the reflected
#       rel / target / download attributes.
#
#==============================================================================================

# mark: HTMLAnchorElement.cpp ====================================================================

## Ported from HTMLAnchorElement.cpp:24 Web::HTML::HTMLAnchorElement::HTMLAnchorElement.
pub func html_anchor_element_construct(this: HtmlAnchorElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The HTMLHyperlinkElementUtils base (its URL starts null), then the vptrs of the mixin bases
    # move to HTMLAnchorElement's tables.
    this.html_hyperlink_element_utils.vtable = &html_anchor_element_html_hyperlink_element_utils_vtable
    this.html_hyperlink_element_utils.class_id = this.cell().class_id
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_anchor_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_anchor_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_anchor_element_animatable_vtable
    element.global_event_handlers.vtable = &html_anchor_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_anchor_element_form_associated_element_vtable

## Ported from HTMLAnchorElement.cpp:31 Web::HTML::HTMLAnchorElement::initialize (override).
pub func html_anchor_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_anchor_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLAnchorElement.cpp:37 Web::HTML::HTMLAnchorElement::visit_edges (override).
pub func html_anchor_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlAnchorElement*)base
    html_element_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t[DomTokenList](visitor, this.m_rel_list)

## Ported from HTMLAnchorElement.cpp:43 Web::HTML::HTMLAnchorElement::attribute_changed (override).
pub func html_anchor_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlAnchorElement*)base
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)

    if html_name_is(name, html_attribute_names_href):
        html_hyperlink_element_utils_set_the_url(&this.html_hyperlink_element_utils)
    elif html_name_is(name, html_attribute_names_rel):
        if let rel_list = this.m_rel_list:
            let value_or_empty = value else ak.string_new()
            dom_token_list_associated_attribute_changed(rel_list, ak.string_bytes_as_string_view(&value_or_empty))

## Ported from HTMLAnchorElement.cpp:55 Web::HTML::HTMLAnchorElement::has_activation_behavior (override).
pub func html_anchor_element_has_activation_behavior_impl(base: DomEventTarget*) -> bool:
    return true

## Ported from HTMLAnchorElement.cpp:61 Web::HTML::HTMLAnchorElement::activation_behavior (override).
# https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements
pub func html_anchor_element_activation_behavior_impl(base: DomEventTarget*, event: DomEvent*):
    let this = (HtmlAnchorElement*)base
    # The activation behavior of an a or area element element given an event event is:

    # 1. If element has no href attribute, then return.
    let href = html_hyperlink_element_utils_href(&this.html_hyperlink_element_utils)
    if ak.string_is_empty(&href):
        return

    # AD-HOC: Do not activate the element for clicks with the ctrl/cmd modifier present. This lets
    #         the browser process open the link in a new tab.
    if dom_event_is_mouse_event(event):
        let mouse_event = as_ui_events_mouse_event(event)
        if ui_events_mouse_event_platform_ctrl_key(mouse_event):
            return

    # 2. Let hyperlinkSuffix be null.
    var hyperlink_suffix: ak.String? = none

    # 3. If element is an a element, and event's target is an img with an ismap attribute specified, then:
    if let target = dom_event_target(event):
        if is_html_image_element(target) and dom_element_has_attribute((DomElement*)target, html_attribute_names_ismap):
            # 1. Let x and y be 0.
            var x = gfx.css_pixels_from_int(0)
            var y = gfx.css_pixels_from_int(0)

            # 2. If event's isTrusted attribute is initialized to true, then set x to the distance in CSS pixels from the left edge of the image
            #    to the location of the click, and set y to the distance in CSS pixels from the top edge of the image to the location of the click.
            if dom_event_is_trusted(event) and dom_event_is_mouse_event(event):
                let mouse_event = as_ui_events_mouse_event(event)
                x = gfx.css_pixels_nearest_value_for_f64(ui_events_mouse_event_offset_x(mouse_event))
                y = gfx.css_pixels_nearest_value_for_f64(ui_events_mouse_event_offset_y(mouse_event))

            # 3. If x is negative, set x to 0.
            if gfx.css_pixels_cmp(&x, gfx.css_pixels_from_int(0)) < 0:
                x = gfx.css_pixels_from_int(0)

            # 4. If y is negative, set y to 0.
            if gfx.css_pixels_cmp(&y, gfx.css_pixels_from_int(0)) < 0:
                y = gfx.css_pixels_from_int(0)

            # 5. Set hyperlinkSuffix to the concatenation of U+003F (?), the value of x expressed as a base-ten integer using ASCII digits,
            #    U+002C (,), and the value of y expressed as a base-ten integer using ASCII digits.
            # (MUST(String::formatted("?{},{}", x.to_int(), y.to_int())))
            let x_string = ak.string_number_i64((i64)gfx.css_pixels_to_int_const(&x))
            let y_string = ak.string_number_i64((i64)gfx.css_pixels_to_int_const(&y))
            var builder = ak.string_builder_new()
            ak.string_builder_append(&builder, "?")
            ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&x_string))
            ak.string_builder_append(&builder, ",")
            ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&y_string))
            hyperlink_suffix = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")

    # 4. Let userInvolvement be event's user navigation involvement.
    var user_involvement = html_user_navigation_involvement(event)

    # 5. If the user has expressed a preference to download the hyperlink, then set userInvolvement to "browser UI".
    # NOTE: That is, if the user has expressed a specific preference for downloading, this no longer counts as merely "activation".
    if html_anchor_element_has_download_preference(this):
        user_involvement = HtmlUserNavigationInvolvement.browser_ui

    # FIXME: 6. If element has a download attribute, or if the user has expressed a preference to download the
    #     hyperlink, then download the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and
    #     userInvolvement set to userInvolvement.

    # 7. Otherwise, follow the hyperlink created by element with hyperlinkSuffix set to hyperlinkSuffix and userInvolvement set to userInvolvement.
    dom_element_follow_the_hyperlink(this.dom_element(), hyperlink_suffix, user_involvement)

## Ported from HTMLAnchorElement.cpp:121 Web::HTML::HTMLAnchorElement::has_download_preference.
pub func html_anchor_element_has_download_preference(this: HtmlAnchorElement*) -> bool:
    return dom_element_has_attribute(this.dom_element(), html_attribute_names_download)

## Ported from HTMLAnchorElement.cpp:127 Web::HTML::HTMLAnchorElement::default_tab_index_value (override).
# https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
pub func html_anchor_element_default_tab_index_value_impl(base: DomElement*) -> i32:
    # See the base function for the spec comments.
    return 0

## Ported from HTMLAnchorElement.cpp:133 Web::HTML::HTMLAnchorElement::default_role (override).
pub func html_anchor_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    let this = (HtmlAnchorElement*)dom_element_from_aria_mixin(base)
    # https://www.w3.org/TR/html-aria/#el-a-no-href
    let href = html_hyperlink_element_utils_href(&this.html_hyperlink_element_utils)
    if not ak.string_is_empty(&href):
        return AriaRole.link
    # https://www.w3.org/TR/html-aria/#el-a
    return AriaRole.generic

## Ported from HTMLAnchorElement.cpp:143 Web::HTML::HTMLAnchorElement::rel_list.
# https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-rellist
pub func html_anchor_element_rel_list(this: HtmlAnchorElement*) -> DomTokenList*:
    # The IDL attribute relList must reflect the rel content attribute.
    if this.m_rel_list == none:
        this.m_rel_list = dom_token_list_create(this.dom_element(), html_attribute_names_rel)
    return this.m_rel_list else trap("unreachable")

## Ported from HTMLAnchorElement.cpp:152 Web::HTML::HTMLAnchorElement::text.
# https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
pub func html_anchor_element_text(this: HtmlAnchorElement*) -> ak.Utf16String:
    # The text attribute's getter must return this element's descendant text content.
    return dom_node_descendant_text_content(this.dom_node())

## Ported from HTMLAnchorElement.cpp:159 Web::HTML::HTMLAnchorElement::set_text.
# https://html.spec.whatwg.org/multipage/text-level-semantics.html#dom-a-text
pub func html_anchor_element_set_text(this: HtmlAnchorElement*, text: ak.Utf16String):
    # The text attribute's setter must string replace all with the given value within this element.
    dom_node_string_replace_all(this.dom_node(), text)

# mark: Inline members of HTMLAnchorElement.h ====================================================

## Ported from HTMLAnchorElement.h:23 Web::HTML::HTMLAnchorElement::extract_an_origin (override).
pub func html_anchor_element_extract_an_origin_impl(base: BindPlatformObject*) -> web_url.Origin?:
    let this = (HtmlAnchorElement*)base
    return html_hyperlink_element_utils_hyperlink_element_utils_extract_an_origin(&this.html_hyperlink_element_utils)

## Ported from HTMLAnchorElement.h:25 Web::HTML::HTMLAnchorElement::rel.
pub func html_anchor_element_rel(this: HtmlAnchorElement*) -> ak.String:
    return dom_element_get_attribute_value(this.dom_element(), html_attribute_names_rel)

## Ported from HTMLAnchorElement.h:26 Web::HTML::HTMLAnchorElement::target.
pub func html_anchor_element_target(this: HtmlAnchorElement*) -> ak.String:
    return dom_element_get_attribute_value(this.dom_element(), html_attribute_names_target)

## Ported from HTMLAnchorElement.h:27 Web::HTML::HTMLAnchorElement::download.
pub func html_anchor_element_download(this: HtmlAnchorElement*) -> ak.String:
    return dom_element_get_attribute_value(this.dom_element(), html_attribute_names_download)

## Ported from HTMLAnchorElement.h:36 Web::HTML::HTMLAnchorElement::is_focusable (override).
# https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-a-element
pub func html_anchor_element_is_focusable_impl(base: DomEventTarget*) -> bool:
    return html_element_is_focusable_impl(base) or dom_element_has_attribute((DomElement*)base, html_attribute_names_href)

## Ported from HTMLAnchorElement.h:38 Web::HTML::HTMLAnchorElement::is_html_anchor_element (override).
pub func html_anchor_element_is_html_anchor_element_impl(base: DomNode*) -> bool:
    return true

## Ported from HTMLAnchorElement.h:57 Web::HTML::HTMLAnchorElement::hyperlink_element_utils_element (override).
pub func html_anchor_element_hyperlink_element_utils_element_impl(base: HtmlHyperlinkElementUtils*) -> DomElement*:
    return (DomElement*)((u8*)base - memory.offset_of(HtmlAnchorElement, html_hyperlink_element_utils))

## Ported from HTMLAnchorElement.h:58 Web::HTML::HTMLAnchorElement::hyperlink_element_utils_element (override).
pub func html_anchor_element_hyperlink_element_utils_element_const_impl(base: HtmlHyperlinkElementUtils*) -> DomElement*:
    return (DomElement*)((u8*)base - memory.offset_of(HtmlAnchorElement, html_hyperlink_element_utils))