Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / form_associated_element_4.lucb

#==============================================================================================
#
#   form_associated_element_4 - HTML::FormAssociatedTextControlElement: editing
#                               and cursor movement
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/FormAssociatedElement.cpp (lines 1038-1359) and the inline functions
#       of FormAssociatedTextControlElement in FormAssociatedElement.h: the InputEventsTarget
#       overrides that edit the control's text node (insertion, deletion, selecting, cursor
#       movement by grapheme, word and line), the selection change steps and the pure virtuals.
#       An InputEventsTarget override casts its base to the mixin (its first field).
#
#==============================================================================================

# mark: Editing (InputEventsTarget) =============================================================

## Ported from FormAssociatedElement.cpp:1038 Web::HTML::FormAssociatedTextControlElement::handle_insert (override).
pub func html_form_associated_text_control_element_handle_insert_impl(base: InputEventsTarget*, input_type: ak.FlyString, data: ak.Utf16String):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    if not html_form_associated_element_is_mutable(&html_form_associated_text_control_element_text_control_to_html_element(this).form_associated_element):
        return

    var data_for_insertion = data

    if let max_length = dom_text_max_length(text_node):
        let remaining_length = (usize)max_length -% (usize)dom_character_data_length_in_utf16_code_units(text_node.dom_character_data())
        if remaining_length < ak.utf16_string_base_length_in_code_units(&data.utf16_string_base):
            data_for_insertion = ak.utf16_string_from_utf16(ak.utf16_string_base_substring_view_usize(&data.utf16_string_base, 0, remaining_length))

    let selection_start = html_form_associated_text_control_element_selection_start(this)
    let selection_end = html_form_associated_text_control_element_selection_end(this)
    html_form_associated_text_control_element_set_range_text(this, data_for_insertion, selection_start, selection_end, BindSelectionMode.end) else trap("MUST: set_range_text")

    dom_node_invalidate_style(text_node.dom_node(), DomStyleInvalidationReason.editing_insertion)

    # The input event's data attribute is only set for certain input types according to:
    # https://w3c.github.io/input-events/#overview
    var data_for_input_event: ak.Utf16String? = none
    if ak.fly_string_eq_fly_string(&input_type, &ui_events_input_types_insert_text) or ak.fly_string_eq_fly_string(&input_type, &ui_events_input_types_insert_from_paste):
        data_for_input_event = data_for_insertion

    html_form_associated_text_control_element_did_edit_text_node(this, input_type, data_for_input_event)
    html_form_associated_text_control_element_scroll_cursor_into_view(this)

