Skip to content

Truly independent web browser

LibWeb: Accept a writing suggestion on Tab ahead of the page

sideshowbarker852692a0bc9f
diff --git a/Libraries/LibWeb/Editing/WritingSuggestionController.h b/Libraries/LibWeb/Editing/WritingSuggestionController.h
index b4b046ffdbf..aaa12efb6d9 100644
--- a/Libraries/LibWeb/Editing/WritingSuggestionController.h
+++ b/Libraries/LibWeb/Editing/WritingSuggestionController.h
@@ -53,6 +53,8 @@ public:
void did_start_input_method_composition();
void did_end_input_method_composition();
void dismiss();
+ // Whether a suggestion is showing, with the caret still where it shows.
+ bool can_accept() const { return caret_is_at_showing_suggestion(); }
// Drops the suggestion that's showing, and returns what it would add if the caret is still where it shows.
Optional<Utf16String> accept();
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 3f87b0b53cd..bccd65fa3b4 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -1894,6 +1894,37 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
return EventResult::Handled;
};
+ // A plain Tab types in a writing suggestion that's showing, rather than moving focus. The suggestion takes the key
+ // ahead of the page, as an input method does — so the page sees the keydown but can't cancel it.
+ if (key == UIEvents::KeyCode::Key_Tab && !(modifiers & (UIEvents::KeyModifier::Mod_Alt | UIEvents::KeyModifier::Mod_Ctrl | UIEvents::KeyModifier::Mod_Shift | UIEvents::KeyModifier::Mod_Super))) {
+ GC::Ref<DOM::Document> document = *m_navigable->active_document();
+ if (auto* controller = document->writing_suggestion_controller_if_exists(); controller && controller->can_accept()) {
+ auto* target = document->active_input_events_target();
+ if (!target)
+ return EventResult::Handled;
+ GC::Root<JS::Cell> rooted_target { target->as_cell() };
+
+ // https://w3c.github.io/uievents/#determine-keydown-keyup-keyCode
+ // If an Input Method Editor is processing key input and the event is keydown, return 229.
+ static constexpr u32 input_method_key_code = 229;
+ fire_keyboard_event(UIEvents::EventNames::keydown, m_navigable, key, modifiers, code_point, repeat, input_method_key_code);
+ if (m_navigable->active_document() != document || !document->is_fully_active() || document->active_input_events_target() != target)
+ return EventResult::Handled;
+
+ // A keydown listener that moved the caret or changed the text leaves nothing to accept.
+ auto suggestion = controller->accept();
+ if (!suggestion.has_value())
+ return EventResult::Handled;
+
+ FIRE(input_event(UIEvents::EventNames::beforeinput, UIEvents::InputTypes::insertText, m_navigable, *suggestion));
+ FIRE(fire_text_input_event(m_navigable, *suggestion));
+ if (m_navigable->active_document() != document || !document->is_fully_active() || document->active_input_events_target() != target)
+ return EventResult::Handled;
+ target->handle_insert(UIEvents::InputTypes::insertText, *suggestion);
+ return EventResult::Handled;
+ }
+ }
+
GC::Root<DOM::Document> native_command_document;
if (native_editing_binding_resolved)
native_command_document = m_navigable->active_document();
@@ -1942,25 +1973,8 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
GC::Ref<DOM::Document> document = *m_navigable->active_document();
if (!(modifiers & UIEvents::KeyModifier::Mod_Ctrl)) {
- if (key == UIEvents::KeyCode::Key_Tab) {
- // Tab types in the writing suggestion that's showing, rather than moving focus. Shift+Tab never does.
- if (!(modifiers & UIEvents::KeyModifier::Mod_Shift)) {
- if (auto* controller = document->writing_suggestion_controller_if_exists()) {
- if (auto suggestion = controller->accept(); suggestion.has_value()) {
- auto* target = document->active_input_events_target();
- if (!target)
- return EventResult::Handled;
- FIRE(input_event(UIEvents::EventNames::beforeinput, UIEvents::InputTypes::insertText, m_navigable, *suggestion));
- FIRE(fire_text_input_event(m_navigable, *suggestion));
- if (m_navigable->active_document() != document || !document->is_fully_active() || document->active_input_events_target() != target)
- return EventResult::Handled;
- target->handle_insert(UIEvents::InputTypes::insertText, *suggestion);
- return EventResult::Handled;
- }
- }
- }
+ if (key == UIEvents::KeyCode::Key_Tab)
return focus_sequentially(modifiers & UIEvents::KeyModifier::Mod_Shift ? HTML::SequentialFocusDirection::Backward : HTML::SequentialFocusDirection::Forward);
- }
}
// https://html.spec.whatwg.org/multipage/interaction.html#close-requests
@@ -2943,7 +2957,7 @@ static GC::RootVector<GC::Ref<DOM::StaticRange>> target_ranges_for_input_event(D
return target_ranges;
}
-EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable& navigable, UIEvents::KeyCode key, u32 modifiers, u32 code_point, bool repeat)
+EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable& navigable, UIEvents::KeyCode key, u32 modifiers, u32 code_point, bool repeat, Optional<u32> key_code)
{
GC::Ptr<DOM::Document> document = navigable.active_document();
if (!document)
@@ -2959,16 +2973,20 @@ EventResult EventHandler::fire_keyboard_event(Utf16FlyString const& event_name,
auto* local_navigable = as_if<HTML::LocalNavigable>(*content_navigable);
if (!local_navigable)
return EventResult::Dropped;
- return fire_keyboard_event(event_name, *local_navigable, key, modifiers, code_point, repeat);
+ return fire_keyboard_event(event_name, *local_navigable, key, modifiers, code_point, repeat, key_code);
}
}
auto event = UIEvents::KeyboardEvent::create_from_platform_event(HTML::relevant_global_object(*document), event_name, key, modifiers, code_point, repeat);
+ if (key_code.has_value())
+ event->set_key_code(*key_code);
return focused_area->dispatch_event(event) ? EventResult::Accepted : EventResult::Cancelled;
}
// FIXME: De-duplicate this. This is just to prevent wasting a KeyboardEvent allocation when recursing into an (i)frame.
auto event = UIEvents::KeyboardEvent::create_from_platform_event(HTML::relevant_global_object(*document), event_name, key, modifiers, code_point, repeat);
+ if (key_code.has_value())
+ event->set_key_code(*key_code);
GC::Ptr target = document->body() ?: &document->root();
return target->dispatch_event(event) ? EventResult::Accepted : EventResult::Cancelled;
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index 9c6558a1ae2..d9df2a7ebd4 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -150,7 +150,7 @@ private:
GC::Ptr<DOM::Node> scroll_target_for_key_input() const;
int page_scroll_distance_for_key_input() const;
- EventResult fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable&, UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat);
+ EventResult fire_keyboard_event(Utf16FlyString const& event_name, HTML::LocalNavigable&, UIEvents::KeyCode, unsigned modifiers, u32 code_point, bool repeat, Optional<u32> key_code = {});
[[nodiscard]] EventResult fire_text_input_event(HTML::LocalNavigable&, Utf16String const& data);
[[nodiscard]] EventResult input_event(Utf16FlyString const& event_name, Utf16FlyString const& input_type, HTML::LocalNavigable&, Variant<u32, Utf16String> code_point_or_string, GC::Ptr<DOM::StaticRange> deletion_range = nullptr);
diff --git a/Libraries/LibWeb/UIEvents/KeyboardEvent.h b/Libraries/LibWeb/UIEvents/KeyboardEvent.h
index cb5482224a3..981f00672e4 100644
--- a/Libraries/LibWeb/UIEvents/KeyboardEvent.h
+++ b/Libraries/LibWeb/UIEvents/KeyboardEvent.h
@@ -42,6 +42,7 @@ public:
virtual ~KeyboardEvent() override;
u32 key_code() const { return m_key_code; }
+ void set_key_code(u32 key_code) { m_key_code = key_code; }
u32 char_code() const { return m_char_code; }
Utf16String const& key() const { return m_key; }
diff --git a/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt
index 6e0b2d895ac..8d873994038 100644
--- a/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt
+++ b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt
@@ -1,25 +1,45 @@
Tab with a suggestion:
value: prediction
focus: area
- events: beforeinput insertText ction, input insertText ction
+ events: keydown Tab 229 229, beforeinput insertText ction, input insertText ction
suggestion: (none)
Tab canceled by the page:
+ value: prediction
+ focus: area
+ events: keydown Tab 229 229, beforeinput insertText ction, input insertText ction
+ suggestion: (none)
+Shift+Tab canceled by the page:
+ value: predi
+ focus: area
+ events: keydown Tab 9 9
+ suggestion: (none)
+Alt+Tab canceled by the page:
+ value: predi
+ focus: area
+ events: keydown Tab 9 9
+ suggestion: (none)
+Super+Tab canceled by the page:
+ value: predi
+ focus: area
+ events: keydown Tab 9 9
+ suggestion: (none)
+Tab whose keydown moves the caret:
value: predi
focus: area
- events: (none)
+ events: keydown Tab 229 229
suggestion: (none)
Tab without a suggestion:
value:
focus: after
- events: (none)
+ events: keydown Tab 9 9
suggestion: (none)
Shift+Tab with a suggestion:
value: predi
focus: before
- events: (none)
+ events: keydown Tab 9 9
suggestion: (none)
Insertion canceled by the page:
value: predi
focus: area
- events: beforeinput insertText ction
+ events: keydown Tab 229 229, beforeinput insertText ction
suggestion: (none)
diff --git a/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html b/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html
index 636b5664d46..ec2d73e5166 100644
--- a/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html
+++ b/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html
@@ -10,6 +10,8 @@
let log = [];
for (const type of ["beforeinput", "input"])
area.addEventListener(type, e => log.push(`${type} ${e.inputType} ${e.data}`));
+ for (const type of ["keydown", "keypress"])
+ area.addEventListener(type, e => log.push(`${type} ${e.key} ${e.keyCode} ${e.which}`));
const reset = async () => {
area.value = "";
@@ -38,8 +40,30 @@
area.addEventListener("keydown", cancelTab);
internals.sendText(area, TAB);
await show("Tab canceled by the page");
+
+ await reset();
+ internals.sendText(area, TAB, internals.MOD_SHIFT);
+ await show("Shift+Tab canceled by the page");
+
+ await reset();
+ internals.sendText(area, TAB, internals.MOD_ALT);
+ await show("Alt+Tab canceled by the page");
+
+ await reset();
+ internals.sendText(area, TAB, internals.MOD_SUPER);
+ await show("Super+Tab canceled by the page");
area.removeEventListener("keydown", cancelTab);
+ await reset();
+ const moveCaret = e => {
+ if (e.key === "Tab")
+ area.setSelectionRange(0, 0);
+ };
+ area.addEventListener("keydown", moveCaret);
+ internals.sendText(area, TAB);
+ await show("Tab whose keydown moves the caret");
+ area.removeEventListener("keydown", moveCaret);
+
area.value = "";
log = [];
internals.sendText(area, TAB);