Skip to content

Truly independent web browser

LibWeb: Focus text controls in select()

Andreas Klingd8e5945cd80f
diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
index 9eaf9960da9..87ef0783c98 100644
--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp
@@ -880,11 +880,20 @@ 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.
auto& html_element = text_control_to_html_element();
- if (is<HTMLInputElement>(html_element)) {
- auto& input_element = static_cast<HTMLInputElement&>(html_element);
- if (!input_element.select_applies() || !input_element.has_selectable_text())
- return {};
- }
+ auto select_does_not_apply = [&] {
+ auto* input_element = as_if<HTMLInputElement>(html_element);
+ return input_element && (!input_element->select_applies() || !input_element->has_selectable_text());
+ };
+ if (select_does_not_apply())
+ return {};
+
+ // INTEROP: select() also focuses the control, without scrolling it into view. Sites rely on this to focus an
+ // input, e.g. Instagram's search box.
+ html_element.focus({ .prevent_scroll = true });
+
+ // AD-HOC: Focus event handlers may have changed the control's type, so check step 1 again.
+ if (select_does_not_apply())
+ return {};
// 2. Set the selection range with 0 and infinity.
set_the_selection_range(0, NumericLimits<WebIDL::UnsignedLong>::max());
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index b09617b4da6..7d12d0b725d 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -1609,7 +1609,7 @@ void HTMLInputElement::did_receive_focus()
if (has_selectable_text()) {
if (document().last_focus_trigger() == FocusTrigger::Key)
- MUST(select());
+ set_the_selection_range(0, NumericLimits<WebIDL::UnsignedLong>::max());
else
document().get_selection()->remove_all_ranges();
}
diff --git a/Tests/LibWeb/Text/expected/HTML/select-focuses-text-control.txt b/Tests/LibWeb/Text/expected/HTML/select-focuses-text-control.txt
index 97d22c20449..14bcb42ca4d 100644
--- a/Tests/LibWeb/Text/expected/HTML/select-focuses-text-control.txt
+++ b/Tests/LibWeb/Text/expected/HTML/select-focuses-text-control.txt
@@ -1,12 +1,12 @@
-empty-input: activeElement=BODY
+empty-input: activeElement=empty-input
empty-input: selection=0-0
-filled-input: activeElement=BODY
+filled-input: activeElement=filled-input
filled-input: selection=0-5
-textarea: activeElement=BODY
+textarea: activeElement=textarea
textarea: selection=0-5
scrollY: 0
-disabled-input: activeElement=BODY
+disabled-input: activeElement=textarea
disabled-input: selection=0-8
-type-changing-input: activeElement=BODY
-type-changing-input: selection=0-4
-events:
+type-changing-input: activeElement=type-changing-input
+type-changing-input: selection=null-null
+events: focus empty-input, blur empty-input, focus filled-input, blur filled-input, focus textarea, blur textarea, focus type-changing-input