Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_element_6.lucb

#==============================================================================================
#
#   html_element_6 - HTML::HTMLElement: accessKeyLabel, draggable, spellcheck,
#                    writingSuggestions, autocapitalize, autocorrect, heading levels and the
#                    inline members of HTMLElement.h
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLElement.cpp (lines 2097-2508) and the inline functions of
#       HTMLElement.h (title, contentEditable state, the labelable / command / button layout
#       virtuals, the popover accessors, inertness, is_html_element and the two mixin
#       overrides that answer the element itself).
#
#==============================================================================================

# mark: accessKeyLabel and draggable ============================================================

## Ported from HTMLElement.cpp:2099 Web::HTML::HTMLElement::access_key_label.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-accesskeylabel
pub func html_element_access_key_label(this: HtmlElement*) -> ak.String:
    # The accessKeyLabel IDL attribute must return a string that represents the element's assigned access key, if any.
    # If the element does not have one, then the IDL attribute must return the empty string.

    # An element's assigned access key is a key combination derived from the element's accesskey content attribute.
    # https://html.spec.whatwg.org/multipage/interaction.html#assigned-access-key

    # 1. If the element has no accesskey attribute, then skip to the fallback step below.
    let access_key = dom_element_get_attribute(this.dom_element(), html_attribute_names_accesskey) else return ak.string_new()
    if ak.string_is_empty(&access_key):
        return ak.string_new()

    # 2. Otherwise, split the attribute's value on ASCII whitespace, and let keys be the resulting tokens.
    # 3. For each value in keys in turn, in the order the tokens appeared in the attribute's value, run the following substeps:
    #    3.1. If the value is not a string exactly one code point in length, then skip the remainder of these steps for this value.
    # NB: We mimic Chromium here and treat the attribute value as a single key rather than splitting on whitespace.
    #     The spec says to split on whitespace and try each token, but no browser besides IE/Edge implemented that.
    #     If there is more than one code point, no access key is assigned. https://github.com/whatwg/html/issues/3769
    let code_points = ak.string_code_points(&access_key)
    if ak.utf8_view_length(&code_points) > 1:
        return ak.string_new()

    # FIXME: 3.2. If the value does not correspond to a key on the system's keyboard, then skip the remainder of these steps for this value.
    # FIXME: 3.3. If the user agent can find a mix of zero or more modifier keys that, combined with the key that corresponds to
    #             the value given in the attribute, can be used as the access key, then the user agent may assign that combination
    #             of keys as the element's assigned access key and return.
    return access_key

    # 4. Fallback: Optionally, the user agent may assign a key combination of its choosing as the element's assigned access key
    #    and then return.
    # 5. If this step is reached, the element has no assigned access key.

## Ported from HTMLElement.cpp:2133 Web::HTML::HTMLElement::draggable.
# https://html.spec.whatwg.org/multipage/dnd.html#dom-draggable
pub func html_element_draggable(this: HtmlElement*) -> bool:
    let attribute = dom_element_get_attribute(this.dom_element(), html_attribute_names_draggable)

    # If an element's draggable content attribute has the state True, the draggable IDL attribute must return true.
    if html_element_string_is(attribute, "true"):
        return true

    # If an element's draggable content attribute has the state False, the draggable IDL attribute must return false.
    if html_element_string_is(attribute, "false"):
        return false

    # Otherwise, the element's draggable content attribute has the state Auto.

    # If the element is an img element, the draggable IDL attribute must return true.
    if is_html_image_element(this):
        return true

    # If the element is an object element that represents an image, the draggable IDL attribute must return true.
    if is_html_object_element(this):
        if html_element_string_is(dom_element_get_attribute(this.dom_element(), html_attribute_names_type), "image"):
            return true

    # If the element is an a element with an href content attribute, the draggable IDL attribute must return true.
    if is_html_anchor_element(this) and dom_element_has_attribute(this.dom_element(), html_attribute_names_href):
        return true

    # Otherwise, the draggable IDL attribute must return false.
    return false