## Ported from FormAssociatedElement.cpp:1068 Web::HTML::FormAssociatedTextControlElement::handle_delete (override).
pub func html_form_associated_text_control_element_handle_delete_impl(base: InputEventsTarget*, input_type: ak.FlyString):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    if not html_form_associated_element_is_mutable(&html_form_associated_text_control_element_text_control_to_html_element(this).form_associated_element):
        return

    var selection_start = html_form_associated_text_control_element_selection_start(this)
    var selection_end = html_form_associated_text_control_element_selection_end(this)

    if selection_start == selection_end:
        if ak.fly_string_eq_fly_string(&input_type, &ui_events_input_types_delete_content_backward):
            if let offset = web_unicode.segmenter_previous_boundary(dom_character_data_grapheme_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
                selection_start = (u32)offset
        else:
            if let offset = web_unicode.segmenter_next_boundary(dom_character_data_grapheme_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
                selection_end = (u32)offset

    html_form_associated_text_control_element_set_range_text(this, ak.utf16_string_new(), selection_start, selection_end, BindSelectionMode.end) else trap("MUST: set_range_text")

    dom_node_invalidate_style(text_node.dom_node(), DomStyleInvalidationReason.editing_deletion)
    html_form_associated_text_control_element_did_edit_text_node(this, input_type, none)
    html_form_associated_text_control_element_scroll_cursor_into_view(this)

## Ported from FormAssociatedElement.cpp:1094 Web::HTML::FormAssociatedTextControlElement::selected_text_for_stringifier (virtual).
pub func html_form_associated_text_control_element_selected_text_for_stringifier_impl(this: HtmlFormAssociatedTextControlElement*) -> ak.Utf16String?:
    # https://w3c.github.io/selection-api/#dom-selection-stringifier
    # Used for clipboard copy and window.getSelection().toString() when this element is active.
    let start = (usize)html_form_associated_text_control_element_selection_start(this)
    let end = (usize)html_form_associated_text_control_element_selection_end(this)
    if start >= end:
        return none

    let relevant_value = html_form_associated_text_control_element_relevant_value(this)
    return ak.utf16_string_from_utf16(ak.utf16_string_base_substring_view_usize(&relevant_value.utf16_string_base, start, end - start))

## Ported from FormAssociatedElement.cpp:1106 Web::HTML::FormAssociatedTextControlElement::collapse_selection_to_offset.
func html_form_associated_text_control_element_collapse_selection_to_offset(this: HtmlFormAssociatedTextControlElement*, position: usize):
    this.m_selection_start = (u32)position
    this.m_selection_end = (u32)position

## Ported from FormAssociatedElement.cpp:1112 Web::HTML::FormAssociatedTextControlElement::scroll_cursor_into_view.
func html_form_associated_text_control_element_scroll_cursor_into_view(this: HtmlFormAssociatedTextControlElement*):
    let element = html_form_associated_text_control_element_text_control_to_html_element(this)
    dom_document_update_layout(html_element_document(element), DomUpdateLayoutReason.scroll_cursor_into_view)

    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return

    let paintable = dom_node_paintable(text_node.dom_node()) else return

    paint_paintable_scroll_ancestor_to_offset_into_view(paintable, (usize)this.m_selection_end)

## Ported from FormAssociatedElement.cpp:1128 Web::HTML::FormAssociatedTextControlElement::selection_was_changed.
func html_form_associated_text_control_element_selection_was_changed(this: HtmlFormAssociatedTextControlElement*, source: HtmlSelectionSource):
    let element = html_form_associated_text_control_element_text_control_to_html_element(this)
    if let input_element = as_if_html_input_element(element):
        dom_schedule_a_selectionchange_event(input_element.dom_node(), html_element_document(element))
    elif let text_area_element = as_if_html_text_area_element(element):
        dom_schedule_a_selectionchange_event(text_area_element.dom_node(), html_element_document(element))
    else:
        trap("VERIFY_NOT_REACHED")

    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    # NB: Called during selection change handling, layout may be stale.
    let text_paintable = dom_node_unsafe_paintable(text_node.dom_node()) else return

    if this.m_selection_start == this.m_selection_end:
        paint_paintable_set_selection_state(text_paintable, PaintPaintableSelectionState.none_)
        dom_document_reset_cursor_blink_cycle(dom_node_document(text_node.dom_node()))
    else:
        paint_paintable_set_selection_state(text_paintable, PaintPaintableSelectionState.start_and_end)
    paint_paintable_set_needs_repaint(text_paintable, InvalidateDisplayList.yes)

    # AD-HOC: Only scroll the cursor into view for UI-driven selection changes (like keyboard input). Programmatic
    #         changes (input.value, setSelectionRange) do not cause the cursor to scroll into view. This matches the
    #         behavior of other browsers.
    if source == HtmlSelectionSource.ui:
        if let navigable = dom_document_navigable(html_element_document(element)):
            if not event_handler_is_handling_mouse_selection(html_navigable_event_handler(navigable)):
                html_form_associated_text_control_element_scroll_cursor_into_view(this)

## Ported from FormAssociatedElement.cpp:1165 Web::HTML::FormAssociatedTextControlElement::select_all (override).
pub func html_form_associated_text_control_element_select_all_impl(base: InputEventsTarget*):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    html_form_associated_text_control_element_set_the_selection_range(this, 0, (u32)dom_node_length(text_node.dom_node()))
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1174 Web::HTML::FormAssociatedTextControlElement::set_selection_anchor (override).
pub func html_form_associated_text_control_element_set_selection_anchor_impl(base: InputEventsTarget*, anchor_node: DomNode*, anchor_offset: usize):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let editing_host_manager = dom_document_editing_host_manager(html_element_document(html_form_associated_text_control_element_text_control_to_html_element(this)))
    input_events_target_set_selection_anchor(&editing_host_manager.input_events_target, anchor_node, anchor_offset)
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    if anchor_node != text_node.dom_node():
        return
    html_form_associated_text_control_element_collapse_selection_to_offset(this, anchor_offset)
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1185 Web::HTML::FormAssociatedTextControlElement::set_selection_focus (override).
pub func html_form_associated_text_control_element_set_selection_focus_impl(base: InputEventsTarget*, focus_node: DomNode*, focus_offset: usize):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let editing_host_manager = dom_document_editing_host_manager(html_element_document(html_form_associated_text_control_element_text_control_to_html_element(this)))
    input_events_target_set_selection_focus(&editing_host_manager.input_events_target, focus_node, focus_offset)
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    if focus_node != text_node.dom_node():
        return
    this.m_selection_end = (u32)focus_offset
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

# mark: Cursor movement =========================================================================

## Ported from FormAssociatedElement.cpp:1196 Web::HTML::FormAssociatedTextControlElement::move_cursor_to_start (override).
pub func html_form_associated_text_control_element_move_cursor_to_start_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    if html_form_associated_text_control_element_form_associated_element_to_text_node(this) == none:
        return
    if collapse == InputEventsTargetCollapseSelection.yes:
        html_form_associated_text_control_element_collapse_selection_to_offset(this, 0)
    else:
        this.m_selection_end = 0
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1209 Web::HTML::FormAssociatedTextControlElement::move_cursor_to_end (override).
pub func html_form_associated_text_control_element_move_cursor_to_end_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    if collapse == InputEventsTargetCollapseSelection.yes:
        html_form_associated_text_control_element_collapse_selection_to_offset(this, dom_node_length(text_node.dom_node()))
    else:
        this.m_selection_end = (u32)dom_node_length(text_node.dom_node())
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1222 Web::HTML::FormAssociatedTextControlElement::increment_cursor_position_offset (override).
pub func html_form_associated_text_control_element_increment_cursor_position_offset_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    # If there is a selection range, collapse to the end (max) of that range without moving forward
    if collapse == InputEventsTargetCollapseSelection.yes and this.m_selection_start != this.m_selection_end:
        let maximum = this.m_selection_end if this.m_selection_start < this.m_selection_end else this.m_selection_start
        html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)maximum)
    # Otherwise, move forward if possible
    elif let offset = web_unicode.segmenter_next_boundary(dom_character_data_grapheme_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
        if collapse == InputEventsTargetCollapseSelection.yes:
            html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)offset)
        else:
            this.m_selection_end = (u32)offset
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1242 Web::HTML::FormAssociatedTextControlElement::decrement_cursor_position_offset (override).
pub func html_form_associated_text_control_element_decrement_cursor_position_offset_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return
    # If there is a selection range, collapse to the start (min) of that range without moving backward
    if collapse == InputEventsTargetCollapseSelection.yes and this.m_selection_start != this.m_selection_end:
        html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)html_min_u32(this.m_selection_start, this.m_selection_end))
    # Otherwise, move backward if possible
    elif let offset = web_unicode.segmenter_previous_boundary(dom_character_data_grapheme_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
        if collapse == InputEventsTargetCollapseSelection.yes:
            html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)offset)
        else:
            this.m_selection_end = (u32)offset
    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1262 Web::HTML::FormAssociatedTextControlElement::increment_cursor_position_to_next_word (override).
