Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_input_element_5.lucb

#==============================================================================================
#
#   html_input_element_5 - HTML::HTMLInputElement: hints, cloning, radio groups, dimensions
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLInputElement.cpp (lines 2058-2437): the presentational hints
#       of image buttons, the cloning steps, radio button groups, the legacy-pre-activation
#       and legacy-canceled-activation behaviors, the Layout::ImageProvider overrides, the
#       default tab index, and the maxLength, minLength, size, height and width IDL attributes.
#
#==============================================================================================

# mark: Presentational hints ====================================================================

## Ported from HTMLInputElement.cpp:2058 Web::HTML::HTMLInputElement::is_presentational_hint (override).
pub func html_input_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
    let this = (HtmlInputElement*)base
    if dom_element_is_presentational_hint_impl(base, name):
        return true

    if html_input_element_type_state(this) != HtmlInputElementTypeAttributeState.image_button:
        return false

    return html_name_is_one_of(name, [html_attribute_names_align, html_attribute_names_border, html_attribute_names_height, html_attribute_names_hspace, html_attribute_names_vspace, html_attribute_names_width])

## Ported from HTMLInputElement.cpp:2075 Web::HTML::HTMLInputElement::apply_presentational_hints (override).
pub func html_input_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    let this = (HtmlInputElement*)base
    dom_element_apply_presentational_hints_impl(base, properties)

    if html_input_element_type_state(this) != HtmlInputElementTypeAttributeState.image_button:
        return

    for attribute in dom_element_attribute_span(base):
        let name = dom_attr_name(attribute)
        let value_string = dom_attr_value(attribute)
        let value = ak.string_bytes_as_string_view(&value_string)
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_align):
            if ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("center")):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.text_align, value = css_keyword_style_value_create(css_data.Keyword.center).css_style_value()))
            elif ak.string_view_equals_ignoring_ascii_case(&value, ak.sv("middle")):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.text_align, value = css_keyword_style_value_create(css_data.Keyword.middle).css_style_value()))
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_border):
            if let parsed_value = html_parse_non_negative_integer(value):
                let width_style_value = css_length_style_value_create(css_length_make_px_f64(f64(parsed_value))).css_style_value()
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_top_width, value = width_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_right_width, value = width_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_bottom_width, value = width_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_left_width, value = width_style_value))

                let border_style_value = css_keyword_style_value_create(css_data.Keyword.solid).css_style_value()
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_top_style, value = border_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_right_style, value = border_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_bottom_style, value = border_style_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_left_style, value = border_style_value))
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_height):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.height, value = parsed_value))
        # https://html.spec.whatwg.org/multipage/rendering.html#attributes-for-embedded-content-and-images:maps-to-the-dimension-property
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_hspace):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_left, value = parsed_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_right, value = parsed_value))
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_vspace):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_top, value = parsed_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_bottom, value = parsed_value))
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_width):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.width, value = parsed_value))

# mark: Cloning and radio button groups =========================================================

## Ported from HTMLInputElement.cpp:2127 Web::HTML::HTMLInputElement::cloned (override).
# https://html.spec.whatwg.org/multipage/input.html#the-input-element%3Aconcept-node-clone-ext
pub func html_input_element_cloned_impl(base: DomNode*, copy: DomNode*, subtree: bool) -> !:
    let this = (HtmlInputElement*)base
    try html_element_cloned_impl(base, copy, subtree)

    # The cloning steps for input elements given node, copy, and subtree are to propagate the value, dirty value flag, checkedness, and dirty checkedness flag from node to copy.
    let input_clone = as_html_input_element(copy)
    input_clone.m_value = this.m_value
    input_clone.m_dirty_value = this.m_dirty_value
    input_clone.m_checked = this.m_checked
    input_clone.m_dirty_checkedness = this.m_dirty_checkedness

    # AD-HOC: The spec doesn't mention propagating this state, but there is a WPT test that expects cloned nodes to preserve it.
    input_clone.m_indeterminate = this.m_indeterminate

## `non_empty_equals` of is_in_same_radio_button_group: `!value_a.is_empty() && value_a == value_b`.
func html_non_empty_equals(value_a: ak.FlyString, value_b: ak.FlyString) -> bool:
    return not ak.fly_string_is_empty(&value_a) and ak.fly_string_eq_fly_string(&value_a, &value_b)