## Ported from HTMLElement.cpp:2169 Web::HTML::HTMLElement::set_draggable.
pub func html_element_set_draggable(this: HtmlElement*, draggable: bool):
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_draggable, ak.string_from_str("true" if draggable else "false"))

# mark: spellcheck and writingSuggestions =======================================================

## `first_ancestor_of_type<HTMLElement>()`.
func html_element_first_ancestor_html_element(this: HtmlElement*) -> HtmlElement*?:
    let ancestor = tree_node_first_ancestor_of_type(this.dom_node(), &html_element_class) else return none
    return as_html_element(ancestor)

## Ported from HTMLElement.cpp:2175 Web::HTML::HTMLElement::spellcheck.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-spellcheck
pub func html_element_spellcheck(this: HtmlElement*) -> bool:
    # The spellcheck attribute is an enumerated attribute with the following keywords and states:
    # Keyword            | State | Brief description
    # true               | True  | Spelling and grammar will be checked.
    # (the empty string) |       |
    # false              | False | and grammar will not be checked.

    # The attribute's missing value default and invalid value default are both the Default state. The default state
    # indicates that the element is to act according to a default behavior, possibly based on the parent element's
    # own spellcheck state, as defined below.

    # For each element, user agents must establish a default behavior, either through defaults or through preferences
    # expressed by the user. There are three possible default behaviors for each element:

    # true-by-default
    #     The element will be checked for spelling and grammar if its contents are editable and spellchecking is not
    #     explicitly disabled through the spellcheck attribute.
    # false-by-default
    #     The element will never be checked for spelling and grammar unless spellchecking is explicitly enabled
    #     through the spellcheck attribute.
    # inherit-by-default
    #     The element's default behavior is the same as its parent element's. Elements that have no parent element
    #     cannot have this as their default behavior.

    # NOTE: We use "true-by-default" for elements which are editable, editing hosts, or form associated text control
    #       elements "false-by-default" for root elements, and "inherit-by-default" for other elements.

    let maybe_spellcheck_attribute = dom_element_attribute(this.dom_element(), html_attribute_names_spellcheck)

    # The spellcheck IDL attribute, on getting, must return true if the element's spellcheck content attribute is in the True state,
    if html_element_string_is(maybe_spellcheck_attribute, "true") or html_element_string_is_empty(maybe_spellcheck_attribute):
        return true

    if not html_element_string_is(maybe_spellcheck_attribute, "false"):
        # or if the element's spellcheck content attribute is in the Default state and the element's default behavior is true-by-default,
        if dom_node_is_editable_or_editing_host(this.dom_node()) or as_if_form_associated_text_control_element(this) != none:
            return true

        # or if the element's spellcheck content attribute is in the Default state and the element's default behavior is inherit-by-default
        if let parent_html_element = html_element_first_ancestor_html_element(this):
            # and the element's parent element's spellcheck IDL attribute would return true;
            if html_element_spellcheck(parent_html_element):
                return true

    # if none of those conditions applies, then the attribute must instead return false.
    return false

## Ported from HTMLElement.cpp:2227 Web::HTML::HTMLElement::set_spellcheck.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-spellcheck
pub func html_element_set_spellcheck(this: HtmlElement*, spellcheck: bool):
    # On setting, if the new value is true, then the element's spellcheck content attribute must be set to "true", otherwise it must be set to "false".
    if spellcheck:
        dom_element_set_attribute_value(this.dom_element(), html_attribute_names_spellcheck, ak.string_from_str("true"))
    else:
        dom_element_set_attribute_value(this.dom_element(), html_attribute_names_spellcheck, ak.string_from_str("false"))

