Skip to content

fix: Various elements and their default roles #406

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@babel/runtime": "^7.6.2",
"@sheerun/mutationobserver-shim": "^0.3.2",
"@types/testing-library__dom": "^6.0.0",
"aria-query": "3.0.0",
"aria-query": "https://pkg.csb.dev/eps1lon/aria-query/commit/7dc0809b/aria-query",
"pretty-format": "^24.9.0",
"wait-for-expect": "^3.0.0"
},
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/__snapshots__/role-helpers.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,36 @@ textbox:
data-testid="a-textarea"
/>

--------------------------------------------------
combobox:

<input
data-testid="a-combobox-1"
list="a"
type="text"
/>

<select
data-testid="a-combobox-2"
/>

--------------------------------------------------
listbox:

<select
data-testid="a-listbox-1"
multiple=""
/>

<select
data-testid="a-listbox-2"
multiple=""
/>

<select
data-testid="a-listbox-3"
size="3"
/>

--------------------------------------------------"
`;
4 changes: 2 additions & 2 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,12 @@ test('queryAllByRole returns semantic html elements', () => {
expect(queryAllByRole(/heading/i)).toHaveLength(6)
expect(queryAllByRole('list')).toHaveLength(2)
expect(queryAllByRole(/listitem/i)).toHaveLength(3)
expect(queryAllByRole(/textbox/i)).toHaveLength(2)
expect(queryAllByRole(/textbox/i)).toHaveLength(1)
expect(queryAllByRole(/checkbox/i)).toHaveLength(1)
expect(queryAllByRole(/radio/i)).toHaveLength(1)
expect(queryAllByRole('row')).toHaveLength(3)
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(2)
expect(queryAllByRole(/img/i)).toHaveLength(1)
})

Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ function setup() {
<input type='radio' data-testid='a-radio-2' />
<input type='text' data-testid='a-input-1' />
<input type='text' data-testid='a-input-2' />
<!-- https://github.com/A11yance/aria-query/pull/34/files#r351310368 -->
<input type='text' list="" data-testid='a-input-3' />
<input type='text' list="a" data-testid='a-combobox-1' />
<textarea data-testid='a-textarea'></textarea>
<select data-testid='a-combobox-2'></select>
<select data-testid='a-listbox-1' multiple></select>
<select data-testid='a-listbox-2' multiple=""></select>
<select data-testid='a-listbox-3' size="3"></select>
</form>

<ul data-testid='b-list'>
Expand Down Expand Up @@ -93,7 +100,13 @@ function setup() {
radio2: getByTestId('a-radio-2'),
input: getByTestId('a-input-1'),
input2: getByTestId('a-input-2'),
input3: getByTestId('a-input-3'),
textarea: getByTestId('a-textarea'),
combobox1: getByTestId('a-combobox-1'),
combobox2: getByTestId('a-combobox-2'),
listbox1: getByTestId('a-listbox-1'),
listbox2: getByTestId('a-listbox-2'),
listbox3: getByTestId('a-listbox-3'),
}
}

Expand Down Expand Up @@ -126,6 +139,11 @@ test('getRoles returns expected roles for various dom nodes', () => {
input,
input2,
textarea,
combobox1,
combobox2,
listbox1,
listbox2,
listbox3,
} = setup()

expect(getRoles(section)).toEqual({
Expand All @@ -145,6 +163,8 @@ test('getRoles returns expected roles for various dom nodes', () => {
rowgroup: [tbody],
command: [menuItem, menuItem2],
menuitem: [menuItem, menuItem2],
combobox: [combobox1, combobox2],
listbox: [listbox1, listbox2, listbox3],
})
})

Expand Down
48 changes: 43 additions & 5 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,54 @@ function getImplicitAriaRoles(currentNode) {
}
}

// <form /> with an accessible name?
if (currentNode.matches('form')) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just restoring the old implementation that did not care about accessible name.

Technically <form> does not have the role="form" because the accessible name is missing (don't ask me why. I haven't tested it yet with AT; just following the spec). We could apply some heuristics to guess if it actually has one but if we want to be strict we would need to run accessible name computation (expensive).

Assuming I finish byRole('form', { name: 'some-name' }) we could leave this quirk and then ignore any element returned getImplicitAriaRoles if the accessible name is empty (and potentially warn if a form is queried without providing a name).

return ['form']
}

// <section /> with an accessible name?
if (currentNode.matches('section')) {
return ['region']
}

return []
}

function buildElementRoleList(elementRolesMap) {
function makeElementSelector({name, attributes = []}) {
return `${name}${attributes
.map(({name: attributeName, value}) =>
value ? `[${attributeName}=${value}]` : `[${attributeName}]`,
)
.join('')}`
const inclusiveAttributeSelectors = []
const exclusiveAttributeSelectors = []
attributes.forEach(({name: attributeName, constraints = [], value}) => {
const selector = value
? `[${attributeName}="${value}"]`
: `[${attributeName}]`

const shouldBeUndefined = constraints.indexOf('undefined') !== -1
const shouldBeGreaterOne = constraints.indexOf('>1') !== -1
const shouldHaveAnyValue = constraints.indexOf('set') !== -1

if (shouldHaveAnyValue) {
inclusiveAttributeSelectors.push(selector)
exclusiveAttributeSelectors.push(`[${attributeName}=""]`)
} else if (shouldBeGreaterOne) {
exclusiveAttributeSelectors.push(`[${attributeName}^="-"]`)
exclusiveAttributeSelectors.push(`[${attributeName}="0"]`)
exclusiveAttributeSelectors.push(`[${attributeName}="1"]`)
} else if (shouldBeUndefined) {
exclusiveAttributeSelectors.push(selector)
} else {
inclusiveAttributeSelectors.push(selector)
}
})

const inclusiveAttributeSelector = inclusiveAttributeSelectors.join('')
const exclusiveAttributeSelector = exclusiveAttributeSelectors.join('')

return `${name}${inclusiveAttributeSelector}${
exclusiveAttributeSelector === ''
? ''
: `:not(${exclusiveAttributeSelector})`
}`
}

function getSelectorSpecificity({attributes = []}) {
Expand Down