-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(list/definition-list): only allow required owned roles (#3707)
* fix(list/definition-list): Clarify remediation messages * Add docs * Tweaks * Remove log
- Loading branch information
1 parent
7e2f0af
commit a920d35
Showing
12 changed files
with
462 additions
and
487 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { isVisibleToScreenReaders } from '../../commons/dom'; | ||
import { getExplicitRole } from '../../commons/aria'; | ||
|
||
export default function invalidChildrenEvaluate( | ||
node, | ||
options = {}, | ||
virtualNode | ||
) { | ||
const relatedNodes = []; | ||
const issues = []; | ||
if (!virtualNode.children) { | ||
return undefined; | ||
} | ||
|
||
const vChildren = mapWithNested(virtualNode.children); | ||
while (vChildren.length) { | ||
const { vChild, nested } = vChildren.shift(); | ||
if (options.divGroups && !nested && isDivGroup(vChild)) { | ||
if (!vChild.children) { | ||
return undefined; | ||
} | ||
const vGrandChildren = mapWithNested(vChild.children, true); | ||
vChildren.push(...vGrandChildren); | ||
continue; | ||
} | ||
|
||
const issue = getInvalidSelector(vChild, nested, options); | ||
if (!issue) { | ||
continue; | ||
} | ||
if (!issues.includes(issue)) { | ||
issues.push(issue); | ||
} | ||
if (vChild?.actualNode?.nodeType === 1) { | ||
relatedNodes.push(vChild.actualNode); | ||
} | ||
} | ||
if (issues.length === 0) { | ||
return false; | ||
} | ||
|
||
this.data({ values: issues.join(', ') }); | ||
this.relatedNodes(relatedNodes); | ||
return true; | ||
} | ||
|
||
function getInvalidSelector( | ||
vChild, | ||
nested, | ||
{ validRoles = [], validNodeNames = [] } | ||
) { | ||
const { nodeName, nodeType, nodeValue } = vChild.props; | ||
const selector = nested ? 'div > ' : ''; | ||
if (nodeType === 3 && nodeValue.trim() !== '') { | ||
return selector + `#text`; | ||
} | ||
if (nodeType !== 1 || !isVisibleToScreenReaders(vChild)) { | ||
return false; | ||
} | ||
|
||
const role = getExplicitRole(vChild); | ||
if (role) { | ||
return validRoles.includes(role) ? false : selector + `[role=${role}]`; | ||
} else { | ||
return validNodeNames.includes(nodeName) ? false : selector + nodeName; | ||
} | ||
} | ||
|
||
function isDivGroup(vNode) { | ||
return vNode.props.nodeName === 'div' && getExplicitRole(vNode) === null; | ||
} | ||
|
||
function mapWithNested(vNodes, nested = false) { | ||
return vNodes.map(vChild => ({ vChild, nested })); | ||
} |
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,11 +1,16 @@ | ||
{ | ||
"id": "only-dlitems", | ||
"evaluate": "only-dlitems-evaluate", | ||
"evaluate": "invalid-children-evaluate", | ||
"options": { | ||
"validRoles": ["definition", "term", "listitem"], | ||
"validNodeNames": ["dt", "dd"], | ||
"divGroups": true | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "List element only has direct children that are allowed inside <dt> or <dd> elements", | ||
"fail": "List element has direct children that are not allowed inside <dt> or <dd> elements" | ||
"pass": "dl element only has direct children that are allowed inside; <dt>, <dd>, or <div> elements", | ||
"fail": "dl element has direct children that are not allowed: ${data.values}" | ||
} | ||
} | ||
} |
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,14 +1,15 @@ | ||
{ | ||
"id": "only-listitems", | ||
"evaluate": "only-listitems-evaluate", | ||
"evaluate": "invalid-children-evaluate", | ||
"options": { | ||
"validRoles": ["listitem"], | ||
"validNodeNames": ["li"] | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "List element only has direct children that are allowed inside <li> elements", | ||
"fail": { | ||
"default": "List element has direct children that are not allowed inside <li> elements", | ||
"roleNotValid": "List element has direct children with a role that is not allowed: ${data.roles}" | ||
} | ||
"fail": "List element has direct children that are not allowed: ${data.values}" | ||
} | ||
} | ||
} |
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,5 +1,5 @@ | ||
function noRoleMatches(node) { | ||
return !node.getAttribute('role'); | ||
function noRoleMatches(node, vNode) { | ||
return !vNode.attr('role'); | ||
} | ||
|
||
export default noRoleMatches; |
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
Oops, something went wrong.