Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / aria / state_and_properties.lucb

#==============================================================================================
#
#   state_and_properties - ARIA::StateAndProperties: the states and properties of an element's
#                          role as their attribute names and string values
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ARIA/StateAndProperties.cpp (lines 1-468). The string values
#       are what RoleType::serialize_as_json reports for each supported, required or
#       prohibited state and property of a role object.
#
#       Copyright (c) 2023, Jonah Shafran <jonahshafran@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: String values ===========================================================================

## `value ? "true"_string : "false"_string`.
func aria_bool_to_string(value: bool) -> ak.String:
    return ak.string_from_str("true" if value else "false")

## Appends `to_add` to `builder`, after a space unless it is the first token (the StringBuilder
## loops of AriaDropEffect and AriaRelevant).
func aria_append_token(builder: ak.StringBuilder*, to_add: ak.StringView):
    if ak.string_builder_is_empty(builder):
        ak.string_builder_append_string_view(builder, to_add)
    else:
        ak.string_builder_append_string_view(builder, ak.sv(" "))
        ak.string_builder_append_string_view(builder, to_add)

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:12 Web::ARIA::state_or_property_to_string_value.
pub func aria_state_or_property_to_string_value(state_or_property: AriaStateAndProperties, aria_data: AriaData,
                                                default_value: AriaDefaultValueType) -> ak.String!:
    let data = &aria_data
    match state_or_property:
        .aria_active_descendant:
            return aria_data_aria_active_descendant_or_default(data) else ak.string_new()
        .aria_atomic:
            var value: bool
            match default_value:
                .bool_(default_bool): value = aria_data_aria_atomic_or_default(data, default_bool)
                _: value = aria_data_aria_atomic_or_default(data)
            return aria_bool_to_string(value)
        .aria_auto_complete:
            let value = aria_data_aria_auto_complete_or_default(data)
            match value:
                .none_: return ak.string_from_str("none")
                .list: return ak.string_from_str("list")
                .both: return ak.string_from_str("both")
                .inline: return ak.string_from_str("inline")
                _: trap("VERIFY_NOT_REACHED")
        .aria_braille_label:
            return aria_data_aria_braille_label_or_default(data)
        .aria_braille_role_description:
            return aria_data_aria_braille_role_description_or_default(data)
        .aria_busy:
            return try ak.string_from_utf8(ak.sv("true" if aria_data_aria_busy_or_default(data) else "false"))
        .aria_checked:
            return try aria_tristate_to_string(aria_data_aria_checked_or_default(data))
        .aria_col_count:
            return try aria_optional_integer_to_string(aria_data_aria_col_count_or_default(data))
        .aria_col_index:
            return try aria_optional_integer_to_string(aria_data_aria_col_index_or_default(data))
        .aria_col_index_text:
            return aria_data_aria_col_index_text_or_default(data)
        .aria_col_span:
            return try aria_optional_integer_to_string(aria_data_aria_col_span_or_default(data))
        .aria_controls:
            return try aria_id_reference_list_to_string(aria_data_aria_controls_or_default(data))
        .aria_current:
            let value = aria_data_aria_current_or_default(data)
            match value:
                .false_: return ak.string_from_str("false")
                .true_: return ak.string_from_str("true")
                .date: return ak.string_from_str("date")
                .location: return ak.string_from_str("location")
                .page: return ak.string_from_str("page")
                .step: return ak.string_from_str("step")
                .time: return ak.string_from_str("time")
                _: trap("VERIFY_NOT_REACHED")
        .aria_described_by:
            return try aria_id_reference_list_to_string(aria_data_aria_described_by_or_default(data))
        .aria_description:
            return aria_data_aria_description_or_default(data)
        .aria_details:
            return aria_data_aria_details_or_default(data) else ak.string_new()
        .aria_disabled:
            return aria_bool_to_string(aria_data_aria_disabled_or_default(data))
        .aria_drop_effect:
            var builder = ak.string_builder_new()
            let value = aria_data_aria_drop_effect_or_default(data)
            for drop_effect in ak.vector_span_const(&value):
                var to_add: ak.StringView
                match drop_effect:
                    .copy: to_add = ak.sv("copy")
                    .execute: to_add = ak.sv("execute")
                    .link: to_add = ak.sv("link")
                    .move: to_add = ak.sv("move")
                    .none_: to_add = ak.sv("none")
                    .popup: to_add = ak.sv("popup")
                    _: trap("VERIFY_NOT_REACHED")
                aria_append_token(&builder, to_add)
            return try ak.string_builder_to_string(&builder)
        .aria_error_message:
            return aria_data_aria_error_message_or_default(data) else ak.string_new()
        .aria_expanded:
            return try aria_optional_bool_to_string(aria_data_aria_expanded_or_default(data))
        .aria_flow_to:
            return try aria_id_reference_list_to_string(aria_data_aria_flow_to_or_default(data))
        .aria_grabbed:
            return try aria_optional_bool_to_string(aria_data_aria_grabbed_or_default(data))
        .aria_has_popup:
            let value = aria_data_aria_has_popup_or_default(data)
            match value:
                .false_: return ak.string_from_str("false")
                .true_: return ak.string_from_str("true")
                .menu: return ak.string_from_str("menu")
                .listbox: return ak.string_from_str("listbox")
                .tree: return ak.string_from_str("tree")
                .grid: return ak.string_from_str("grid")
                .dialog: return ak.string_from_str("dialog")
                _: trap("VERIFY_NOT_REACHED")
        .aria_hidden:
            return try aria_optional_bool_to_string(aria_data_aria_hidden_or_default(data))
        .aria_invalid:
            let value = aria_data_aria_invalid_or_default(data)
            match value:
                .grammar: return ak.string_from_str("grammar")
                .false_: return ak.string_from_str("false")
                .spelling: return ak.string_from_str("spelling")
                .true_: return ak.string_from_str("true")
                _: trap("VERIFY_NOT_REACHED")
        .aria_key_shortcuts:
            return aria_data_aria_key_shortcuts_or_default(data)
        .aria_label:
            return aria_data_aria_label_or_default(data)
        .aria_labelled_by:
            return try aria_id_reference_list_to_string(aria_data_aria_labelled_by_or_default(data))
        .aria_level:
            return try aria_optional_integer_to_string(aria_data_aria_level_or_default(data))
        .aria_live:
            var value: AriaLive
            match default_value:
                .aria_live(default_live): value = aria_data_aria_live_or_default(data, default_live)
                _: value = aria_data_aria_live_or_default(data)

            match value:
                .assertive: return ak.string_from_str("assertive")
                .off: return ak.string_from_str("off")
                .polite: return ak.string_from_str("polite")
                _: trap("VERIFY_NOT_REACHED")
        .aria_modal:
            return aria_bool_to_string(aria_data_aria_modal_or_default(data))
        .aria_multi_line:
            return aria_bool_to_string(aria_data_aria_multi_line_or_default(data))
        .aria_multi_selectable:
            return aria_bool_to_string(aria_data_aria_multi_selectable_or_default(data))
        .aria_orientation:
            var value: AriaOrientation
            match default_value:
                .aria_orientation(default_orientation): value = aria_data_aria_orientation_or_default(data, default_orientation)
                _: value = aria_data_aria_orientation_or_default(data)

            match value:
                .horizontal: return ak.string_from_str("horizontal")
                .undefined: return ak.string_from_str("undefined")
                .vertical: return ak.string_from_str("vertical")
                _: trap("VERIFY_NOT_REACHED")
        .aria_owns:
            return try aria_id_reference_list_to_string(aria_data_aria_owns_or_default(data))
        .aria_placeholder:
            return aria_data_aria_placeholder_or_default(data)
        .aria_pos_in_set:
            return try aria_optional_integer_to_string(aria_data_aria_pos_in_set_or_default(data))
        .aria_pressed:
            return try aria_tristate_to_string(aria_data_aria_pressed_or_default(data))
        .aria_read_only:
            return aria_bool_to_string(aria_data_aria_read_only_or_default(data))
        .aria_relevant:
            var builder = ak.string_builder_new()
            let value = aria_data_aria_relevant_or_default(data)
            for relevant in ak.vector_span_const(&value):
                var to_add: ak.StringView
                match relevant:
                    .additions: to_add = ak.sv("additions")
                    .additions_text: to_add = ak.sv("additions text")
                    .all: to_add = ak.sv("all")
                    .removals: to_add = ak.sv("removals")
                    .text: to_add = ak.sv("text")
                    _: trap("VERIFY_NOT_REACHED")
                aria_append_token(&builder, to_add)
            return try ak.string_builder_to_string(&builder)
        .aria_required:
            return try ak.string_from_utf8(ak.sv("true" if aria_data_aria_required_or_default(data) else "false"))
        .aria_role_description:
            return aria_data_aria_role_description_or_default(data)
        .aria_row_count:
            return try aria_optional_integer_to_string(aria_data_aria_row_count_or_default(data))
        .aria_row_index:
            return try aria_optional_integer_to_string(aria_data_aria_row_index_or_default(data))
        .aria_row_index_text:
            return aria_data_aria_row_index_text_or_default(data)
        .aria_row_span:
            return try aria_optional_integer_to_string(aria_data_aria_row_span_or_default(data))
        .aria_selected:
            return try aria_optional_bool_to_string(aria_data_aria_selected_or_default(data))
        .aria_set_size:
            return try aria_optional_integer_to_string(aria_data_aria_set_size_or_default(data))
        .aria_sort:
            let value = aria_data_aria_sort_or_default(data)
            match value:
                .ascending: return ak.string_from_str("ascending")
                .descending: return ak.string_from_str("descending")
                .none_: return ak.string_from_str("none")
                .other: return ak.string_from_str("other")
                _: trap("VERIFY_NOT_REACHED")
        .aria_value_max:
            match default_value:
                .f64_(default_number): return try aria_optional_number_to_string(aria_data_aria_value_max_or_default(data, default_number))
                _: return try aria_optional_number_to_string(aria_data_aria_value_max_or_default(data))
        .aria_value_min:
            match default_value:
                .f64_(default_number): return try aria_optional_number_to_string(aria_data_aria_value_min_or_default(data, default_number))
                _: return try aria_optional_number_to_string(aria_data_aria_value_min_or_default(data))
        .aria_value_now:
            return try aria_optional_number_to_string(aria_data_aria_value_now_or_default(data))
        .aria_value_text:
            return aria_data_aria_value_text_or_default(data)
        _: trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:303 Web::ARIA::tristate_to_string.
