-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add computeAccessibleDescription (#210)
- Loading branch information
Showing
14 changed files
with
800 additions
and
611 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
"dom-accessibility-api": patch | ||
--- | ||
|
||
Implement accessbile description computation | ||
|
||
```ts | ||
import { computeAccessibleDescription } from "dom-accessibility-api"; | ||
|
||
const description = computeAccessibleDescription(element); | ||
``` | ||
|
||
Warning: It always considers `title` attributes if the description is empty. | ||
Even if the `title` attribute was already used for the accessible name. | ||
This is fails a web-platform-test. | ||
The other failing test is due to `aria-label` being ignored for the description which is correct by spec. | ||
It's likely an issue with wpt. | ||
The other tests are passing (13/15). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { computeAccessibleDescription } from "../accessible-description"; | ||
import { renderIntoDocument } from "./helpers/test-utils"; | ||
import { prettyDOM } from "@testing-library/dom"; | ||
import diff from "jest-diff"; | ||
|
||
expect.extend({ | ||
toHaveAccessibleDescription(received, expected) { | ||
if (received == null) { | ||
return { | ||
message: () => | ||
`The element was not an Element but '${String(received)}'`, | ||
pass: false, | ||
}; | ||
} | ||
|
||
const actual = computeAccessibleDescription(received); | ||
if (actual !== expected) { | ||
return { | ||
message: () => | ||
`expected ${prettyDOM( | ||
received | ||
)} to have accessible description '${expected}' but got '${actual}'\n${diff( | ||
expected, | ||
actual | ||
)}`, | ||
pass: false, | ||
}; | ||
} | ||
|
||
return { | ||
message: () => | ||
`expected ${prettyDOM( | ||
received | ||
)} not to have accessible description '${expected}'\n${diff( | ||
expected, | ||
actual | ||
)}`, | ||
pass: true, | ||
}; | ||
}, | ||
}); | ||
|
||
function testMarkup(markup, accessibleDescription) { | ||
const container = renderIntoDocument(markup); | ||
|
||
const testNode = container.querySelector("[data-test]"); | ||
expect(testNode).toHaveAccessibleDescription(accessibleDescription); | ||
} | ||
|
||
describe("wpt copies", () => { | ||
test.each([ | ||
[ | ||
`<img src="foo.jpg" data-test alt="test" aria-describedby="t1"><span id="t1" role="presentation">foo</span>`, | ||
"foo", | ||
], | ||
[ | ||
`<a data-test href="#" aria-label="California" title="San Francisco" >United States</a>`, | ||
"San Francisco", | ||
], | ||
])(`#%#`, (markup, expectedAccessibleName) => | ||
testMarkup(markup, expectedAccessibleName) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
computeTextAlternative, | ||
ComputeTextAlternativeOptions, | ||
} from "./accessible-name-and-description"; | ||
import { queryIdRefs } from "./util"; | ||
|
||
/** | ||
* implements https://w3c.github.io/accname/#mapping_additional_nd_description | ||
* @param root | ||
* @param [options] | ||
* @parma [options.getComputedStyle] - mock window.getComputedStyle. Needs `content`, `display` and `visibility` | ||
*/ | ||
export function computeAccessibleDescription( | ||
root: Element, | ||
options: ComputeTextAlternativeOptions = {} | ||
): string { | ||
let description = queryIdRefs(root, "aria-describedby") | ||
.map((element) => { | ||
return computeTextAlternative(element, { | ||
...options, | ||
compute: "description", | ||
}); | ||
}) | ||
.join(" "); | ||
|
||
// TODO: Technically we need to make sure that node wasn't used for the accessible name | ||
// This causes `description_1.0_combobox-focusable-manual` to fail | ||
// | ||
// https://www.w3.org/TR/html-aam-1.0/#accessible-name-and-description-computation | ||
// says for so many elements to use the `title` that we assume all elements are considered | ||
if (description === "") { | ||
const title = root.getAttribute("title"); | ||
description = title === null ? "" : title; | ||
} | ||
|
||
return description; | ||
} |
Oops, something went wrong.