Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_text_area_element_2.lucb

#==============================================================================================
#
#   html_text_area_element_2 - HTML::HTMLTextAreaElement: shadow tree, edits, validity, inlines
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLTextAreaElement.cpp (lines 338-489) and the inline functions of
#       HTMLTextAreaElement.h: the user-agent shadow tree (inner text and placeholder), the
#       children changed and attribute change steps, edits of the text node, the return key,
#       constraint validation and the TextAreaBox layout node.
#
#==============================================================================================

# mark: The shadow tree =========================================================================

## Ported from HTMLTextAreaElement.cpp:338 Web::HTML::HTMLTextAreaElement::create_shadow_tree_if_needed.
func html_text_area_element_create_shadow_tree_if_needed(this: HtmlTextAreaElement*):
    if dom_element_shadow_root(this.dom_element()) != none:
        return

    let realm = html_element_realm(this.html_element())
    let document = dom_node_document(this.dom_node())
    let shadow_root = realm_create_dom_shadow_root(realm, document, this.dom_element(), BindShadowRootMode.closed)
    dom_shadow_root_set_user_agent_internal(shadow_root, true)
    dom_element_set_shadow_root(this.dom_element(), shadow_root)

    let element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("MUST: DOM::create_element")
    html_append_child(shadow_root.dom_node(), element.dom_node())

    let inner_text_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("MUST: DOM::create_element")
    this.m_inner_text_element = inner_text_element
    html_append_child(element.dom_node(), inner_text_element.dom_node())

    # NOTE: If `children_changed()` was called before now, `m_raw_value` will hold the text content.
    #       Otherwise, it will get filled in whenever that does get called.
    let text_node = realm_create_dom_text_utf16_string(realm, document, this.m_raw_value)
    this.m_text_node = text_node
    html_text_area_element_handle_maxlength_attribute(this)
    html_append_child(inner_text_element.dom_node(), text_node.dom_node())

    let placeholder_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("MUST: DOM::create_element")
    this.m_placeholder_element = placeholder_element
    dom_element_set_use_pseudo_element(placeholder_element, css_data.PseudoElement.placeholder)
    html_append_child(element.dom_node(), placeholder_element.dom_node())

    let placeholder = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_placeholder)
    let placeholder_text_node = realm_create_dom_text_utf16_string(realm, document, ak.utf16_string_from_utf8_string(&placeholder))
    this.m_placeholder_text_node = placeholder_text_node
    html_append_child(placeholder_element.dom_node(), placeholder_text_node.dom_node())

    html_text_area_element_update_placeholder_visibility(this)

## Ported from HTMLTextAreaElement.cpp:370 Web::HTML::HTMLTextAreaElement::handle_maxlength_attribute.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength
func html_text_area_element_handle_maxlength_attribute(this: HtmlTextAreaElement*):
    if let text_node = this.m_text_node:
        let max_length = html_text_area_element_max_length(this)
        if max_length >= 0:
            dom_text_set_max_length(text_node, (u64)max_length)
        else:
            dom_text_set_max_length(text_node, none)

## Ported from HTMLTextAreaElement.cpp:382 Web::HTML::HTMLTextAreaElement::update_placeholder_visibility.
func html_text_area_element_update_placeholder_visibility(this: HtmlTextAreaElement*):
    let placeholder_element = this.m_placeholder_element else return
    let text_node = this.m_text_node else return
    let inner_text_element = this.m_inner_text_element else trap("VERIFY: GC::Ptr<Element>::operator-> on null")
    let placeholder_text = dom_element_get_attribute(this.dom_element(), html_attribute_names_placeholder)
    let data = dom_character_data_data(text_node.dom_character_data())
    if placeholder_text != none and html_utf16_is_empty(&data):
        css_style_properties_set_property_property_id(dom_element_style_for_bindings(inner_text_element), css_data.PropertyId.display, ak.sv("inline"), ak.sv("")) else trap("MUST: set_property")
        css_style_properties_set_property_property_id(dom_element_style_for_bindings(placeholder_element), css_data.PropertyId.display, ak.sv("inline"), ak.sv("")) else trap("MUST: set_property")
    else:
        css_style_properties_set_property_property_id(dom_element_style_for_bindings(inner_text_element), css_data.PropertyId.display, ak.sv("block"), ak.sv("")) else trap("MUST: set_property")
        css_style_properties_set_property_property_id(dom_element_style_for_bindings(placeholder_element), css_data.PropertyId.display, ak.sv("none"), ak.sv("")) else trap("MUST: set_property")