## Ported from HTMLInputElement.cpp:2145 Web::HTML::is_in_same_radio_button_group.
# https://html.spec.whatwg.org/multipage/input.html#radio-button-group
func html_is_in_same_radio_button_group(a: HtmlInputElement*, b: HtmlInputElement*) -> bool:
    # The radio button group that contains an input element a also contains all the
    # other input elements b that fulfill all of the following conditions:
    # - Both a and b are in the same tree.
    if tree_node_root(a.dom_node()) != tree_node_root(b.dom_node()):
        return false
    # - The input element b's type attribute is in the Radio Button state.
    if html_input_element_type_state(a) != html_input_element_type_state(b) or html_input_element_type_state(b) != HtmlInputElementTypeAttributeState.radio_button:
        return false
    # - Either a and b have the same form owner, or they both have no form owner.
    if html_input_element_form(a) != html_input_element_form(b):
        return false
    # - They both have a name attribute, their name attributes are not empty, and the
    # value of a's name attribute equals the value of b's name attribute.
    let a_name = dom_element_name(a.dom_element()) else return false
    let b_name = dom_element_name(b.dom_element()) else return false
    return html_non_empty_equals(a_name, b_name)

## Ported from HTMLInputElement.cpp:2168 Web::HTML::HTMLInputElement::set_checked_within_group.
# https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
func html_input_element_set_checked_within_group(this: HtmlInputElement*):
    if html_input_element_checked(this):
        return

    html_input_element_set_checked(this, true)

    # No point iterating the tree if we have an empty name.
    let name = dom_element_name(this.dom_element()) else return
    if ak.fly_string_is_empty(&name):
        return

    html_input_element_uncheck_others_in_radio_button_group(this)

# mark: Legacy activation behavior ==============================================================

## Ported from HTMLInputElement.cpp:2187 Web::HTML::HTMLInputElement::legacy_pre_activation_behavior (override).
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
pub func html_input_element_legacy_pre_activation_behavior_impl(base: DomEventTarget*):
    let this = (HtmlInputElement*)base
    this.m_before_legacy_pre_activation_behavior_checked = html_input_element_checked(this)
    this.m_before_legacy_pre_activation_behavior_indeterminate = html_input_element_indeterminate(this)

    # 1. If this element's type attribute is in the Checkbox state, then set
    # this element's checkedness to its opposite value (i.e. true if it is
    # false, false if it is true) and set this element's indeterminate IDL
    # attribute to false.
    if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.checkbox:
        html_input_element_set_checked(this, not html_input_element_checked(this))
        html_input_element_set_indeterminate(this, false)

    # 2. If this element's type attribute is in the Radio Button state, then
    # get a reference to the element in this element's radio button group that
    # has its checkedness set to true, if any, and then set this element's
    # checkedness to true.
    if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.radio_button:
        for node in dom_node_inclusive_subtree(tree_node_root(this.dom_node())):
            let element = as_if_html_input_element(node) else continue
            if html_input_element_checked(element) and html_is_in_same_radio_button_group(this, element):
                this.m_legacy_pre_activation_behavior_checked_element_in_group = element
                break

        html_input_element_set_checked_within_group(this)

## Ported from HTMLInputElement.cpp:2219 Web::HTML::HTMLInputElement::legacy_cancelled_activation_behavior (override).
# https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
pub func html_input_element_legacy_cancelled_activation_behavior_impl(base: DomEventTarget*):
    let this = (HtmlInputElement*)base
    # 1. If the element's type attribute is in the Checkbox state, then set the
    # element's checkedness and the element's indeterminate IDL attribute back
    # to the values they had before the legacy-pre-activation behavior was run.
    if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.checkbox:
        html_input_element_set_checked(this, this.m_before_legacy_pre_activation_behavior_checked)
        html_input_element_set_indeterminate(this, this.m_before_legacy_pre_activation_behavior_indeterminate)

    # 2. If this element's type attribute is in the Radio Button state, then
    # if the element to which a reference was obtained in the
    # legacy-pre-activation behavior, if any, is still in what is now this
    # element' s radio button group, if it still has one, and if so, set
    # that element 's checkedness to true; or else, if there was no such
    # element, or that element is no longer in this element' s radio button
    # group, or if this element no longer has a radio button group, set
    # this element's checkedness to false.
    if html_input_element_type_state(this) == HtmlInputElementTypeAttributeState.radio_button:
        var did_reselect_previous_element = false
        if let element_in_group = this.m_legacy_pre_activation_behavior_checked_element_in_group:
            if html_is_in_same_radio_button_group(this, element_in_group):
                html_input_element_set_checked_within_group(element_in_group)
                did_reselect_previous_element = true

            this.m_legacy_pre_activation_behavior_checked_element_in_group = none

        if not did_reselect_previous_element:
            html_input_element_set_checked(this, false)

