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: Consider <label /> when computing the accessible name of <output /> #666

Merged
merged 4 commits into from
Jun 2, 2021
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
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)
Comment on lines -496 to -498
Copy link
Owner Author

Choose a reason for hiding this comment

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

This was an incomplete check whether the node is labelable. However, getLabels already has this check baked in.

) {
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