pub func html_form_associated_text_control_element_increment_cursor_position_to_next_word_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return

    while true:
        if let offset = web_unicode.segmenter_next_boundary(dom_character_data_word_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
            let data = dom_character_data_data(text_node.dom_character_data())
            let word = ak.utf16_string_base_substring_view_usize(&data.utf16_string_base, (usize)this.m_selection_end, (usize)offset - (usize)this.m_selection_end)
            if collapse == InputEventsTargetCollapseSelection.yes:
                html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)offset)
            else:
                this.m_selection_end = (u32)offset
            if web_unicode.segmenter_should_continue_beyond_word(word):
                continue
        break

    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1285 Web::HTML::FormAssociatedTextControlElement::decrement_cursor_position_to_previous_word (override).
pub func html_form_associated_text_control_element_decrement_cursor_position_to_previous_word_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return

    while true:
        if let offset = web_unicode.segmenter_previous_boundary(dom_character_data_word_segmenter(text_node.dom_character_data()), (usize)this.m_selection_end, web_unicode.SegmenterInclusive.no):
            let data = dom_character_data_data(text_node.dom_character_data())
            let word = ak.utf16_string_base_substring_view_usize(&data.utf16_string_base, (usize)offset, (usize)this.m_selection_end - (usize)offset)
            if collapse == InputEventsTargetCollapseSelection.yes:
                html_form_associated_text_control_element_collapse_selection_to_offset(this, (usize)offset)
            else:
                this.m_selection_end = (u32)offset
            if web_unicode.segmenter_should_continue_beyond_word(word):
                continue
        break

    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1308 Web::HTML::FormAssociatedTextControlElement::increment_cursor_position_to_next_line (override).
