Skip to content

Commit

Permalink
fix: img with missing alt should have role img (#430)
Browse files Browse the repository at this point in the history
Co-authored-by: Cameron Cundiff <cundiff@adobe.com>
Co-authored-by: Sebastian Silbermann <silbermann.sebastian@gmail.com>
  • Loading branch information
3 people authored Sep 21, 2020
1 parent c9e8dc7 commit 76e8f93
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .changeset/light-ladybugs-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"dom-accessibility-api": patch
---

Maintain `img` role for `img` with missing `alt` attribute.

Previously `<img />` would be treated the same as `<img alt />`.
`<img />` is now treated as `role="img"` [as specified](https://w3c.github.io/html-aam/#el-img-empty-alt).
1 change: 1 addition & 0 deletions sources/__tests__/getRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const cases = [
["html", null, createElementFactory("html", {})],
["iframe", null, createElementFactory("iframe", {})],
["img with alt=\"some text\"", "img", createElementFactory("img", {alt: "text"})],
["img with missing alt", "img", createElementFactory("img", {})],
["img with alt=\"\"", null, createElementFactory("img", {alt: ""})],
["input type=button", "button", createElementFactory("input", {type: "button"})],
["input type=checkbox", "checkbox", createElementFactory("input", {type: "checkbox"})],
Expand Down
6 changes: 4 additions & 2 deletions sources/getRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ function getImplicitRole(element: Element): string | null {
return "link";
}
break;
case "img":
if ((element.getAttribute("alt") || "").length > 0) {
case "img": {
const alt: string | null = element.getAttribute("alt");
if (alt === null || alt.length > 0) {
return "img";
}
break;
}
case "input": {
const { type } = element as HTMLInputElement;
switch (type) {
Expand Down

0 comments on commit 76e8f93

Please sign in to comment.