dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / html_text_area_element_1.lucb
#==============================================================================================
#
# html_text_area_element_1 - HTML::HTMLTextAreaElement: <textarea>, its value and attributes
#
# DESCRIPTION:
# Port of LibWeb/HTML/HTMLTextAreaElement.cpp (lines 1-336): construction (which moves the
# mixin tables to HTMLTextAreaElement's, as C++'s vptrs move), the adjusted computed
# style, focus, the reset and clear algorithms, cloning, the default value, value, raw
# value and API value, maxLength/minLength/cols/rows and the selection bindings.
# html_text_area_element_2 has the shadow tree, the children changed and attribute change
# steps, edits, constraint validation, the layout node and the header's inline functions.
#
# Copyright (c) 2020, the SerenityOS developers.
# Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
# Copyright (c) 2024, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
# Copyright (c) 2024-2026, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Helpers =================================================================================
## The textarea element of its FormAssociatedElement base (C++'s `this` adjustment).
func html_text_area_element_from_form_associated_element(base: HtmlFormAssociatedElement*) -> HtmlTextAreaElement*:
return (HtmlTextAreaElement*)((u8*)base - memory.offset_of(HtmlElement, form_associated_element))
## The textarea element of its FormAssociatedTextControlElement base.
func html_text_area_element_from_text_control(base: HtmlFormAssociatedTextControlElement*) -> HtmlTextAreaElement*:
return (HtmlTextAreaElement*)((u8*)base - memory.offset_of(HtmlTextAreaElement, form_associated_text_control_element))
## The FormAssociatedElement base of a textarea element.
func html_text_area_element_form_associated(this: HtmlTextAreaElement*) -> HtmlFormAssociatedElement*:
return &this.html_element_base.form_associated_element
## The FormAssociatedTextControlElement base of a textarea element.
func html_text_area_element_text_control(this: HtmlTextAreaElement*) -> HtmlFormAssociatedTextControlElement*:
return &this.form_associated_text_control_element
# mark: Construction ============================================================================
## Ported from HTMLTextAreaElement.cpp:34 Web::HTML::HTMLTextAreaElement::HTMLTextAreaElement.
pub func html_text_area_element_construct(this: HtmlTextAreaElement*, document: DomDocument*, qualified_name: DomQualifiedName):
html_element_construct(this.html_element(), document, qualified_name)
# The FormAssociatedTextControlElement and AutocompleteElement bases get their tables, then the
# vptrs of the mixin bases move to HTMLTextAreaElement's tables.
html_form_associated_text_control_element_init_fields(&this.form_associated_text_control_element)
this.form_associated_text_control_element.input_events_target_base.vtable = (const InputEventsTargetVTable*)&html_text_area_element_form_associated_text_control_element_vtable
this.form_associated_text_control_element.input_events_target_base.class_id = this.cell().class_id
this.autocomplete_element.vtable = &html_text_area_element_autocomplete_element_vtable
this.autocomplete_element.class_id = this.cell().class_id
let element = this.html_element()
element.element.slottable_mixin.vtable = &html_text_area_element_slottable_mixin_vtable
element.element.aria_mixin.vtable = &html_text_area_element_aria_mixin_vtable
element.element.animatable.vtable = &html_text_area_element_animatable_vtable
element.global_event_handlers.vtable = &html_text_area_element_global_event_handlers_vtable
element.form_associated_element.vtable = &html_text_area_element_form_associated_element_vtable
## Ported from HTMLTextAreaElement.cpp:41 Web::HTML::HTMLTextAreaElement::adjust_computed_style (override).
pub func html_text_area_element_adjust_computed_style_impl(base: DomElement*, style: CssComputedProperties*):
_ = base
# https://drafts.csswg.org/css-display-3/#unbox
let display = css_computed_properties_display(style)
if css_display_is_contents(&display):
css_computed_properties_set_property(style, css_data.PropertyId.display, css_display_style_value_create(css_display_from_short(CssDisplayShort.none_)).css_style_value())
# AD-HOC: We rewrite `display: inline` to `display: inline-block`.
# This is required for the internal shadow tree to work correctly in layout.
let display_now = css_computed_properties_display(style)
if css_display_is_inline_outside(&display_now) and css_display_is_flow_inside(&display_now):
css_computed_properties_set_property(style, css_data.PropertyId.display, css_display_style_value_create(css_display_from_short(CssDisplayShort.inline_block)).css_style_value())
## Ported from HTMLTextAreaElement.cpp:53 Web::HTML::HTMLTextAreaElement::initialize (override).
pub func html_text_area_element_initialize_impl(base: JsCell*, realm: Realm*):
web_set_prototype_for_interface(base, realm, BindInterfaceName.html_text_area_element)
html_element_initialize_impl(base, realm)
## Ported from HTMLTextAreaElement.cpp:59 Web::HTML::HTMLTextAreaElement::visit_edges (override).
pub func html_text_area_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlTextAreaElement*)base
html_element_visit_edges_impl(base, visitor)
gc.cell_visitor_visit_t(visitor, this.m_placeholder_element)
gc.cell_visitor_visit_t(visitor, this.m_placeholder_text_node)
gc.cell_visitor_visit_t(visitor, this.m_inner_text_element)
gc.cell_visitor_visit_t(visitor, this.m_text_node)
# mark: Focus ===================================================================================
## Ported from HTMLTextAreaElement.cpp:68 Web::HTML::HTMLTextAreaElement::did_receive_focus (override).
pub func html_text_area_element_did_receive_focus_impl(base: DomElement*):
let this = (HtmlTextAreaElement*)base
let text_node = this.m_text_node else return
dom_node_set_needs_repaint(text_node.dom_node())
if let placeholder_text_node = this.m_placeholder_text_node:
dom_node_set_needs_repaint(placeholder_text_node.dom_node())
selection_selection_remove_all_ranges(dom_document_get_selection(dom_node_document(this.dom_node())) else trap("VERIFY: GC::Ptr<Selection>::operator-> on null"))
## The captures of did_lose_focus's element task: `[this]`.
struct HtmlTextAreaElementDidLoseFocusTask:
var this: HtmlTextAreaElement*
## did_lose_focus's element task: fire a change event.
func html_text_area_element_did_lose_focus_task(context: void*?):
let captures = (HtmlTextAreaElementDidLoseFocusTask*)(context else trap("no captures"))
let this = captures.this
let change_event = dom_event_create(html_element_realm(this.html_element()), html_event_names_change)
dom_event_set_bubbles(change_event, true)
_ = dom_event_target_dispatch_event(this.dom_event_target(), change_event)
## Ported from HTMLTextAreaElement.cpp:80 Web::HTML::HTMLTextAreaElement::did_lose_focus (override).
pub func html_text_area_element_did_lose_focus_impl(base: DomElement*):
let this = (HtmlTextAreaElement*)base
if let text_node = this.m_text_node:
dom_node_set_needs_repaint(text_node.dom_node())
if let placeholder_text_node = this.m_placeholder_text_node:
dom_node_set_needs_repaint(placeholder_text_node.dom_node())
# The change event fires when the value is committed, if that makes sense for the control,
# or else when the control loses focus
let captures = new HtmlTextAreaElementDidLoseFocusTask(this = this) else trap("out of memory")
_ = dom_element_queue_an_element_task(this.dom_element(), HtmlTaskSource.user_interaction, ak.Function0[unit](call = html_text_area_element_did_lose_focus_task, context = captures))
## Ported from HTMLTextAreaElement.cpp:98 Web::HTML::HTMLTextAreaElement::default_tab_index_value (override).
# https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
pub func html_text_area_element_default_tab_index_value_impl(base: DomElement*) -> i32:
_ = base
# See the base function for the spec comments.
return 0
# mark: Reset, clear and cloning ================================================================
## `MUST(m_text_node->replace_data(0, m_text_node->length_in_utf16_code_units(), m_raw_value))`.
func html_text_area_element_replace_text_node_data(this: HtmlTextAreaElement*, text_node: DomText*):
let length = (usize)dom_character_data_length_in_utf16_code_units(text_node.dom_character_data())
dom_character_data_replace_data(text_node.dom_character_data(), 0, length, ak.utf16_string_base_utf16_view(&this.m_raw_value.utf16_string_base)) else trap("MUST: replace_data")
## Ported from HTMLTextAreaElement.cpp:105 Web::HTML::HTMLTextAreaElement::reset_algorithm (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-form-reset-control
pub func html_text_area_element_reset_algorithm_impl(base: HtmlFormAssociatedElement*):
let this = html_text_area_element_from_form_associated_element(base)
# The reset algorithm for textarea elements is to set the user validity to false, the dirty value flag back to false,
this.m_user_validity = false
this.m_dirty_value = false
# and the raw value to its child text content.
html_text_area_element_set_raw_value(this, dom_node_child_text_content(this.dom_node()))
if let text_node = this.m_text_node:
html_text_area_element_replace_text_node_data(this, text_node)
html_text_area_element_update_placeholder_visibility(this)
## The captures of clear_algorithm's element task: `[this]`.
struct HtmlTextAreaElementClearAlgorithmTask:
var this: HtmlTextAreaElement*
## clear_algorithm's element task: fire an input event.
func html_text_area_element_clear_algorithm_task(context: void*?):
let captures = (HtmlTextAreaElementClearAlgorithmTask*)(context else trap("no captures"))
let this = captures.this
var input_event_init: UiEventsInputEventInit = ---
ui_events_input_event_init_init_fields(&input_event_init)
input_event_init.ui_event_init.event_init.bubbles = true
input_event_init.ui_event_init.event_init.composed = true
let input_event = ui_events_input_event_create_from_platform_event(html_element_realm(this.html_element()), html_event_names_input, input_event_init)
# (InputEvent is opaque until phase 4; it is a DOM::Event first.)
_ = dom_event_target_dispatch_event(this.dom_event_target(), (DomEvent*)input_event)
## Ported from HTMLTextAreaElement.cpp:120 Web::HTML::HTMLTextAreaElement::clear_algorithm (override).
# https://w3c.github.io/webdriver/#dfn-clear-algorithm
pub func html_text_area_element_clear_algorithm_impl(base: HtmlFormAssociatedElement*):
let this = html_text_area_element_from_form_associated_element(base)
# The clear algorithm for textarea elements is to set the dirty value flag back to false,
this.m_dirty_value = false
# and set the raw value of element to an empty string.
html_text_area_element_set_raw_value(this, ak.utf16_string_new())
# Unlike their associated reset algorithms, changes made to form controls as part of these algorithms do count as
# changes caused by the user (and thus, e.g. do cause input events to fire).
let captures = new HtmlTextAreaElementClearAlgorithmTask(this = this) else trap("out of memory")
_ = dom_element_queue_an_element_task(this.dom_element(), HtmlTaskSource.user_interaction, ak.Function0[unit](call = html_text_area_element_clear_algorithm_task, context = captures))
## Ported from HTMLTextAreaElement.cpp:140 Web::HTML::HTMLTextAreaElement::cloned (override).
# https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-node-clone-ext
pub func html_text_area_element_cloned_impl(base: DomNode*, copy: DomNode*, subtree: bool) -> !:
let this = (HtmlTextAreaElement*)base
try html_element_cloned_impl(base, copy, subtree)
# The cloning steps for textarea elements given node, copy, and subtree are to propagate the raw value and dirty value flag from node to copy.
let textarea_copy = as_html_text_area_element(copy)
textarea_copy.m_raw_value = this.m_raw_value
textarea_copy.m_dirty_value = this.m_dirty_value
## Ported from HTMLTextAreaElement.cpp:152 Web::HTML::HTMLTextAreaElement::form_associated_element_was_inserted (override).
pub func html_text_area_element_form_associated_element_was_inserted_impl(base: HtmlFormAssociatedElement*):
html_text_area_element_create_shadow_tree_if_needed(html_text_area_element_from_form_associated_element(base))
# mark: The value ===============================================================================
## Ported from HTMLTextAreaElement.cpp:158 Web::HTML::HTMLTextAreaElement::default_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
pub func html_text_area_element_default_value(this: HtmlTextAreaElement*) -> ak.Utf16String:
# The defaultValue attribute's getter must return the element's child text content.
return dom_node_child_text_content(this.dom_node())
## Ported from HTMLTextAreaElement.cpp:165 Web::HTML::HTMLTextAreaElement::set_default_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-defaultvalue
pub func html_text_area_element_set_default_value(this: HtmlTextAreaElement*, default_value: ak.Utf16String):
# The defaultValue attribute's setter must string replace all with the given value within this element.
dom_node_string_replace_all(this.dom_node(), default_value)
## Ported from HTMLTextAreaElement.cpp:172 Web::HTML::HTMLTextAreaElement::value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
pub func html_text_area_element_value(this: HtmlTextAreaElement*) -> ak.Utf16String:
# The value IDL attribute must, on getting, return the element's API value.
return html_text_area_element_api_value(this)
## Ported from HTMLTextAreaElement.cpp:179 Web::HTML::HTMLTextAreaElement::set_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-value
pub func html_text_area_element_set_value(this: HtmlTextAreaElement*, value: ak.Utf16String):
# 1. Let oldAPIValue be this element's API value.
let old_api_value = html_text_area_element_api_value(this)
# 2. Set this element's raw value to the new value.
html_text_area_element_set_raw_value(this, value)
# 3. Set this element's dirty value flag to true.
this.m_dirty_value = true
# 4. If the new API value is different from oldAPIValue, then move the text entry cursor position to the end of
# the text control, unselecting any selected text and resetting the selection direction to "none".
let new_api_value = html_text_area_element_api_value(this)
if not html_utf16_equals(&new_api_value, &old_api_value):
if let text_node = this.m_text_node:
html_text_area_element_replace_text_node_data(this, text_node)
html_text_area_element_update_placeholder_visibility(this)
let length = (u32)dom_node_length(text_node.dom_node())
html_form_associated_text_control_element_set_the_selection_range(html_text_area_element_text_control(this), length, length)
## Ported from HTMLTextAreaElement.cpp:202 Web::HTML::HTMLTextAreaElement::set_raw_value.
func html_text_area_element_set_raw_value(this: HtmlTextAreaElement*, value: ak.Utf16String):
let old_raw_value = this.m_raw_value
this.m_raw_value = value
this.m_api_value = none
if not html_utf16_equals(&this.m_raw_value, &old_raw_value):
html_form_associated_text_control_element_relevant_value_was_changed(html_text_area_element_text_control(this))
## Ported from HTMLTextAreaElement.cpp:213 Web::HTML::HTMLTextAreaElement::api_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-api-value-3
pub func html_text_area_element_api_value(this: HtmlTextAreaElement*) -> ak.Utf16String:
# The algorithm for obtaining the element's API value is to return the element's raw value, with newlines normalized.
if let api_value = this.m_api_value:
return api_value
let api_value = web_infra.normalize_newlines_utf16_string(this.m_raw_value)
this.m_api_value = api_value
return api_value
## Ported from HTMLTextAreaElement.cpp:220 Web::HTML::HTMLTextAreaElement::set_relevant_value (override).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
pub func html_text_area_element_set_relevant_value_impl(base: HtmlFormAssociatedTextControlElement*, value: ak.Utf16String) -> !:
html_text_area_element_set_value(html_text_area_element_from_text_control(base), value)
## Ported from HTMLTextAreaElement.cpp:227 Web::HTML::HTMLTextAreaElement::text_length.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-textlength
pub func html_text_area_element_text_length(this: HtmlTextAreaElement*) -> u32:
# The textLength IDL attribute must return the length of the element's API value.
let api_value = html_text_area_element_api_value(this)
return (u32)ak.utf16_string_base_length_in_code_units(&api_value.utf16_string_base)
# mark: Reflected IDL attributes ================================================================
## Ported from HTMLTextAreaElement.cpp:234 Web::HTML::HTMLTextAreaElement::max_length.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
pub func html_text_area_element_max_length(this: HtmlTextAreaElement*) -> i32:
# The maxLength IDL attribute must reflect the maxlength content attribute, limited to only non-negative numbers.
if let maxlength_string = dom_element_get_attribute(this.dom_element(), 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 HTMLTextAreaElement.cpp:244 Web::HTML::HTMLTextAreaElement::set_max_length.
pub func html_text_area_element_set_max_length(this: HtmlTextAreaElement*, 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_element_realm(this.html_element()), value))
## Ported from HTMLTextAreaElement.cpp:252 Web::HTML::HTMLTextAreaElement::min_length.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-minlength
pub func html_text_area_element_min_length(this: HtmlTextAreaElement*) -> i32:
# The minLength IDL attribute must reflect the minlength content attribute, limited to only non-negative numbers.
if let minlength_string = dom_element_get_attribute(this.dom_element(), 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 HTMLTextAreaElement.cpp:262 Web::HTML::HTMLTextAreaElement::set_min_length.
pub func html_text_area_element_set_min_length(this: HtmlTextAreaElement*, 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_element_realm(this.html_element()), value))
## Ported from HTMLTextAreaElement.cpp:270 Web::HTML::HTMLTextAreaElement::cols.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-cols
pub func html_text_area_element_cols(this: HtmlTextAreaElement*) -> u32:
# The cols and rows attributes are limited to only positive numbers with fallback. The cols IDL attribute's default value is 20.
if let cols_string = dom_element_get_attribute(this.dom_element(), html_attribute_names_cols):
if let cols = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&cols_string)):
if cols > 0 and cols <= 2147483647:
return cols
return 20
## Ported from HTMLTextAreaElement.cpp:280 Web::HTML::HTMLTextAreaElement::set_cols.
pub func html_text_area_element_set_cols(this: HtmlTextAreaElement*, cols: u32):
var new_cols = cols
if new_cols == 0 or new_cols > 2147483647:
new_cols = 20
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_cols, ak.string_number_u64(new_cols))
## Ported from HTMLTextAreaElement.cpp:289 Web::HTML::HTMLTextAreaElement::rows.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-rows
pub func html_text_area_element_rows(this: HtmlTextAreaElement*) -> u32:
# The cols and rows attributes are limited to only positive numbers with fallback. The rows IDL attribute's default value is 2.
if let rows_string = dom_element_get_attribute(this.dom_element(), html_attribute_names_rows):
if let rows = html_parse_non_negative_integer(ak.string_bytes_as_string_view(&rows_string)):
if rows > 0 and rows <= 2147483647:
return rows
return 2
## Ported from HTMLTextAreaElement.cpp:299 Web::HTML::HTMLTextAreaElement::set_rows.
pub func html_text_area_element_set_rows(this: HtmlTextAreaElement*, rows: u32):
var new_rows = rows
if new_rows == 0 or new_rows > 2147483647:
new_rows = 2
dom_element_set_attribute_value(this.dom_element(), html_attribute_names_rows, ak.string_number_u64(new_rows))
## Ported from HTMLTextAreaElement.cpp:307 Web::HTML::HTMLTextAreaElement::selection_start_binding.
pub func html_text_area_element_selection_start_binding(this: HtmlTextAreaElement*) -> u32:
return html_form_associated_text_control_element_selection_start_binding(html_text_area_element_text_control(this)) else trap("VERIFY: Optional::value() on an empty optional")
## Ported from HTMLTextAreaElement.cpp:312 Web::HTML::HTMLTextAreaElement::set_selection_start_binding.
pub func html_text_area_element_set_selection_start_binding(this: HtmlTextAreaElement*, value: u32) -> !:
try html_form_associated_text_control_element_set_selection_start_binding(html_text_area_element_text_control(this), value)
## Ported from HTMLTextAreaElement.cpp:317 Web::HTML::HTMLTextAreaElement::selection_end_binding.
pub func html_text_area_element_selection_end_binding(this: HtmlTextAreaElement*) -> u32:
return html_form_associated_text_control_element_selection_end_binding(html_text_area_element_text_control(this)) else trap("VERIFY: Optional::value() on an empty optional")
## Ported from HTMLTextAreaElement.cpp:322 Web::HTML::HTMLTextAreaElement::set_selection_end_binding.
pub func html_text_area_element_set_selection_end_binding(this: HtmlTextAreaElement*, value: u32) -> !:
try html_form_associated_text_control_element_set_selection_end_binding(html_text_area_element_text_control(this), value)
## Ported from HTMLTextAreaElement.cpp:327 Web::HTML::HTMLTextAreaElement::selection_direction_binding.
pub func html_text_area_element_selection_direction_binding(this: HtmlTextAreaElement*) -> ak.String:
return html_form_associated_text_control_element_selection_direction(html_text_area_element_text_control(this)) else trap("VERIFY: Optional::value() on an empty optional")
## Ported from HTMLTextAreaElement.cpp:332 Web::HTML::HTMLTextAreaElement::set_selection_direction_binding.
pub func html_text_area_element_set_selection_direction_binding(this: HtmlTextAreaElement*, direction: ak.String):
# NOTE: The selectionDirection setter never returns an error for textarea elements.
html_form_associated_text_control_element_set_selection_direction_binding(html_text_area_element_text_control(this), direction) else trap("MUST: set_selection_direction_binding")