-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(aria-allowed-attr): no inconsistent aria-checked on HTML checkbox…
…es (#3895) * feat(aria-allowed-attr): no inconsistent aria-checked on HTML checkboxes * Add integration tests * Add non-table row test * Cleanup / docs work * Handle checkbox indeterminate state * Fix virtual rule test * Apply suggestions from code review Co-authored-by: Steven Lambert <2433219+straker@users.noreply.github.com> * Fix broken test --------- Co-authored-by: Steven Lambert <2433219+straker@users.noreply.github.com>
- Loading branch information
1 parent
0f405c6
commit 704043e
Showing
18 changed files
with
498 additions
and
173 deletions.
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
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,20 @@ | ||
import getRole from '../../commons/aria/get-role'; | ||
import ariaConditionalCheckboxAttr from './aria-conditional-checkbox-attr-evaluate'; | ||
import ariaConditionalRowAttr from './aria-conditional-row-attr-evaluate'; | ||
|
||
const conditionalRoleMap = { | ||
row: ariaConditionalRowAttr, | ||
checkbox: ariaConditionalCheckboxAttr | ||
}; | ||
|
||
export default function ariaConditionalAttrEvaluate( | ||
node, | ||
options, | ||
virtualNode | ||
) { | ||
const role = getRole(virtualNode); | ||
if (!conditionalRoleMap[role]) { | ||
return true; | ||
} | ||
return conditionalRoleMap[role].call(this, node, options, virtualNode); | ||
} |
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,23 @@ | ||
{ | ||
"id": "aria-conditional-attr", | ||
"evaluate": "aria-conditional-attr-evaluate", | ||
"options": { | ||
"invalidTableRowAttrs": [ | ||
"aria-posinset", | ||
"aria-setsize", | ||
"aria-expanded", | ||
"aria-level" | ||
] | ||
}, | ||
"metadata": { | ||
"impact": "serious", | ||
"messages": { | ||
"pass": "ARIA attribute is allowed", | ||
"fail": { | ||
"checkbox": "Remove aria-checked, or set it to \"${data.checkState}\" to match the real checkbox state", | ||
"rowSingular": "This attribute is supported with treegrid rows, but not ${data.ownerRole}: ${data.invalidAttrs}", | ||
"rowPlural": "These attributes are supported with treegrid rows, but not ${data.ownerRole}: ${data.invalidAttrs}" | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
lib/checks/aria/aria-conditional-checkbox-attr-evaluate.js
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,39 @@ | ||
export default function ariaConditionalCheckboxAttr( | ||
node, | ||
options, | ||
virtualNode | ||
) { | ||
const { nodeName, type } = virtualNode.props; | ||
const ariaChecked = normalizeAriaChecked(virtualNode.attr('aria-checked')); | ||
if (nodeName !== 'input' || type !== 'checkbox' || !ariaChecked) { | ||
return true; | ||
} | ||
|
||
const checkState = getCheckState(virtualNode); | ||
if (ariaChecked === checkState) { | ||
return true; | ||
} | ||
this.data({ | ||
messageKey: 'checkbox', | ||
checkState | ||
}); | ||
return false; | ||
} | ||
|
||
function getCheckState(vNode) { | ||
if (vNode.props.indeterminate) { | ||
return 'mixed'; | ||
} | ||
return vNode.props.checked ? 'true' : 'false'; | ||
} | ||
|
||
function normalizeAriaChecked(ariaCheckedVal) { | ||
if (!ariaCheckedVal) { | ||
return ''; | ||
} | ||
ariaCheckedVal = ariaCheckedVal.toLowerCase(); | ||
if (['mixed', 'true'].includes(ariaCheckedVal)) { | ||
return ariaCheckedVal; | ||
} | ||
return 'false'; | ||
} |
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,36 @@ | ||
import getRole from '../../commons/aria/get-role'; | ||
import { closest } from '../../core/utils'; | ||
|
||
export default function ariaConditionalRowAttr( | ||
node, | ||
{ invalidTableRowAttrs } = {}, | ||
virtualNode | ||
) { | ||
const invalidAttrs = | ||
invalidTableRowAttrs?.filter?.(invalidAttr => { | ||
return virtualNode.hasAttr(invalidAttr); | ||
}) ?? []; | ||
if (invalidAttrs.length === 0) { | ||
return true; | ||
} | ||
|
||
const owner = getRowOwner(virtualNode); | ||
const ownerRole = owner && getRole(owner); | ||
if (!ownerRole || ownerRole === 'treegrid') { | ||
return true; | ||
} | ||
|
||
const messageKey = `row${invalidAttrs.length > 1 ? 'Plural' : 'Singular'}`; | ||
this.data({ messageKey, invalidAttrs, ownerRole }); | ||
return false; | ||
} | ||
|
||
function getRowOwner(virtualNode) { | ||
// check if the parent exists otherwise a TypeError will occur (virtual-nodes specifically) | ||
if (!virtualNode.parent) { | ||
return; | ||
} | ||
const rowOwnerQuery = | ||
'table:not([role]), [role~="treegrid"], [role~="table"], [role~="grid"]'; | ||
return closest(virtualNode, rowOwnerQuery); | ||
} |
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
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.