LibWeb: Subclass FormAssociatedElement text selection methods

These are only relevant to HTMLInputElement and HTMLTextArea elements.
This commit is contained in:
Tim Ledbetter 2024-08-30 17:22:58 +01:00 committed by Andreas Kling
commit 206262cd55
Notes: github-actions[bot] 2024-08-31 09:50:53 +00:00
5 changed files with 47 additions and 47 deletions

View file

@ -166,18 +166,7 @@ void FormAssociatedElement::reset_form_owner()
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
String FormAssociatedElement::relevant_value() const
{
auto const& html_element = form_associated_element_to_html_element();
if (is<HTMLInputElement>(html_element))
return static_cast<HTMLInputElement const&>(html_element).value();
if (is<HTMLTextAreaElement>(html_element))
return static_cast<HTMLTextAreaElement const&>(html_element).api_value();
VERIFY_NOT_REACHED();
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
void FormAssociatedElement::relevant_value_was_changed(JS::GCPtr<DOM::Text> text_node)
void FormAssociatedTextControlElement::relevant_value_was_changed(JS::GCPtr<DOM::Text> text_node)
{
auto the_relevant_value = relevant_value();
auto relevant_value_length = the_relevant_value.code_points().length();
@ -214,7 +203,7 @@ void FormAssociatedElement::relevant_value_was_changed(JS::GCPtr<DOM::Text> text
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
WebIDL::ExceptionOr<void> FormAssociatedElement::select()
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::select()
{
// 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.
@ -232,7 +221,7 @@ WebIDL::ExceptionOr<void> FormAssociatedElement::select()
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionstart
Optional<WebIDL::UnsignedLong> FormAssociatedElement::selection_start() const
Optional<WebIDL::UnsignedLong> FormAssociatedTextControlElement::selection_start() const
{
// 1. If this element is an input element, and selectionStart does not apply to this element, return null.
auto const& html_element = form_associated_element_to_html_element();
@ -255,7 +244,7 @@ Optional<WebIDL::UnsignedLong> FormAssociatedElement::selection_start() const
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionstart-2
WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_start(Optional<WebIDL::UnsignedLong> const& value)
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_start(Optional<WebIDL::UnsignedLong> const& value)
{
// 1. If this element is an input element, and selectionStart does not apply to this element,
// throw an "InvalidStateError" DOMException.
@ -280,7 +269,7 @@ WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_start(Optional<We
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-selectionend
Optional<WebIDL::UnsignedLong> FormAssociatedElement::selection_end() const
Optional<WebIDL::UnsignedLong> FormAssociatedTextControlElement::selection_end() const
{
// 1. If this element is an input element, and selectionEnd does not apply to this element, return
// null.
@ -304,7 +293,7 @@ Optional<WebIDL::UnsignedLong> FormAssociatedElement::selection_end() const
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#textFieldSelection:dom-textarea/input-selectionend-3
WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_end(Optional<WebIDL::UnsignedLong> const& value)
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_end(Optional<WebIDL::UnsignedLong> const& value)
{
// 1. If this element is an input element, and selectionEnd does not apply to this element,
// throw an "InvalidStateError" DOMException.
@ -322,7 +311,7 @@ WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_end(Optional<WebI
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#selection-direction
Optional<String> FormAssociatedElement::selection_direction() const
Optional<String> FormAssociatedTextControlElement::selection_direction() const
{
// 1. If this element is an input element, and selectionDirection does not apply to this
// element, return null.
@ -347,7 +336,7 @@ Optional<String> FormAssociatedElement::selection_direction() const
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-direction
void FormAssociatedElement::set_selection_direction(Optional<String> direction)
void FormAssociatedTextControlElement::set_selection_direction(Optional<String> direction)
{
// 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
@ -357,7 +346,7 @@ void FormAssociatedElement::set_selection_direction(Optional<String> direction)
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction)
WebIDL::ExceptionOr<void> FormAssociatedTextControlElement::set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction)
{
// 1. If this element is an input element, and setSelectionRange() does not apply to this
// element, throw an "InvalidStateError" DOMException.
@ -371,7 +360,7 @@ WebIDL::ExceptionOr<void> FormAssociatedElement::set_selection_range(Optional<We
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#set-the-selection-range
void FormAssociatedElement::set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction)
void FormAssociatedTextControlElement::set_the_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, SelectionDirection direction)
{
// 1. If start is null, let start be zero.
start = start.value_or(0);

View file

@ -91,15 +91,40 @@ public:
virtual String value() const { return String {}; }
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
String relevant_value() const;
virtual HTMLElement& form_associated_element_to_html_element() = 0;
HTMLElement const& form_associated_element_to_html_element() const { return const_cast<FormAssociatedElement&>(*this).form_associated_element_to_html_element(); }
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-form-reset-control
virtual void reset_algorithm() {};
protected:
FormAssociatedElement() = default;
virtual ~FormAssociatedElement() = default;
virtual void form_associated_element_was_inserted() { }
virtual void form_associated_element_was_removed(DOM::Node*) { }
virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
void form_node_was_inserted();
void form_node_was_removed();
void form_node_attribute_changed(FlyString const&, Optional<String> const&);
virtual void selection_was_changed() { }
private:
void reset_form_owner();
WeakPtr<HTMLFormElement> m_form;
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
bool m_parser_inserted { false };
};
class FormAssociatedTextControlElement : public FormAssociatedElement {
public:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
virtual String relevant_value() = 0;
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
WebIDL::ExceptionOr<void> select();
@ -123,30 +148,10 @@ public:
WebIDL::ExceptionOr<void> set_selection_range(Optional<WebIDL::UnsignedLong> start, Optional<WebIDL::UnsignedLong> end, Optional<String> direction);
protected:
FormAssociatedElement() = default;
virtual ~FormAssociatedElement() = default;
virtual void form_associated_element_was_inserted() { }
virtual void form_associated_element_was_removed(DOM::Node*) { }
virtual void form_associated_element_attribute_changed(FlyString const&, Optional<String> const&) { }
void form_node_was_inserted();
void form_node_was_removed();
void form_node_attribute_changed(FlyString const&, Optional<String> const&);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
void relevant_value_was_changed(JS::GCPtr<DOM::Text>);
virtual void selection_was_changed() { }
private:
void reset_form_owner();
WeakPtr<HTMLFormElement> m_form;
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag
bool m_parser_inserted { false };
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-selection
WebIDL::UnsignedLong m_selection_start { 0 };
WebIDL::UnsignedLong m_selection_end { 0 };

View file

@ -49,7 +49,7 @@ namespace Web::HTML {
class HTMLInputElement final
: public HTMLElement
, public FormAssociatedElement
, public FormAssociatedTextControlElement
, public DOM::EditableTextNodeOwner
, public Layout::ImageProvider {
WEB_PLATFORM_OBJECT(HTMLInputElement, HTMLElement);
@ -77,6 +77,9 @@ public:
virtual String value() const override;
WebIDL::ExceptionOr<void> set_value(String const&);
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
virtual String relevant_value() override { return value(); }
void commit_pending_changes();
String placeholder() const;

View file

@ -20,7 +20,7 @@ namespace Web::HTML {
class HTMLTextAreaElement final
: public HTMLElement
, public FormAssociatedElement
, public FormAssociatedTextControlElement
, public DOM::EditableTextNodeOwner {
WEB_PLATFORM_OBJECT(HTMLTextAreaElement, HTMLElement);
JS_DECLARE_ALLOCATOR(HTMLTextAreaElement);
@ -84,6 +84,9 @@ public:
// https://html.spec.whatwg.org/multipage/form-elements.html#the-textarea-element:concept-fe-api-value-3
String api_value() const;
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value
virtual String relevant_value() override { return api_value(); }
u32 text_length() const;
bool check_validity();

View file

@ -1159,7 +1159,7 @@ void EventHandler::update_selection_range_for_input_or_textarea()
// FIXME: support selection directions other than ::Forward
auto direction = HTML::SelectionDirection::Forward;
Optional<HTML::FormAssociatedElement&> target {};
Optional<HTML::FormAssociatedTextControlElement&> target {};
if (is<HTML::HTMLInputElement>(shadow_host))
target = static_cast<HTML::HTMLInputElement&>(shadow_host);
else if (is<HTML::HTMLTextAreaElement>(shadow_host))