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 1 commit
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
4 changes: 2 additions & 2 deletions lib/checks/mobile/target-offset.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"metadata": {
"impact": "serious",
"messages": {
"pass": "Target is sufficiently offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)",
"fail": "Target is insufficient offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)"
"pass": "Target has sufficiently offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"pass": "Target has sufficiently offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)",
"pass": "Target has sufficient offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)",

"fail": "Target has insufficient offset from its closest neighbor (${data.closestOffset}px should be at least ${data.minOffset}px)"
}
}
}
10 changes: 6 additions & 4 deletions lib/commons/dom/create-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,13 @@ function findScrollRegionParent(vNode, parentVNode) {
*/
function addNodeToGrid(grid, vNode) {
const gridSize = constants.gridSize;
// save a reference to where this element is in the grid so we
// can find it even if it's in a subgrid
vNode._grid = grid;

vNode.clientRects.forEach(rect => {
if (rect.right <= 0 || rect.bottom <= 0) {
return;
}
// save a reference to where this element is in the grid so we
// can find it even if it's in a subgrid
vNode._grid ??= grid;
Comment on lines +328 to +333
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMPORTANT: These lines have been modified to fix a bug where elements outside the viewport could still be added to the grid.

const x = rect.left;
const y = rect.top;

Expand Down
44 changes: 25 additions & 19 deletions lib/commons/dom/find-nearby-elms.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import createGrid from './create-grid';

function findNearbyElms(vNode, margin = 0) {
export default function findNearbyElms(vNode, margin = 0) {
/*eslint no-bitwise: 0*/
const gridSize = createGrid();
Copy link
Contributor

Choose a reason for hiding this comment

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

So createGrid was written with color-contrast in mind and so doesn't include visually hidden elements in the grid. However, now that we want to use it for clickable widget elements it creates a bug. Take for example the following code (modified B4 example):

<script>function clicked() { console.log('click') }</script>
<span class="failed h2 w3"></span>
<span style="opacity: 0" class="failed h2 w3" onclick="clicked()"></span>
<span class="failed h2 w3"></span>

Both of the visible spans pass the target-size rule, but the opacity 0 span is still clickable, meaning that it still would cause a problem of clicking the wrong element when too close together. I would imagine this should fail the rule still.

Copy link
Contributor

Choose a reason for hiding this comment

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

Another question: How should we handle things on-top of these nodes? For example, taking the following code (modified B4 example again):

<style>
  .foo {
    width: 400px;
    height: 400px;
    position: absolute !important;
    background: red;
    top: -5px;
    z-index: 2;
  }
</style>
<div>
  <div class="foo"></div>
  <span class="failed h2 w3"></span>
  <span class="failed h2 w3"></span>
  <span class="failed h2 w3"></span>
</div>

The nodes still fail even though it's not possible to click on them. Same if a modal is currently open. Should we fail these nodes still?

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 ended up doing the overlap thing in the target-size check, rather than in the matches, since target-size is already looking at overlapping elements. Much quicker to sort out there.

const neighbors = [];
if (!vNode._grid?.cells?.length) {
return []; // Elements not in the grid don't have ._grid
}

const rect = vNode.boundingClientRect;
const gridCells = vNode._grid.cells;
const boundaries = {
topRow: ((rect.top - margin) / gridSize) | 0,
bottomRow: ((rect.bottom + margin) / gridSize) | 0,
leftCol: ((rect.left - margin) / gridSize) | 0,
rightCol: ((rect.right + margin) / gridSize) | 0
};

const topRow = ((rect.top - margin) / gridSize) | 0;
const bottomRow = ((rect.bottom + margin) / gridSize) | 0;
const leftCol = ((rect.left - margin) / gridSize) | 0;
const rightCol = ((rect.right + margin) / gridSize) | 0;
const neighbors = [];
loopGridCells(gridCells, boundaries, vNeighbor => {
if (vNeighbor && vNeighbor !== vNode && !neighbors.includes(vNeighbor)) {
neighbors.push(vNeighbor);
}
});
return neighbors;
}

function loopGridCells(gridCells, boundaries, cb) {
const { topRow, bottomRow, leftCol, rightCol } = boundaries;
for (let row = topRow; row <= bottomRow; row++) {
for (let col = leftCol; col <= rightCol; col++) {
for (let i = 0; i <= gridCells[row][col].length; i++) {
var vNeighbour = gridCells[row][col][i];
// Avoid duplication
if (
vNeighbour &&
vNeighbour !== vNode &&
!neighbors.includes(vNeighbour)
) {
neighbors.push(vNeighbour);
}
// Don't loop on elements outside the grid
const length = gridCells[row]?.[col]?.length ?? -1;
for (let i = 0; i <= length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (let i = 0; i <= length; i++) {
for (let i = 0; i < length; i++) {

cb(gridCells[row][col][i]);
}
}
}
return neighbors;
}

export default findNearbyElms;
148 changes: 148 additions & 0 deletions test/commons/dom/create-grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Additional tests for createGrid are part of createRectStack tests,
// which is what createGrid was originally part of
describe('create-grid', function () {
var fixture;
var createGrid = axe.commons.dom.createGrid;
var fixtureSetup = axe.testUtils.fixtureSetup;

function findPositions(grid, vNode) {
var positions = [];
grid.cells.forEach(function (rowCells, rowIndex) {
rowCells.forEach(function (cells, colIndex) {
if (cells.includes(vNode)) {
positions.push({ x: rowIndex, y: colIndex });
}
});
});
return positions;
}

it('returns the grid size', function () {
axe.setup();
assert.equal(createGrid(), axe.constants.gridSize);
});

it('sets ._grid to nodes in the grid', function () {
fixture = fixtureSetup('<span>Hello world</span>');
assert.isUndefined(fixture._grid);
assert.isUndefined(fixture.children[0]._grid);

createGrid();
assert.isDefined(fixture._grid);
assert.equal(fixture._grid, fixture.children[0]._grid);
});

it('adds elements to the correct cell in the grid', function () {
fixture = fixtureSetup('<span>Hello world</span>');
createGrid();
var positions = findPositions(fixture._grid, fixture.children[0]);
assert.deepEqual(positions, [{ x: 0, y: 0 }]);
});

it('adds large elements to multiple cell', function () {
fixture = fixtureSetup(
'<span style="display: inline-block; width: 300px; height: 300px;">' +
'Hello world</span>'
);
createGrid();

var positions = findPositions(fixture._grid, fixture.children[0]);
assert.deepEqual(positions, [
{ x: 0, y: 0 },
{ x: 0, y: 1 },
{ x: 1, y: 0 },
{ x: 1, y: 1 }
]);
});

describe('hidden elements', function () {
beforeEach(function () {
// Ensure the fixture itself is part of the grid, even if its content isn't
document
.querySelector('#fixture')
.setAttribute('style', 'min-height: 10px');
});

it('does not add hidden elements', function () {
fixture = fixtureSetup('<div style="display: none">hidden</div>');
createGrid();
var position = findPositions(fixture._grid, fixture.children[0]);
assert.isEmpty(position);
assert.isUndefined(fixture.children[0]._grid);
});

it('does not add off screen elements', function () {
fixture = fixtureSetup(
'<div style="position: fixed; top: -3em">off screen</div>'
);
createGrid();
var position = findPositions(fixture._grid, fixture.children[0]);
assert.isEmpty(position);
assert.isUndefined(fixture.children[0]._grid);
});

it('does add partially on screen elements', function () {
fixture = fixtureSetup(
'<div style="position: fixed; top: -1em; min-height: 2em">off screen</div>'
);
createGrid();
var position = findPositions(fixture._grid, fixture.children[0]);
assert.deepEqual(position, [{ x: 0, y: 0 }]);
});
});

describe('subGrids', function () {
it('sets the .subGrid property', function () {
fixture = fixtureSetup(
'<div style="overflow: scroll; height: 100px;">' +
'<span style="display: inline-block; height: 300px" id="x">x</span>' +
'</div>'
);
var vOverflow = fixture.children[0];
assert.isUndefined(vOverflow._subGrid);
createGrid();
assert.isDefined(vOverflow._subGrid);
assert.notEqual(vOverflow._grid, vOverflow._subGrid);
});

it('sets the ._grid of children as the subGrid', function () {
fixture = fixtureSetup(
'<div style="overflow: scroll; height: 100px;">' +
'<span style="display: inline-block; height: 300px" id="x">x</span>' +
'</div>'
);
createGrid();
var vOverflow = fixture.children[0];
var vSpan = vOverflow.children[0];
assert.equal(vOverflow._subGrid, vSpan._grid);
});

it('does not add scrollable children to the root grid', function () {
fixture = fixtureSetup(
'<div style="overflow: scroll; height: 100px;">' +
'<span style="display: inline-block; height: 300px" id="x">x</span>' +
'</div>'
);
createGrid();
var vSpan = fixture.children[0].children[0];
var position = findPositions(fixture._grid, vSpan);
assert.isEmpty(position);
});

it('adds scrollable children to the subGrid', function () {
fixture = fixtureSetup(
'<div style="overflow: scroll; height: 100px;">' +
'<span style="display: inline-block; height: 300px" id="x">x</span>' +
'</div>'
);
createGrid();
var vOverflow = fixture.children[0];
var vSpan = vOverflow.children[0];
var position = findPositions(vOverflow._subGrid, vSpan);
assert.deepEqual(position, [
{ x: 0, y: 0 },
{ x: 1, y: 0 }
]);
});
});
});
67 changes: 47 additions & 20 deletions test/commons/dom/find-nearby-elms.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,55 @@ describe('findNearbyElms', function () {
return ids;
}

beforeEach(function () {
fixture = fixtureSetup(
'<div id="n0" style="height:30px; margin-bottom:30px;">0</div>' +
'<div id="n1" style="height:30px; margin-bottom:30px;">1</div>' +
'<div id="n2" style="height:30px; margin-bottom:30px;">2</div>' +
'<div id="n3" style="height:30px; margin-bottom:30px;">3</div>' +
'<div id="n4" style="height:30px; margin-bottom:30px;">4</div>' +
'<div id="n5" style="height:30px; margin-bottom:30px;">5</div>' +
'<div id="n6" style="height:30px; margin-bottom:30px;">6</div>' +
'<div id="n7" style="height:30px; margin-bottom:30px;">7</div>' +
'<div id="n8" style="height:30px; margin-bottom:30px;">8</div>' +
'<div id="n9" style="height:30px; margin-bottom:30px;">9</div>'
);
});
describe('in the viewport', function () {
beforeEach(function () {
fixture = fixtureSetup(
'<div id="n0" style="height:30px; margin-bottom:30px;">0</div>' +
'<div id="n1" style="height:30px; margin-bottom:30px;">1</div>' +
'<div id="n2" style="height:30px; margin-bottom:30px;">2</div>' +
'<div id="n3" style="height:30px; margin-bottom:30px;">3</div>' +
'<div id="n4" style="height:30px; margin-bottom:30px;">4</div>' +
'<div id="n5" style="height:30px; margin-bottom:30px;">5</div>' +
'<div id="n6" style="height:30px; margin-bottom:30px;">6</div>' +
'<div id="n7" style="height:30px; margin-bottom:30px;">7</div>' +
'<div id="n8" style="height:30px; margin-bottom:30px;">8</div>' +
'<div id="n9" style="height:30px; margin-bottom:30px;">9</div>'
);
});

it('returns node from the same grid cell', function () {
var nearbyElms = findNearbyElms(fixture.children[1]);
assert.deepEqual(getIds(nearbyElms), ['n0', 'n2', 'n3']);
});

it('returns node from the same grid cell', function () {
var nearbyElms = findNearbyElms(fixture.children[1]);
assert.deepEqual(getIds(nearbyElms), ['n0', 'n2', 'n3']);
it('returns node from multiple grid cells when crossing a boundary', function () {
var nearbyElms = findNearbyElms(fixture.children[5]);
assert.deepEqual(getIds(nearbyElms), ['n3', 'n4', 'n6']);
});
});

it('returns node from multiple grid cells when crossing a boundary', function () {
var nearbyElms = findNearbyElms(fixture.children[5]);
assert.deepEqual(getIds(nearbyElms), ['n3', 'n4', 'n6']);
describe('on the edge', function () {
beforeEach(function () {
fixture = fixtureSetup(
'<div id="n0" style="position: fixed; top:-30px; height: 60px">0</div>' +
'<div id="n1" style="position: fixed; top:-30px; height: 30px">1</div>' +
'<div id="n2" style="position: fixed; top:0; height: 30px">2</div>'
);
});

it('ignores cells outside the document boundary', function () {
var nearbyElms = findNearbyElms(fixture.children[0]);
assert.deepEqual(getIds(nearbyElms), ['n2']);
});

it('returns no neighbors for off-screen elements', function () {
var nearbyElms = findNearbyElms(fixture.children[1]);
assert.deepEqual(getIds(nearbyElms), []);
});

it('returns element partially on screen as neighbors', function () {
var nearbyElms = findNearbyElms(fixture.children[2]);
assert.deepEqual(getIds(nearbyElms), ['n0']);
});
});
});