Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp (C++)

Code before:
    } else if (name == HTML::AttributeNames::maxlength) {
        handle_maxlength_attribute();
    }
}

void HTMLTextAreaElement::did_edit_text_node()
{
    VERIFY(m_text_node);
    set_raw_value(m_text_node->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. User agents may wait for a suitable break in the user's
    // interaction before queuing the task; for example, a user agent could wait for the user to have not hit a key for
    // 100ms, so as to only fire the event when the user pauses, instead of continuously for each keystroke.
    m_input_event_timer->restart(100);

    // 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.
    m_dirty_value = true;

    update_placeholder_visibility();
}

void HTMLTextAreaElement::queue_firing_input_event()
{
    queue_an_element_task(HTML::Task::Source::UserInteraction, [this]() {
        // FIXME: If a string was added to this textarea, this input event's .data should be set to it.
        auto change_event = DOM::Event::create(realm(), HTML::EventNames::input, { .bubbles = true, .composed = true });
        dispatch_event(change_event);
    });
}

bool HTMLTextAreaElement::is_focusable() const
{
    return enabled();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element%3Asuffering-from-being-missing
bool HTMLTextAreaElement::suffering_from_being_missing() const
{
    // 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.
    return has_attribute(HTML::AttributeNames::required) && is_mutable() && value().is_empty();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-mutable
bool HTMLTextAreaElement::is_mutable() const
{
    // A textarea element is mutable if it is neither disabled nor has a readonly attribute specified.
    return enabled() && !has_attribute(AttributeNames::readonly);
}

Review comment (suggestion):
Looks good! Would it be possible to just change this over to `'\n'` instead as the final thing? I personally don't have the ASCII mapping memorized 😅 

Code after:
    } else if (name == HTML::AttributeNames::maxlength) {
        handle_maxlength_attribute();
    }
}

void HTMLTextAreaElement::did_edit_text_node()
{
    VERIFY(m_text_node);
    set_raw_value(m_text_node->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. User agents may wait for a suitable break in the user's
    // interaction before queuing the task; for example, a user agent could wait for the user to have not hit a key for
    // 100ms, so as to only fire the event when the user pauses, instead of continuously for each keystroke.
    m_input_event_timer->restart(100);

    // 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.
    m_dirty_value = true;

    update_placeholder_visibility();
}

EventResult HTMLTextAreaElement::handle_return_key(FlyString const&)
{
    handle_insert(Utf16String::from_code_point(0x0A)); // Avoid the platform codepoint
    return EventResult::Handled;
}

void HTMLTextAreaElement::queue_firing_input_event()
{
    queue_an_element_task(HTML::Task::Source::UserInteraction, [this]() {
        // FIXME: If a string was added to this textarea, this input event's .data should be set to it.
        auto change_event = DOM::Event::create(realm(), HTML::EventNames::input, { .bubbles = true, .composed = true });
        dispatch_event(change_event);
    });
}

bool HTMLTextAreaElement::is_focusable() const
{
    return enabled();
}

// https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element%3Asuffering-from-being-missing
bool HTMLTextAreaElement::suffering_from_being_missing() const
{
    // 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.
    return has_attribute(HTML::AttributeNames::required) && is_mutable() && value().is_empty();
}