Skip to content

support {checked: true} for checkbox / radio #692

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 9 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 23 additions & 1 deletion src/__tests__/ariaAttributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {render} from './helpers/test-utils'
import {render, renderIntoDocument} from './helpers/test-utils'

test('`selected` throws on unsupported roles', () => {
const {getByRole} = render(`<input aria-selected="true" type="text">`)
Expand All @@ -9,6 +9,28 @@ test('`selected` throws on unsupported roles', () => {
)
})

test('`checked: true|false` matches `checked` checkboxes', () => {
const {getByRole} = renderIntoDocument(
`<div>
<input type="checkbox" checked />
<input type="checkbox" />
</div>`,
)
expect(getByRole('checkbox', {checked: true})).toBeInTheDocument()
expect(getByRole('checkbox', {checked: false})).toBeInTheDocument()
})

test('`checked: true|false` matches `checked` elements with proper role', () => {
const {getByRole} = renderIntoDocument(
`<div>
<span role="checkbox" aria-checked="true">✔</span>
<span role="checkbox" aria-checked="false">𝒙</span>
</div>`,
)
expect(getByRole('checkbox', {checked: true})).toBeInTheDocument()
expect(getByRole('checkbox', {checked: false})).toBeInTheDocument()
})

test('`selected: true` matches `aria-selected="true"` on supported roles', () => {
const {getAllByRole} = render(`
<select>
Expand Down
12 changes: 12 additions & 0 deletions src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {computeAccessibleName} from 'dom-accessibility-api'
import {roles as allRoles} from 'aria-query'
import {
computeAriaSelected,
computeAriaChecked,
getImplicitAriaRoles,
prettyRoles,
isInaccessible,
Expand Down Expand Up @@ -29,6 +30,7 @@ function queryAllByRole(
normalizer,
queryFallbacks = false,
selected,
checked,
} = {},
) {
checkContainerType(container)
Expand All @@ -42,6 +44,13 @@ function queryAllByRole(
}
}

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

const subtreeIsInaccessibleCache = new WeakMap()
function cachedIsSubtreeInaccessible(element) {
if (!subtreeIsInaccessibleCache.has(element)) {
Expand Down Expand Up @@ -82,6 +91,9 @@ function queryAllByRole(
if (selected !== undefined) {
return selected === computeAriaSelected(element)
}
if (checked !== undefined) {
return checked === computeAriaChecked(element)
}
// don't care if aria attributes are unspecified
return true
})
Expand Down
32 changes: 31 additions & 1 deletion src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,37 @@ function computeAriaSelected(element) {
if (element.tagName === 'OPTION') {
return element.selected
}
if ('checked' in element) {
return element.checked
}

// explicit value
const attributeValue =
element.getAttribute('aria-selected') ||
element.getAttribute('aria-checked')
if (attributeValue === 'true') {
return true
}
if (attributeValue === 'false') {
return false
}
return undefined
}

/**
* @param {Element} element -
* @returns {boolean | undefined} - false/true if (not)checked, undefined if not checked-able
*/
function computeAriaChecked(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-56
// https://www.w3.org/TR/html-aam-1.0/#details-id-67
if ('checked' in element) {
return element.checked
}

// explicit value
const attributeValue = element.getAttribute('aria-selected')
const attributeValue = element.getAttribute('aria-checked')
if (attributeValue === 'true') {
return true
}
Expand All @@ -204,4 +233,5 @@ export {
prettyRoles,
isInaccessible,
computeAriaSelected,
computeAriaChecked,
}
5 changes: 5 additions & 0 deletions types/queries.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export interface ByRoleOptions extends MatcherOptions {
* selected in the accessibility tree, i.e., `aria-selected="true"`
*/
selected?: boolean
/**
* If true only includes elements in the query set that are marked as
* checked in the accessibility tree, i.e., `aria-checked="true"`
*/
checked?: boolean
/**
* Includes every role used in the `role` attribute
* For example *ByRole('progressbar', {queryFallbacks: true})` will find <div role="meter progressbar">`.
Expand Down