Skip to content

Commit 383bdb6

Browse files
authored
fix: Use label attribute for naming of <optgroup> (#629)
1 parent 0485441 commit 383bdb6

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

.changeset/pretty-tigers-brake.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
"dom-accessibility-api": patch
3+
---
4+
5+
Use label attribute for naming of `<optgroup>` elements.
6+
7+
Given
8+
9+
```jsx
10+
<select>
11+
<optgroup label="foo">
12+
<option value="1">bar</option>
13+
</optgroup>
14+
</select>
15+
```
16+
17+
Previously the `<optgroup />` would not have an accessible name.
18+
Though [2D in `accname` 1.2](https://www.w3.org/TR/accname-1.2/) could be interpreted to use the `label` attribute:
19+
20+
> Otherwise, if the current node's native markup provides an attribute (e.g. title) or element (e.g. HTML label) that defines a text alternative, return that alternative [...]
21+
22+
This was confirmed in NVDA + FireFox.

sources/__tests__/accessible-name.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ describe("to upstream", () => {
101101
`<li data-test role="menuitemcheckbox"><em>greek</em> iota</li>`,
102102
"greek iota",
103103
],
104+
[
105+
"optgroup",
106+
`<select><optgroup data-test label="foo"><option value="1">baz</option></optgroup></select>`,
107+
"foo",
108+
],
104109
[
105110
"radio",
106111
`<div data-test role="radio"><em>greek</em> kappa</div>`,

sources/accessible-name-and-description.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
safeWindow,
1414
isHTMLFieldSetElement,
1515
isHTMLLegendElement,
16+
isHTMLOptGroupElement,
1617
isHTMLTableElement,
1718
isHTMLSlotElement,
1819
isSVGSVGElement,
@@ -462,6 +463,11 @@ export function computeTextAlternative(
462463
if (nameFromAlt !== null) {
463464
return nameFromAlt;
464465
}
466+
} else if (isHTMLOptGroupElement(node)) {
467+
const nameFromLabel = useAttribute(node, "label");
468+
if (nameFromLabel !== null) {
469+
return nameFromLabel;
470+
}
465471
}
466472

467473
if (

sources/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export function isHTMLInputElement(
2929
return isElement(node) && getLocalName(node) === "input";
3030
}
3131

32+
export function isHTMLOptGroupElement(
33+
node: Node | null
34+
): node is HTMLOptGroupElement {
35+
return isElement(node) && getLocalName(node) === "optgroup";
36+
}
37+
3238
export function isHTMLSelectElement(
3339
node: Node | null
3440
): node is HTMLSelectElement {

0 commit comments

Comments
 (0)