Skip to content

feat(ByRole): Allow filter by selected state #540

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

Merged
merged 3 commits into from
May 6, 2020
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
85 changes: 85 additions & 0 deletions src/__tests__/ariaAttributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {render} from './helpers/test-utils'

test('`selected` throws on unsupported roles', () => {
const {getByRole} = render(`<input aria-selected="true" type="text">`)
expect(() =>
getByRole('textbox', {selected: true}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"aria-selected\\" is not supported on role \\"textbox\\"."`,
)
})

test('`selected: true` matches `aria-selected="true"` on supported roles', () => {
const {getAllByRole} = render(`
<select>
<option selected id="selected-native-option" />
<option id="unselected-native-option" />
</select>
<div role="listbox">
<div role="option" aria-selected="true" id="selected-listbox-option" />
<div role="option" aria-selected="false" id="unselected-listbox-option" />
<div role="option" id="not-selectable-listbox-option" />
</div>
<div role="tree">
<div role="treeitem" aria-selected="true" id="selected-treeitem" />
<div role="treeitem" aria-selected="false" id="unselected-treeitem" />
<div role="treeitem" id="not-selectable-treeitem" />
</div>
<table>
<thead>
<tr>
<th scope="col" aria-selected="true" id="selected-native-columnheader" />
<div role="columnheader" aria-selected="true" id="selected-columnheader" />
<th scope="col" id="unselected-native-columnheader" />
</tr>
</thead>
<tbody>
<tr>
<th scope="row" aria-selected="true" id="selected-native-rowheader" />
<td />
<td />
</tr>
<tr>
<div role="rowheader" aria-selected="true" id="selected-rowheader" />
<td />
<td />
</tr>
</tbody>
</table>
<div role="grid">
<div role="gridcell" aria-selected="true" id="selected-gridcell" />
<div role="gridcell" aria-selected="false" id="unselected-gridcell" />
<div role="gridcell" id="not-selectable-gridcell" />
</div>
<div role="tablist">
<div role="tab" aria-selected="true" id="selected-tab" />
<div role="tab" aria-selected="false" id= "unselected-tab" />
<div role="tab" id="unselectable-tab" />
</div>
`)

expect(
getAllByRole('columnheader', {selected: true}).map(({id}) => id),
).toEqual(['selected-native-columnheader', 'selected-columnheader'])

expect(getAllByRole('gridcell', {selected: true}).map(({id}) => id)).toEqual([
'selected-gridcell',
])

expect(getAllByRole('option', {selected: true}).map(({id}) => id)).toEqual([
'selected-native-option',
'selected-listbox-option',
])

expect(
getAllByRole('rowheader', {selected: true}).map(({id}) => id),
).toEqual(['selected-rowheader', 'selected-native-rowheader'])

expect(getAllByRole('treeitem', {selected: true}).map(({id}) => id)).toEqual([
'selected-treeitem',
])

expect(getAllByRole('tab', {selected: true}).map(({id}) => id)).toEqual([
'selected-tab',
])
})
17 changes: 17 additions & 0 deletions src/queries/role.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {computeAccessibleName} from 'dom-accessibility-api'
import {roles as allRoles} from 'aria-query'
import {
computeAriaSelected,
getImplicitAriaRoles,
prettyRoles,
isInaccessible,
Expand All @@ -24,11 +26,19 @@ function queryAllByRole(
trim,
normalizer,
queryFallbacks = false,
selected,
} = {},
) {
const matcher = exact ? matches : fuzzyMatches
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})

if (selected !== undefined) {
// guard against unknown roles
if (allRoles.get(role)?.props['aria-selected'] === undefined) {
throw new Error(`"aria-selected" is not supported on role "${role}".`)
}
}

const subtreeIsInaccessibleCache = new WeakMap()
function cachedIsSubtreeInaccessible(element) {
if (!subtreeIsInaccessibleCache.has(element)) {
Expand Down Expand Up @@ -65,6 +75,13 @@ function queryAllByRole(
matcher(implicitRole, node, role, matchNormalizer),
)
})
.filter(element => {
if (selected !== undefined) {
return selected === computeAriaSelected(element)
}
// don't care if aria attributes are unspecified
return true
})
.filter(element => {
return hidden === false
? isInaccessible(element, {
Expand Down
22 changes: 22 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,33 @@ function prettyRoles(dom, {hidden}) {
const logRoles = (dom, {hidden = false} = {}) =>
console.log(prettyRoles(dom, {hidden}))

/**
* @param {Element} element -
* @returns {boolean | undefined} - false/true if (not)selected, undefined if not selectable
*/
function computeAriaSelected(element) {
// implicit value from html-aam mappings: https://www.w3.org/TR/html-aam-1.0/#html-attribute-state-and-property-mappings
// https://www.w3.org/TR/html-aam-1.0/#details-id-97
if (element.tagName === 'OPTION') {
return element.selected
}
// explicit value
const attributeValue = element.getAttribute('aria-selected')
if (attributeValue === 'true') {
return true
}
if (attributeValue === 'false') {
return false
}
return undefined
}

export {
getRoles,
logRoles,
getImplicitAriaRoles,
isSubtreeInaccessible,
prettyRoles,
isInaccessible,
computeAriaSelected,
}