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(new-rule): Add WCAG 2.2 target-size rule #3616

Merged
merged 30 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7d9b70c
feat(new-rule): Add WCAG 2.2 target-size rule
WilcoFiers Aug 19, 2022
54817d7
chore: handle rounding errors
WilcoFiers Feb 3, 2022
80299b2
Fix has-visual-overlap test
WilcoFiers Aug 31, 2022
302db49
Fix failing tests?
WilcoFiers Aug 31, 2022
0172fbe
Rename target-size.json file
WilcoFiers Aug 31, 2022
5b1c1a3
Disable target-spacing rule on APG
WilcoFiers Aug 31, 2022
a0eaaf8
Improve target-size messaging
WilcoFiers Aug 31, 2022
c4029a5
Update is-in-text-block
WilcoFiers Sep 2, 2022
55b037c
Add target-size matches method
WilcoFiers Sep 2, 2022
81feac6
Add integration tests for target-size
WilcoFiers Sep 2, 2022
eb27049
Document target-size check options
WilcoFiers Sep 2, 2022
217ac64
put create-grid into its own file
WilcoFiers Sep 2, 2022
10b8972
Make all links focusable
WilcoFiers Sep 2, 2022
297524f
Simplify target-size full rest
WilcoFiers Sep 2, 2022
a25d7a0
Try to fix IE issue
WilcoFiers Sep 2, 2022
f6b97df
More cleanup and testing
WilcoFiers Sep 2, 2022
8ab55c7
Solve oddities with IE
WilcoFiers Sep 2, 2022
496f227
Rename minSpacing option to minSize
WilcoFiers Sep 15, 2022
8ea60ec
Merge branch 'develop' into target-size-v2
WilcoFiers Sep 15, 2022
a5a0512
Remove IE11 xit
WilcoFiers Sep 15, 2022
368d612
Fix off screen grid issues
WilcoFiers Sep 16, 2022
b4462e5
Simplify using role types
WilcoFiers Sep 16, 2022
9e7e9b6
cleanup
WilcoFiers Sep 16, 2022
4fefa21
Handle fully obscuring elements
WilcoFiers Sep 20, 2022
7885fe8
fix failing test
WilcoFiers Sep 20, 2022
840cf4e
Resolve last comments
WilcoFiers Sep 20, 2022
1e204c1
fix getRoleType throwing in NodeJS context
WilcoFiers Sep 20, 2022
b2f12e8
Editorial test fix
WilcoFiers Sep 20, 2022
17ce8ea
Address review
WilcoFiers Sep 21, 2022
d72b683
Merge branch 'develop' into target-size-v2
WilcoFiers Sep 21, 2022
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
14 changes: 14 additions & 0 deletions doc/check-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
- [p-as-heading](#p-as-heading)
- [avoid-inline-spacing](#avoid-inline-spacing)
- [scope-value](#scope-value)
- [target-offset](#target-offset)
- [target-size](#target-size)
- [region](#region)
- [inline-style-property](#inline-style-property)

Expand Down Expand Up @@ -487,6 +489,18 @@ h6:not([role]),
| -------- | :-------------------------------------------------------- | :------------------------- |
| `values` | <pre lang=js>['row', 'col', 'rowgroup', 'colgroup']</pre> | List of valid scope values |

### target-offset

| Option | Default | Description |
| ----------- | :------ | :--------------------------------------------------------------------------------------------------------- |
| `minOffset` | `24` | Minimum space required from the farthest edge of the target, to the closest edge of the neighboring target |

### target-size

| Option | Default | Description |
| --------- | :------ | :------------------------------------------------------------------------------------------------------- |
| `minSize` | `24` | Minimum width and height a component should have, that is not obscured by some other interactive element |

### region

| Option | Default | Description |
Expand Down
9 changes: 5 additions & 4 deletions doc/rule-descriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@

## WCAG 2.1 Level A & AA Rules

| Rule ID | Description | Impact | Tags | Issue Type | ACT Rules |
| :----------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------- | :------ | :------------------------------------- | :--------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [autocomplete-valid](https://dequeuniversity.com/rules/axe/4.4/autocomplete-valid?application=RuleDescription) | Ensure the autocomplete attribute is correct and suitable for the form field | Serious | cat.forms, wcag21aa, wcag135, ACT | failure | [73f2c2](https://act-rules.github.io/rules/73f2c2) |
| [avoid-inline-spacing](https://dequeuniversity.com/rules/axe/4.4/avoid-inline-spacing?application=RuleDescription) | Ensure that text spacing set through style attributes can be adjusted with custom stylesheets | Serious | cat.structure, wcag21aa, wcag1412, ACT | failure | [24afc2](https://act-rules.github.io/rules/24afc2), [9e45ec](https://act-rules.github.io/rules/9e45ec), [78fd32](https://act-rules.github.io/rules/78fd32) |
| Rule ID | Description | Impact | Tags | Issue Type | ACT Rules |
| :----------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------- | :------ | :------------------------------------------- | :--------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [autocomplete-valid](https://dequeuniversity.com/rules/axe/4.4/autocomplete-valid?application=RuleDescription) | Ensure the autocomplete attribute is correct and suitable for the form field | Serious | cat.forms, wcag21aa, wcag135, ACT | failure | [73f2c2](https://act-rules.github.io/rules/73f2c2) |
| [avoid-inline-spacing](https://dequeuniversity.com/rules/axe/4.4/avoid-inline-spacing?application=RuleDescription) | Ensure that text spacing set through style attributes can be adjusted with custom stylesheets | Serious | cat.structure, wcag21aa, wcag1412, ACT | failure | [24afc2](https://act-rules.github.io/rules/24afc2), [9e45ec](https://act-rules.github.io/rules/9e45ec), [78fd32](https://act-rules.github.io/rules/78fd32) |
| [target-size](https://dequeuniversity.com/rules/axe/4.4/target-size?application=RuleDescription) | Ensure touch target have sufficient size and space | Serious | wcag22aa, sc258, cat.sensory-and-visual-cues | failure | |

## Best Practices Rules

Expand Down
34 changes: 34 additions & 0 deletions lib/checks/mobile/target-offset-evaluate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { findNearbyElms } from '../../commons/dom';
import { getRole, getRoleType } from '../../commons/aria';
import { getOffset } from '../../commons/math';

const roundingMargin = 0.05;

export default function targetOffsetEvaluate(node, options, vNode) {
const minOffset = options?.minOffset || 24;
const closeNeighbors = [];
let closestOffset = minOffset;
for (const vNeighbor of findNearbyElms(vNode, minOffset)) {
const role = getRole(vNeighbor);
if (getRoleType(role) !== 'widget') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this match nodes based on how the matcher does it? So something like:

(node, vNode) => isWidgetType(vNode),
(node, vNode) => isNotAreaElement(vNode),
(node, vNode) => !svgNamespaceMatches(node, vNode),
(node, vNode) => isFocusable(vNode)

This would fix a potential problem where a neighbor button is disabled (modified B4 example again). In this case the first and last button report as violations even though you can't click on the middle button.

<div>
  <button class="failed h2 w3"></button>
  <button disabled class="failed h2 w3"></button>
  <button class="failed h2 w3"></button>
</div>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, although not exactly. Disabled buttons should probably be excluded... that's a fair enough point, but I don't think we should exclude inline links for example, or area / SVG widgets. If you put a button too close to your SVG, the button's still the problem, even if axe passes on the SVG.

continue;
}
const offset = roundToSingleDecimal(getOffset(vNode, vNeighbor));
if (offset + roundingMargin >= minOffset) {
continue;
}
closestOffset = Math.min(closestOffset, offset);
closeNeighbors.push(vNeighbor.actualNode);
}

this.data({ closestOffset, minOffset });
if (closeNeighbors.length > 0) {
this.relatedNodes(closeNeighbors);
return false;
}
return true;
}

function roundToSingleDecimal(num) {
return Math.round(num * 10) / 10;
}
14 changes: 14 additions & 0 deletions lib/checks/mobile/target-offset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "target-offset",
"evaluate": "target-offset-evaluate",
"options": {
"minOffset": 24
},
"metadata": {
"impact": "serious",
"messages": {
"pass": "Target is sufficiently offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)",
WilcoFiers marked this conversation as resolved.
Show resolved Hide resolved
"fail": "Target is insufficient offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)"
WilcoFiers marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
77 changes: 77 additions & 0 deletions lib/checks/mobile/target-size-evaluate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { findNearbyElms } from '../../commons/dom';
import { getRole, getRoleType } from '../../commons/aria';
import { splitRects, hasVisualOverlap } from '../../commons/math';

const roundingMargin = 0.05;

/**
* Determine if an element has a minimum size, taking into account
* any elements that may obscure it.
*/
export default function targetSize(node, options, vNode) {
const minSpacing = options?.minSpacing || 24;
const hasMinimumSize = ({ width, height }) => {
return (
width + roundingMargin >= minSpacing &&
height + roundingMargin >= minSpacing
);
};

const nodeRect = vNode.boundingClientRect;
if (!hasMinimumSize(nodeRect)) {
this.data({ minSpacing, ...toDecimalSize(nodeRect) });
return false;
}

// Handle overlapping elements;
const nearbyElms = findNearbyElms(vNode);
const obscuringElms = nearbyElms.filter(vNeighbor => {
const role = getRole(vNeighbor);
return getRoleType(role) === 'widget' && hasVisualOverlap(vNode, vNeighbor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here about filtering same as the matcher function

});

if (obscuringElms.length === 0) {
this.data({ minSpacing, ...toDecimalSize(nodeRect) });
return true; // No obscuring elements; pass
}

// Find areas of the target that are not obscured
const obscuringRects = obscuringElms.map(
({ boundingClientRect: rect }) => rect
);
const unobscuredRects = splitRects(nodeRect, obscuringRects);

// Of the unobscured inner rects, work out the largest
const largestInnerRect = unobscuredRects.reduce((rectA, rectB) => {
const rectAisMinimum = hasMinimumSize(rectA);
const rectBisMinimum = hasMinimumSize(rectB);
// Prioritize rects that pass the minimum
if (rectAisMinimum !== rectBisMinimum) {
return rectAisMinimum ? rectA : rectB;
}
const areaA = rectA.width * rectA.height;
const areaB = rectB.width * rectB.height;
return areaA > areaB ? rectA : rectB;
});

if (!hasMinimumSize(largestInnerRect)) {
this.data({
messageKey: 'obscured',
minSpacing,
...toDecimalSize(largestInnerRect)
});
this.relatedNodes(obscuringElms.map(({ actualNode }) => actualNode));
// Element is (partially?) obscured, with insufficient space
return false;
}

this.data({ minSpacing, ...toDecimalSize(largestInnerRect) });
return true;
}

function toDecimalSize(rect) {
return {
width: Math.round(rect.width * 10) / 10,
height: Math.round(rect.height * 10) / 10
};
}
17 changes: 17 additions & 0 deletions lib/checks/mobile/target-size.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "target-size",
"evaluate": "target-size-evaluate",
"options": {
"minSpacing": 24
WilcoFiers marked this conversation as resolved.
Show resolved Hide resolved
},
"metadata": {
"impact": "serious",
"messages": {
"pass": "Control has sufficient size (${data.width}px by ${data.height}px, should be at least ${data.minSpacing}px by ${data.minSpacing}px)",
WilcoFiers marked this conversation as resolved.
Show resolved Hide resolved
"fail": {
"default": "Element has insufficient size (${data.width}px by ${data.height}px, should be at least ${data.minSpacing}px by ${data.minSpacing}px)",
"obscured": "Element has insufficient size because it is obscured (smallest space is ${data.width}px by ${data.height}px, should be at least ${data.minSpacing}px by ${data.minSpacing}px)"
}
}
}
}
Loading