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

feat(list/definition-list): only allow required owned roles #3707

Merged
merged 4 commits into from
Oct 17, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/check-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [target-size](#target-size)
- [region](#region)
- [inline-style-property](#inline-style-property)
- [invalid-children](#invalid-children)

## How Checks Work

Expand Down Expand Up @@ -526,3 +527,13 @@ This evaluate method is used in the following checks. Default vary between check
| `normalValue` | The value to use when `normal` is set, defaults to `0` |

If `minValue` and `maxValue` are both undefined, the check returns `false` if the property is used with !important. If done along with `noImportant: true`, the check returns false if the property is set at all in the style attribute.

### invalid-children

This evaluation method is used in the `list` and `definition-list` rule to determine whether its child nodes are allowed.

| Option | Description |
| ---------------- | :---------------------------------------------------------------------------------- |
| `validNodeNames` | Nodes without role allowed as children |
| `validRoles` | Roles allowed on child elements |
| `divGroups` | Whether the child nodes can be grouped in a div without any role (false by default) |
75 changes: 75 additions & 0 deletions lib/checks/lists/invalid-children-evaluate.js
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);
Copy link
Contributor

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)

<ul>
  <li role="none" aria-label="hello"></li>
  <li role="none" tabindex="0">Hello</li>
</ul>

Copy link
Contributor Author

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?

<!-- This passes today, because it's an li and we then don't look at the role -->
<ul><li role="button">Button!</li></ul>

Copy link
Contributor

@straker straker Oct 17, 2022

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 one li

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 }));
}
11 changes: 8 additions & 3 deletions lib/checks/lists/only-dlitems.json
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}"
}
}
}
11 changes: 6 additions & 5 deletions lib/checks/lists/only-listitems.json
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}"
}
}
}
4 changes: 2 additions & 2 deletions lib/rules/no-role-matches.js
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;
9 changes: 3 additions & 6 deletions locales/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,12 @@
}
},
"only-dlitems": {
"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}"
},
"only-listitems": {
"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}"
},
"structured-dlitems": {
"pass": "When not empty, element has both <dt> and <dd> elements",
Expand Down
Loading