Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw error when inserting menu items out-of-range (backport: 5-0-x) #17435

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/browser/api/menu.js
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions spec/api-menu-spec.js
Expand Up @@ -688,6 +688,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' },
Expand Down