pub func aria_tristate_to_string(value: AriaTristate) -> ak.String!:
    match value:
        .false_: return ak.string_from_str("false")
        .true_: return ak.string_from_str("true")
        .undefined: return ak.string_from_str("undefined")
        .mixed: return ak.string_from_str("mixed")
        _: trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:318 Web::ARIA::optional_integer_to_string.
pub func aria_optional_integer_to_string(value: i32?) -> ak.String!:
    if let integer = value:
        return ak.string_number_i64(integer)
    return ak.string_new()

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:325 Web::ARIA::optional_bool_to_string.
pub func aria_optional_bool_to_string(value: bool?) -> ak.String!:
    let present = value else return ak.string_from_str("undefined")
    if present:
        return ak.string_from_str("true")
    return ak.string_from_str("false")

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:334 Web::ARIA::optional_number_to_string.
pub func aria_optional_number_to_string(value: f64?) -> ak.String!:
    let number = value else return ak.string_from_str("undefined")
    return ak.string_number_f64(number)

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:341 Web::ARIA::id_reference_list_to_string.
pub func aria_id_reference_list_to_string(value: ak.Vector[ak.String]) -> ak.String!:
    var builder = ak.string_builder_new()
    for id in ak.vector_span_const(&value):
        if ak.string_builder_is_empty(&builder):
            ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&id))
        else:
            ak.string_builder_append_string_view(&builder, ak.sv(" "))
            ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&id))
    return try ak.string_builder_to_string(&builder)

