-
Notifications
You must be signed in to change notification settings - Fork 789
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
Changes from 29 commits
7d9b70c
54817d7
80299b2
302db49
0172fbe
5b1c1a3
a0eaaf8
c4029a5
55b037c
81feac6
eb27049
217ac64
10b8972
297524f
a25d7a0
f6b97df
8ab55c7
496f227
8ea60ec
a5a0512
368d612
b4462e5
9e7e9b6
4fefa21
7885fe8
840cf4e
1e204c1
b2f12e8
17ce8ea
d72b683
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { findNearbyElms, isFocusable } from '../../commons/dom'; | ||
import { 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)) { | ||
if (getRoleType(vNeighbor) !== 'widget' || !isFocusable(vNeighbor)) { | ||
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; | ||
} |
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 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)" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { findNearbyElms, isFocusable } from '../../commons/dom'; | ||
import { 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 minSize = options?.minSize || 24; | ||
const nodeRect = vNode.boundingClientRect; | ||
const hasMinimumSize = ({ width, height }) => { | ||
return ( | ||
width + roundingMargin >= minSize && height + roundingMargin >= minSize | ||
); | ||
}; | ||
|
||
const obscuringElms = []; | ||
for (const vNeighbor of findNearbyElms(vNode)) { | ||
if ( | ||
!hasVisualOverlap(vNode, vNeighbor) || | ||
getCssPointerEvents(vNeighbor) === 'none' | ||
) { | ||
continue; | ||
} | ||
if (isEnclosedRect(vNode, vNeighbor)) { | ||
this.relatedNodes([vNeighbor.actualNode]); | ||
this.data({ messageKey: 'obscured' }); | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add the node as a related node |
||
} | ||
obscuringElms.push(vNeighbor); | ||
} | ||
|
||
if (!hasMinimumSize(nodeRect)) { | ||
this.data({ minSize, ...toDecimalSize(nodeRect) }); | ||
return false; | ||
} | ||
|
||
const obscuredWidgets = obscuringElms.filter( | ||
vNeighbor => getRoleType(vNeighbor) === 'widget' && isFocusable(vNeighbor) | ||
); | ||
|
||
if (obscuredWidgets.length === 0) { | ||
this.data({ minSize, ...toDecimalSize(nodeRect) }); | ||
return true; // No obscuring elements; pass | ||
} | ||
this.relatedNodes(obscuredWidgets.map(({ actualNode }) => actualNode)); | ||
|
||
// Find areas of the target that are not obscured | ||
const obscuringRects = obscuredWidgets.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)) { | ||
// Element is (partially?) obscured, with insufficient space | ||
this.data({ | ||
messageKey: 'partiallyObscured', | ||
minSize, | ||
...toDecimalSize(largestInnerRect) | ||
}); | ||
return false; | ||
} | ||
|
||
this.data({ minSize, ...toDecimalSize(largestInnerRect) }); | ||
return true; | ||
} | ||
|
||
function isEnclosedRect(vNodeA, vNodeB) { | ||
const rectA = vNodeA.boundingClientRect; | ||
const rectB = vNodeB.boundingClientRect; | ||
return ( | ||
rectA.top >= rectB.top && | ||
rectA.left >= rectB.left && | ||
rectA.bottom <= rectB.bottom && | ||
rectA.right <= rectB.right | ||
); | ||
} | ||
|
||
function getCssPointerEvents(vNode) { | ||
return vNode.getComputedStylePropertyValue('pointer-events'); | ||
} | ||
|
||
function toDecimalSize(rect) { | ||
return { | ||
width: Math.round(rect.width * 10) / 10, | ||
height: Math.round(rect.height * 10) / 10 | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"id": "target-size", | ||
"evaluate": "target-size-evaluate", | ||
"options": { | ||
"minSize": 24 | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": { | ||
"default": "Control has sufficient size (${data.width}px by ${data.height}px, should be at least ${data.minSize}px by ${data.minSize}px)", | ||
"obscured": "Control is ignored because it is fully obscured and thus not clickable" | ||
}, | ||
"fail": { | ||
"default": "Element has insufficient size (${data.width}px by ${data.height}px, should be at least ${data.minSize}px by ${data.minSize}px)", | ||
"partiallyObscured": "Element has insufficient size because it is partially obscured (smallest space is ${data.width}px by ${data.height}px, should be at least ${data.minSize}px by ${data.minSize}px)" | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
import standards from '../../standards'; | ||
import AbstractVirtualNode from '../../core/base/virtual-node/abstract-virtual-node'; | ||
|
||
/** | ||
* Get the "type" of role; either widget, composite, abstract, landmark or `null` | ||
* @method getRoleType | ||
* @memberof axe.commons.aria | ||
* @instance | ||
* @param {String} role The role to check | ||
* @param {String|Null|Node|Element} role The role to check, or element to check the role of | ||
* @return {Mixed} String if a matching role and its type are found, otherwise `null` | ||
*/ | ||
function getRoleType(role) { | ||
const roleDef = standards.ariaRoles[role]; | ||
|
||
if (!roleDef) { | ||
return null; | ||
if ( | ||
role instanceof AbstractVirtualNode || | ||
(window?.Node && role instanceof window.Node) | ||
) { | ||
role = axe.commons.aria.getRole(role); | ||
Comment on lines
+13
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: I've noticed that you've lately been wanting more strict type checking and guarding in our APIs. Is this something you want to do throughout the code base for our public API methods? The simpler check here would be to see if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't noticed that. Have I really? I used instanceof here because that's what we usually do when we want to check nodes. There are lots of Looking at where/how this function has been used, it's going to get |
||
} | ||
|
||
return roleDef.type; | ||
const roleDef = standards.ariaRoles[role]; | ||
return roleDef?.type || null; | ||
} | ||
|
||
export default getRoleType; |
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.
If you want me to pull these into their own PR let me know. It's not unrelated to the PR... but it's not very related either.