mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: throw error when inserting menu items out-of-range (#17461)
* fix: throw error when inserting menu items out-of-range * also check pos < 0
This commit is contained in:
@@ -109,6 +109,12 @@ Menu.prototype.insert = function (pos, item) {
|
||||
throw new TypeError('Invalid item')
|
||||
}
|
||||
|
||||
if (pos < 0) {
|
||||
throw new RangeError(`Position ${pos} cannot be less than 0`)
|
||||
} else if (pos > this.getItemCount()) {
|
||||
throw new RangeError(`Position ${pos} cannot be greater than the total MenuItem count`)
|
||||
}
|
||||
|
||||
// insert item depending on its type
|
||||
insertItemByType.call(this, item, pos)
|
||||
|
||||
|
||||
@@ -614,6 +614,24 @@ describe('Menu module', () => {
|
||||
})
|
||||
|
||||
describe('Menu.insert', () => {
|
||||
it('should throw when attempting to insert at out-of-range indices', () => {
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{ label: '1' },
|
||||
{ label: '2' },
|
||||
{ label: '3' }
|
||||
])
|
||||
|
||||
const item = new MenuItem({ label: 'badInsert' })
|
||||
|
||||
expect(() => {
|
||||
menu.insert(9999, item)
|
||||
}).to.throw(/Position 9999 cannot be greater than the total MenuItem count/)
|
||||
|
||||
expect(() => {
|
||||
menu.insert(-9999, item)
|
||||
}).to.throw(/Position -9999 cannot be less than 0/)
|
||||
})
|
||||
|
||||
it('should store item in @items by its index', () => {
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{ label: '1' },
|
||||
|
||||
Reference in New Issue
Block a user