Skip to content

Commit

Permalink
Merge pull request brave#7147 from siemiatj/issue-7098-sanitize-templ…
Browse files Browse the repository at this point in the history
…ates

Make sanitizeTemplateItems recursively check submenus in templates
  • Loading branch information
bsclifton authored Feb 10, 2017
2 parents 46f4822 + eb94d07 commit 30243b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/common/lib/menuUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,19 @@ const isItemValid = (currentItem, previousItem) => {
return false
}
}
return currentItem && (typeof currentItem.l10nLabelId === 'string' || typeof currentItem.label === 'string' || currentItem.type === 'separator')

return currentItem && (typeof currentItem.l10nLabelId === 'string' || typeof currentItem.label === 'string' ||
currentItem.type === 'separator' || typeof currentItem.slice === 'function')
}

/**
* Remove invalid entries from a menu template:
* - null or falsey entries
* - extra menu separators
* - entries which don't have a label (or l10nLabelId) if their type is not 'separator'
*/
module.exports.sanitizeTemplateItems = (template) => {
const sanitizeTemplateItems = (template) => {
const reduced = template.reduce((result, currentValue, currentIndex, array) => {
const previousItem = result.length > 0
? result[result.length - 1]
: undefined
if (currentValue && currentValue.submenu) {
currentValue.submenu = sanitizeTemplateItems(currentValue.submenu)
}
if (isItemValid(currentValue, previousItem)) {
result.push(currentValue)
}
Expand All @@ -185,3 +184,11 @@ module.exports.sanitizeTemplateItems = (template) => {

return result
}

/**
* Remove invalid entries from a menu template:
* - null or falsey entries
* - extra menu separators
* - entries which don't have a label (or l10nLabelId) if their type is not 'separator'
*/
module.exports.sanitizeTemplateItems = sanitizeTemplateItems
8 changes: 8 additions & 0 deletions test/unit/app/common/lib/menuUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ describe('menuUtil tests', function () {
const expectedResult = [{l10nLabelId: 'lol1'}]
assert.deepEqual(result, expectedResult)
})
it('checks submenus recursively', function () {
const template = [separator, {test: 'test'}, {label: 'lol'},
{ label: 'submenu', submenu: [separator, {label: 'foo'}] }]
const result = menuUtil.sanitizeTemplateItems(template)
const expectedResult = [{label: 'lol'}, {label: 'submenu', submenu: [{label: 'foo'}]}]

assert.deepEqual(result, expectedResult)
})
it('removes items which are missing label or type', function () {
const template = [{}, {test: 'test'}, {label: 'lol'}]
const result = menuUtil.sanitizeTemplateItems(template)
Expand Down

0 comments on commit 30243b5

Please sign in to comment.