## Ported from HTMLTextAreaElement.cpp:399 Web::HTML::HTMLTextAreaElement::children_changed (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:children-changed-steps
pub func html_text_area_element_children_changed_impl(base: DomNode*, metadata: DomNodeChildrenChangedMetadata):
    let this = (HtmlTextAreaElement*)base
    dom_element_children_changed_impl(base, metadata)

    # The children changed steps for textarea elements must, if the element's dirty value flag is false,
    # set the element's raw value to its child text content.
    if not this.m_dirty_value:
        html_text_area_element_set_raw_value(this, dom_node_child_text_content(this.dom_node()))
        if let text_node = this.m_text_node:
            dom_character_data_set_data(text_node.dom_character_data(), this.m_raw_value)
        html_text_area_element_update_placeholder_visibility(this)

## Ported from HTMLTextAreaElement.cpp:413 Web::HTML::HTMLTextAreaElement::form_associated_element_attribute_changed (override).
pub func html_text_area_element_form_associated_element_attribute_changed_impl(base: HtmlFormAssociatedElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    _ = old_value
    _ = namespace_
    let this = html_text_area_element_from_form_associated_element(base)
    if ak.fly_string_eq_fly_string(&name, &html_attribute_names_placeholder):
        if let placeholder_text_node = this.m_placeholder_text_node:
            let placeholder = value else ak.string_new()
            dom_character_data_set_data(placeholder_text_node.dom_character_data(), ak.utf16_string_from_utf8_string(&placeholder))
        html_text_area_element_update_placeholder_visibility(this)
    elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_maxlength):
        html_text_area_element_handle_maxlength_attribute(this)

# mark: User interaction ========================================================================

## The captures of did_edit_text_node's element task: `[this, input_type, data]`.
struct HtmlTextAreaElementDidEditTextNodeTask:
    var this: HtmlTextAreaElement*
    var input_type: ak.FlyString
    var data: ak.Utf16String?

## did_edit_text_node's element task: fire an input event.
func html_text_area_element_did_edit_text_node_task(context: void*?):
    let captures = (HtmlTextAreaElementDidEditTextNodeTask*)(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
    input_event_init.input_type = captures.input_type
    input_event_init.data = captures.data
    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:424 Web::HTML::HTMLTextAreaElement::did_edit_text_node (override).
pub func html_text_area_element_did_edit_text_node_impl(base: HtmlFormAssociatedTextControlElement*, input_type: ak.FlyString, data: ak.Utf16String?):
    let this = html_text_area_element_from_text_control(base)
    let text_node = this.m_text_node else trap("VERIFY: m_text_node")
    html_text_area_element_set_raw_value(this, dom_character_data_data(text_node.dom_character_data()))

    # Any time the user causes the element's raw value to change, the user agent must queue an element task on the user
    # interaction task source given the textarea element to fire an event named input at the textarea element, with the
    # bubbles and composed attributes initialized to true.
    let captures = new HtmlTextAreaElementDidEditTextNodeTask(this = this, input_type = input_type, data = data) 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_edit_text_node_task, context = captures))

    # A textarea element's dirty value flag must be set to true whenever the user interacts with the control in a way that changes the raw value.
    this.m_dirty_value = true

    html_text_area_element_update_placeholder_visibility(this)

## Ported from HTMLTextAreaElement.cpp:448 Web::HTML::HTMLTextAreaElement::handle_return_key (override).
pub func html_text_area_element_handle_return_key_impl(base: InputEventsTarget*, input_type: ak.FlyString) -> EventResult:
    input_events_target_handle_insert(base, input_type, ak.utf16_string_from_code_point(0x0A))  # Avoid the platform codepoint
    return EventResult.handled

