Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_input_element_7.lucb

#==============================================================================================
#
#   html_input_element_7 - HTML::HTMLInputElement: roles, activation and applicability
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLInputElement.cpp (lines 3118-3541): the default ARIA role,
#       buttons and submit buttons, single-line controls, the activation behavior, which IDL
#       attributes and methods apply to which states, the value mode of the value IDL
#       attribute, focusability and reversed ranges.
#
#==============================================================================================

# mark: Role and buttons ========================================================================

## Ported from HTMLInputElement.cpp:3118 Web::HTML::HTMLInputElement::default_role (override).
pub func html_input_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    let this = (HtmlInputElement*)dom_element_from_aria_mixin(base)
    let type_state = html_input_element_type_state(this)
    # http://wpt.live/html-aam/roles-dynamic-switch.tentative.window.html "Disconnected <input type=checkbox switch>"
    if not dom_node_is_connected(this.dom_node()):
        return none
    # https://www.w3.org/TR/html-aria/#el-input-button
    if type_state == HtmlInputElementTypeAttributeState.button:
        return AriaRole.button
    # https://www.w3.org/TR/html-aria/#el-input-checkbox
    if type_state == HtmlInputElementTypeAttributeState.checkbox:
        # https://github.com/w3c/html-aam/issues/496
        if html_input_element_has_attribute(this, html_attribute_names_switch):
            return AriaRole.switch
        return AriaRole.checkbox
    # https://www.w3.org/TR/html-aria/#el-input-email
    if type_state == HtmlInputElementTypeAttributeState.email and not html_input_element_has_attribute(this, html_attribute_names_list):
        return AriaRole.textbox
    # https://www.w3.org/TR/html-aria/#el-input-image
    if type_state == HtmlInputElementTypeAttributeState.image_button:
        return AriaRole.button
    # https://www.w3.org/TR/html-aria/#el-input-number
    if type_state == HtmlInputElementTypeAttributeState.number:
        return AriaRole.spinbutton
    # https://www.w3.org/TR/html-aria/#el-input-radio
    if type_state == HtmlInputElementTypeAttributeState.radio_button:
        return AriaRole.radio
    # https://www.w3.org/TR/html-aria/#el-input-range
    if type_state == HtmlInputElementTypeAttributeState.range:
        return AriaRole.slider
    # https://www.w3.org/TR/html-aria/#el-input-reset
    if type_state == HtmlInputElementTypeAttributeState.reset_button:
        return AriaRole.button
    # https://www.w3.org/TR/html-aria/#el-input-text-list
    if (type_state == HtmlInputElementTypeAttributeState.text or type_state == HtmlInputElementTypeAttributeState.search or type_state == HtmlInputElementTypeAttributeState.telephone or type_state == HtmlInputElementTypeAttributeState.url or type_state == HtmlInputElementTypeAttributeState.email) and html_input_element_has_attribute(this, html_attribute_names_list):
        return AriaRole.combobox
    # https://www.w3.org/TR/html-aria/#el-input-search
    if type_state == HtmlInputElementTypeAttributeState.search and not html_input_element_has_attribute(this, html_attribute_names_list):
        return AriaRole.searchbox
    # https://www.w3.org/TR/html-aria/#el-input-submit
    if type_state == HtmlInputElementTypeAttributeState.submit_button:
        return AriaRole.button
    # https://www.w3.org/TR/html-aria/#el-input-tel
    if type_state == HtmlInputElementTypeAttributeState.telephone:
        return AriaRole.textbox
    # https://www.w3.org/TR/html-aria/#el-input-text
    if type_state == HtmlInputElementTypeAttributeState.text and not html_input_element_has_attribute(this, html_attribute_names_list):
        return AriaRole.textbox
    # https://www.w3.org/TR/html-aria/#el-input-url
    if type_state == HtmlInputElementTypeAttributeState.url and not html_input_element_has_attribute(this, html_attribute_names_list):
        return AriaRole.textbox

    # https://www.w3.org/TR/html-aria/#el-input-color
    # https://www.w3.org/TR/html-aria/#el-input-date
    # https://www.w3.org/TR/html-aria/#el-input-datetime-local
    # https://www.w3.org/TR/html-aria/#el-input-file
    # https://www.w3.org/TR/html-aria/#el-input-hidden
    # https://www.w3.org/TR/html-aria/#el-input-month
    # https://www.w3.org/TR/html-aria/#el-input-password
    # https://www.w3.org/TR/html-aria/#el-input-time
    # https://www.w3.org/TR/html-aria/#el-input-week
    return none

