Skip to content

Commit

Permalink
fix: Legend element as accessible name for Fielset element (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardozv28 committed May 16, 2020
1 parent 1bed102 commit 737dfae
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .changeset/pink-lizards-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"dom-accessibility-api": patch
---

Consider <legend> for the name of its <fieldset> element.

```html
<fieldset>
<legend><em>my</em> fieldset</legend>
</fieldset>
```

Computing the name for this fieldset would've returned an emptry string previously. It now correctly computes `my fieldset` following the [accessible name computation for `fieldset` elements](https://w3c.github.io/html-aam/#fieldset-and-legend-elements)
14 changes: 12 additions & 2 deletions sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function testMarkup(markup, accessibleName) {
}

describe("to upstream", () => {
// name from content
test.each([
// name from content
[
"cell",
`<div data-test role="cell"><em>greek</em> alpha</div>`,
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("to upstream", () => {
`<li data-test role="treeitem"><em>greek</em> pi</li>`,
"greek pi",
],
])(`role %s`, (_, markup, expectedAccessibleName) =>
])(`role %s has name from content`, (_, markup, expectedAccessibleName) =>
testMarkup(markup, expectedAccessibleName)
);

Expand Down Expand Up @@ -189,6 +189,7 @@ describe("to upstream", () => {
});
});

// misc tests
test.each([
[
`
Expand Down Expand Up @@ -281,6 +282,15 @@ test.each([
`<label for="textarea">A TextArea</label><textarea data-test id="textarea" />`,
"A TextArea",
],
// https://w3c.github.io/html-aam/#fieldset-and-legend-elements
[
`<fieldset data-test><legend><em>greek</em> rho</legend></fieldset>`,
"greek rho",
],
[
`<fieldset data-test aria-owns="legend"></fieldset><legend id="legend"><em>greek</em> rho</legend>`,
"",
],
])(`test #%#`, testMarkup);

describe("prohibited naming", () => {
Expand Down
17 changes: 17 additions & 0 deletions sources/accessible-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
isHTMLSelectElement,
isHTMLTextAreaElement,
safeWindow,
isHTMLFieldSetElement,
isHTMLLegendElement,
} from "./util";

/**
Expand Down Expand Up @@ -362,6 +364,21 @@ export function computeAccessibleName(
}

function computeElementTextAlternative(node: Node): string | null {
// https://w3c.github.io/html-aam/#fieldset-and-legend-elements
if (isHTMLFieldSetElement(node)) {
consultedNodes.add(node);
for (const child of ArrayFrom(node.childNodes)) {
if (isHTMLLegendElement(child)) {
return computeTextAlternative(child, {
isEmbeddedInLabel: false,
isReferenced: false,
recursion: false,
});
}
}
return null;
}

if (
!(
isHTMLInputElement(node) ||
Expand Down
20 changes: 20 additions & 0 deletions sources/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ export function safeWindow(node: Node): Window {
}
return defaultView;
}

export function isHTMLFieldSetElement(
node: Node | null
): node is HTMLFieldSetElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLFieldSetElement
);
}

export function isHTMLLegendElement(
node: Node | null
): node is HTMLLegendElement {
return (
isElement(node) &&
// @ts-ignore
node instanceof node.ownerDocument.defaultView.HTMLLegendElement
);
}

0 comments on commit 737dfae

Please sign in to comment.