## Ported from HTMLElement.cpp:2237 Web::HTML::HTMLElement::writing_suggestions.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-writingsuggestions
pub func html_element_writing_suggestions(this: HtmlElement*) -> ak.String:
    # The writingsuggestions content attribute is an enumerated attribute with the following keywords and states:
    # Keyword            | State | Brief description
    # true               | True  | Writing suggestions should be offered on this element.
    # (the empty string) |       |
    # false              | False | Writing suggestions should not be offered on this element.

    # The attribute's missing value default is the Default state. The default state indicates that the element is to
    # act according to a default behavior, possibly based on the parent element's own writingsuggestions state, as
    # defined below.

    # The attribute's invalid value default is the True state.

    # 1. If element's writingsuggestions content attribute is in the False state, return "false".
    let maybe_writing_suggestions_attribute = dom_element_attribute(this.dom_element(), html_attribute_names_writingsuggestions)

    if html_element_string_is(maybe_writing_suggestions_attribute, "false"):
        return ak.string_from_str("false")

    # 2. If element's writingsuggestions content attribute is in the Default state, element has a parent element, and the computed writing suggestions value of element's parent element is "false", then return "false".
    if maybe_writing_suggestions_attribute == none:
        if let parent = html_element_first_ancestor_html_element(this):
            let parent_value = html_element_writing_suggestions(parent)
            if ak.string_eq_string_view(&parent_value, ak.sv("false")):
                return ak.string_from_str("false")

    # 3. Return "true".
    return ak.string_from_str("true")

## Ported from HTMLElement.cpp:2267 Web::HTML::HTMLElement::set_writing_suggestions.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-writingsuggestions
pub func html_element_set_writing_suggestions(this: HtmlElement*, given_value: ak.String):
    # 1. Set this's writingsuggestions content attribute to the given value.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_writingsuggestions, given_value)

# mark: autocapitalize and autocorrect ==========================================================

## `form_associated_element->form()` of an autocapitalize-and-autocorrect inheriting element.
func html_element_inheriting_form_owner(this: HtmlElement*) -> HtmlFormElement*?:
    let form_associated_element = as_if_form_associated_element(this) else return none
    if not html_form_associated_element_is_autocapitalize_and_autocorrect_inheriting(form_associated_element):
        return none
    return html_form_associated_element_form(form_associated_element)

## Ported from HTMLElement.cpp:2274 Web::HTML::HTMLElement::own_autocapitalization_hint.
# https://html.spec.whatwg.org/multipage/interaction.html#own-autocapitalization-hint
pub func html_element_own_autocapitalization_hint(this: HtmlElement*) -> HtmlElementAutocapitalizationHint:
    # The autocapitalization processing model is based on selecting among five autocapitalization hints, defined as follows:
    #
    # Default
    #     The user agent and input method should make their own determination of whether or not to enable autocapitalization.
    # none
    #     No autocapitalization should be applied (all letters should default to lowercase).
    # Sentences
    #     The first letter of each sentence should default to a capital letter; all other letters should default to lowercase.
    # Words
    #     The first letter of each word should default to a capital letter; all other letters should default to lowercase.
    # Characters
    #     All letters should default to uppercase.

    # The autocapitalize attribute is an enumerated attribute whose states are the possible autocapitalization hints.
    # The autocapitalization hint specified by the attribute's state combines with other considerations to form the
    # used autocapitalization hint, which informs the behavior of the user agent. The keywords for this attribute and
    # their state mappings are as follows:

    # Keyword    | State
    # off        | None
    # none       |
    # on         | Sentences
    # sentences  |
    # words      | Words
    # characters | Characters

    # The attribute's missing value default is the Default state, and its invalid value default is the Sentences state.

    # To compute the own autocapitalization hint of an element element, run the following steps:
    # 1. If the autocapitalize content attribute is present on element, and its value is not the empty string, return the
    #    state of the attribute.
    let maybe_autocapitalize_attribute = dom_element_attribute(this.dom_element(), html_attribute_names_autocapitalize)

    if let autocapitalize_attribute = maybe_autocapitalize_attribute:
        if not ak.string_is_empty(&autocapitalize_attribute):
            let autocapitalize_attribute_string_view = ak.string_bytes_as_string_view(&autocapitalize_attribute)

            let none_keywords: str[2] = ["off", "none"]
            if ak.string_view_is_one_of_ignoring_ascii_case(&autocapitalize_attribute_string_view, none_keywords):
                return HtmlElementAutocapitalizationHint.none_

            if ak.string_view_equals_ignoring_ascii_case(&autocapitalize_attribute_string_view, ak.sv("words")):
                return HtmlElementAutocapitalizationHint.words

            if ak.string_view_equals_ignoring_ascii_case(&autocapitalize_attribute_string_view, ak.sv("characters")):
                return HtmlElementAutocapitalizationHint.characters

            return HtmlElementAutocapitalizationHint.sentences

    # 2. If element is an autocapitalize-and-autocorrect inheriting element and has a non-null form owner, return the
    #    own autocapitalization hint of element's form owner.
    if let form = html_element_inheriting_form_owner(this):
        return html_element_own_autocapitalization_hint(form.html_element())

    # 3. Return Default.
    return HtmlElementAutocapitalizationHint.default

