Skip to content

Commit

Permalink
fix: Use label attribute for naming of <optgroup> (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed May 3, 2021
1 parent 0485441 commit 383bdb6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .changeset/pretty-tigers-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"dom-accessibility-api": patch
---

Use label attribute for naming of `<optgroup>` elements.

Given

```jsx
<select>
<optgroup label="foo">
<option value="1">bar</option>
</optgroup>
</select>
```

Previously the `<optgroup />` would not have an accessible name.
Though [2D in `accname` 1.2](https://www.w3.org/TR/accname-1.2/) could be interpreted to use the `label` attribute:

> 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 [...]
This was confirmed in NVDA + FireFox.
5 changes: 5 additions & 0 deletions sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ describe("to upstream", () => {
`<li data-test role="menuitemcheckbox"><em>greek</em> iota</li>`,
"greek iota",
],
[
"optgroup",
`<select><optgroup data-test label="foo"><option value="1">baz</option></optgroup></select>`,
"foo",
],
[
"radio",
`<div data-test role="radio"><em>greek</em> kappa</div>`,
Expand Down
6 changes: 6 additions & 0 deletions sources/accessible-name-and-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
safeWindow,
isHTMLFieldSetElement,
isHTMLLegendElement,
isHTMLOptGroupElement,
isHTMLTableElement,
isHTMLSlotElement,
isSVGSVGElement,
Expand Down Expand Up @@ -462,6 +463,11 @@ export function computeTextAlternative(
if (nameFromAlt !== null) {
return nameFromAlt;
}
} else if (isHTMLOptGroupElement(node)) {
const nameFromLabel = useAttribute(node, "label");
if (nameFromLabel !== null) {
return nameFromLabel;
}
}

if (
Expand Down
6 changes: 6 additions & 0 deletions sources/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export function isHTMLInputElement(
return isElement(node) && getLocalName(node) === "input";
}

export function isHTMLOptGroupElement(
node: Node | null
): node is HTMLOptGroupElement {
return isElement(node) && getLocalName(node) === "optgroup";
}

export function isHTMLSelectElement(
node: Node | null
): node is HTMLSelectElement {
Expand Down

0 comments on commit 383bdb6

Please sign in to comment.