Skip to content

Commit

Permalink
fix: Consider <label /> when computing the accessible name of `<out…
Browse files Browse the repository at this point in the history
…put />` (#666)
  • Loading branch information
eps1lon committed Jun 2, 2021
1 parent 93e1418 commit 26ee73d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
15 changes: 15 additions & 0 deletions .changeset/breezy-garlics-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"dom-accessibility-api": patch
---

Consider `<label />` when computing the accessible name of `<output />`

Given

```html
<label for="outputid">Output Label</label> <output id="outputid"></output>
```

Previously the accessible name of the `<output />` would ignore the `<label />`.
However, an [`<output />` is labelable](https://html.spec.whatwg.org/#the-output-element) and therefore the accessible name is now computed using `<label />` elements if they exists.
In this example the accessible name is `"Output Label"`.
10 changes: 10 additions & 0 deletions sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ describe("to upstream", () => {
testMarkup(markup, expectedAccessibleName)
);

test("output is labelable", () => {
const container = renderIntoDocument(`
<label for="outputid">Output Label</label>
<output id="outputid" data-test></output>
`);

const output = container.querySelector("output");
expect(output).toHaveAccessibleName("Output Label");
});

test.each([
[
// TODO
Expand Down
39 changes: 16 additions & 23 deletions sources/accessible-name-and-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ function getLabels(element: Element): HTMLLabelElement[] | null {
return ArrayFrom(labelsProperty);
}

// polyfill
if (!isLabelableElement(element)) {
return null;
}
Expand Down Expand Up @@ -492,29 +493,21 @@ export function computeTextAlternative(
}
}

if (
isHTMLInputElement(node) ||
isHTMLSelectElement(node) ||
isHTMLTextAreaElement(node)
) {
const input = node;

const labels = getLabels(input);
if (labels !== null && labels.length !== 0) {
consultedNodes.add(input);
return ArrayFrom(labels)
.map((element) => {
return computeTextAlternative(element, {
isEmbeddedInLabel: true,
isReferenced: false,
recursion: true,
});
})
.filter((label) => {
return label.length > 0;
})
.join(" ");
}
const labels = getLabels(node);
if (labels !== null && labels.length !== 0) {
consultedNodes.add(node);
return ArrayFrom(labels)
.map((element) => {
return computeTextAlternative(element, {
isEmbeddedInLabel: true,
isReferenced: false,
recursion: true,
});
})
.filter((label) => {
return label.length > 0;
})
.join(" ");
}

// https://w3c.github.io/html-aam/#input-type-image-accessible-name-computation
Expand Down

0 comments on commit 26ee73d

Please sign in to comment.