pub func html_form_associated_text_control_element_increment_cursor_position_to_next_line_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return

    let new_offset = compute_cursor_position_on_next_line(text_node, (usize)this.m_selection_end) else return

    if collapse == InputEventsTargetCollapseSelection.yes:
        html_form_associated_text_control_element_collapse_selection_to_offset(this, new_offset)
    else:
        this.m_selection_end = (u32)new_offset

    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1326 Web::HTML::FormAssociatedTextControlElement::decrement_cursor_position_to_previous_line (override).
pub func html_form_associated_text_control_element_decrement_cursor_position_to_previous_line_impl(base: InputEventsTarget*, collapse: InputEventsTargetCollapseSelection):
    let this = (HtmlFormAssociatedTextControlElement*)base
    let text_node = html_form_associated_text_control_element_form_associated_element_to_text_node(this) else return

    let new_offset = compute_cursor_position_on_previous_line(text_node, (usize)this.m_selection_end) else return

    if collapse == InputEventsTargetCollapseSelection.yes:
        html_form_associated_text_control_element_collapse_selection_to_offset(this, new_offset)
    else:
        this.m_selection_end = (u32)new_offset

    html_form_associated_text_control_element_selection_was_changed(this, HtmlSelectionSource.ui)

## Ported from FormAssociatedElement.cpp:1344 Web::HTML::FormAssociatedTextControlElement::cursor_position.
pub func html_form_associated_text_control_element_cursor_position(this: HtmlFormAssociatedTextControlElement*) -> DomPosition*?:
    let node = html_form_associated_text_control_element_form_associated_element_to_text_node_const(this) else return none
    if this.m_selection_start != this.m_selection_end:
        return none
    return dom_position_create(bind_platform_object_realm(node.bind_platform_object()), node.dom_node(), this.m_selection_start)

## Ported from FormAssociatedElement.cpp:1354 Web::HTML::FormAssociatedTextControlElement::as_cell (override).
pub func html_form_associated_text_control_element_as_cell_impl(base: InputEventsTarget*) -> JsCell*:
    let this = (HtmlFormAssociatedTextControlElement*)base
    return html_form_associated_text_control_element_text_control_to_html_element(this).js_cell()

