Bug 1086524 - Focus window on Esc in address bar r=dao,urlbar-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D218893
This commit is contained in:
Collin Richards
2024-10-16 11:47:11 +00:00
parent 03e5396a93
commit 9f857b20cb
9 changed files with 133 additions and 11 deletions

View File

@@ -34,12 +34,6 @@ async function runTests() {
newWin.focus(); newWin.focus();
await focused; await focused;
// Ensure the URL bar is ready for a new URL to be typed.
// Sometimes, when this test runs, the existing text isn't selected when the
// URL bar is focused. Pressing escape twice ensures that the popup is
// closed and that the existing text is selected.
EventUtils.synthesizeKey("KEY_Escape", {}, newWin);
EventUtils.synthesizeKey("KEY_Escape", {}, newWin);
let caretMoved = waitForEvent( let caretMoved = waitForEvent(
EVENT_TEXT_CARET_MOVED, EVENT_TEXT_CARET_MOVED,
event => event.accessible.role == ROLE_ENTRY event => event.accessible.role == ROLE_ENTRY

View File

@@ -410,6 +410,10 @@ pref("browser.urlbar.speculativeConnect.enabled", true);
// search for bookmarklets typing "javascript: " followed by the actual query. // search for bookmarklets typing "javascript: " followed by the actual query.
pref("browser.urlbar.filter.javascript", true); pref("browser.urlbar.filter.javascript", true);
// Focus the content document when pressing the Escape key, if there's no
// remaining typed history.
pref("browser.urlbar.focusContentDocumentOnEsc", true);
// Enable a certain level of urlbar logging to the Browser Console. See // Enable a certain level of urlbar logging to the Browser Console. See
// ConsoleInstance.webidl. // ConsoleInstance.webidl.
pref("browser.urlbar.loglevel", "Error"); pref("browser.urlbar.loglevel", "Error");

View File

@@ -276,6 +276,7 @@ export class UrlbarController {
* will be deferred by the event bufferer, but preventDefault() and friends * will be deferred by the event bufferer, but preventDefault() and friends
* should still happen synchronously. * should still happen synchronously.
*/ */
// eslint-disable-next-line complexity
handleKeyNavigation(event, executeAction = true) { handleKeyNavigation(event, executeAction = true) {
const isMac = AppConstants.platform == "macosx"; const isMac = AppConstants.platform == "macosx";
// Handle readline/emacs-style navigation bindings on Mac. // Handle readline/emacs-style navigation bindings on Mac.
@@ -321,6 +322,12 @@ export class UrlbarController {
if (executeAction) { if (executeAction) {
if (this.view.isOpen) { if (this.view.isOpen) {
this.view.close(); this.view.close();
} else if (lazy.UrlbarPrefs.get("focusContentDocumentOnEsc")) {
if (this.browserWindow.gBrowser.userTypedValue) {
this.input.handleRevert(true);
} else {
this.browserWindow.gBrowser.selectedBrowser.focus();
}
} else { } else {
this.input.handleRevert({ this.input.handleRevert({
escapeSearchMode: true, escapeSearchMode: true,

View File

@@ -146,6 +146,10 @@ const PREF_URLBAR_DEFAULTS = new Map([
// When true, `javascript:` URLs are not included in search results. // When true, `javascript:` URLs are not included in search results.
["filter.javascript", true], ["filter.javascript", true],
// Focus the content document when pressing the Escape key, if there's no
// remaining typed history.
["focusContentDocumentOnEsc", true],
// Applies URL highlighting and other styling to the text in the urlbar input. // Applies URL highlighting and other styling to the text in the urlbar input.
["formatting.enabled", true], ["formatting.enabled", true],

View File

@@ -130,6 +130,10 @@ browser.urlbar.filter.javascript (boolean, default: true)
When true, `javascript:` URLs are not included in search results for safety When true, `javascript:` URLs are not included in search results for safety
reasons. reasons.
browser.urlbar.focusContentDocumentOnEsc (boolean, default: true)
Focus the content document when pressing the Escape key, if there's no
remaining typed history.
browser.urlbar.formatting.enabled (boolean, default: true) browser.urlbar.formatting.enabled (boolean, default: true)
Applies URL highlighting and other styling to the text in the urlbar input Applies URL highlighting and other styling to the text in the urlbar input
field. This should usually be enabled for security reasons. field. This should usually be enabled for security reasons.

View File

@@ -246,6 +246,8 @@ support-files = [
["browser_enterAfterMouseOver.js"] ["browser_enterAfterMouseOver.js"]
["browser_focusContentDocumentEsc.js"]
["browser_focusedCmdK.js"] ["browser_focusedCmdK.js"]
["browser_groupLabels.js"] ["browser_groupLabels.js"]

View File

@@ -21,8 +21,8 @@ add_setup(async function () {
function synthesizeRevert() { function synthesizeRevert() {
gURLBar.focus(); gURLBar.focus();
info("Double escape and revert Urlbar."); info("Escape to revert Urlbar.");
EventUtils.synthesizeKey("KEY_Escape", { repeat: 2 }); EventUtils.synthesizeKey("KEY_Escape");
} }
// Users should be able to revert the URL bar // Users should be able to revert the URL bar

View File

@@ -0,0 +1,110 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function () {
// Test that pressing Esc will focus the document when no text was typed
let focusBrowserPromise = BrowserTestUtils.waitForEvent(
gBrowser.selectedBrowser,
"focus"
);
gURLBar.focus();
EventUtils.synthesizeKey("KEY_Escape");
await focusBrowserPromise;
Assert.equal(
document.activeElement,
gBrowser.selectedBrowser,
"Content document should be focused"
);
});
add_task(async function () {
// Test that repeatedly pressing Esc after typing in the url bar will
// 1. close results panel
// 2. reset / undo previously typed text
// 3. focus content document
//
await UrlbarTestUtils.promisePopupOpen(window, () =>
UrlbarTestUtils.inputIntoURLBar(window, "hello")
);
is(gURLBar.value, "hello", "URL bar value should match after sending a key");
// assert results panel is open
Assert.equal(
UrlbarTestUtils.isPopupOpen(window),
true,
"Popup should be open"
);
// 1. first escape press should close panel
EventUtils.synthesizeKey("KEY_Escape");
// assert results panel is closed
Assert.equal(
UrlbarTestUtils.isPopupOpen(window),
false,
"Popup shouldn't be open"
);
// assert urlbar is still focused
Assert.equal(
document.activeElement,
gURLBar.inputField,
"URL Bar should be focused"
);
// 2. second escape press should undo typed text
EventUtils.synthesizeKey("KEY_Escape");
is(gURLBar.value, "", "URL bar value should be reset after escape");
Assert.equal(
document.activeElement,
gURLBar.inputField,
"URL Bar should still be focused"
);
let focusBrowserPromise = BrowserTestUtils.waitForEvent(
gBrowser.selectedBrowser,
"focus"
);
// 3. third escape press should focus content document
EventUtils.synthesizeKey("KEY_Escape");
await focusBrowserPromise;
Assert.equal(
document.activeElement,
gBrowser.selectedBrowser,
"Content document should be focused"
);
});
add_task(async function () {
// Test that pressing Esc will not focus the document when the preference
// is unset
Preferences.set("browser.urlbar.focusContentDocumentOnEsc", false);
let focusUrlPromise = BrowserTestUtils.waitForEvent(
gURLBar.inputField,
"focus"
);
gURLBar.focus();
await focusUrlPromise;
EventUtils.synthesizeKey("KEY_Escape");
// assert results panel is closed
Assert.equal(
UrlbarTestUtils.isPopupOpen(window),
false,
"Popup shouldn't be open"
);
Assert.equal(
document.activeElement,
gURLBar.inputField,
"URL Bar should be focused"
);
// cleanup
Preferences.set("browser.urlbar.focusContentDocumentOnEsc", true);
});

View File

@@ -212,9 +212,6 @@ async function test_composition(keepPanelOpenDuringImeComposition) {
EventUtils.synthesizeKey("KEY_Backspace", {}); EventUtils.synthesizeKey("KEY_Backspace", {});
EventUtils.synthesizeKey("KEY_Backspace", {}); EventUtils.synthesizeKey("KEY_Backspace", {});
Assert.equal(gURLBar.value, "", "Check urlbar value"); Assert.equal(gURLBar.value, "", "Check urlbar value");
await UrlbarTestUtils.promisePopupClose(window, () => {
EventUtils.synthesizeKey("KEY_Escape", {});
});
info("With autofill, compositionstart shouldn't open the popup"); info("With autofill, compositionstart shouldn't open the popup");
Assert.ok(!UrlbarTestUtils.isPopupOpen(window), "Popup should be closed"); Assert.ok(!UrlbarTestUtils.isPopupOpen(window), "Popup should be closed");