## Ported from HTMLTextAreaElement.cpp:454 Web::HTML::HTMLTextAreaElement::is_focusable (override).
pub func html_text_area_element_is_focusable_impl(base: DomEventTarget*) -> bool:
    return html_form_associated_element_enabled(html_text_area_element_form_associated((HtmlTextAreaElement*)base))

# mark: Constraint validation and layout ========================================================

## Ported from HTMLTextAreaElement.cpp:460 Web::HTML::HTMLTextAreaElement::suffering_from_being_missing (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element%3Asuffering-from-being-missing
pub func html_text_area_element_suffering_from_being_missing_impl(base: HtmlFormAssociatedElement*) -> bool:
    let this = html_text_area_element_from_form_associated_element(base)
    # If the element has its required attribute specified, and the element is mutable, and the element's value is the empty string, then the element is suffering from
    # being missing.
    if not dom_element_has_attribute(this.dom_element(), html_attribute_names_required) or not html_text_area_element_is_mutable_impl(base):
        return false
    let value = html_text_area_element_value(this)
    return html_utf16_is_empty(&value)

## Ported from HTMLTextAreaElement.cpp:468 Web::HTML::HTMLTextAreaElement::is_mutable (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-mutable
pub func html_text_area_element_is_mutable_impl(base: HtmlFormAssociatedElement*) -> bool:
    let this = html_text_area_element_from_form_associated_element(base)
    # A textarea element is mutable if it is neither disabled nor has a readonly attribute specified.
    return html_form_associated_element_enabled(base) and not dom_element_has_attribute(this.dom_element(), html_attribute_names_readonly)

## Ported from HTMLTextAreaElement.cpp:475 Web::HTML::HTMLTextAreaElement::placeholder_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-placeholder
pub func html_text_area_element_placeholder_value(this: HtmlTextAreaElement*) -> ak.String?:
    let text_node = this.m_text_node else return none
    let data = dom_character_data_data(text_node.dom_character_data())
    if not html_utf16_is_empty(&data):
        return none
    if not dom_element_has_attribute(this.dom_element(), html_attribute_names_placeholder):
        return none
    return dom_element_get_attribute_value(this.dom_element(), html_attribute_names_placeholder)

## Ported from HTMLTextAreaElement.cpp:484 Web::HTML::HTMLTextAreaElement::create_layout_node (override).
pub func html_text_area_element_create_layout_node_impl(base: DomElement*, style: CssComputedProperties*) -> LayoutNode*?:
    # heap().allocate<Layout::TextAreaBox>(...): a layout node's initialize is JS::Cell's, which does nothing.
    return realm_create_layout_text_area_box(dom_element_realm(base), dom_node_document(base.dom_node()), base, style).layout_node()

# mark: Inline functions of HTMLTextAreaElement.h ===============================================

## Ported from HTMLTextAreaElement.h:29 Web::HTML::HTMLTextAreaElement::autocomplete_element_to_html_element (override).
pub func html_text_area_element_autocomplete_element_to_html_element_impl(base: HtmlAutocompleteElement*) -> HtmlElement*:
    return (HtmlElement*)((u8*)base - memory.offset_of(HtmlTextAreaElement, autocomplete_element))

## Ported from HTMLTextAreaElement.h:36 Web::HTML::HTMLTextAreaElement::type.
## (C++ returns a function-local static String; a module global is not scanned by the collector,
## so the port makes the same value each time.)
pub func html_text_area_element_type(this: HtmlTextAreaElement*) -> ak.String:
    _ = this
    return ak.string_from_str("textarea")

