Skip to content

Commit 5b0f48e

Browse files
authored
fix: Prefer button subtree over title attribute (#811)
1 parent a0a0d81 commit 5b0f48e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.changeset/twelve-icons-dance.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"dom-accessibility-api": patch
3+
---
4+
5+
Prefer button subtree over `title` attribute.
6+
7+
```diff
8+
const name = computeAccessibleName(<button title="from-title">from-content</button>);
9+
-'from-title' === name
10+
+'from-content' === name
11+
```
12+
13+
`<button title="from-title">from-content</button>` would previously compute the accessible name "from-title".
14+
This is correct in ACCNAME 1.2 but is changed in the latest editors draft.
15+
The latest editors draft specifically refers to HTML-AAM which says that the subtree should take precedent over the `title` attribute.
16+
`computeAccessibleName` now calculates "from-content" as the accessible name.

sources/__tests__/accessible-name.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ test.each([
387387
// https://www.w3.org/TR/svg-aam-1.0/
388388
[`<svg data-test><title><em>greek</em> rho</title></svg>`, "greek rho"],
389389
[`<button title="" data-test>click me</button>`, "click me"],
390+
[
391+
`<button title="You should really click this" data-test>click me</button>`,
392+
"click me",
393+
],
390394
// https://w3c.github.io/html-aam/#input-type-button-input-type-submit-and-input-type-reset-accessible-name-computation
391395
[`<input data-test value="Submit form" type="submit" />`, "Submit form"],
392396
// https://w3c.github.io/html-aam/#input-type-image

sources/accessible-name-and-description.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export function computeTextAlternative(
398398
accumulatedText = `${accumulatedText} ${afterContent}`;
399399
}
400400

401-
return accumulatedText;
401+
return accumulatedText.trim();
402402
}
403403

404404
function computeElementTextAlternative(node: Node): string | null {
@@ -538,6 +538,18 @@ export function computeTextAlternative(
538538
return "Submit Query";
539539
}
540540

541+
if (hasAnyConcreteRoles(node, ["button"])) {
542+
// https://www.w3.org/TR/html-aam-1.0/#button-element
543+
const nameFromSubTree = computeMiscTextAlternative(node, {
544+
isEmbeddedInLabel: false,
545+
isReferenced: false,
546+
});
547+
if (nameFromSubTree !== "") {
548+
return nameFromSubTree;
549+
}
550+
return useAttribute(node, "title");
551+
}
552+
541553
return useAttribute(node, "title");
542554
}
543555

0 commit comments

Comments
 (0)