## Ported from HTMLInputElement.cpp:3187 Web::HTML::HTMLInputElement::is_button (override).
pub func html_input_element_is_button_impl(base: HtmlFormAssociatedElement*) -> bool:
    let this = html_input_element_from_form_associated_element(base)
    let type_state = html_input_element_type_state(this)
    # https://html.spec.whatwg.org/multipage/input.html#submit-button-state-(type=submit):concept-button
    # https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):concept-button
    # https://html.spec.whatwg.org/multipage/input.html#reset-button-state-(type=reset):concept-button
    # https://html.spec.whatwg.org/multipage/input.html#button-state-(type=button):concept-button
    return type_state == HtmlInputElementTypeAttributeState.submit_button or type_state == HtmlInputElementTypeAttributeState.image_button or type_state == HtmlInputElementTypeAttributeState.reset_button or type_state == HtmlInputElementTypeAttributeState.button

## Ported from HTMLInputElement.cpp:3199 Web::HTML::HTMLInputElement::is_submit_button (override).
pub func html_input_element_is_submit_button_impl(base: HtmlFormAssociatedElement*) -> bool:
    let this = html_input_element_from_form_associated_element(base)
    let type_state = html_input_element_type_state(this)
    # https://html.spec.whatwg.org/multipage/input.html#submit-button-state-(type=submit):concept-submit-button
    # https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):concept-submit-button
    return type_state == HtmlInputElementTypeAttributeState.submit_button or type_state == HtmlInputElementTypeAttributeState.image_button

## Ported from HTMLInputElement.cpp:3210 Web::HTML::HTMLInputElement::is_single_line.
# https://html.spec.whatwg.org/multipage/input.html#text-(type=text)-state-and-search-state-(type=search)
# https://html.spec.whatwg.org/multipage/input.html#password-state-(type=password)
# "one line plain text edit control"
pub func html_input_element_is_single_line(this: HtmlInputElement*) -> bool:
    let type_state = html_input_element_type_state(this)
    # NOTE: For web compatibility reasons, we consider other types
    #       in addition to Text, Search, and Password as single line inputs.
    return type_state == HtmlInputElementTypeAttributeState.text or type_state == HtmlInputElementTypeAttributeState.search or type_state == HtmlInputElementTypeAttributeState.password or type_state == HtmlInputElementTypeAttributeState.email or type_state == HtmlInputElementTypeAttributeState.telephone or type_state == HtmlInputElementTypeAttributeState.number

# mark: Activation ==============================================================================

## Ported from HTMLInputElement.cpp:3222 Web::HTML::HTMLInputElement::has_activation_behavior (override).
pub func html_input_element_has_activation_behavior_impl(base: DomEventTarget*) -> bool:
    _ = base
    return true

## Ported from HTMLInputElement.cpp:3228 Web::HTML::HTMLInputElement::activation_behavior (override).
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:activation-behaviour
pub func html_input_element_activation_behavior_impl(base: DomEventTarget*, event: DomEvent*):
    let this = (HtmlInputElement*)base
    # The activation behavior for input elements are these steps:

    # 1. If element is not mutable, and element's type attribute is neither in the Checkbox nor in the Radio state, then return.
    if not html_input_element_is_mutable_impl(html_input_element_form_associated(this)) and this.m_type != HtmlInputElementTypeAttributeState.checkbox and this.m_type != HtmlInputElementTypeAttributeState.radio_button:
        return

    # 2. Run element's input activation behavior, if any, and do nothing otherwise.
    html_input_element_run_input_activation_behavior(this, event) else trap("FIXME: should propagate errors")

    # 3. If element has a form owner and element's type attribute is not in the Button state, then return.
    if html_input_element_form(this) != none and html_input_element_type_state(this) != HtmlInputElementTypeAttributeState.button:
        return

    # 4. Run the popover target attribute activation behavior given element and event's target.
    if let target = dom_event_target(event):
        if is_dom_node(target):
            html_popover_target_attributes_popover_target_activation_behaviour(this.dom_node(), as_dom_node(target))