# mark: Inline functions of FormAssociatedTextControlElement ====================================

## Ported from FormAssociatedElement.h:225 Web::HTML::FormAssociatedTextControlElement::relevant_value (virtual).
pub func html_form_associated_text_control_element_relevant_value_impl(this: HtmlFormAssociatedTextControlElement*) -> ak.Utf16String:
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::relevant_value")

## Ported from FormAssociatedElement.h:226 Web::HTML::FormAssociatedTextControlElement::set_relevant_value (virtual).
pub func html_form_associated_text_control_element_set_relevant_value_impl(this: HtmlFormAssociatedTextControlElement*, arg0: ak.Utf16String) -> !:
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::set_relevant_value")

## Ported from FormAssociatedElement.h:229 Web::HTML::FormAssociatedTextControlElement::set_dirty_value_flag (virtual).
pub func html_form_associated_text_control_element_set_dirty_value_flag_impl(this: HtmlFormAssociatedTextControlElement*, flag: bool):
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::set_dirty_value_flag")

## Ported from FormAssociatedElement.h:262 Web::HTML::FormAssociatedTextControlElement::has_scheduled_selectionchange_event.
# https://w3c.github.io/selection-api/#dfn-has-scheduled-selectionchange-event
pub func html_form_associated_text_control_element_has_scheduled_selectionchange_event(this: HtmlFormAssociatedTextControlElement*) -> bool:
    return this.m_has_scheduled_selectionchange_event

## Ported from FormAssociatedElement.h:263 Web::HTML::FormAssociatedTextControlElement::set_scheduled_selectionchange_event.
pub func html_form_associated_text_control_element_set_scheduled_selectionchange_event(this: HtmlFormAssociatedTextControlElement*, value: bool):
    this.m_has_scheduled_selectionchange_event = value

## Ported from FormAssociatedElement.h:265 Web::HTML::FormAssociatedTextControlElement::text_control_to_html_element (virtual).
pub func html_form_associated_text_control_element_text_control_to_html_element_impl(this: HtmlFormAssociatedTextControlElement*) -> HtmlElement*:
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::text_control_to_html_element")

## Ported from FormAssociatedElement.h:266 Web::HTML::FormAssociatedTextControlElement::text_control_to_html_element.
pub func html_form_associated_text_control_element_text_control_to_html_element_const(this: HtmlFormAssociatedTextControlElement*) -> HtmlElement*:
    return html_form_associated_text_control_element_text_control_to_html_element(this)

## Ported from FormAssociatedElement.h:268 Web::HTML::FormAssociatedTextControlElement::did_edit_text_node (virtual).
pub func html_form_associated_text_control_element_did_edit_text_node_impl(this: HtmlFormAssociatedTextControlElement*, input_type: ak.FlyString, data: ak.Utf16String?):
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::did_edit_text_node")

## Ported from FormAssociatedElement.h:270 Web::HTML::FormAssociatedTextControlElement::form_associated_element_to_text_node (virtual).
pub func html_form_associated_text_control_element_form_associated_element_to_text_node_impl(this: HtmlFormAssociatedTextControlElement*) -> DomText*?:
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::form_associated_element_to_text_node")

## Ported from FormAssociatedElement.h:271 Web::HTML::FormAssociatedTextControlElement::form_associated_element_to_text_node (virtual).
pub func html_form_associated_text_control_element_form_associated_element_to_text_node_const_impl(this: HtmlFormAssociatedTextControlElement*) -> DomText*?:
    return html_form_associated_text_control_element_form_associated_element_to_text_node(this)

## Ported from FormAssociatedElement.h:273 Web::HTML::FormAssociatedTextControlElement::text_control_scroll_container (virtual).
pub func html_form_associated_text_control_element_text_control_scroll_container_impl(this: HtmlFormAssociatedTextControlElement*) -> DomElement*?:
    trap("pure virtual: Web::HTML::FormAssociatedTextControlElement::text_control_scroll_container")