## Ported from HTMLInputElement.cpp:2254 Web::HTML::HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called (override).
pub func html_input_element_legacy_cancelled_activation_behavior_was_not_called_impl(base: DomEventTarget*):
    let this = (HtmlInputElement*)base
    this.m_legacy_pre_activation_behavior_checked_element_in_group = none

# mark: Layout::ImageProvider ===================================================================

## Ported from HTMLInputElement.cpp:2259 Web::HTML::HTMLInputElement::image_data.
func html_input_element_image_data(this: HtmlInputElement*) -> HtmlDecodedImageData*?:
    if let resource_request = this.m_resource_request:
        return html_shared_resource_request_image_data(resource_request)
    return none

## Ported from HTMLInputElement.cpp:2266 Web::HTML::HTMLInputElement::is_image_available (override).
pub func html_input_element_is_image_available_impl(base: LayoutImageProvider*) -> bool:
    return html_input_element_image_data(html_input_element_from_image_provider(base)) != none

## Ported from HTMLInputElement.cpp:2271 Web::HTML::HTMLInputElement::intrinsic_width (override).
pub func html_input_element_intrinsic_width_impl(base: LayoutImageProvider*) -> gfx.CssPixels?:
    if let image_data = html_input_element_image_data(html_input_element_from_image_provider(base)):
        return html_decoded_image_data_intrinsic_width(image_data)
    return none

## Ported from HTMLInputElement.cpp:2278 Web::HTML::HTMLInputElement::intrinsic_height (override).
pub func html_input_element_intrinsic_height_impl(base: LayoutImageProvider*) -> gfx.CssPixels?:
    if let image_data = html_input_element_image_data(html_input_element_from_image_provider(base)):
        return html_decoded_image_data_intrinsic_height(image_data)
    return none

## Ported from HTMLInputElement.cpp:2285 Web::HTML::HTMLInputElement::intrinsic_aspect_ratio (override).
pub func html_input_element_intrinsic_aspect_ratio_impl(base: LayoutImageProvider*) -> gfx.CssPixelFraction?:
    if let image_data = html_input_element_image_data(html_input_element_from_image_provider(base)):
        return html_decoded_image_data_intrinsic_aspect_ratio(image_data)
    return none

## Ported from HTMLInputElement.cpp:2292 Web::HTML::HTMLInputElement::current_image_bitmap_sized (override).
pub func html_input_element_current_image_bitmap_sized_impl(base: LayoutImageProvider*, size: gfx.IntSize) -> gfx.ImmutableBitmap*?:
    if let image_data = html_input_element_image_data(html_input_element_from_image_provider(base)):
        return html_decoded_image_data_bitmap(image_data, 0, size)
    return none

## Ported from HTMLInputElement.cpp:2299 Web::HTML::HTMLInputElement::set_visible_in_viewport (override).
pub func html_input_element_set_visible_in_viewport_impl(base: LayoutImageProvider*, arg0: bool):
    _ = base
    _ = arg0
    # FIXME: Loosen grip on image data when it's not visible, e.g via volatile memory.

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

# mark: Reflected IDL attributes ================================================================

## Ported from HTMLInputElement.cpp:2312 Web::HTML::HTMLInputElement::max_length.
#  https://html.spec.whatwg.org/multipage/input.html#dom-input-maxlength
pub func html_input_element_max_length(this: HtmlInputElement*) -> i32:
    # The maxLength IDL attribute must reflect the maxlength content attribute, limited to only non-negative numbers.
    if let maxlength_string = html_input_element_get_attribute(this, html_attribute_names_maxlength):
        if let maxlength = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&maxlength_string)):
            if maxlength <= 2147483647:
                return i32(maxlength)
    return -1

## Ported from HTMLInputElement.cpp:2322 Web::HTML::HTMLInputElement::set_max_length.
pub func html_input_element_set_max_length(this: HtmlInputElement*, value: i32) -> !:
    # The maxLength IDL attribute must reflect the maxlength content attribute, limited to only non-negative numbers.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_maxlength, try html_convert_non_negative_integer_to_string(html_input_element_realm(this), value))

## Ported from HTMLInputElement.cpp:2330 Web::HTML::HTMLInputElement::min_length.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-minlength
pub func html_input_element_min_length(this: HtmlInputElement*) -> i32:
    # The minLength IDL attribute must reflect the minlength content attribute, limited to only non-negative numbers.
    if let minlength_string = html_input_element_get_attribute(this, html_attribute_names_minlength):
        if let minlength = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&minlength_string)):
            if minlength <= 2147483647:
                return i32(minlength)
    return -1

