Truly independent web browser
LibWeb: Accept a writing suggestion with Tab
sideshowbarkera74c44e359dd
diff --git a/Libraries/LibWeb/Editing/WritingSuggestionController.cpp b/Libraries/LibWeb/Editing/WritingSuggestionController.cppindex 5e00a6ec6e2..56aa14c04dc 100644--- a/Libraries/LibWeb/Editing/WritingSuggestionController.cpp+++ b/Libraries/LibWeb/Editing/WritingSuggestionController.cpp@@ -133,6 +133,17 @@ void WritingSuggestionController::dismiss()set_showing({});}+Optional<Utf16String> WritingSuggestionController::accept()+{+ if (!m_showing.has_value())+ return {};+ Optional<Utf16String> remainder;+ if (caret_is_at_showing_suggestion())+ remainder = m_showing->remainder;+ dismiss();+ return remainder;+}+void WritingSuggestionController::did_focus(GC::Ptr<DOM::Node>){dismiss();diff --git a/Libraries/LibWeb/Editing/WritingSuggestionController.h b/Libraries/LibWeb/Editing/WritingSuggestionController.hindex 1a81b492e64..b4b046ffdbf 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();+ // Drops the suggestion that's showing, and returns what it would add if the caret is still where it shows.+ Optional<Utf16String> accept();bool is_settled() const;diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex 7423cff1385..d3d6b7e183c 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -28,6 +28,7 @@#include <LibWeb/DOM/Text.h>#include <LibWeb/Editing/EditingHistory.h>#include <LibWeb/Editing/Internal/Algorithms.h>+#include <LibWeb/Editing/WritingSuggestionController.h>#include <LibWeb/HTML/CloseWatcherManager.h>#include <LibWeb/HTML/DataTransfer.h>#include <LibWeb/HTML/DragDataStore.h>@@ -1897,8 +1898,14 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, uif (native_editing_binding_resolved)native_command_document = m_navigable->active_document();auto dispatch_result = fire_keyboard_event(UIEvents::EventNames::keydown, m_navigable, key, modifiers, code_point, repeat);- if (dispatch_result != EventResult::Accepted)+ if (dispatch_result != EventResult::Accepted) {+ // A page that handles Tab or Escape itself gets the key, and the writing suggestion is dropped.+ if (first_is_one_of(key, UIEvents::KeyCode::Key_Tab, UIEvents::KeyCode::Key_Escape)) {+ if (auto document = m_navigable->active_document(); document && document->writing_suggestion_controller_if_exists())+ document->writing_suggestion_controller_if_exists()->dismiss();+ }return dispatch_result;+ }// https://w3c.github.io/uievents/#event-type-keypress// If supported by a user agent, this event MUST be dispatched when a key is pressed down, if and only if that key@@ -1936,6 +1943,22 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, uif (!(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;+ }+ }+ }return focus_sequentially(modifiers & UIEvents::KeyModifier::Mod_Shift ? HTML::SequentialFocusDirection::Backward : HTML::SequentialFocusDirection::Forward);}}@@ -1949,6 +1972,14 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, ureturn EventResult::Handled;}+ // AD-HOC: The spec treats any Escape keydown that the page doesn't cancel as a close request. But while a+ // writing suggestion shows, Escape only dismisses it, as it does in native text fields — so dismissing+ // a suggestion in a modal dialog doesn't also close the dialog.+ if (auto* controller = document->writing_suggestion_controller_if_exists(); controller && controller->showing().has_value()) {+ controller->dismiss();+ return EventResult::Handled;+ }+// 1. If document's fullscreen element is not null, then:if (document->fullscreen()) {// 1. Fully exit fullscreen given document's node navigable's top-level traversable's active document.diff --git a/Tests/LibWeb/Text/expected/Editing/writing-suggestion-escape.txt b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-escape.txtnew file mode 100644index 00000000000..3da1f380267--- /dev/null+++ b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-escape.txt@@ -0,0 +1,3 @@+showing: ction+after Escape: (none), closed: false+after a second Escape: closed: truediff --git a/Tests/LibWeb/Text/expected/Editing/writing-suggestion-not-drawn.txt b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-not-drawn.txtnew file mode 100644index 00000000000..b6f9c02b0b5--- /dev/null+++ b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-not-drawn.txt@@ -0,0 +1,3 @@+suggestion: (none)+value: predi+focus: nextdiff --git a/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txtnew file mode 100644index 00000000000..6e0b2d895ac--- /dev/null+++ b/Tests/LibWeb/Text/expected/Editing/writing-suggestion-tab.txt@@ -0,0 +1,25 @@+Tab with a suggestion:+ value: prediction+ focus: area+ events: beforeinput insertText ction, input insertText ction+ suggestion: (none)+Tab canceled by the page:+ value: predi+ focus: area+ events: (none)+ suggestion: (none)+Tab without a suggestion:+ value:+ focus: after+ events: (none)+ suggestion: (none)+Shift+Tab with a suggestion:+ value: predi+ focus: before+ events: (none)+ suggestion: (none)+Insertion canceled by the page:+ value: predi+ focus: area+ events: beforeinput insertText ction+ suggestion: (none)diff --git a/Tests/LibWeb/Text/input/Editing/writing-suggestion-escape.html b/Tests/LibWeb/Text/input/Editing/writing-suggestion-escape.htmlnew file mode 100644index 00000000000..979368e8d3a--- /dev/null+++ b/Tests/LibWeb/Text/input/Editing/writing-suggestion-escape.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<textarea id="area"></textarea>+<script>+ promiseTest(async () => {+ const area = document.getElementById("area");+ const watcher = new CloseWatcher();+ let closed = false;+ watcher.onclose = () => closed = true;++ internals.sendText(area, "predi");+ await internals.writingSuggestionSettled();+ println(`showing: ${internals.writingSuggestion()}`);++ internals.sendKey(area, "Escape");+ await internals.writingSuggestionSettled();+ println(`after Escape: ${internals.writingSuggestion()}, closed: ${closed}`);++ internals.sendKey(area, "Escape");+ println(`after a second Escape: closed: ${closed}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/Editing/writing-suggestion-not-drawn.html b/Tests/LibWeb/Text/input/Editing/writing-suggestion-not-drawn.htmlnew file mode 100644index 00000000000..165becb4d02--- /dev/null+++ b/Tests/LibWeb/Text/input/Editing/writing-suggestion-not-drawn.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ p::first-letter { color: red; }+</style>+<p><input id="field"><input id="next"></p>+<script>+ promiseTest(async () => {+ // A ::first-letter ancestor may split the text, so no suggestion is drawn there, and none may be accepted.+ const field = document.getElementById("field");+ internals.sendText(field, "predi");+ await internals.writingSuggestionSettled();+ println(`suggestion: ${internals.writingSuggestion()}`);+ internals.sendText(field, "");+ println(`value: ${field.value}`);+ println(`focus: ${document.activeElement.id}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html b/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.htmlnew file mode 100644index 00000000000..636b5664d46--- /dev/null+++ b/Tests/LibWeb/Text/input/Editing/writing-suggestion-tab.html@@ -0,0 +1,58 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<input id="before">+<textarea id="area"></textarea>+<input id="after">+<script>+ promiseTest(async () => {+ const TAB = "";+ const area = document.getElementById("area");+ let log = [];+ for (const type of ["beforeinput", "input"])+ area.addEventListener(type, e => log.push(`${type} ${e.inputType} ${e.data}`));++ const reset = async () => {+ area.value = "";+ internals.sendText(area, "predi");+ await internals.writingSuggestionSettled();+ log = [];+ };+ const show = async label => {+ await internals.writingSuggestionSettled();+ println(`${label}:`);+ println(` value: ${area.value}`);+ println(` focus: ${document.activeElement.id}`);+ println(` events: ${log.join(", ") || "(none)"}`);+ println(` suggestion: ${internals.writingSuggestion()}`);+ };++ await reset();+ internals.sendText(area, TAB);+ await show("Tab with a suggestion");++ await reset();+ const cancelTab = e => {+ if (e.key === "Tab")+ e.preventDefault();+ };+ area.addEventListener("keydown", cancelTab);+ internals.sendText(area, TAB);+ await show("Tab canceled by the page");+ area.removeEventListener("keydown", cancelTab);++ area.value = "";+ log = [];+ internals.sendText(area, TAB);+ await show("Tab without a suggestion");++ await reset();+ internals.sendText(area, TAB, internals.MOD_SHIFT);+ await show("Shift+Tab with a suggestion");++ await reset();+ const cancelInsertion = e => e.preventDefault();+ area.addEventListener("beforeinput", cancelInsertion);+ internals.sendText(area, TAB);+ await show("Insertion canceled by the page");+ });+</script>