# mark: Attribute names =========================================================================

## Ported from Libraries/LibWeb/ARIA/StateAndProperties.cpp:355 Web::ARIA::state_or_property_to_string.
pub func aria_state_or_property_to_string(value: AriaStateAndProperties) -> ak.StringView:
    match value:
        .aria_active_descendant: return ak.sv("aria-activedescendant")
        .aria_atomic: return ak.sv("aria-atomic")
        .aria_auto_complete: return ak.sv("aria-autocomplete")
        .aria_braille_label: return ak.sv("aria-braillelabel")
        .aria_braille_role_description: return ak.sv("aria-brailleroledescription")
        .aria_busy: return ak.sv("aria-busy")
        .aria_checked: return ak.sv("aria-checked")
        .aria_col_count: return ak.sv("aria-colcount")
        .aria_col_index: return ak.sv("aria-colindex")
        .aria_col_index_text: return ak.sv("aria-colindextext")
        .aria_col_span: return ak.sv("aria-colspan")
        .aria_controls: return ak.sv("aria-controls")
        .aria_current: return ak.sv("aria-current")
        .aria_described_by: return ak.sv("aria-describedby")
        .aria_description: return ak.sv("aria-description")
        .aria_details: return ak.sv("aria-details")
        .aria_disabled: return ak.sv("aria-disabled")
        .aria_drop_effect: return ak.sv("aria-dropeffect")
        .aria_error_message: return ak.sv("aria-errormessage")
        .aria_expanded: return ak.sv("aria-expanded")
        .aria_flow_to: return ak.sv("aria-flowto")
        .aria_grabbed: return ak.sv("aria-grabbed")
        .aria_has_popup: return ak.sv("aria-haspopup")
        .aria_hidden: return ak.sv("aria-hidden")
        .aria_invalid: return ak.sv("aria-invalid")
        .aria_key_shortcuts: return ak.sv("aria-keyshortcuts")
        .aria_label: return ak.sv("aria-label")
        .aria_labelled_by: return ak.sv("aria-labelledby")
        .aria_level: return ak.sv("aria-level")
        .aria_live: return ak.sv("aria-live")
        .aria_modal: return ak.sv("aria-modal")
        .aria_multi_line: return ak.sv("aria-multiline")
        .aria_multi_selectable: return ak.sv("aria-multiselectable")
        .aria_orientation: return ak.sv("aria-orientation")
        .aria_owns: return ak.sv("aria-owns")
        .aria_placeholder: return ak.sv("aria-placeholder")
        .aria_pos_in_set: return ak.sv("aria-posinset")
        .aria_pressed: return ak.sv("aria-pressed")
        .aria_read_only: return ak.sv("aria-readonly")
        .aria_relevant: return ak.sv("aria-relevant")
        .aria_required: return ak.sv("aria-required")
        .aria_role_description: return ak.sv("aria-roledescription")
        .aria_row_count: return ak.sv("aria-rowcount")
        .aria_row_index: return ak.sv("aria-rowindex")
        .aria_row_index_text: return ak.sv("aria-rowindextext")
        .aria_row_span: return ak.sv("aria-rowspan")
        .aria_selected: return ak.sv("aria-selected")
        .aria_set_size: return ak.sv("aria-setsize")
        .aria_sort: return ak.sv("aria-sort")
        .aria_value_max: return ak.sv("aria-valuemax")
        .aria_value_min: return ak.sv("aria-valuemin")
        .aria_value_now: return ak.sv("aria-valuenow")
        .aria_value_text: return ak.sv("aria-valuetext")
        _: trap("VERIFY_NOT_REACHED")