## Ported from HTMLElement.cpp:2335 Web::HTML::HTMLElement::autocapitalize.
# https://html.spec.whatwg.org/multipage/interaction.html#attr-autocapitalize
pub func html_element_autocapitalize(this: HtmlElement*) -> ak.String:
    # The autocapitalize getter steps are to:
    # 1. Let state be the own autocapitalization hint of this.
    let state = html_element_own_autocapitalization_hint(this)

    # 2. If state is Default, then return the empty string.
    # 3. If state is None, then return "none".
    # 4. If state is Sentences, then return "sentences".
    # 5. Return the keyword value corresponding to state.
    match state:
        .default: return ak.string_new()
        .none_: return ak.string_from_str("none")
        .sentences: return ak.string_from_str("sentences")
        .words: return ak.string_from_str("words")
        .characters: return ak.string_from_str("characters")
        _: trap("VERIFY_NOT_REACHED")

## Ported from HTMLElement.cpp:2361 Web::HTML::HTMLElement::set_autocapitalize.
pub func html_element_set_autocapitalize(this: HtmlElement*, given_value: ak.String):
    # The autocapitalize setter steps are to set the autocapitalize content attribute to the given value.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_autocapitalize, given_value)

## used_autocorrection_state's autocorrect_attribute_state: `[](Optional<String> attribute) {...}`.
func html_element_autocorrect_attribute_state(attribute: ak.String?) -> HtmlElementAutocorrectionState:
    if html_element_string_is(attribute, "off"):
        return HtmlElementAutocorrectionState.off

    return HtmlElementAutocorrectionState.on

## Ported from HTMLElement.cpp:2368 Web::HTML::HTMLElement::used_autocorrection_state.
# https://html.spec.whatwg.org/multipage/interaction.html#used-autocorrection-state
pub func html_element_used_autocorrection_state(this: HtmlElement*) -> HtmlElementAutocorrectionState:
    # The autocorrect attribute is an enumerated attribute with the following keywords and states:
    # Keyword            | State | Brief description
    # on                 | On    | The user agent is permitted to automatically correct spelling errors while the user
    # (the empty string) |       | types. Whether spelling is automatically corrected while typing left is for the user
    #                    |       | agent to decide, and may depend on the element as well as the user's preferences.
    # off                | Off   | The user agent is not allowed to automatically correct spelling while the user types.

    # The attribute's invalid value default and missing value default are both the On state.

    # To compute the used autocorrection state of an element element, run these steps:
    # 1. If element is an input element whose type attribute is in one of the URL, E-mail, or Password states, then
    #    return Off.
    if let input_element = as_if_html_input_element(this):
        let type_state = html_input_element_type_state(input_element)
        if type_state == HtmlInputElementTypeAttributeState.url or type_state == HtmlInputElementTypeAttributeState.email or type_state == HtmlInputElementTypeAttributeState.password:
            return HtmlElementAutocorrectionState.off

    # 2. If the autocorrect content attribute is present on element, then return the state of the attribute.
    let maybe_autocorrect_attribute = dom_element_attribute(this.dom_element(), html_attribute_names_autocorrect)

    if maybe_autocorrect_attribute != none:
        return html_element_autocorrect_attribute_state(maybe_autocorrect_attribute)

    # 3. If element is an autocapitalize-and-autocorrect inheriting element and has a non-null form owner, then return
    #    the state of element's form owner's autocorrect attribute.
    if let form = html_element_inheriting_form_owner(this):
        return html_element_autocorrect_attribute_state(dom_element_attribute(form.dom_element(), html_attribute_names_autocorrect))

    # 4. Return On.
    return HtmlElementAutocorrectionState.on

