fix: prevent Menu.buildFromTemplate with empty array (#23308)

Prevent issues with menu creation and subsequent pane focus from menu bar by preventing menus from being created from an empty array. I can't conceive a valid use case for this, since if one wants to remove a menu they should be be passing null to win.setMenu() or calling win.removeMenu(). This issue is also specific to top-level menus, and not submenus, so the new check and exception is scoped to top-level menus.
This commit is contained in:
shelley vohr 2020-04-30 08:29:02 -07:00 committed by GitHub
parent 448017b9ee
commit f50f725a9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View file

@ -16,7 +16,7 @@ const MenuItem = function (options) {
}
this.submenu = this.submenu || roles.getDefaultSubmenu(this.role);
if (this.submenu != null && this.submenu.constructor !== Menu) {
this.submenu = Menu.buildFromTemplate(this.submenu);
this.submenu = Menu.buildFromTemplate(this.submenu, true);
}
if (this.type == null && this.submenu != null) {
this.type = 'submenu';

View file

@ -173,13 +173,17 @@ Menu.setApplicationMenu = function (menu) {
}
};
Menu.buildFromTemplate = function (template) {
Menu.buildFromTemplate = function (template, isSubmenu = false) {
if (!Array.isArray(template)) {
throw new TypeError('Invalid template for Menu: Menu template must be an array');
} else if (!isSubmenu && template.length === 0) {
throw new TypeError('Invalid template for Menu: Menu template must be an non-empty array');
}
if (!areValidTemplateItems(template)) {
throw new TypeError('Invalid template for MenuItem: must have at least one of label, role or type');
}
const filtered = removeExtraSeparators(template);
const sorted = sortTemplate(filtered);

View file

@ -86,12 +86,25 @@ describe('Menu module', function () {
Menu.buildFromTemplate([{ visible: true }]);
}).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/);
});
it('does throw exception for undefined', () => {
expect(() => {
Menu.buildFromTemplate([undefined as any]);
}).to.throw(/Invalid template for MenuItem: must have at least one of label, role or type/);
});
it('throws when an empty array is passed as a template', () => {
expect(() => {
Menu.buildFromTemplate([]);
}).to.throw(/Invalid template for Menu: Menu template must be an non-empty array/);
});
it('throws when an non-array is passed as a template', () => {
expect(() => {
Menu.buildFromTemplate('hello' as any);
}).to.throw(/Invalid template for Menu: Menu template must be an array/);
});
describe('Menu sorting and building', () => {
describe('sorts groups', () => {
it('does a simple sort', () => {