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

Aria required children #38

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
127 changes: 127 additions & 0 deletions src/rules/aria-required-children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { AccessibilityError } from "../scanner";
import { querySelectorAll } from "../utils";

// TODO: This list is incomplete!
type Role =
| "article"
| "cell"
| "checkbox"
| "columnheader"
| "combobox"
| "deletion"
| "feed"
| "grid"
| "gridcell"
| "group"
| "heading"
| "insertion"
| "list"
| "listbox"
| "listitem"
| "menu"
| "menubar"
| "menuitem"
| "menuitemcheckbox"
| "menuitemradio"
| "meter"
| "option"
| "radio"
| "row"
| "rowgroup"
| "rowheader"
| "scrollbar"
| "separator"
| "seperator"
| "slider"
| "suggestion"
| "switch"
| "tab"
| "table"
| "tablist"
| "tree"
| "treegrid"
| "treeitem";

/**
* Required States and Properties:
*
* @see https://w3c.github.io/aria/#authorErrorDefaultValuesTable
*/
const roleToRequiredChildRoleMapping: Partial<Record<Role, Role[]>> = {
feed: ["article"],
grid: ["rowgroup", "row"],
list: ["listitem"],
listbox: ["group", "option"],
menu: [
"group",
"menuitemradio",
"menuitem",
"menuitemcheckbox",
"menu",
"separator",
],
menubar: [
"group",
"menuitemradio",
"menuitem",
"menuitemcheckbox",
"menu",
"separator",
],
row: ["cell", "columnheader", "gridcell", "rowheader"],
rowgroup: ["row"],
suggestion: ["insertion", "deletion"],
table: ["rowgroup", "row"],
tablist: ["tab"],
tree: ["group", "treeitem"],
treegrid: ["rowgroup", "row"],
};

export const references = {
act: {
id: "ff89c9",
text: "ARIA required context role",
url: "https://act-rules.github.io/rules/ff89c9",
},
axe: {
id: "aria-required-children",
text: "Certain ARIA roles must contain particular children",
url: `https://dequeuniversity.com/rules/axe/4.4/aria-required-children?application=RuleDescription`,
},
};

export function ariaRequiredChildren(el: Element): AccessibilityError[] {
const errors = [];

// Loop over all the different rules.
for (const [role, requiredChildren] of Object.entries(
roleToRequiredChildRoleMapping,
)) {
// Find all the elements with a role that we are interested in.
for (const parent of querySelectorAll(`[role=${role}]`, el)) {
let isValid = false;

// Look for children of the parents with the correct roles.
// TODO: Probably special case `aria-owns` as that allows you to not have
// the items as descendants.
const childSelector = requiredChildren.reduce((selector, childRole) => {
if (!selector) return `[role=${childRole}]`;
return `${selector},[role=${childRole}]`;
}, "");
for (const child of querySelectorAll(childSelector, parent)) {
if (!child) continue;
// TODO: Check if child is valid somehow
isValid = true;
}

if (isValid) continue;
errors.push({
element: parent,
text: references.axe.text,
url: references.axe.text,
});
}
}

return errors;
}
2 changes: 2 additions & 0 deletions src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import label from "./rules/label";
import linkName from "./rules/link-name";
import nestedInteractive from "./rules/nested-interactive";
import validLang from "./rules/valid-lang";
import { ariaRequiredChildren } from "./rules/aria-required-children";

import { Logger } from "./logger";

Expand Down Expand Up @@ -38,6 +39,7 @@ export const allRules: Rule[] = [
linkName,
nestedInteractive,
validLang,
ariaRequiredChildren,
];

