-
Notifications
You must be signed in to change notification settings - Fork 776
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(only-listitem): add message about invalid role on li elements (#1954
- Loading branch information
Showing
3 changed files
with
56 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,58 @@ | ||
const { dom } = axe.commons; | ||
const getIsListItemRole = (role, tagName) => { | ||
return role === 'listitem' || (tagName === 'LI' && !role); | ||
}; | ||
const { dom, aria } = axe.commons; | ||
|
||
const getHasListItem = (hasListItem, tagName, isListItemRole) => { | ||
return hasListItem || (tagName === 'LI' && isListItemRole) || isListItemRole; | ||
}; | ||
let hasNonEmptyTextNode = false; | ||
let atLeastOneListitem = false; | ||
let isEmpty = true; | ||
let badNodes = []; | ||
let badRoleNodes = []; | ||
let badRoles = []; | ||
|
||
let base = { | ||
badNodes: [], | ||
isEmpty: true, | ||
hasNonEmptyTextNode: false, | ||
hasListItem: false, | ||
liItemsWithRole: 0 | ||
}; | ||
virtualNode.children.forEach(vNode => { | ||
const { actualNode } = vNode; | ||
|
||
let out = virtualNode.children.reduce((out, { actualNode }) => { | ||
const tagName = actualNode.nodeName.toUpperCase(); | ||
if (actualNode.nodeType === 3 && actualNode.nodeValue.trim() !== '') { | ||
hasNonEmptyTextNode = true; | ||
return; | ||
} | ||
|
||
if (actualNode.nodeType === 1 && dom.isVisible(actualNode, true, false)) { | ||
const role = (actualNode.getAttribute('role') || '').toLowerCase(); | ||
const isListItemRole = getIsListItemRole(role, tagName); | ||
if (actualNode.nodeType !== 1 || !dom.isVisible(actualNode, true, false)) { | ||
return; | ||
} | ||
|
||
out.hasListItem = getHasListItem(out.hasListItem, tagName, isListItemRole); | ||
isEmpty = false; | ||
const isLi = actualNode.nodeName.toUpperCase() === 'LI'; | ||
const role = aria.getRole(vNode); | ||
const isListItemRole = role === 'listitem'; | ||
|
||
if (isListItemRole) { | ||
out.isEmpty = false; | ||
} | ||
if (tagName === 'LI' && !isListItemRole) { | ||
out.liItemsWithRole++; | ||
} | ||
if (tagName !== 'LI' && !isListItemRole) { | ||
out.badNodes.push(actualNode); | ||
} | ||
if (!isLi && !isListItemRole) { | ||
badNodes.push(actualNode); | ||
} | ||
if (actualNode.nodeType === 3) { | ||
if (actualNode.nodeValue.trim() !== '') { | ||
out.hasNonEmptyTextNode = true; | ||
|
||
if (isLi && !isListItemRole) { | ||
badRoleNodes.push(actualNode); | ||
|
||
if (!badRoles.includes(role)) { | ||
badRoles.push(role); | ||
} | ||
} | ||
|
||
return out; | ||
}, base); | ||
|
||
const virtualNodeChildrenOfTypeLi = virtualNode.children.filter( | ||
({ actualNode }) => { | ||
return actualNode.nodeName.toUpperCase() === 'LI'; | ||
if (isListItemRole) { | ||
atLeastOneListitem = true; | ||
} | ||
); | ||
}); | ||
|
||
const allLiItemsHaveRole = | ||
out.liItemsWithRole > 0 && | ||
virtualNodeChildrenOfTypeLi.length === out.liItemsWithRole; | ||
if (hasNonEmptyTextNode || badNodes.length) { | ||
this.relatedNodes(badNodes); | ||
return true; | ||
} | ||
|
||
if (out.badNodes.length) { | ||
this.relatedNodes(out.badNodes); | ||
if (isEmpty || atLeastOneListitem) { | ||
return false; | ||
} | ||
|
||
const isInvalidListItem = !( | ||
out.hasListItem || | ||
(out.isEmpty && !allLiItemsHaveRole) | ||
); | ||
return isInvalidListItem || !!out.badNodes.length || out.hasNonEmptyTextNode; | ||
this.relatedNodes(badRoleNodes); | ||
this.data({ | ||
messageKey: 'roleNotValid', | ||
roles: badRoles.join(', ') | ||
}); | ||
return true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters