dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / form_associated_element_3.lucb
#==============================================================================================
#
# form_associated_element_3 - HTML::FormAssociatedTextControlElement: the selection
# of text controls
#
# DESCRIPTION:
# Port of LibWeb/HTML/FormAssociatedElement.cpp (lines 635-1036) and FormAssociatedElement.h:
# the relevant value's selection (selectionStart, selectionEnd, selectionDirection,
# select(), setRangeText(), setSelectionRange() and "set the selection range").
#
# FormAssociatedTextControlElement is a stateful mixin (input and textarea, regions r24 and
# r25) whose first field is its InputEventsTarget base. WebIDL::UnsignedLong is u32; where
# C++ mixes it with size_t the arithmetic is done in usize and narrowed where C++ narrows.
#
#==============================================================================================
# mark: The relevant value ======================================================================
## Ported from FormAssociatedElement.cpp:636 Web::HTML::FormAssociatedTextControlElement::relevant_value_was_changed.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
pub func html_form_associated_text_control_element_relevant_value_was_changed(this: HtmlFormAssociatedTextControlElement*):
let the_relevant_value = html_form_associated_text_control_element_relevant_value(this)
let relevant_value_length = ak.utf16_string_base_length_in_code_units(&the_relevant_value.utf16_string_base)
# 1. If the element has a selection:
if this.m_selection_start < this.m_selection_end:
# 1. If the start of the selection is now past the end of the relevant value, set it to
# the end of the relevant value.
if (usize)this.m_selection_start > relevant_value_length:
this.m_selection_start = (u32)relevant_value_length
# 2. If the end of the selection is now past the end of the relevant value, set it to the
# end of the relevant value.
if (usize)this.m_selection_end > relevant_value_length:
this.m_selection_end = (u32)relevant_value_length
# 3. If the user agent does not support empty selection, and both the start and end of the
# selection are now pointing to the end of the relevant value, then instead set the
# element's text entry cursor position to the end of the relevant value, removing any
# selection.
# NOTE: We support empty selections.
return
# 2. Otherwise, the element must have a text entry cursor position position. If it is now past
# the end of the relevant value, set it to the end of the relevant value.
if (usize)this.m_selection_start > relevant_value_length:
this.m_selection_start = (u32)relevant_value_length
# mark: select(), selectionStart, selectionEnd and selectionDirection ===========================
## Ported from FormAssociatedElement.cpp:668 Web::HTML::FormAssociatedTextControlElement::select.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
pub func html_form_associated_text_control_element_select(this: HtmlFormAssociatedTextControlElement*) -> !:
# 1. If this element is an input element, and either select() does not apply to this element
# or the corresponding control has no selectable text, return.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_select_applies(input_element) or not html_input_element_has_selectable_text(input_element):
return
# 2. Set the selection range with 0 and infinity.
html_form_associated_text_control_element_set_the_selection_range(this, 0, ak.u32_max)
## Ported from FormAssociatedElement.cpp:685 Web::HTML::FormAssociatedTextControlElement::selection_start_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
pub func html_form_associated_text_control_element_selection_start_binding(this: HtmlFormAssociatedTextControlElement*) -> u32?:
# 1. If this element is an input element, and selectionStart does not apply to this element, return null.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_or_range_applies(input_element):
return none
# 2. If there is no selection, return the code unit offset within the relevant value to the character that
# immediately follows the text entry cursor.
if this.m_selection_start == this.m_selection_end:
return this.m_selection_start
# 3. Return the code unit offset within the relevant value to the character that immediately follows the start of
# the selection.
return this.m_selection_start if this.m_selection_start < this.m_selection_end else this.m_selection_end
## Ported from FormAssociatedElement.cpp:706 Web::HTML::FormAssociatedTextControlElement::selection_start.
pub func html_form_associated_text_control_element_selection_start(this: HtmlFormAssociatedTextControlElement*) -> u32:
return this.m_selection_start if this.m_selection_start < this.m_selection_end else this.m_selection_end
## Ported from FormAssociatedElement.cpp:712 Web::HTML::FormAssociatedTextControlElement::set_selection_start_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionstart-2
pub func html_form_associated_text_control_element_set_selection_start_binding(this: HtmlFormAssociatedTextControlElement*, value: u32?) -> !:
# 1. If this element is an input element, and selectionStart does not apply to this element,
# throw an "InvalidStateError" DOMException.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_or_range_applies(input_element):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_element_realm(html_element), ak.utf16_string_from_str("setSelectionStart does not apply to this input type"))))
# 2. Let end be the value of this element's selectionEnd attribute.
var end = this.m_selection_end
# 3. If end is less than the given value, set end to the given value.
if let given = value:
if end < given:
end = given
# 4. Set the selection range with the given value, end, and the value of this element's
# selectionDirection attribute.
html_form_associated_text_control_element_set_the_selection_range(this, value, end, html_form_associated_text_control_element_selection_direction_state(this))
## Ported from FormAssociatedElement.cpp:737 Web::HTML::FormAssociatedTextControlElement::selection_end_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
pub func html_form_associated_text_control_element_selection_end_binding(this: HtmlFormAssociatedTextControlElement*) -> u32?:
# 1. If this element is an input element, and selectionEnd does not apply to this element, return
# null.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_or_range_applies(input_element):
return none
# 2. If there is no selection, return the code unit offset within the relevant value to the
# character that immediately follows the text entry cursor.
if this.m_selection_start == this.m_selection_end:
return this.m_selection_start
# 3. Return the code unit offset within the relevant value to the character that immediately
# follows the end of the selection.
return this.m_selection_end if this.m_selection_start < this.m_selection_end else this.m_selection_start
## Ported from FormAssociatedElement.cpp:759 Web::HTML::FormAssociatedTextControlElement::selection_end.
pub func html_form_associated_text_control_element_selection_end(this: HtmlFormAssociatedTextControlElement*) -> u32:
return this.m_selection_end if this.m_selection_start < this.m_selection_end else this.m_selection_start
## Ported from FormAssociatedElement.cpp:765 Web::HTML::FormAssociatedTextControlElement::set_selection_end_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionend-3
pub func html_form_associated_text_control_element_set_selection_end_binding(this: HtmlFormAssociatedTextControlElement*, value: u32?) -> !:
# 1. If this element is an input element, and selectionEnd does not apply to this element,
# throw an "InvalidStateError" DOMException.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_or_range_applies(input_element):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_element_realm(html_element), ak.utf16_string_from_str("setSelectionEnd does not apply to this input type"))))
# 2. Set the selection range with the value of this element's selectionStart attribute, the
# given value, and the value of this element's selectionDirection attribute.
html_form_associated_text_control_element_set_the_selection_range(this, this.m_selection_start, value, html_form_associated_text_control_element_selection_direction_state(this))
## Ported from FormAssociatedElement.cpp:783 Web::HTML::FormAssociatedTextControlElement::selection_direction.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#selection-direction
pub func html_form_associated_text_control_element_selection_direction(this: HtmlFormAssociatedTextControlElement*) -> ak.String?:
# 1. If this element is an input element, and selectionDirection does not apply to this
# element, return null.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_or_range_applies(input_element):
return none
# 2. Return this element's selection direction.
match this.m_selection_direction:
.forward:
return ak.string_from_str("forward")
.backward:
return ak.string_from_str("backward")
.none_:
return ak.string_from_str("none")
_:
trap("VERIFY_NOT_REACHED")
## Ported from FormAssociatedElement.cpp:808 Web::HTML::FormAssociatedTextControlElement::set_selection_direction.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-direction
pub func html_form_associated_text_control_element_set_selection_direction(this: HtmlFormAssociatedTextControlElement*, direction: ak.String?):
# To set the selection direction of an element to a given direction, update the element's
# selection direction to the given direction, unless the direction is "none" and the
# platform does not support that direction; in that case, update the element's selection
# direction to "forward".
this.m_selection_direction = html_string_to_selection_direction(direction)
## Ported from FormAssociatedElement.cpp:818 Web::HTML::FormAssociatedTextControlElement::set_selection_direction_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectiondirection
pub func html_form_associated_text_control_element_set_selection_direction_binding(this: HtmlFormAssociatedTextControlElement*, direction: ak.String?) -> !:
# 1. If this element is an input element, and selectionDirection does not apply to this element,
# throw an "InvalidStateError" DOMException.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element):
let input_element = as_html_input_element(html_element)
if not html_input_element_selection_direction_applies(input_element):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_element_realm(input_element.html_element()), ak.utf16_string_from_str("selectionDirection does not apply to element"))))
html_form_associated_text_control_element_set_the_selection_range(this, this.m_selection_start, this.m_selection_end, html_string_to_selection_direction(direction))
## Ported from FormAssociatedElement.h:248 Web::HTML::FormAssociatedTextControlElement::selection_direction_state.
pub func html_form_associated_text_control_element_selection_direction_state(this: HtmlFormAssociatedTextControlElement*) -> HtmlSelectionDirection:
return this.m_selection_direction
# mark: setRangeText() and setSelectionRange() ==================================================
## Ported from FormAssociatedElement.cpp:834 Web::HTML::FormAssociatedTextControlElement::set_range_text_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
pub func html_form_associated_text_control_element_set_range_text_binding(this: HtmlFormAssociatedTextControlElement*, replacement: ak.Utf16String) -> !:
try html_form_associated_text_control_element_set_range_text_binding_u32(this, replacement, this.m_selection_start, this.m_selection_end)
## Ported from FormAssociatedElement.cpp:840 Web::HTML::FormAssociatedTextControlElement::set_range_text_binding.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
pub func html_form_associated_text_control_element_set_range_text_binding_u32(this: HtmlFormAssociatedTextControlElement*, replacement: ak.Utf16String, start: u32, end: u32, selection_mode: BindSelectionMode = BindSelectionMode.preserve) -> !:
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
# 1. If this element is an input element, and setRangeText() does not apply to this element,
# throw an "InvalidStateError" DOMException.
if is_html_input_element(html_element) and not html_input_element_selection_or_range_applies(as_html_input_element(html_element)):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_element_realm(html_element), ak.utf16_string_from_str("setRangeText does not apply to this input type"))))
try html_form_associated_text_control_element_set_range_text(this, replacement, start, end, selection_mode)
## Ported from FormAssociatedElement.cpp:853 Web::HTML::FormAssociatedTextControlElement::set_range_text.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
pub func html_form_associated_text_control_element_set_range_text(this: HtmlFormAssociatedTextControlElement*, replacement: ak.Utf16String, start: u32, end: u32, selection_mode: BindSelectionMode = BindSelectionMode.preserve) -> !:
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
var start_ = start
var end_ = end
# 2. Set this element's dirty value flag to true.
html_form_associated_text_control_element_set_dirty_value_flag(this, true)
# 3. If the method has only one argument, then let start and end have the values of the selectionStart attribute and the selectionEnd attribute respectively.
# Otherwise, let start, end have the values of the second and third arguments respectively.
# NOTE: This is handled by the caller.
# 4. If start is greater than end, then throw an "IndexSizeError" DOMException.
if start_ > end_:
try idl_throw(.idl_dom_exception(idl_index_size_error_create(html_element_realm(html_element), ak.utf16_string_from_str("The start argument must be less than or equal to the end argument"))))
# 5. If start is greater than the length of the relevant value of the text control, then set it to the length of the relevant value of the text control.
var the_relevant_value = html_form_associated_text_control_element_relevant_value(this)
let relevant_value_length = ak.utf16_string_base_length_in_code_units(&the_relevant_value.utf16_string_base)
if (usize)start_ > relevant_value_length:
start_ = (u32)relevant_value_length
# 6. If end is greater than the length of the relevant value of the text control, then set it to the length of the relevant value of the text control.
if (usize)end_ > relevant_value_length:
end_ = (u32)relevant_value_length
# 7. Let selection start be the current value of the selectionStart attribute.
var selection_start = this.m_selection_start
# 8. Let selection end be the current value of the selectionEnd attribute.
var selection_end = this.m_selection_end
# 9. If start is less than end, delete the sequence of code units within the element's relevant value starting with
# the code unit at the startth position and ending with the code unit at the (end-1)th position.
if start_ < end_:
var builder: ak.StringBuilder = ---
ak.string_builder_construct_string_builder_mode_usize(&builder, ak.StringBuilderMode.utf16, ak.utf16_string_base_length_in_code_units(&the_relevant_value.utf16_string_base) - (usize)(end_ - start_))
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_substring_view_usize(&the_relevant_value.utf16_string_base, 0, (usize)start_))
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_substring_view(&the_relevant_value.utf16_string_base, (usize)end_))
the_relevant_value = ak.string_builder_to_utf16_string(&builder)
# 10. Insert the value of the first argument into the text of the relevant value of the text control, immediately before the startth code unit.
var builder: ak.StringBuilder = ---
ak.string_builder_construct_string_builder_mode_usize(&builder, ak.StringBuilderMode.utf16, ak.utf16_string_base_length_in_code_units(&the_relevant_value.utf16_string_base) + ak.utf16_string_base_length_in_code_units(&replacement.utf16_string_base))
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_substring_view_usize(&the_relevant_value.utf16_string_base, 0, (usize)start_))
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_utf16_view(&replacement.utf16_string_base))
ak.string_builder_append_utf16_view(&builder, ak.utf16_string_base_substring_view(&the_relevant_value.utf16_string_base, (usize)start_))
the_relevant_value = ak.string_builder_to_utf16_string(&builder)
try html_form_associated_text_control_element_set_relevant_value(this, the_relevant_value)
# 11. Let new length be the length of the value of the first argument.
let new_length = ak.utf16_string_base_length_in_code_units(&replacement.utf16_string_base)
# 12. Let new end be the sum of start and new length.
let new_end = (usize)start_ + new_length
# 13. Run the appropriate set of substeps from the following list:
match selection_mode:
# If the fourth argument's value is "select"
.select:
# Let selection start be start.
selection_start = start_
# Let selection end be new end.
selection_end = (u32)new_end
# If the fourth argument's value is "start"
.start:
# Let selection start and selection end be start.
selection_start = start_
selection_end = start_
# If the fourth argument's value is "end"
.end:
selection_start = (u32)new_end
selection_end = (u32)new_end
# If the fourth argument's value is "preserve"
.preserve:
# 1. Let old length be end minus start.
let old_length = end_ - start_
# 2. Let delta be new length minus old length.
let delta = new_length -% (usize)old_length
# 3. If selection start is greater than end, then increment it by delta.
# (If delta is negative, i.e. the new text is shorter than the old text, then this will decrease the value of selection start.)
# Otherwise: if selection start is greater than start, then set it to start.
# (This snaps the start of the selection to the start of the new text if it was in the middle of the text that it replaced.)
if selection_start > end_:
selection_start = (u32)((usize)selection_start +% delta)
elif selection_start > start_:
selection_start = start_
# 4. If selection end is greater than end, then increment it by delta in the same way.
# Otherwise: if selection end is greater than start, then set it to new end.
# (This snaps the end of the selection to the end of the new text if it was in the middle of the text that it replaced.)
if selection_end > end_:
selection_end = (u32)((usize)selection_end +% delta)
elif selection_end > start_:
selection_end = (u32)new_end
_:
trap("VERIFY_NOT_REACHED")
# 14. Set the selection range with selection start and selection end.
html_form_associated_text_control_element_set_the_selection_range(this, selection_start, selection_end)
## Ported from FormAssociatedElement.cpp:967 Web::HTML::FormAssociatedTextControlElement::set_selection_range.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
pub func html_form_associated_text_control_element_set_selection_range(this: HtmlFormAssociatedTextControlElement*, start: u32?, end: u32?, direction: ak.String?) -> !:
# 1. If this element is an input element, and setSelectionRange() does not apply to this
# element, throw an "InvalidStateError" DOMException.
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
if is_html_input_element(html_element) and not html_input_element_selection_or_range_applies(as_html_input_element(html_element)):
try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(html_element_realm(html_element), ak.utf16_string_from_str("setSelectionRange does not apply to this input type"))))
# 2. Set the selection range with start, end, and direction.
html_form_associated_text_control_element_set_the_selection_range(this, start, end, html_string_to_selection_direction(direction))
## The captures of the select event task queued by set the selection range: the element.
struct HtmlFormAssociatedTextControlElementSelectEventTask:
var html_element: HtmlElement*
## The task's steps: fire select at the element, bubbling.
static func run(context: void*?):
let captures = (HtmlFormAssociatedTextControlElementSelectEventTask*)(context else trap("select event task: no captures"))
let html_element = captures.html_element
let select_event = dom_event_create(html_element_realm(html_element), html_event_names_select, DomEventInit(bubbles = true))
_ = dom_event_target_dispatch_event(html_element.dom_event_target(), select_event)
## Ported from FormAssociatedElement.cpp:981 Web::HTML::FormAssociatedTextControlElement::set_the_selection_range.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-range
pub func html_form_associated_text_control_element_set_the_selection_range(this: HtmlFormAssociatedTextControlElement*, start: u32?, end: u32?, direction: HtmlSelectionDirection = HtmlSelectionDirection.none_, source: HtmlSelectionSource = HtmlSelectionSource.dom):
# 1. If start is null, let start be 0.
let start_ = start else 0
# 2. If end is null, let end be 0.
let end_ = end else 0
# 3. Set the selection of the text control to the sequence of code units within the relevant
# value starting with the code unit at the startth position (in logical order) and ending
# with the code unit at the (end-1)th position. Arguments greater than the length of the
# relevant value of the text control (including the special value infinity) must be treated
# as pointing at the end of the text control.
let the_relevant_value = html_form_associated_text_control_element_relevant_value(this)
let relevant_value_length = ak.utf16_string_base_length_in_code_units(&the_relevant_value.utf16_string_base)
# AK::min(T, IdentityType<T>) converts the length to the start's u32.
var new_selection_start = html_min_u32(start_, (u32)relevant_value_length)
let new_selection_end = html_min_u32(end_, (u32)relevant_value_length)
# If end is less than or equal to start, then the start of the selection and the end of the
# selection must both be placed immediately before the character with offset end. In UAs
# where there is no concept of an empty selection, this must set the cursor to be just
# before the character with offset end.
new_selection_start = html_min_u32(new_selection_start, new_selection_end)
var was_modified = this.m_selection_start != new_selection_start or this.m_selection_end != new_selection_end
this.m_selection_start = new_selection_start
this.m_selection_end = new_selection_end
# 4. If direction is not identical to either "backward" or "forward", or if the direction
# argument was not given, set direction to "none".
# NOTE: This is handled by the argument's default value and ::string_to_selection_direction().
# 5. Set the selection direction of the text control to direction.
was_modified = was_modified or this.m_selection_direction != direction
this.m_selection_direction = direction
# 6. If the previous steps caused the selection of the text control to be modified (in either
# extent or direction), then queue an element task on the user interaction task source
# given the element to fire an event named select at the element, with the bubbles attribute
# initialized to true.
if was_modified:
let html_element = html_form_associated_text_control_element_text_control_to_html_element(this)
# AD-HOC: We don't fire the event if the user moves the cursor without selecting any text.
# This is not in the spec but matches how other browsers behave.
if source == HtmlSelectionSource.dom or this.m_selection_start != this.m_selection_end:
let captures = new HtmlFormAssociatedTextControlElementSelectEventTask(html_element = html_element) else trap("out of memory")
_ = dom_element_queue_an_element_task(html_element.dom_element(), HtmlTaskSource.user_interaction, ak.Function0[unit](call = HtmlFormAssociatedTextControlElementSelectEventTask.run, context = captures))
html_form_associated_text_control_element_selection_was_changed(this, source)
## AK::min of two u32s.
func html_min_u32(a: u32, b: u32) -> u32:
return b if b < a else a