## Ported from HTMLInputElement.cpp:2340 Web::HTML::HTMLInputElement::set_min_length.
pub func html_input_element_set_min_length(this: HtmlInputElement*, value: i32) -> !:
    # The minLength IDL attribute must reflect the minlength content attribute, limited to only non-negative numbers.
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_minlength, try html_convert_non_negative_integer_to_string(html_input_element_realm(this), value))

## Ported from HTMLInputElement.cpp:2348 Web::HTML::HTMLInputElement::size.
# https://html.spec.whatwg.org/multipage/input.html#the-size-attribute
pub func html_input_element_size(this: HtmlInputElement*) -> u32:
    # The size attribute, if specified, must have a value that is a valid non-negative integer greater than zero.
    # The size IDL attribute is limited to only positive numbers and has a default value of 20.
    if let size_string = html_input_element_get_attribute(this, html_attribute_names_size):
        if let size = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&size_string)):
            if size != 0 and size <= 2147483647:
                return size
    return 20

## Ported from HTMLInputElement.cpp:2359 Web::HTML::HTMLInputElement::set_size.
pub func html_input_element_set_size(this: HtmlInputElement*, value: u32) -> !:
    if value == 0:
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(html_input_element_realm(this), ak.utf16_string_from_str("Size must be greater than zero"))))
    var size = value
    if size > 2147483647:
        size = 20
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_size, ak.string_number_u64(size))

## Ported from HTMLInputElement.cpp:2370 Web::HTML::HTMLInputElement::height.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-height
pub func html_input_element_height(this: HtmlInputElement*) -> u32:
    dom_document_update_layout_if_needed_for_node(html_input_element_document(this), this.dom_node(), DomUpdateLayoutReason.html_input_element_height)

    # When the input element's type attribute is not in the Image Button state, then no image is available.
    if html_input_element_type_state(this) != HtmlInputElementTypeAttributeState.image_button:
        return 0

    # Return the rendered height of the image, in CSS pixels, if the image is being rendered.
    if let paintable_box = dom_node_paintable_box(this.dom_node()):
        return (u32)paint_paintable_box_content_height(paintable_box).to_int()

    # On setting [the width or height IDL attribute], they must act as if they reflected the respective content attributes of the same name.
    if let height_string = html_input_element_get_attribute(this, html_attribute_names_height):
        if let height = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&height_string)):
            if height <= 2147483647:
                return height

    # ...or else the natural height and height of the image, in CSS pixels, if an image is available but not being rendered
    if let bitmap = layout_image_provider_current_image_bitmap(&this.image_provider):
        return (u32)gfx.immutable_bitmap_height(bitmap)

    # ...or else 0, if the image is not available or does not have intrinsic dimensions.
    return 0

## Ported from HTMLInputElement.cpp:2396 Web::HTML::HTMLInputElement::set_height.
pub func html_input_element_set_height(this: HtmlInputElement*, value: u32):
    var height = value
    if height > 2147483647:
        height = 0

    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_height, ak.string_number_u64(height))

## Ported from HTMLInputElement.cpp:2405 Web::HTML::HTMLInputElement::width.
# https://html.spec.whatwg.org/multipage/input.html#dom-input-width
pub func html_input_element_width(this: HtmlInputElement*) -> u32:
    dom_document_update_layout_if_needed_for_node(html_input_element_document(this), this.dom_node(), DomUpdateLayoutReason.html_input_element_width)

    # When the input element's type attribute is not in the Image Button state, then no image is available.
    if html_input_element_type_state(this) != HtmlInputElementTypeAttributeState.image_button:
        return 0

    # Return the rendered width of the image, in CSS pixels, if the image is being rendered.
    if let paintable_box = dom_node_paintable_box(this.dom_node()):
        return (u32)paint_paintable_box_content_width(paintable_box).to_int()

    # On setting [the width or height IDL attribute], they must act as if they reflected the respective content attributes of the same name.
    if let width_string = html_input_element_get_attribute(this, html_attribute_names_width):
        if let width = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&width_string)):
            if width <= 2147483647:
                return width

    # ...or else the natural width and height of the image, in CSS pixels, if an image is available but not being rendered
    if let bitmap = layout_image_provider_current_image_bitmap(&this.image_provider):
        return (u32)gfx.immutable_bitmap_width(bitmap)

    # ...or else 0, if the image is not available or does not have intrinsic dimensions.
    return 0

## Ported from HTMLInputElement.cpp:2431 Web::HTML::HTMLInputElement::set_width.
pub func html_input_element_set_width(this: HtmlInputElement*, value: u32):
    var width = value
    if width > 2147483647:
        width = 0

    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_width, ak.string_number_u64(width))