LibWeb: Propagate input/textarea selection update to document selection

Calling `.setSelectionRange()` or `.select()` now updates the document
selection as well, visualizing the text selection.
This commit is contained in:
Jelle Raaijmakers 2024-08-26 14:08:40 +02:00 committed by Tim Flynn
commit 732e3fa82f
Notes: github-actions[bot] 2024-08-27 11:12:49 +00:00
6 changed files with 42 additions and 7 deletions

View file

@ -411,15 +411,22 @@ void FormAssociatedElement::set_the_selection_range(Optional<WebIDL::UnsignedLon
// 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) {
auto& html_element = form_associated_element_to_html_element();
// AD-HOC: If there is no selection, we do not fire the event. This seems to correspond to how
// other browsers behave.
if (was_modified && m_selection_start != m_selection_end) {
auto& html_element = form_associated_element_to_html_element();
if (m_selection_start != m_selection_end) {
html_element.queue_an_element_task(Task::Source::UserInteraction, [&html_element] {
auto select_event = DOM::Event::create(html_element.realm(), EventNames::select, { .bubbles = true });
static_cast<DOM::EventTarget*>(&html_element)->dispatch_event(select_event);
});
}
// AD-HOC: Notify the element that the selection was changed, so it can perform
// element-specific updates.
selection_was_changed();
}
}
}

View file

@ -137,6 +137,8 @@ protected:
// 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();

View file

@ -45,6 +45,7 @@
#include <LibWeb/MimeSniff/Resource.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Selection/Selection.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/MouseEvent.h>
#include <LibWeb/WebIDL/DOMException.h>
@ -2353,4 +2354,13 @@ HTMLInputElement::ValueAttributeMode HTMLInputElement::value_attribute_mode() co
VERIFY_NOT_REACHED();
}
void HTMLInputElement::selection_was_changed()
{
auto selection = document().get_selection();
if (!selection || selection->range_count() == 0)
return;
MUST(selection->set_base_and_extent(*m_text_node, selection_start().value(), *m_text_node, selection_end().value()));
}
}

View file

@ -199,6 +199,9 @@ public:
bool select_applies() const;
bool selection_or_range_applies() const;
protected:
void selection_was_changed() override;
private:
HTMLInputElement(DOM::Document&, DOM::QualifiedName);

View file

@ -22,6 +22,7 @@
#include <LibWeb/HTML/Numbers.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Namespace.h>
#include <LibWeb/Selection/Selection.h>
namespace Web::HTML {
@ -452,4 +453,13 @@ void HTMLTextAreaElement::queue_firing_input_event()
});
}
void HTMLTextAreaElement::selection_was_changed()
{
auto selection = document().get_selection();
if (!selection || selection->range_count() == 0)
return;
MUST(selection->set_base_and_extent(*m_text_node, selection_start().value(), *m_text_node, selection_end().value()));
}
}

View file

@ -114,6 +114,9 @@ public:
String selection_direction_binding() const;
void set_selection_direction_binding(String direction);
protected:
void selection_was_changed() override;
private:
HTMLTextAreaElement(DOM::Document&, DOM::QualifiedName);