Skip to content
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

fix: role for section element with no accessible name #28

Merged
merged 1 commit into from
Jan 25, 2025
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
5 changes: 5 additions & 0 deletions .changeset/cold-starfishes-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"html-aria": patch
---

Fix role for section element with no accessible name
4 changes: 4 additions & 0 deletions src/get-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export function getRole(element: VirtualElement | HTMLElement, options?: GetRole
case 'footer': {
return getFooterRole(options);
}
case 'section': {
const name = calculateAccessibleName({ tagName, attributes });
return name ? tag.defaultRole : 'generic';
}
case 'select': {
return getSelectRole({ attributes });
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ export function calculateAccessibleName(element: VirtualElement): string | undef

switch (tagName) {
case 'img': {
// according to spec, aria-label is technically allowed for <img> (even if alt is preferred)
/**
* According to spec, aria-label is technically allowed for <img> (even if alt is preferred)
* @see https://www.w3.org/TR/html-aam-1.0/#img-element-accessible-name-computation
*/
return (attributes?.alt || attributes?.['aria-label'] || attributes?.['aria-labelledby']) as string;
}
case 'section': {
/**
* @see https://www.w3.org/TR/html-aam-1.0/#section-and-grouping-element-accessible-name-computation
*/
return (attributes?.['aria-label'] || attributes?.['aria-labelledby']) as string;
}
}
}

Expand Down
17 changes: 15 additions & 2 deletions test/get-role.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ describe('getRole', () => {
['html', { given: [{ tagName: 'html' }], want: 'document' }],
['i', { given: [{ tagName: 'i' }], want: 'generic' }],
['iframe', { given: [{ tagName: 'iframe' }], want: undefined }],
['img (name)', { given: [{ tagName: 'img', attributes: { alt: 'My image' } }], want: 'img' }],
['img (named by alt)', { given: [{ tagName: 'img', attributes: { alt: 'My image' } }], want: 'img' }],
['img (named by label)', { given: [{ tagName: 'img', attributes: { 'aria-label': 'My image' } }], want: 'img' }],
[
'img (named by labelledby)',
{ given: [{ tagName: 'img', attributes: { 'aria-labelledby': 'My image' } }], want: 'img' },
],
['img (no name)', { given: [{ tagName: 'img' }], want: 'none' }],
['input', { given: [{ tagName: 'input' }], want: 'textbox' }],
['input[type=button]', { given: [{ tagName: 'input', attributes: { type: 'button' } }], want: 'button' }],
Expand Down Expand Up @@ -226,7 +231,15 @@ describe('getRole', () => {
['samp', { given: [{ tagName: 'samp' }], want: 'generic' }],
['script', { given: [{ tagName: 'script' }], want: undefined }],
['search', { given: [{ tagName: 'search' }], want: 'search' }],
['section', { given: [{ tagName: 'section' }], want: 'region' }],
[
'section (named by label)',
{ given: [{ tagName: 'section', attributes: { 'aria-label': 'My section' } }], want: 'region' },
],
[
'section (named by labelledby)',
{ given: [{ tagName: 'section', attributes: { 'aria-labelledby': 'My section' } }], want: 'region' },
],
['section (no name)', { given: [{ tagName: 'section' }], want: 'generic' }],
['select', { given: [{ tagName: 'select' }], want: 'combobox' }],
['select[size=0]', { given: [{ tagName: 'select', attributes: { size: 0 } }], want: 'combobox' }],
['select[size=1]', { given: [{ tagName: 'select', attributes: { size: 1 } }], want: 'combobox' }],
Expand Down
13 changes: 10 additions & 3 deletions test/get-supported-attributes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ const tests: [
['html', { given: [{ tagName: 'html' }], want: NO_ATTRIBUTES }],
['i', { given: [{ tagName: 'i' }], want: removeProhibited(GLOBAL_ATTRIBUTES, { nameProhibited: true }) }],
['iframe', { given: [{ tagName: 'iframe' }], want: GLOBAL_ATTRIBUTES }],
['img', { given: [{ tagName: 'img' }], want: ['aria-hidden'] }],
[
'img',
'img (name)',
{
given: [{ tagName: 'img', attributes: { alt: 'Alt text' } }],
want: [...GLOBAL_ATTRIBUTES, ...roles.img.supported],
},
],
['img (no name)', { given: [{ tagName: 'img' }], want: ['aria-hidden'] }],
['input[type=button]', { given: [{ tagName: 'input', attributes: { type: 'button' } }], want: BUTTON_ATTRIBUTES }],
[
'input[type=checkbox]',
Expand Down Expand Up @@ -411,7 +411,14 @@ const tests: [
['samp', { given: [{ tagName: 'samp' }], want: GENERIC_NO_NAMING }],
['script', { given: [{ tagName: 'script' }], want: NO_ATTRIBUTES }],
['search', { given: [{ tagName: 'search' }], want: [...GLOBAL_ATTRIBUTES, ...roles.search.supported] }],
['section', { given: [{ tagName: 'section' }], want: [...GLOBAL_ATTRIBUTES, ...roles.region.supported] }],
[
'section (name)',
{
given: [{ tagName: 'section', attributes: { 'aria-label': 'My section' } }],
want: [...GLOBAL_ATTRIBUTES, ...roles.region.supported],
},
],
['section (no name)', { given: [{ tagName: 'section' }], want: GENERIC_NO_NAMING }],
['select', { given: [{ tagName: 'select' }], want: COMBOBOX_ATTRIBUTES }],
['select[size=0]', { given: [{ tagName: 'select', attributes: { size: 0 } }], want: COMBOBOX_ATTRIBUTES }],
['select[size=1]', { given: [{ tagName: 'select', attributes: { size: 1 } }], want: COMBOBOX_ATTRIBUTES }],
Expand Down
Loading