## Ported from HTMLTextAreaElement.h:52 Web::HTML::HTMLTextAreaElement::is_form_associated_element (override).
pub func html_text_area_element_is_form_associated_element_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:56 Web::HTML::HTMLTextAreaElement::is_listed (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-listed
pub func html_text_area_element_is_listed_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:59 Web::HTML::HTMLTextAreaElement::is_submittable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-submit
pub func html_text_area_element_is_submittable_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:62 Web::HTML::HTMLTextAreaElement::is_resettable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-reset
pub func html_text_area_element_is_resettable_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:65 Web::HTML::HTMLTextAreaElement::is_autocapitalize_and_autocorrect_inheriting (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
pub func html_text_area_element_is_autocapitalize_and_autocorrect_inheriting_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:68 Web::HTML::HTMLTextAreaElement::is_labelable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-label
pub func html_text_area_element_is_labelable_impl(base: HtmlElement*) -> bool:
    _ = base
    return true

## Ported from HTMLTextAreaElement.h:81 Web::HTML::HTMLTextAreaElement::default_role (override).
# https://www.w3.org/TR/html-aria/#el-textarea
pub func html_text_area_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    _ = base
    return AriaRole.textbox

## Ported from HTMLTextAreaElement.h:87 Web::HTML::HTMLTextAreaElement::form_value (override).
pub func html_text_area_element_form_value_impl(base: HtmlFormAssociatedElement*) -> ak.Utf16String:
    return html_text_area_element_value(html_text_area_element_from_form_associated_element(base))

## Ported from HTMLTextAreaElement.h:94 Web::HTML::HTMLTextAreaElement::relevant_value (override).
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
pub func html_text_area_element_relevant_value_impl(base: HtmlFormAssociatedTextControlElement*) -> ak.Utf16String:
    return html_text_area_element_api_value(html_text_area_element_from_text_control(base))

## Ported from HTMLTextAreaElement.h:97 Web::HTML::HTMLTextAreaElement::set_dirty_value_flag (override).
pub func html_text_area_element_set_dirty_value_flag_impl(base: HtmlFormAssociatedTextControlElement*, flag: bool):
    html_text_area_element_from_text_control(base).m_dirty_value = flag

## Ported from HTMLTextAreaElement.h:99 Web::HTML::HTMLTextAreaElement::user_validity.
pub func html_text_area_element_user_validity(this: HtmlTextAreaElement*) -> bool:
    return this.m_user_validity

## Ported from HTMLTextAreaElement.h:100 Web::HTML::HTMLTextAreaElement::set_user_validity.
pub func html_text_area_element_set_user_validity(this: HtmlTextAreaElement*, flag: bool):
    this.m_user_validity = flag

## Ported from HTMLTextAreaElement.h:128 Web::HTML::HTMLTextAreaElement::set_dirty_value_flag(Badge<FormAssociatedElement>, bool).
pub func html_text_area_element_set_dirty_value_flag_form_associated_element(this: HtmlTextAreaElement*, flag: bool):
    this.m_dirty_value = flag

## Ported from HTMLTextAreaElement.h:131 Web::HTML::HTMLTextAreaElement::text_control_to_html_element (override).
pub func html_text_area_element_text_control_to_html_element_impl(base: HtmlFormAssociatedTextControlElement*) -> HtmlElement*:
    return html_text_area_element_from_text_control(base).html_element()

## Ported from HTMLTextAreaElement.h:133 Web::HTML::HTMLTextAreaElement::form_associated_element_to_text_node (override).
pub func html_text_area_element_form_associated_element_to_text_node_impl(base: HtmlFormAssociatedTextControlElement*) -> DomText*?:
    return html_text_area_element_from_text_control(base).m_text_node

## Ported from HTMLTextAreaElement.h:134 Web::HTML::HTMLTextAreaElement::text_control_scroll_container (override).
pub func html_text_area_element_text_control_scroll_container_impl(base: HtmlFormAssociatedTextControlElement*) -> DomElement*?:
    return html_text_area_element_from_text_control(base).dom_element()

## Ported from HTMLTextAreaElement.h:142 Web::HTML::HTMLTextAreaElement::placeholder_element.
pub func html_text_area_element_placeholder_element(this: HtmlTextAreaElement*) -> DomElement*?:
    return this.m_placeholder_element

## Ported from HTMLTextAreaElement.h:143 Web::HTML::HTMLTextAreaElement::placeholder_element (const).
pub func html_text_area_element_placeholder_element_const(this: HtmlTextAreaElement*) -> DomElement*?:
    return this.m_placeholder_element

## Ported from HTMLTextAreaElement.h:152 Web::HTML::HTMLTextAreaElement::is_html_textarea_element (override).
pub func html_text_area_element_is_html_textarea_element_impl(base: DomNode*) -> bool:
    _ = base
    return true