Skip to content

Commit 28e4b18

Browse files
committed
role-helpers
1 parent 62b98d8 commit 28e4b18

File tree

2 files changed

+78
-15
lines changed

2 files changed

+78
-15
lines changed

src/role-helpers.js renamed to src/role-helpers.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const elementRoleList = buildElementRoleList(elementRoles)
88
* @param {Element} element -
99
* @returns {boolean} - `true` if `element` and its subtree are inaccessible
1010
*/
11-
function isSubtreeInaccessible(element) {
11+
function isSubtreeInaccessible(element: HTMLElement): boolean {
1212
if (element.hidden === true) {
1313
return true
1414
}
@@ -25,6 +25,9 @@ function isSubtreeInaccessible(element) {
2525
return false
2626
}
2727

28+
interface IsInaccessibleOptions {
29+
isSubtreeInaccessible?: typeof isSubtreeInaccessible
30+
}
2831
/**
2932
* Partial implementation https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
3033
* which should only be used for elements with a non-presentational role i.e.
@@ -39,7 +42,10 @@ function isSubtreeInaccessible(element) {
3942
* can be used to return cached results from previous isSubtreeInaccessible calls
4043
* @returns {boolean} true if excluded, otherwise false
4144
*/
42-
function isInaccessible(element, options = {}) {
45+
function isInaccessible(
46+
element: HTMLElement,
47+
options: IsInaccessibleOptions = {},
48+
): boolean {
4349
const {
4450
isSubtreeInaccessible: isSubtreeInaccessibleImpl = isSubtreeInaccessible,
4551
} = options
@@ -118,8 +124,8 @@ function buildElementRoleList(elementRolesMap) {
118124
return result.sort(bySelectorSpecificity)
119125
}
120126

121-
function getRoles(container, {hidden = false} = {}) {
122-
function flattenDOM(node) {
127+
function getRoles(container, {hidden = false} = {}): Record<string, Element[]> {
128+
function flattenDOM(node: ParentNode) {
123129
return [
124130
node,
125131
...Array.from(node.children).reduce(
@@ -133,8 +139,8 @@ function getRoles(container, {hidden = false} = {}) {
133139
.filter(element => {
134140
return hidden === false ? isInaccessible(element) === false : true
135141
})
136-
.reduce((acc, node) => {
137-
let roles = []
142+
.reduce((acc, node: Element) => {
143+
let roles: Array<string> = []
138144
// TODO: This violates html-aria which does not allow any role on every element
139145
if (node.hasAttribute('role')) {
140146
roles = node.getAttribute('role').split(' ').slice(0, 1)
@@ -152,7 +158,7 @@ function getRoles(container, {hidden = false} = {}) {
152158
}, {})
153159
}
154160

155-
function prettyRoles(dom, {hidden}) {
161+
function prettyRoles(dom: HTMLElement, {hidden}) {
156162
const roles = getRoles(dom, {hidden})
157163

158164
return Object.entries(roles)
@@ -161,7 +167,7 @@ function prettyRoles(dom, {hidden}) {
161167
const elementsString = elements
162168
.map(el => {
163169
const nameString = `Name "${computeAccessibleName(el)}":\n`
164-
const domString = prettyDOM(el.cloneNode(false))
170+
const domString = prettyDOM(el.cloneNode(false) as Element)
165171
return `${nameString}${domString}`
166172
})
167173
.join('\n\n')
@@ -171,7 +177,7 @@ function prettyRoles(dom, {hidden}) {
171177
.join('\n')
172178
}
173179

174-
const logRoles = (dom, {hidden = false} = {}) =>
180+
const logRoles = (dom: HTMLElement, {hidden = false} = {}) =>
175181
console.log(prettyRoles(dom, {hidden}))
176182

177183
/**

types/role-helpers.d.ts

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,65 @@
1-
export function logRoles(container: HTMLElement): string
2-
export function getRoles(
3-
container: HTMLElement,
4-
): {[index: string]: HTMLElement[]}
51
/**
6-
* https://testing-library.com/docs/dom-testing-library/api-helpers#isinaccessible
2+
* @param {Element} element -
3+
* @returns {boolean} - `true` if `element` and its subtree are inaccessible
74
*/
8-
export function isInaccessible(element: Element): boolean
5+
declare function isSubtreeInaccessible(element: HTMLElement): boolean
6+
interface IsInaccessibleOptions {
7+
isSubtreeInaccessible?: typeof isSubtreeInaccessible
8+
}
9+
/**
10+
* Partial implementation https://www.w3.org/TR/wai-aria-1.2/#tree_exclusion
11+
* which should only be used for elements with a non-presentational role i.e.
12+
* `role="none"` and `role="presentation"` will not be excluded.
13+
*
14+
* Implements aria-hidden semantics (i.e. parent overrides child)
15+
* Ignores "Child Presentational: True" characteristics
16+
*
17+
* @param {Element} element -
18+
* @param {object} [options] -
19+
* @param {function (element: Element): boolean} options.isSubtreeInaccessible -
20+
* can be used to return cached results from previous isSubtreeInaccessible calls
21+
* @returns {boolean} true if excluded, otherwise false
22+
*/
23+
declare function isInaccessible(
24+
element: HTMLElement,
25+
options?: IsInaccessibleOptions,
26+
): boolean
27+
declare function getImplicitAriaRoles(currentNode: any): any[]
28+
declare function getRoles(
29+
container: any,
30+
{
31+
hidden,
32+
}?: {
33+
hidden?: boolean
34+
},
35+
): Record<string, Element[]>
36+
declare function prettyRoles(
37+
dom: HTMLElement,
38+
{
39+
hidden,
40+
}: {
41+
hidden: any
42+
},
43+
): string
44+
declare const logRoles: (
45+
dom: HTMLElement,
46+
{
47+
hidden,
48+
}?: {
49+
hidden?: boolean
50+
},
51+
) => void
52+
/**
53+
* @param {Element} element -
54+
* @returns {boolean | undefined} - false/true if (not)selected, undefined if not selectable
55+
*/
56+
declare function computeAriaSelected(element: any): any
57+
export {
58+
getRoles,
59+
logRoles,
60+
getImplicitAriaRoles,
61+
isSubtreeInaccessible,
62+
prettyRoles,
63+
isInaccessible,
64+
computeAriaSelected,
65+
}

0 commit comments

Comments
 (0)