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:
parent
dbd09454c4
commit
b07a576c22
4 changed files with 28 additions and 4 deletions
|
|
@ -6,8 +6,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <LibWeb/Export.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
3
Tests/LibWeb/Text/expected/input-select-on-tab-focus.txt
Normal file
3
Tests/LibWeb/Text/expected/input-select-on-tab-focus.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a: 0 0
|
||||
b: 0 5
|
||||
c: 0 0
|
||||
19
Tests/LibWeb/Text/input/input-select-on-tab-focus.html
Normal file
19
Tests/LibWeb/Text/input/input-select-on-tab-focus.html
Normal 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>
|
||||
Loading…
Reference in a new issue