mirror of
https://github.com/electron/electron.git
synced 2026-01-28 00:38:35 -05:00
fix: Stricter Testing For Menu Items (#13992)
This PR includes stricter testing for empty objects so that false context menus are not created along with the tests to ensure future compatibility.
This commit is contained in:
@@ -156,25 +156,28 @@ Menu.setApplicationMenu = function (menu) {
|
||||
|
||||
Menu.buildFromTemplate = function (template) {
|
||||
if (!Array.isArray(template)) {
|
||||
throw new TypeError('Invalid template for Menu')
|
||||
throw new TypeError('Invalid template for Menu: Menu template must be an array')
|
||||
}
|
||||
|
||||
const menu = new Menu()
|
||||
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)
|
||||
|
||||
sorted.forEach((item) => {
|
||||
if (typeof item !== 'object') {
|
||||
throw new TypeError('Invalid template for MenuItem')
|
||||
}
|
||||
menu.append(new MenuItem(item))
|
||||
})
|
||||
sorted.forEach((item) => menu.append(new MenuItem(item)))
|
||||
|
||||
return menu
|
||||
}
|
||||
|
||||
/* Helper Functions */
|
||||
|
||||
// validate the template against having the wrong attribute
|
||||
function areValidTemplateItems (template) {
|
||||
return template.every(item =>
|
||||
item != null && typeof item === 'object' && (item.hasOwnProperty('label') || item.hasOwnProperty('role') || item.type === 'separator'))
|
||||
}
|
||||
|
||||
function sortTemplate (template) {
|
||||
const sorted = sortMenuItems(template)
|
||||
for (let id in sorted) {
|
||||
|
||||
Reference in New Issue
Block a user