-
Notifications
You must be signed in to change notification settings - Fork 784
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
feat(list/definition-list): only allow required owned roles #3707
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should we use
getRole
instead to account for role conflict. Something like this would fail even though each is treated as a listitem (this currently does not fail today)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think people should rely on fallback roles to pass. We're failing this after all. The one thing I do think is worth considering is to allow role=none on empty elements. We don't allow that currently, so I think that's a separate PR.
One thing I'd like your input on is in how this rule is becoming stricter. Something like the following was passing before. I don't think it should have been, but I'm not sure if we need to more explicitly call it out in the PR title, or even create a separate PR so its separated in the changelog. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think changing the pr title to something along the lines of
feat(list/definition-list): only allow required owned roles
would be sufficient for that. That would indicate a new failing case, and the clarification of the remediation message would then be a by-product that doesn't need to be called out. This should also be able to cover the now stricter syntax of other roles not being allowed when there was at least oneli