export async function requestIdleScan(
Expand Down
5 changes: 4 additions & 1 deletion tests/act/act.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ const rulesToIgnore = [
"efbfc7",
"f51b46",
"fd3a94",
"ff89c9",
"ffbc54",
"ffd0e9",
"m6b1q3",
Expand All @@ -117,6 +116,7 @@ const rulesToIgnore = [
"ucwvc8",
"ye5d6e",
"bf051a",
"ff89c9",
];

const ignoredExamples = [
Expand All @@ -126,6 +126,9 @@ const ignoredExamples = [
"https://act-rules.github.io/testcases/qt1vmo/0ef4f516db9ed70cb25f39c99637272808b8e60f.html",
];

// TODO: Instead of dynamic tests which behave weird in Web Test Runner, we
// should generate the tests from the HTML testcases. It would be great if it's
// easy to regenerate so we don't accidentally change the tests.
describe("ACT Rules", function () {
for (const rule of applicableRules) {
const {
Expand Down
202 changes: 202 additions & 0 deletions tests/aria-required-children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { fixture, expect } from "@open-wc/testing";
import { Scanner } from "../src/scanner";
import { ariaRequiredChildren } from "../src/rules/aria-required-children";

const scanner = new Scanner([ariaRequiredChildren]);

// Just to get prettier working ;)
const html = String.raw;

const passes = [
html`<div role="list" id="pass1">
<div role="listitem" id="ignore1">Item 1</div>
</div>`,
html`<div>
<div role="list" id="pass2" aria-owns="ignore2"></div>
<div role="listitem" id="ignore2"></div>
</div>`,
html` <div>
<div role="list" id="pass3" aria-owns="parent"></div>
<div id="parent">
<div role="listitem" id="ignore5"></div>
</div>
</div>`,
html` <div role="grid" id="pass4">
<div role="row" id="fail5">
<span>Item 1</span>
</div>
</div>`,
html`<div role="grid" id="pass5">
<div role="presentation" id="ignore8">
<div role="row" id="pass6">
<div role="none" id="ingore9">
<span role="cell" id="ignore10">Item 1</span>
</div>
</div>
</div>
</div>`,
html`<div role="menu" id="pass7">
<ul role="group" id="ignore11">
<li role="menuitem" id="ignore12">>Inbox</li>
<li role="separator" id="ignored13"></li>
<li role="menuitem" id="ignore14">>Archive</li>
<li role="menuitem" id="ignore15">>Trash</li>
</ul>
</div>`,
html`<div role="suggestion" id="pass8">
<span role="deletion">option</span>
<span role="insertion">option</span>
</div>`,
html`<div role="listbox" id="pass9">
<div role="group">
<div role="option">option</div>
</div>
</div>`,
html`<div role="list" id="pass10" aria-busy="true"></div>`,
html`<div role="suggestion" id="pass11" aria-busy="true"></div>`,
html`<div role="menu" id="pass12" aria-busy="true"></div>`,
html`<div role="listbox" id="pass13" aria-busy="true"></div>`,
html`<div role="list" id="pass14">
<script></script>
<style></style>
<canvas hidden></canvas>
<div><div role="listitem">Item 1</div></div>
<div role="generic"><div role="listitem">item 2</div></div>
<div role="presentation"><div role="listitem">item 2</div></div>
<div role="none"><div role="listitem">item 2</div></div>
</div>`,
html`<ul role="menu" id="pass15">
<li role="none">
<a href="#" role="menuitem">Foo</a>
<ul role="menu" id="pass16">
<li role="none">
<a href="#" role="menuitem">Bar</a>
</li>
</ul>
</li>
</ul>`,
html`<nav role="menubar" id="pass17">
<a role="menuitem" href="">Item 1</a>
<a role="menuitem" href="">Item 2</a>
<span role="separator"></span>
<a role="menuitem" href="">Item 3</a>
</nav>`,
html`<div role="menu" id="pass18">
<div role="menuitem">menu item</div>
<div aria-hidden="true">shouldn't be flagged but is</div>
</div>`,
html`<div role="list" id="pass19">
<div role="presentation">
<div style="display: none">ignore</div>
<div style="visibility: hidden" aria-hidden="true">ignore</div>
<li>item 1</li>
<li>item 2</li>
</div>
</div>`,
];

const violations = [
html`<div role="list" id="fail1">
<div role="menuitem" id="ignore3"></div>
</div>`,
html`<div role="list" id="fail2" aria-owns="ingore4"></div>`,
html`<div role="row" id="fail3"></div>`,
html`<div role="menuitem" id="ingore4"></div>`,
html`<div role="list" id="fail4">
<div role="tabpanel" id="ignore6">
<div role="listitem" id="ignore7">List item 1</div>
</div>
</div>`,
html`<div role="grid" id="pass4">
<div role="row" id="fail5">
<span>Item 1</span>
</div>
</div>`,
html`<div role="suggestion" id="fail6"></div>`,
html`<div role="suggestion" id="fail7">
<div aria-busy="true"></div>
</div>`,
html`<div role="suggestion" id="fail8" aria-busy="false"></div>`,
html`<div role="list" id="fail9">
<li>Item 1</li>
<span role="link">Item 2</span>
</div>`,
html`<div role="list" id="fail10">
<div aria-live="polite">
<div role="listitem">List item 1</div>
<div role="listitem">List item 2</div>
</div>
</div>`,
html`<div role="list" id="fail11">
<div tabindex="0">
<div role="listitem">List item 1</div>
<div role="listitem">List item 2</div>
</div>
</div>`,
html`<div role="list" id="fail12">
<div aria-busy="true"></div>
</div>`,
html`<div role="list" id="fail13" aria-busy="true">
<div role="alert">unallowed role</div>
</div>`,
];

const incomplete = [
`<div role="grid" id="incomplete1"></div>`,
`<div role="list" id="incomplete2"></div>`,
`<div role="listbox" id="incomplete3"></div>`,
`<div role="table" id="incomplete4"></div>`,
`<div role="tablist" id="incomplete5"></div>`,
`<div role="tree" id="incomplete6"></div>`,
`<div role="treegrid" id="incomplete7"></div>`,
`<div role="rowgroup" id="incomplete8"></div>`,
` <div role="listbox" id="incomplete9">
<div></div>
</div>`,
`<div role="menu" id="incomplete10">
<!-- -->
<span role="menuitem" hidden></span>
<span role="none">
<span role="menuitem" aria-hidden="true">hidden</span>
</span>
</div>`,
'<div role="menubar" id="incomplete11"></div>',
];

const inapplicable = [
`<div role="doc-bibliography" id="inapplicable1"></div>`,
`<div role="doc-endnotes" id="inapplicable2"></div>`,
`<div role="radiogroup" id="inapplicable3">
<div role="heading" aria-level="2">Heading</div>
<ul>
<li><div role="radio"></div></li>
<li><div role="radio"></div></li>
</ul>
</div>`,
];

describe("aria-required-attr", async function () {
for (const markup of passes) {
const el = await fixture(markup);
it(el.outerHTML, async () => {
const results = (await scanner.scan(el)).map(({ text, url }) => {
return { text, url };
});
expect(results).to.be.empty;
});
}
for (const markup of violations) {
const el = await fixture(markup);
it(el.outerHTML, async () => {
const results = (await scanner.scan(el)).map(({ text, url }) => {
return { text, url };
});
expect(results).to.eql([
{
text: "TODO",
url: "TODO",
},
]);
});
}
});
Loading
Loading