## Ported from HTMLElement.cpp:2412 Web::HTML::HTMLElement::autocorrect.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-autocorrect
pub func html_element_autocorrect(this: HtmlElement*) -> bool:
    # The autocorrect getter steps are: return true if the element's used autocorrection state is On and false if the
    # element's used autocorrection state is Off.
    return html_element_used_autocorrection_state(this) == HtmlElementAutocorrectionState.on

## Ported from HTMLElement.cpp:2420 Web::HTML::HTMLElement::set_autocorrect.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-autocorrect
pub func html_element_set_autocorrect(this: HtmlElement*, given_value: bool):
    # The setter steps are: if the given value is true, then the element's autocorrect attribute must be set to "on";
    # otherwise it must be set to "off".
    if given_value:
        dom_element_set_attribute_value(this.dom_element(), html_attribute_names_autocorrect, ak.string_from_str("on"))
    else:
        dom_element_set_attribute_value(this.dom_element(), html_attribute_names_autocorrect, ak.string_from_str("off"))

# mark: Heading levels ==========================================================================

## Ported from HTMLElement.cpp:2431 Web::HTML::HTMLElement::computed_heading_level.
# https://html.spec.whatwg.org/multipage/sections.html#get-an-element's-computed-heading-level
pub func html_element_computed_heading_level(this: HtmlElement*) -> u32:
    # 1. Let level be 0.
    var level: u32 = 0

    # 2. If element's local name is h1, then set level to 1.
    if html_element_local_name_is(this, html_tag_names_h1):
        level = 1
    # 3. If element's local name is h2, then set level to 2.
    elif html_element_local_name_is(this, html_tag_names_h2):
        level = 2
    # 4. If element's local name is h3, then set level to 3.
    elif html_element_local_name_is(this, html_tag_names_h3):
        level = 3
    # 5. If element's local name is h4, then set level to 4.
    elif html_element_local_name_is(this, html_tag_names_h4):
        level = 4
    # 6. If element's local name is h5, then set level to 5.
    elif html_element_local_name_is(this, html_tag_names_h5):
        level = 5
    # 7. If element's local name is h6, then set level to 6.
    elif html_element_local_name_is(this, html_tag_names_h6):
        level = 6

    # 8. Assert: level is not 0.
    assert(level != 0)

    # 9. Increment level by the result of getting an element's computed heading offset given element.
    level = level +% html_element_computed_heading_offset(this)

    # 10. If level is greater than 9, then return 9.
    if level > 9:
        return 9

    # 11. Return level.
    return level

## Ported from HTMLElement.cpp:2469 Web::HTML::HTMLElement::computed_heading_offset.
pub func html_element_computed_heading_offset(this: HtmlElement*) -> u32:
    # 1. Let offset be 0.
    var offset: u32 = 0

    # 2. Let inclusiveAncestor be element.
    var inclusive_ancestor: DomNode*? = this.dom_node()

    # 3. While inclusiveAncestor is not null:
    while let ancestor = inclusive_ancestor:
        # 1. Let nextOffset be 0.
        var next_offset: u32 = 0

        # 2. If inclusiveAncestor is an HTML element and has a headingoffset attribute, then parse its value using the
        #    rules for parsing non-negative integers.
        #    If the result of parsing the value is not an error, then set nextOffset to that value.
        let inclusive_ancestor_html_element = as_if_html_element(ancestor)
        if let html_element = inclusive_ancestor_html_element:
            if let attribute = dom_element_get_attribute(html_element.dom_element(), html_attribute_names_headingoffset):
                if let heading_offset = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&attribute)):
                    next_offset = heading_offset

        # 3. Increment offset by nextOffset.
        offset = offset +% next_offset

        # 4. If inclusiveAncestor is an HTML element and has a headingreset attribute, then return offset.
        if let html_element = inclusive_ancestor_html_element:
            if dom_element_has_attribute(html_element.dom_element(), html_attribute_names_headingreset):
                return offset

        # 5. Set inclusiveAncestor to the parent node of inclusiveAncestor within the flat tree.
        inclusive_ancestor = none
        if let parent = dom_node_flat_tree_parent(ancestor):
            inclusive_ancestor = parent.dom_node()

    # 4. Return offset.
    return offset

