diff --git a/lib/browser/api/menu-item.js b/lib/browser/api/menu-item.js index a9949aa195c1..98b8e9980e28 100644 --- a/lib/browser/api/menu-item.js +++ b/lib/browser/api/menu-item.js @@ -7,18 +7,11 @@ let nextCommandId = 0 const MenuItem = function (options) { const {Menu} = require('electron') - this.selector = options.selector - this.type = options.type - this.role = options.role - this.label = options.label - this.sublabel = options.sublabel - this.accelerator = options.accelerator - this.icon = options.icon - this.enabled = options.enabled - this.visible = options.visible - this.checked = options.checked + // Preserve extra fields specified by user + for (let key in options) { + if (!(key in this)) this[key] = options[key] + } - this.submenu = options.submenu if (this.submenu != null && this.submenu.constructor !== Menu) { this.submenu = Menu.buildFromTemplate(this.submenu) } @@ -70,10 +63,7 @@ MenuItem.prototype.getDefaultRoleAccelerator = function () { return roles.getDefaultAccelerator(this.role) } -MenuItem.prototype.overrideProperty = function (name, defaultValue) { - if (defaultValue == null) { - defaultValue = null - } +MenuItem.prototype.overrideProperty = function (name, defaultValue = null) { if (this[name] == null) { this[name] = defaultValue } diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index a68383cfd65c..167e9a744ec0 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -272,7 +272,7 @@ Menu.getApplicationMenu = function () { Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder Menu.buildFromTemplate = function (template) { - var insertIndex, item, j, k, key, len, len1, menu, menuItem, positionedTemplate + var insertIndex, item, j, k, len, len1, menu, menuItem, positionedTemplate if (!Array.isArray(template)) { throw new TypeError('Invalid template for Menu') } @@ -295,12 +295,6 @@ Menu.buildFromTemplate = function (template) { throw new TypeError('Invalid template for MenuItem') } menuItem = new MenuItem(item) - for (key in item) { - // Preserve extra fields specified by user - if (!menuItem.hasOwnProperty(key)) { - menuItem[key] = item[key] - } - } menu.append(menuItem) } return menu diff --git a/spec/api-menu-spec.js b/spec/api-menu-spec.js index 1528d563901e..bf5c9871cd3a 100644 --- a/spec/api-menu-spec.js +++ b/spec/api-menu-spec.js @@ -430,3 +430,25 @@ describe('menu module', function () { }) }) }) + +describe('MenuItem with custom properties in constructor', function () { + it('preserves the custom properties', function () { + var template = [{ + label: 'menu 1', + customProp: 'foo', + submenu: [] + }] + + var menu = Menu.buildFromTemplate(template) + menu.items[0].submenu.append(new MenuItem({ + label: 'item 1', + customProp: 'bar', + overrideProperty: 'oops not allowed' + })) + + assert.equal(menu.items[0].customProp, 'foo') + assert.equal(menu.items[0].submenu.items[0].label, 'item 1') + assert.equal(menu.items[0].submenu.items[0].customProp, 'bar') + assert.equal(typeof menu.items[0].submenu.items[0].overrideProperty, 'function') + }) +})