Truly independent web browser
LibWeb: Focus right-clicked controls before opening the context menu
Timothy Flynn4c381bf8c87b
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex 310515d099b..a5732dd3dc8 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -504,6 +504,22 @@ EventResult EventHandler::handle_mousedown(CSSPixelPoint visual_viewport_positiois_context_menu_trigger |= button == UIEvents::MouseButton::Primary && (modifiers & UIEvents::KeyModifier::Mod_Ctrl) != 0;#endif+ if (is_context_menu_trigger && dispatch_result == PointerEventDispatchResult::RunDefaultActions && !pressed_on_chrome) {+ // Focus the clicked control before dispatching contextmenu so the menu acts on that control.+ if (m_navigable->active_document() != document)+ return EventResult::Accepted;+ document->update_layout(DOM::UpdateLayoutReason::EventHandlerHandleMouseDown);+ if (!has_committed_root_box())+ return EventResult::Accepted;++ if (auto focus_candidate = focus_candidate_for_position(read, visual_viewport_position))+ HTML::run_focusing_steps(focus_candidate, nullptr, HTML::FocusTrigger::Click);++ // NB: Focusing may have run script that navigated away from the document.+ if (m_navigable->active_document() != document)+ return EventResult::Accepted;+ }+// FIXME: The spec allows this to be fired following mousedown or mouseup. The native behavior on Windows is to// do so in mouseup, so we should make this configurable by the UI.// NB: The contextmenu event is dispatched even if the page cancelled the pointerdown or mousedown event,@@ -4430,23 +4446,6 @@ Optional<Editing::ContextMenuMisspelling> EventHandler::select_context_menu_missif (!marker.has_value() || !range_contains_viewport_position(DOM::Range::create(*text, marker->start, *text, marker->end), coordinates.viewport_position))return {};- // A misspelling in a field the user has left gets the field focused first, so the guess the user picks can replace- // it there.- if (!document.active_input_events_target(text)) {- if (auto focus_candidate = focus_candidate_for_position(read, coordinates.visual_viewport_position))- HTML::run_focusing_steps(focus_candidate, nullptr, HTML::FocusTrigger::Click);- else if (auto editing_host = text->editing_host())- HTML::run_focusing_steps(editing_host, nullptr, HTML::FocusTrigger::Click);-- // NB: Focusing may have invalidated layout.- document.update_layout(DOM::UpdateLayoutReason::EventHandlerShowContextMenu);-- // A focus listener runs inside the focusing steps, and an edit it makes moves or drops the text's markers — so- // the misspelling the click landed on counts only if its marker is still where it was.- if (!document.spelling_markers().markers_for(*text).contains_slow(*marker))- return {};- }-// A field that didn't take focus has nowhere for a guess to go.auto* target = document.active_input_events_target(text);if (!target)diff --git a/Tests/LibWeb/Text/expected/Editing/spellcheck-context-menu-beside-misspelling.txt b/Tests/LibWeb/Text/expected/Editing/spellcheck-context-menu-beside-misspelling.txtindex cc69cf2a062..26a2201ddf4 100644--- a/Tests/LibWeb/Text/expected/Editing/spellcheck-context-menu-beside-misspelling.txt+++ b/Tests/LibWeb/Text/expected/Editing/spellcheck-context-menu-beside-misspelling.txt@@ -1,3 +1,3 @@flagged: 4-8-focused: other+focused: areaselection: 0-0diff --git a/Tests/LibWeb/Text/expected/UIEvents/context-menu-focus.txt b/Tests/LibWeb/Text/expected/UIEvents/context-menu-focus.txtnew file mode 100644index 00000000000..5f25a779e1c--- /dev/null+++ b/Tests/LibWeb/Text/expected/UIEvents/context-menu-focus.txt@@ -0,0 +1,16 @@+contextmenu: focused=input+right-click #input: focused=input, menu=true+contextmenu: focused=textarea+right-click #textarea: focused=textarea, menu=true+contextmenu: focused=editable+right-click #editable: focused=editable, menu=true+right-click #disabled: focused=other, menu=false+mousedown: focused=other+focus: focused=input+contextmenu: focused=input+contextmenu: focused=input+canceled contextmenu: focused=input, menu=false+contextmenu: focused=other+canceled mousedown: focused=other, menu=true+contextmenu: focused=other+canceled pointerdown: focused=other, menu=truediff --git a/Tests/LibWeb/Text/input/Editing/spellcheck-context-menu-focus-listener-edits.html b/Tests/LibWeb/Text/input/Editing/spellcheck-context-menu-focus-listener-edits.htmlindex 3967a5eac78..929672b657a 100644--- a/Tests/LibWeb/Text/input/Editing/spellcheck-context-menu-focus-listener-edits.html+++ b/Tests/LibWeb/Text/input/Editing/spellcheck-context-menu-focus-listener-edits.html@@ -47,7 +47,7 @@// A focus listener that inserts text before the misspelling moves it away from the offsets the click found.clickAt(...pointIn(other, 0));editable.addEventListener("focus", () => { editable.firstChild.insertData(0, "zz"); }, { once: true });- clickAt(...pointIn(editable, 6), 2);+ clickAt(...pointIn(editable, 5), 2);println(`editable flagged after the insert: ${ranges(editable.firstChild)}`);println(`menu after a focus listener moved the misspelling: ${await internals.activateSpellingMenuItem("none")}`);println(`text: ${editable.textContent}`);diff --git a/Tests/LibWeb/Text/input/UIEvents/context-menu-focus.html b/Tests/LibWeb/Text/input/UIEvents/context-menu-focus.htmlnew file mode 100644index 00000000000..52a8ef5539e--- /dev/null+++ b/Tests/LibWeb/Text/input/UIEvents/context-menu-focus.html@@ -0,0 +1,55 @@+<!doctype html>+<script src="../include.js"></script>+<style>+ input,+ textarea,+ [contenteditable] {+ display: block;+ width: 200px;+ height: 30px;+ }+</style>+<input id="other" />+<input id="input" />+<textarea id="textarea"></textarea>+<div id="editable" contenteditable><span id="child">editable text</span></div>+<input id="disabled" disabled />+<script>+ test(() => {+ const other = document.getElementById("other");+ const input = document.getElementById("input");+ const rightClick = element => {+ const rect = element.getBoundingClientRect();+ const x = rect.left + rect.width - 10;+ const y = rect.top + rect.height / 2;+ internals.mouseDown(x, y, 1, internals.BUTTON_RIGHT);+ internals.mouseUp(x, y, internals.BUTTON_RIGHT);+ };+ document.addEventListener("contextmenu", () => println(`contextmenu: focused=${document.activeElement.id}`));++ for (const id of ["input", "textarea", "editable", "disabled"]) {+ other.focus();+ rightClick(document.getElementById(id));+ println(+ `right-click #${id}: focused=${document.activeElement.id}, menu=${!!internals.takeContextMenuRequest()}`+ );+ }++ other.focus();+ input.addEventListener("mousedown", () => println(`mousedown: focused=${document.activeElement.id}`), {+ once: true,+ });+ input.addEventListener("focus", () => println(`focus: focused=${document.activeElement.id}`), { once: true });+ rightClick(input);+ internals.takeContextMenuRequest();++ for (const type of ["contextmenu", "mousedown", "pointerdown"]) {+ other.focus();+ input.addEventListener(type, event => event.preventDefault(), { once: true });+ rightClick(input);+ println(+ `canceled ${type}: focused=${document.activeElement.id}, menu=${!!internals.takeContextMenuRequest()}`+ );+ }+ });+</script>