# mark: HTMLElement.h ===========================================================================

## Ported from HTMLElement.h:86 Web::HTML::HTMLElement::title.
pub func html_element_title(this: HtmlElement*) -> ak.String?:
    return dom_element_attribute(this.dom_element(), html_attribute_names_title)

## Ported from HTMLElement.h:97 Web::HTML::HTMLElement::content_editable_state.
pub func html_element_content_editable_state(this: HtmlElement*) -> HtmlContentEditableState:
    return this.m_content_editable_state

## Ported from HTMLElement.h:150 Web::HTML::HTMLElement::is_labelable (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#category-label
pub func html_element_is_labelable_impl(this: HtmlElement*) -> bool:
    return html_element_is_form_associated_custom_element(this)

## Ported from HTMLElement.h:160 Web::HTML::HTMLElement::opened_in_popover_mode.
pub func html_element_opened_in_popover_mode(this: HtmlElement*) -> ak.String?:
    return this.m_opened_in_popover_mode

## Ported from HTMLElement.h:169 Web::HTML::HTMLElement::popover_visibility_state.
pub func html_element_popover_visibility_state(this: HtmlElement*) -> HtmlElementPopoverVisibilityState:
    return this.m_popover_visibility_state

## Ported from HTMLElement.h:184 Web::HTML::HTMLElement::is_inert.
pub func html_element_is_inert(this: HtmlElement*) -> bool:
    return this.m_inert

## Ported from HTMLElement.h:189 Web::HTML::HTMLElement::is_valid_command (virtual).
pub func html_element_is_valid_command_impl(this: HtmlElement*, arg0: ak.String*) -> bool:
    return false

## Ported from HTMLElement.h:190 Web::HTML::HTMLElement::command_steps (virtual).
pub func html_element_command_steps_impl(this: HtmlElement*, arg0: DomElement*, arg1: ak.String*):
    return

## Ported from HTMLElement.h:195 Web::HTML::HTMLElement::uses_button_layout (virtual).
# https://html.spec.whatwg.org/multipage/rendering.html#button-layout
pub func html_element_uses_button_layout_impl(this: HtmlElement*) -> bool:
    return false

## Ported from HTMLElement.h:211 Web::HTML::HTMLElement::set_inert.
pub func html_element_set_inert(this: HtmlElement*, inert: bool):
    this.m_inert = inert

## Ported from HTMLElement.h:217 Web::HTML::HTMLElement::is_html_element (override).
pub func html_element_is_html_element_impl(base: DomNode*) -> bool:
    return true

## Ported from HTMLElement.h:220 Web::HTML::HTMLElement::form_associated_element_to_html_element (override).
pub func html_element_form_associated_element_to_html_element_impl(base: HtmlFormAssociatedElement*) -> HtmlElement*:
    return (HtmlElement*)((u8*)base - memory.offset_of(HtmlElement, form_associated_element))

## Ported from HTMLElement.h:225 Web::HTML::HTMLElement::global_event_handlers_to_event_target (override).
pub func html_element_global_event_handlers_to_event_target_impl(base: HtmlGlobalEventHandlers*, arg0: ak.FlyString) -> DomEventTarget*?:
    return (DomEventTarget*)((u8*)base - memory.offset_of(HtmlElement, global_event_handlers))