## Ported from HTMLInputElement.cpp:3248 Web::HTML::HTMLInputElement::has_input_activation_behavior.
pub func html_input_element_has_input_activation_behavior(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .checkbox, .color, .file_upload, .image_button, .radio_button, .reset_button, .submit_button:
            return true
        _:
            return false

# mark: What applies ============================================================================

## Ported from HTMLInputElement.cpp:3265 Web::HTML::HTMLInputElement::select_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_select_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .button, .checkbox, .hidden, .image_button, .radio_button, .range, .reset_button, .submit_button:
            return false
        _:
            return true

## Ported from HTMLInputElement.cpp:3283 Web::HTML::HTMLInputElement::selection_or_range_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_selection_or_range_applies(this: HtmlInputElement*) -> bool:
    return html_input_element_selection_or_range_applies_for_type_state(html_input_element_type_state(this))

## Ported from HTMLInputElement.cpp:3289 Web::HTML::HTMLInputElement::selection_direction_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_selection_direction_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .text, .search, .telephone, .url, .password:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3304 Web::HTML::HTMLInputElement::pattern_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_pattern_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .text, .search, .telephone, .url, .email, .password:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3320 Web::HTML::HTMLInputElement::multiple_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_multiple_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .email, .file_upload:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3332 Web::HTML::HTMLInputElement::required_applies.
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_required_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .text, .search, .telephone, .url, .email, .password, .date, .month, .week, .time, .local_date_and_time, .number, .checkbox, .radio_button, .file_upload:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3357 Web::HTML::HTMLInputElement::checked_applies (static).
# https://html.spec.whatwg.org/multipage/input.html#do-not-apply
pub func html_input_element_checked_applies_html_input_element_type_attrib(type_state: HtmlInputElementTypeAttributeState) -> bool:
    match type_state:
        .checkbox, .radio_button:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3368 Web::HTML::HTMLInputElement::checked_applies.
pub func html_input_element_checked_applies(this: HtmlInputElement*) -> bool:
    return html_input_element_checked_applies_html_input_element_type_attrib(html_input_element_type_state(this))

## Ported from HTMLInputElement.cpp:3373 Web::HTML::HTMLInputElement::has_selectable_text.
pub func html_input_element_has_selectable_text(this: HtmlInputElement*) -> bool:
    # Potential FIXME: Date, Month, Week, Time and LocalDateAndTime are rendered as a basic text input for now,
    # thus they have selectable text, this need to change when we will have a visual date/time selector.

    match html_input_element_type_state(this):
        .text, .search, .telephone, .url, .password, .date, .month, .week, .time, .local_date_and_time, .number, .email:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3397 Web::HTML::HTMLInputElement::selection_or_range_applies_for_type_state (static).
pub func html_input_element_selection_or_range_applies_for_type_state(type_state: HtmlInputElementTypeAttributeState) -> bool:
    match type_state:
        .text, .search, .telephone, .url, .password:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3412 Web::HTML::HTMLInputElement::change_event_applies.
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:event-change-2
pub func html_input_element_change_event_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .checkbox, .color, .date, .email, .file_upload, .local_date_and_time, .month, .number, .password, .radio_button, .range, .search, .telephone, .text, .time, .url, .week:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3439 Web::HTML::HTMLInputElement::value_as_date_applies.
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-valueasdate-3
pub func html_input_element_value_as_date_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .date, .month, .week, .time:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3453 Web::HTML::HTMLInputElement::value_as_number_applies.
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-valueasnumber-3
pub func html_input_element_value_as_number_applies(this: HtmlInputElement*) -> bool:
    match html_input_element_type_state(this):
        .date, .month, .week, .time, .local_date_and_time, .number, .range:
            return true
        _:
            return false

## Ported from HTMLInputElement.cpp:3470 Web::HTML::HTMLInputElement::step_applies.
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-step-3
pub func html_input_element_step_applies(this: HtmlInputElement*) -> bool:
    return html_input_element_value_as_number_applies(this)

## Ported from HTMLInputElement.cpp:3476 Web::HTML::HTMLInputElement::step_up_or_down_applies.
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-stepup-3
pub func html_input_element_step_up_or_down_applies(this: HtmlInputElement*) -> bool:
    return html_input_element_value_as_number_applies(this)

## Ported from HTMLInputElement.cpp:3482 Web::HTML::HTMLInputElement::value_attribute_mode_for_type_state (static).
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-value-2
func html_input_element_value_attribute_mode_for_type_state(type_state: HtmlInputElementTypeAttributeState) -> HtmlInputElementValueAttributeMode:
    match type_state:
        .text, .search, .telephone, .url, .email, .password, .date, .month, .week, .time, .local_date_and_time, .number, .range, .color:
            return HtmlInputElementValueAttributeMode.value

        .hidden, .submit_button, .image_button, .reset_button, .button:
            return HtmlInputElementValueAttributeMode.default

        .checkbox, .radio_button:
            return HtmlInputElementValueAttributeMode.default_on

        .file_upload:
            return HtmlInputElementValueAttributeMode.filename
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from HTMLInputElement.cpp:3519 Web::HTML::HTMLInputElement::value_attribute_mode.
func html_input_element_value_attribute_mode(this: HtmlInputElement*) -> HtmlInputElementValueAttributeMode:
    return html_input_element_value_attribute_mode_for_type_state(html_input_element_type_state(this))

## Ported from HTMLInputElement.cpp:3524 Web::HTML::HTMLInputElement::is_focusable (override).
pub func html_input_element_is_focusable_impl(base: DomEventTarget*) -> bool:
    let this = (HtmlInputElement*)base
    return this.m_type != HtmlInputElementTypeAttributeState.hidden and html_form_associated_element_enabled(html_input_element_form_associated(this))

## Ported from HTMLInputElement.cpp:3530 Web::HTML::HTMLInputElement::has_reversed_range.
# https://html.spec.whatwg.org/multipage/input.html#has-a-reversed-range
pub func html_input_element_has_reversed_range(this: HtmlInputElement*) -> bool:
    let minimum = html_input_element_min(this) else return false
    let maximum = html_input_element_max(this) else return false
    # An element has a reversed range if it has a periodic domain and its maximum is less than its minimum.
    return html_input_element_has_periodic_domain(this) and maximum < minimum