Bug 1816074 - Prevent activating menubar without a selectable menu item. r=smaug

We might not have an active item if all items are disabled.

Add the null-check just to be safe too.

Differential Revision: https://phabricator.services.mozilla.com/D169638
This commit is contained in:
Emilio Cobos Álvarez 2023-02-14 16:01:50 +00:00
Родитель 1249da2f38
Коммит 62df734605
3 изменённых файлов: 33 добавлений и 5 удалений

Просмотреть файл

@ -109,7 +109,7 @@ void MenuBarListener::ToggleMenuActiveState(ByKeyboard aByKeyboard) {
if (aByKeyboard == ByKeyboard::Yes) {
menuBar->SetActiveByKeyboard();
}
menuBar->SetActive(true);
// This will activate the menubar if needed.
menuBar->SelectFirstItem();
}
}
@ -226,9 +226,10 @@ nsresult MenuBarListener::KeyPress(Event* aKeyEvent) {
if (mMenuBar && mMenuBar->IsActive()) {
# ifdef MOZ_WIDGET_GTK
RefPtr child = mMenuBar->GetActiveMenuChild();
// In GTK, this also opens the first menu.
child->OpenMenuPopup(false);
if (RefPtr child = mMenuBar->GetActiveMenuChild()) {
// In GTK, this also opens the first menu.
child->OpenMenuPopup(false);
}
# endif
aKeyEvent->StopPropagation();
aKeyEvent->PreventDefault();
@ -270,7 +271,7 @@ nsresult MenuBarListener::KeyPress(Event* aKeyEvent) {
RefPtr menuBar = mMenuBar;
menuBar->SetActiveByKeyboard();
menuBar->SetActive(true);
// This will activate the menubar as needed.
menuForKey->OpenMenuPopup(true);
// The opened menu will listen next keyup event.

Просмотреть файл

@ -52,6 +52,9 @@ function startPopupTests(tests) {
document.addEventListener("DOMMenuBarActive", eventOccurred);
document.addEventListener("DOMMenuBarInactive", eventOccurred);
// This is useful to explicitly finish a test that shouldn't trigger events.
document.addEventListener("TestDone", eventOccurred);
gPopupTests = tests;
gWindowUtils = SpecialPowers.getDOMWindowUtils(window);

Просмотреть файл

@ -930,6 +930,30 @@ var popupTests = [
},
},
{
testname: "Trying to activate menubar without activatable items shouldn't crash",
events: [ "TestDone menubar" ],
test() {
const items = document.querySelectorAll("menubar > menu");
let wasDisabled = {};
for (let item of items) {
wasDisabled[item] = item.disabled;
item.disabled = true;
}
synthesizeKey("KEY_F10");
setTimeout(function() {
synthesizeKey("KEY_F10");
for (let item of items) {
item.disabled = wasDisabled[item];
}
document.getElementById("menubar").dispatchEvent(new CustomEvent("TestDone", { bubbles: true }));
}, 0);
}
},
// bug 625151
{
testname: "Alt key state before deactivating the window shouldn't prevent " +