LibWeb: Select contents of <input> when tabbing through fields

Browsers seem to make it convenient to replace an <input>'s contents by
selecting all text on focusing, but only if you used keyboard
navigation. Programmatic focus and clicking on the field do not show
this behavior.
This commit is contained in:
Jelle Raaijmakers 2026-02-10 12:32:47 +01:00 committed by Jelle Raaijmakers
parent dbd09454c4
commit b07a576c22
4 changed files with 28 additions and 4 deletions

View file

@ -6,8 +6,6 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/Optional.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>

View file

@ -1472,8 +1472,12 @@ void HTMLInputElement::did_receive_focus()
if (m_placeholder_text_node)
m_placeholder_text_node->invalidate_style(DOM::StyleInvalidationReason::DidReceiveFocus);
if (has_selectable_text())
document().get_selection()->remove_all_ranges();
if (has_selectable_text()) {
if (document().last_focus_trigger() == FocusTrigger::Key)
MUST(select());
else
document().get_selection()->remove_all_ranges();
}
}
void HTMLInputElement::did_lose_focus()

View file

@ -0,0 +1,3 @@
a: 0 0
b: 0 5
c: 0 0

View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<input id="a" value="hello" />
<input id="b" value="world" />
<input id="c" />
<script src="include.js"></script>
<script>
test(() => {
a.focus();
a.offsetWidth;
println(`a: ${a.selectionStart} ${a.selectionEnd}`);
internals.sendKey(a, "Tab");
println(`b: ${b.selectionStart} ${b.selectionEnd}`);
internals.sendKey(b, "Tab");
println(`c: ${c.selectionStart} ${c.selectionEnd}`);
});
</script>