@a11y-pulse/orphaned-tab-roles-audit 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -4
- package/dist/browser-scripts.d.ts +23 -1
- package/dist/classify.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +91 -18
- package/dist/index.js.map +1 -1
- package/dist/result.d.ts +10 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -37,7 +37,9 @@ console.log(result.failed[0]);
|
|
|
37
37
|
// selector: "#nav-dogs",
|
|
38
38
|
// html: '<a id="nav-dogs" role="tab" href="/dogs">',
|
|
39
39
|
// role: "tab",
|
|
40
|
-
// finding: "tab-used-as-link"
|
|
40
|
+
// finding: "tab-used-as-link",
|
|
41
|
+
// tablist: "#site-nav",
|
|
42
|
+
// href: "https://who.likesdogs.nz/dogs"
|
|
41
43
|
// }
|
|
42
44
|
|
|
43
45
|
await browser.close();
|
|
@@ -54,8 +56,9 @@ For each tab, the first rule that applies:
|
|
|
54
56
|
| none | `passed` | The tab is associated with a tab panel |
|
|
55
57
|
| `tab-used-as-link` | `failed` | The tab is, or sits inside, a link to another document. Fragment links (`#panel`), same-page URLs and `javascript:` links do not count |
|
|
56
58
|
| `controls-not-a-tabpanel` | `needsReview` | `aria-controls` points at an element that is not a tab panel |
|
|
59
|
+
| none | `passed` | The tab is not selected, the selected tab in the same tablist is associated with exactly one tab panel that belongs to it alone, and no tab panel on the page is left without a tab. This is how libraries that mount only the active panel look: React Aria puts `aria-controls` on the selected tab alone, and Radix leaves the other tabs' `aria-controls` naming panels that are not rendered |
|
|
57
60
|
| `tab-without-panel` | `failed` | The page has no tab panels, or the tab has no `aria-controls` and every tab panel on the page is associated with another tab |
|
|
58
|
-
| `association-unverifiable` | `needsReview` | Anything else: tab panels without an association exist, or the tab's `aria-controls` names a panel that is not in the page |
|
|
61
|
+
| `association-unverifiable` | `needsReview` | Anything else: tab panels without an association exist, or the tab's `aria-controls` names a panel that is not in the page and the selected tab in its tablist is not paired one-to-one with a panel |
|
|
59
62
|
|
|
60
63
|
For each tab panel:
|
|
61
64
|
|
|
@@ -65,7 +68,7 @@ For each tab panel:
|
|
|
65
68
|
| `tabpanel-without-tab` | `failed` | The page has no tabs, or every tab on the page is associated with another panel or has an `aria-controls` that points only at tab panels or at nothing mounted yet, so each already has a panel of its own |
|
|
66
69
|
| `association-unverifiable` | `needsReview` | Some tab declares no association, so it may be this panel's |
|
|
67
70
|
|
|
68
|
-
The failures never depend on guessing which panel belongs to which tab, only on there being none left over for it.
|
|
71
|
+
The failures never depend on guessing which panel belongs to which tab, only on there being none left over for it. Panels are counted document-wide, but only a selected tab paired one-to-one with a panel vouches for the other tabs, and only those in its own tablist: an unwired tablist next to a wired one fails, because the wired tablist's panels are all spoken for. A tab's tablist is its nearest ancestor with `role="tablist"`, or the tablist whose `aria-owns` names it.
|
|
69
72
|
|
|
70
73
|
## Browser support
|
|
71
74
|
|
|
@@ -96,6 +99,13 @@ type OrphanedTabRolesElement = {
|
|
|
96
99
|
| "controls-not-a-tabpanel"
|
|
97
100
|
| "association-unverifiable"
|
|
98
101
|
| null;
|
|
102
|
+
// Tabs only: selector for the tablist the tab belongs to, or null outside one.
|
|
103
|
+
tablist?: string | null;
|
|
104
|
+
// When aria-controls is set: each id, whether it is in the document, and the
|
|
105
|
+
// target's explicit role and tag name.
|
|
106
|
+
controls?: { id: string; found: boolean; role: string | null; tag: string | null }[];
|
|
107
|
+
// For tab-used-as-link: where the link goes.
|
|
108
|
+
href?: string;
|
|
99
109
|
};
|
|
100
110
|
|
|
101
111
|
type OrphanedTabRolesResult = {
|
|
@@ -127,7 +137,7 @@ Cases where the audit reports a problem that is not one. Each says which bucket
|
|
|
127
137
|
In the A11y Pulse audit runner, `failed` is reported in `violations` and `needsReview` in `incomplete`.
|
|
128
138
|
|
|
129
139
|
- **Widgets wired up after load** (`incomplete`). A script that adds `aria-controls` when a tab is first activated leaves its tabs and panels without an association when the audit runs.
|
|
130
|
-
- **Lazily mounted panels** (`incomplete`). A tab whose `aria-controls` names a panel the page has not rendered yet is
|
|
140
|
+
- **Lazily mounted panels with nothing mounted to vouch for them** (`incomplete`). A tab whose `aria-controls` names a panel the page has not rendered yet passes when the selected tab in its tablist has a panel of its own. When it does not (no tab is selected, the selected tab has no panel, or it shares panels with other tabs), or the tab is outside a tablist, it is reviewed. Only a page with no tab panels at all fails it.
|
|
131
141
|
- **Tabs that control a region** (`incomplete`). A tab whose `aria-controls` names a wrapper without `role="tabpanel"` may still work. The tab panels beside it are reviewed too, since the wrapper may hold one of them.
|
|
132
142
|
|
|
133
143
|
## License
|
|
@@ -3,6 +3,15 @@
|
|
|
3
3
|
* not reference any symbols outside their own scope.
|
|
4
4
|
*/
|
|
5
5
|
export type TabRole = "tab" | "tabpanel";
|
|
6
|
+
export type ControlsTarget = {
|
|
7
|
+
id: string;
|
|
8
|
+
/** An element with this id is in the document. */
|
|
9
|
+
found: boolean;
|
|
10
|
+
/** The target's explicit role, or null when it has none or is not in the document. */
|
|
11
|
+
role: string | null;
|
|
12
|
+
/** The target's lowercase tag name, or null when it is not in the document. */
|
|
13
|
+
tag: string | null;
|
|
14
|
+
};
|
|
6
15
|
export type CollectedTabRole = {
|
|
7
16
|
role: TabRole;
|
|
8
17
|
/** Indices of the collected elements this element's `aria-controls` resolves to. */
|
|
@@ -15,12 +24,25 @@ export type CollectedTabRole = {
|
|
|
15
24
|
labelledBy: number[];
|
|
16
25
|
/** A link to another document, so activating it navigates instead of switching panels. */
|
|
17
26
|
navigates: boolean;
|
|
27
|
+
/** Where that link goes, resolved against the page, or null when the element does not navigate. */
|
|
28
|
+
href?: string | null;
|
|
29
|
+
/** `aria-selected="true"`. */
|
|
30
|
+
selected?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Identifies the tablist a tab belongs to: its nearest ancestor with role `tablist`, or the
|
|
33
|
+
* tablist whose `aria-owns` names it. The number is the tablist's index in the stash, after
|
|
34
|
+
* every tab and tab panel. Null outside a tablist.
|
|
35
|
+
*/
|
|
36
|
+
tablist?: number | null;
|
|
37
|
+
/** Every id in `aria-controls`, with what it resolves to. */
|
|
38
|
+
controlsTargets?: ControlsTarget[];
|
|
18
39
|
/** Shallow outerHTML. */
|
|
19
40
|
html: string;
|
|
20
41
|
};
|
|
21
42
|
/**
|
|
22
43
|
* Collect every element whose role is `tab` or `tabpanel` in the main document, hidden ones
|
|
23
44
|
* included, and resolve their `aria-controls` and `aria-labelledby` references. The collected
|
|
24
|
-
* elements are left on `window[stashKey]`, in the same order
|
|
45
|
+
* elements are left on `window[stashKey]`, in the same order, followed by the tablists the tabs
|
|
46
|
+
* belong to.
|
|
25
47
|
*/
|
|
26
48
|
export declare function collectTabRolesScript(stashKey: string): CollectedTabRole[];
|
package/dist/classify.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { CollectedTabRole } from "./browser-scripts";
|
|
|
2
2
|
/**
|
|
3
3
|
* - `tab-used-as-link`: a tab that links to another document and is associated with no panel.
|
|
4
4
|
* - `tab-without-panel`: a tab with no panel it could control, because the page has none or every
|
|
5
|
-
* one belongs to another
|
|
5
|
+
* one belongs to a tab in another tablist.
|
|
6
6
|
* - `tabpanel-without-tab`: a tab panel no tab could control, because the page has no tabs or every
|
|
7
7
|
* one belongs to another panel.
|
|
8
8
|
* - `controls-not-a-tabpanel`: a tab whose `aria-controls` points at something other than a tab
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type { BrowserAdaptor, ElementRef } from "@a11y-pulse/browser-adaptor";
|
|
2
2
|
export type { OrphanedTabRolesOptions } from "./audit";
|
|
3
3
|
export { DEFAULT_REPORT_LIMIT, runOrphanedTabRolesAudit } from "./audit";
|
|
4
|
-
export type { CollectedTabRole, TabRole } from "./browser-scripts";
|
|
4
|
+
export type { CollectedTabRole, ControlsTarget, TabRole } from "./browser-scripts";
|
|
5
5
|
export type { TabRoleClassification, TabRoleFinding, TabRoleVerdict } from "./classify";
|
|
6
6
|
export { classifyTabRoles } from "./classify";
|
|
7
7
|
export type { OrphanedTabRolesElement, OrphanedTabRolesResult } from "./result";
|
package/dist/index.js
CHANGED
|
@@ -16,32 +16,72 @@ function collectTabRolesScript(stashKey) {
|
|
|
16
16
|
const target = document.getElementById(id);
|
|
17
17
|
return target ? [target] : [];
|
|
18
18
|
});
|
|
19
|
-
const
|
|
19
|
+
const navigationTarget = (el) => {
|
|
20
20
|
const link = el.closest("a[href]");
|
|
21
21
|
if (!link) {
|
|
22
|
-
return
|
|
22
|
+
return null;
|
|
23
23
|
}
|
|
24
24
|
const href = link.getAttribute("href") ?? "";
|
|
25
25
|
if (href.startsWith("#") || /^\s*javascript:/i.test(href)) {
|
|
26
|
-
return
|
|
26
|
+
return null;
|
|
27
27
|
}
|
|
28
28
|
const target = new URL(link.href, document.baseURI);
|
|
29
29
|
const here = new URL(location.href);
|
|
30
|
-
return target.origin !== here.origin || target.pathname !== here.pathname || target.search !== here.search;
|
|
30
|
+
return target.origin !== here.origin || target.pathname !== here.pathname || target.search !== here.search ? target.href : null;
|
|
31
31
|
};
|
|
32
|
+
const tablists = [];
|
|
33
|
+
const ownedBy = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const list of Array.from(document.querySelectorAll("[role][aria-owns]"))) {
|
|
35
|
+
if (roleOf(list) === "tablist") {
|
|
36
|
+
for (const owned of idrefs(list, "aria-owns")) {
|
|
37
|
+
ownedBy.set(owned, list);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const tablistOf = (el) => {
|
|
42
|
+
let list = null;
|
|
43
|
+
for (let node = el; node && !list; node = node.parentElement) {
|
|
44
|
+
list = ownedBy.get(node) ?? null;
|
|
45
|
+
if (!list && node !== el && roleOf(node) === "tablist") {
|
|
46
|
+
list = node;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!list) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
if (!tablists.includes(list)) {
|
|
53
|
+
tablists.push(list);
|
|
54
|
+
}
|
|
55
|
+
return elements.length + tablists.indexOf(list);
|
|
56
|
+
};
|
|
57
|
+
const controlsTargets = (el) => (el.getAttribute("aria-controls") ?? "").split(/\s+/).filter(Boolean).map((id) => {
|
|
58
|
+
const target = document.getElementById(id);
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
found: target !== null,
|
|
62
|
+
role: target ? roleOf(target) || null : null,
|
|
63
|
+
tag: target ? target.tagName.toLowerCase() : null
|
|
64
|
+
};
|
|
65
|
+
});
|
|
32
66
|
const collected = elements.map((el) => {
|
|
33
67
|
const controlled = idrefs(el, "aria-controls");
|
|
68
|
+
const href = navigationTarget(el);
|
|
69
|
+
const role = roleOf(el);
|
|
34
70
|
return {
|
|
35
|
-
role
|
|
71
|
+
role,
|
|
36
72
|
controls: controlled.map((target) => elements.indexOf(target)).filter((i) => i !== -1),
|
|
37
73
|
hasControls: Boolean(el.getAttribute("aria-controls")?.trim()),
|
|
38
74
|
controlsOther: controlled.some((target) => roleOf(target) !== "tabpanel"),
|
|
39
75
|
labelledBy: idrefs(el, "aria-labelledby").map((target) => elements.indexOf(target)).filter((i) => i !== -1),
|
|
40
|
-
navigates:
|
|
76
|
+
navigates: href !== null,
|
|
77
|
+
href,
|
|
78
|
+
selected: el.getAttribute("aria-selected") === "true",
|
|
79
|
+
tablist: role === "tab" ? tablistOf(el) : null,
|
|
80
|
+
controlsTargets: controlsTargets(el),
|
|
41
81
|
html: el.cloneNode(false).outerHTML
|
|
42
82
|
};
|
|
43
83
|
});
|
|
44
|
-
window[stashKey] = elements;
|
|
84
|
+
window[stashKey] = [...elements, ...tablists];
|
|
45
85
|
return collected;
|
|
46
86
|
}
|
|
47
87
|
|
|
@@ -73,6 +113,17 @@ function classifyTabRoles(elements) {
|
|
|
73
113
|
const panels = elements.flatMap((element, i) => element.role === "tabpanel" ? [i] : []);
|
|
74
114
|
const associated = (i) => partners[i].size > 0;
|
|
75
115
|
const tabHasOwnPanel = (i) => associated(i) || elements[i].hasControls && !elements[i].controlsOther;
|
|
116
|
+
const pairedOneToOne = (i) => {
|
|
117
|
+
const [only, ...rest] = partners[i];
|
|
118
|
+
return only !== void 0 && rest.length === 0 && partners[only].size === 1;
|
|
119
|
+
};
|
|
120
|
+
const lazyTablists = new Set(
|
|
121
|
+
tabs.filter((i) => elements[i].selected === true && pairedOneToOne(i)).flatMap((i) => {
|
|
122
|
+
const tablist = elements[i].tablist;
|
|
123
|
+
return typeof tablist === "number" ? [tablist] : [];
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
const selectedSiblingVouches = (element) => typeof element.tablist === "number" && lazyTablists.has(element.tablist);
|
|
76
127
|
return elements.map((element, i) => {
|
|
77
128
|
if (associated(i)) {
|
|
78
129
|
return PASSED;
|
|
@@ -86,6 +137,9 @@ function classifyTabRoles(elements) {
|
|
|
86
137
|
if (element.controlsOther) {
|
|
87
138
|
return needsReview("controls-not-a-tabpanel");
|
|
88
139
|
}
|
|
140
|
+
if (!element.selected && selectedSiblingVouches(element) && panels.every(associated)) {
|
|
141
|
+
return PASSED;
|
|
142
|
+
}
|
|
89
143
|
if (panels.length === 0 || !element.hasControls && panels.every(associated)) {
|
|
90
144
|
return failed("tab-without-panel");
|
|
91
145
|
}
|
|
@@ -97,17 +151,36 @@ function classifyTabRoles(elements) {
|
|
|
97
151
|
var DEFAULT_REPORT_LIMIT = 10;
|
|
98
152
|
var STASH_KEY = "__a11yPulseOrphanedTabRoles";
|
|
99
153
|
async function toElements(adaptor, classified) {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
154
|
+
const tablists = [
|
|
155
|
+
...new Set(
|
|
156
|
+
classified.flatMap(
|
|
157
|
+
({ element }) => typeof element.tablist === "number" ? [element.tablist] : []
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
];
|
|
161
|
+
const selectors = await stashedElementSelectors(adaptor, STASH_KEY, [
|
|
162
|
+
...classified.map(({ index }) => index),
|
|
163
|
+
...tablists
|
|
164
|
+
]);
|
|
165
|
+
const tablistSelector = (index) => selectors[classified.length + tablists.indexOf(index)];
|
|
166
|
+
return classified.map(({ element, finding }, i) => {
|
|
167
|
+
const reported = {
|
|
168
|
+
selector: selectors[i],
|
|
169
|
+
html: truncateHtml(element.html),
|
|
170
|
+
role: element.role,
|
|
171
|
+
finding
|
|
172
|
+
};
|
|
173
|
+
if (element.role === "tab") {
|
|
174
|
+
reported.tablist = typeof element.tablist === "number" ? tablistSelector(element.tablist) : null;
|
|
175
|
+
}
|
|
176
|
+
if (element.controlsTargets?.length) {
|
|
177
|
+
reported.controls = element.controlsTargets;
|
|
178
|
+
}
|
|
179
|
+
if (finding === "tab-used-as-link" && element.href) {
|
|
180
|
+
reported.href = element.href;
|
|
181
|
+
}
|
|
182
|
+
return reported;
|
|
183
|
+
});
|
|
111
184
|
}
|
|
112
185
|
async function runOrphanedTabRolesAudit(adaptor, options = {}) {
|
|
113
186
|
const reportLimit = options.reportLimit ?? DEFAULT_REPORT_LIMIT;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/audit.ts","../src/browser-scripts.ts","../src/classify.ts"],"sourcesContent":["import {\n\ttype BrowserAdaptor,\n\treleaseStashedElements,\n\tstashedElementSelectors,\n} from \"@a11y-pulse/browser-adaptor\";\nimport { truncateHtml } from \"@a11y-pulse/browser-adaptor/dom\";\nimport { type CollectedTabRole, collectTabRolesScript } from \"./browser-scripts\";\nimport { classifyTabRoles, type TabRoleClassification, type TabRoleVerdict } from \"./classify\";\nimport type { OrphanedTabRolesElement, OrphanedTabRolesResult } from \"./result\";\n\nexport const DEFAULT_REPORT_LIMIT = 10;\n\nconst STASH_KEY = \"__a11yPulseOrphanedTabRoles\";\n\nexport type OrphanedTabRolesOptions = {\n\t/** Max elements to report in each of `failed`, `needsReview` and `passed`. Defaults to 10. */\n\treportLimit?: number;\n};\n\ntype Classified = TabRoleClassification & { index: number; element: CollectedTabRole };\n\nasync function toElements(\n\tadaptor: BrowserAdaptor,\n\tclassified: readonly Classified[],\n): Promise<OrphanedTabRolesElement[]> {\n\tconst selectors = await stashedElementSelectors(\n\t\tadaptor,\n\t\tSTASH_KEY,\n\t\tclassified.map(({ index }) => index),\n\t);\n\n\treturn classified.map(({ element, finding }, i) => ({\n\t\tselector: selectors[i]!,\n\t\thtml: truncateHtml(element.html),\n\t\trole: element.role,\n\t\tfinding,\n\t}));\n}\n\n/**\n * Check that every tab on the page has a tab panel and every tab panel has a tab.\n */\nexport async function runOrphanedTabRolesAudit(\n\tadaptor: BrowserAdaptor,\n\toptions: OrphanedTabRolesOptions = {},\n): Promise<OrphanedTabRolesResult> {\n\tconst reportLimit = options.reportLimit ?? DEFAULT_REPORT_LIMIT;\n\n\tconst elements = await adaptor.evaluate(collectTabRolesScript, STASH_KEY);\n\n\ttry {\n\t\tconst classified: Classified[] = classifyTabRoles(elements).map((classification, index) => ({\n\t\t\t...classification,\n\t\t\tindex,\n\t\t\telement: elements[index]!,\n\t\t}));\n\t\tconst withVerdict = (verdict: TabRoleVerdict) =>\n\t\t\tclassified.filter((entry) => entry.verdict === verdict);\n\n\t\tconst failed = withVerdict(\"failed\");\n\t\tconst needsReview = withVerdict(\"needs-review\");\n\t\tconst passed = withVerdict(\"passed\");\n\n\t\treturn {\n\t\t\tfailed: await toElements(adaptor, failed.slice(0, reportLimit)),\n\t\t\tneedsReview: await toElements(adaptor, needsReview.slice(0, reportLimit)),\n\t\t\tpassed: await toElements(adaptor, passed.slice(0, reportLimit)),\n\t\t\tsummary: {\n\t\t\t\tchecked: classified.length,\n\t\t\t\tfailed: failed.length,\n\t\t\t\tneedsReview: needsReview.length,\n\t\t\t\tpassed: passed.length,\n\t\t\t},\n\t\t};\n\t} finally {\n\t\tawait releaseStashedElements(adaptor, STASH_KEY);\n\t}\n}\n","/**\n * IMPORTANT: Functions in this file are serialized and injected into the audited page, so they must\n * not reference any symbols outside their own scope.\n */\n\nexport type TabRole = \"tab\" | \"tabpanel\";\n\nexport type CollectedTabRole = {\n\trole: TabRole;\n\t/** Indices of the collected elements this element's `aria-controls` resolves to. */\n\tcontrols: number[];\n\t/** `aria-controls` lists at least one id, whether or not it resolves. */\n\thasControls: boolean;\n\t/** `aria-controls` resolves to an element that is not a tab panel. */\n\tcontrolsOther: boolean;\n\t/** Indices of the collected elements this element's `aria-labelledby` resolves to. */\n\tlabelledBy: number[];\n\t/** A link to another document, so activating it navigates instead of switching panels. */\n\tnavigates: boolean;\n\t/** Shallow outerHTML. */\n\thtml: string;\n};\n\n/**\n * Collect every element whose role is `tab` or `tabpanel` in the main document, hidden ones\n * included, and resolve their `aria-controls` and `aria-labelledby` references. The collected\n * elements are left on `window[stashKey]`, in the same order.\n */\nexport function collectTabRolesScript(stashKey: string): CollectedTabRole[] {\n\tconst roleOf = (el: Element): string =>\n\t\t(el.getAttribute(\"role\") ?? \"\").trim().split(/\\s+/)[0]?.toLowerCase() ?? \"\";\n\n\tconst elements = Array.from(document.querySelectorAll(\"[role]\")).filter((el) => {\n\t\tconst role = roleOf(el);\n\n\t\treturn role === \"tab\" || role === \"tabpanel\";\n\t});\n\n\tconst idrefs = (el: Element, attribute: string): Element[] =>\n\t\t(el.getAttribute(attribute) ?? \"\")\n\t\t\t.split(/\\s+/)\n\t\t\t.filter(Boolean)\n\t\t\t.flatMap((id) => {\n\t\t\t\tconst target = document.getElementById(id);\n\n\t\t\t\treturn target ? [target] : [];\n\t\t\t});\n\n\t// Fragment-only and same-document hrefs are how Bootstrap-style tab anchors point at panels.\n\tconst navigates = (el: Element): boolean => {\n\t\tconst link = el.closest(\"a[href]\") as HTMLAnchorElement | null;\n\n\t\tif (!link) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst href = link.getAttribute(\"href\") ?? \"\";\n\n\t\tif (href.startsWith(\"#\") || /^\\s*javascript:/i.test(href)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tconst target = new URL(link.href, document.baseURI);\n\t\tconst here = new URL(location.href);\n\n\t\treturn (\n\t\t\ttarget.origin !== here.origin ||\n\t\t\ttarget.pathname !== here.pathname ||\n\t\t\ttarget.search !== here.search\n\t\t);\n\t};\n\n\tconst collected = elements.map((el): CollectedTabRole => {\n\t\tconst controlled = idrefs(el, \"aria-controls\");\n\n\t\treturn {\n\t\t\trole: roleOf(el) as TabRole,\n\t\t\tcontrols: controlled.map((target) => elements.indexOf(target)).filter((i) => i !== -1),\n\t\t\thasControls: Boolean(el.getAttribute(\"aria-controls\")?.trim()),\n\t\t\tcontrolsOther: controlled.some((target) => roleOf(target) !== \"tabpanel\"),\n\t\t\tlabelledBy: idrefs(el, \"aria-labelledby\")\n\t\t\t\t.map((target) => elements.indexOf(target))\n\t\t\t\t.filter((i) => i !== -1),\n\t\t\tnavigates: navigates(el),\n\t\t\thtml: (el.cloneNode(false) as Element).outerHTML,\n\t\t};\n\t});\n\n\t(window as unknown as Record<string, unknown>)[stashKey] = elements;\n\n\treturn collected;\n}\n","import type { CollectedTabRole } from \"./browser-scripts\";\n\n/**\n * - `tab-used-as-link`: a tab that links to another document and is associated with no panel.\n * - `tab-without-panel`: a tab with no panel it could control, because the page has none or every\n * one belongs to another tab.\n * - `tabpanel-without-tab`: a tab panel no tab could control, because the page has no tabs or every\n * one belongs to another panel.\n * - `controls-not-a-tabpanel`: a tab whose `aria-controls` points at something other than a tab\n * panel.\n * - `association-unverifiable`: no association is declared, but one may be wired up some other way.\n */\nexport type TabRoleFinding =\n\t| \"tab-used-as-link\"\n\t| \"tab-without-panel\"\n\t| \"tabpanel-without-tab\"\n\t| \"controls-not-a-tabpanel\"\n\t| \"association-unverifiable\";\n\nexport type TabRoleVerdict = \"failed\" | \"needs-review\" | \"passed\";\n\nexport type TabRoleClassification = { verdict: TabRoleVerdict; finding: TabRoleFinding | null };\n\nconst PASSED: TabRoleClassification = { verdict: \"passed\", finding: null };\n\nfunction failed(finding: TabRoleFinding): TabRoleClassification {\n\treturn { verdict: \"failed\", finding };\n}\n\nfunction needsReview(finding: TabRoleFinding): TabRoleClassification {\n\treturn { verdict: \"needs-review\", finding };\n}\n\n/**\n * Classify each collected tab and tab panel by whether it is associated with a counterpart, in the\n * same order.\n */\nexport function classifyTabRoles(elements: readonly CollectedTabRole[]): TabRoleClassification[] {\n\tconst isTab = (i: number) => elements[i]?.role === \"tab\";\n\tconst isPanel = (i: number) => elements[i]?.role === \"tabpanel\";\n\n\t// An explicit association runs either way: the tab's `aria-controls` names the panel, or the\n\t// panel's `aria-labelledby` names the tab.\n\tconst partners = elements.map(() => new Set<number>());\n\n\telements.forEach((element, i) => {\n\t\tconst controlled = element.role === \"tab\" ? element.controls.filter(isPanel) : [];\n\t\tconst labellers = element.role === \"tabpanel\" ? element.labelledBy.filter(isTab) : [];\n\n\t\tfor (const panel of controlled) {\n\t\t\tpartners[i]!.add(panel);\n\t\t\tpartners[panel]!.add(i);\n\t\t}\n\n\t\tfor (const tab of labellers) {\n\t\t\tpartners[i]!.add(tab);\n\t\t\tpartners[tab]!.add(i);\n\t\t}\n\t});\n\n\tconst tabs = elements.flatMap((element, i) => (element.role === \"tab\" ? [i] : []));\n\tconst panels = elements.flatMap((element, i) => (element.role === \"tabpanel\" ? [i] : []));\n\tconst associated = (i: number) => partners[i]!.size > 0;\n\n\t// A tab whose `aria-controls` names a panel that is not mounted yet still has a panel of its own,\n\t// but one that names only other elements may be pointing at a wrapper around its panel.\n\tconst tabHasOwnPanel = (i: number) =>\n\t\tassociated(i) || (elements[i]!.hasControls && !elements[i]!.controlsOther);\n\n\treturn elements.map((element, i) => {\n\t\tif (associated(i)) {\n\t\t\treturn PASSED;\n\t\t}\n\n\t\tif (element.role === \"tabpanel\") {\n\t\t\treturn tabs.every(tabHasOwnPanel)\n\t\t\t\t? failed(\"tabpanel-without-tab\")\n\t\t\t\t: needsReview(\"association-unverifiable\");\n\t\t}\n\n\t\tif (element.navigates) {\n\t\t\treturn failed(\"tab-used-as-link\");\n\t\t}\n\n\t\tif (element.controlsOther) {\n\t\t\treturn needsReview(\"controls-not-a-tabpanel\");\n\t\t}\n\n\t\t// A dangling `aria-controls` is how lazily mounted panels look, so only a page with no panels\n\t\t// at all fails such a tab.\n\t\tif (panels.length === 0 || (!element.hasControls && panels.every(associated))) {\n\t\t\treturn failed(\"tab-without-panel\");\n\t\t}\n\n\t\treturn needsReview(\"association-unverifiable\");\n\t});\n}\n"],"mappings":";AAAA;AAAA,EAEC;AAAA,EACA;AAAA,OACM;AACP,SAAS,oBAAoB;;;ACuBtB,SAAS,sBAAsB,UAAsC;AAC3E,QAAM,SAAS,CAAC,QACd,GAAG,aAAa,MAAM,KAAK,IAAI,KAAK,EAAE,MAAM,KAAK,EAAE,CAAC,GAAG,YAAY,KAAK;AAE1E,QAAM,WAAW,MAAM,KAAK,SAAS,iBAAiB,QAAQ,CAAC,EAAE,OAAO,CAAC,OAAO;AAC/E,UAAM,OAAO,OAAO,EAAE;AAEtB,WAAO,SAAS,SAAS,SAAS;AAAA,EACnC,CAAC;AAED,QAAM,SAAS,CAAC,IAAa,eAC3B,GAAG,aAAa,SAAS,KAAK,IAC7B,MAAM,KAAK,EACX,OAAO,OAAO,EACd,QAAQ,CAAC,OAAO;AAChB,UAAM,SAAS,SAAS,eAAe,EAAE;AAEzC,WAAO,SAAS,CAAC,MAAM,IAAI,CAAC;AAAA,EAC7B,CAAC;AAGH,QAAM,YAAY,CAAC,OAAyB;AAC3C,UAAM,OAAO,GAAG,QAAQ,SAAS;AAEjC,QAAI,CAAC,MAAM;AACV,aAAO;AAAA,IACR;AAEA,UAAM,OAAO,KAAK,aAAa,MAAM,KAAK;AAE1C,QAAI,KAAK,WAAW,GAAG,KAAK,mBAAmB,KAAK,IAAI,GAAG;AAC1D,aAAO;AAAA,IACR;AAEA,UAAM,SAAS,IAAI,IAAI,KA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":["failed","needsReview"]}
|
|
1
|
+
{"version":3,"sources":["../src/audit.ts","../src/browser-scripts.ts","../src/classify.ts"],"sourcesContent":["import {\n\ttype BrowserAdaptor,\n\treleaseStashedElements,\n\tstashedElementSelectors,\n} from \"@a11y-pulse/browser-adaptor\";\nimport { truncateHtml } from \"@a11y-pulse/browser-adaptor/dom\";\nimport { type CollectedTabRole, collectTabRolesScript } from \"./browser-scripts\";\nimport { classifyTabRoles, type TabRoleClassification, type TabRoleVerdict } from \"./classify\";\nimport type { OrphanedTabRolesElement, OrphanedTabRolesResult } from \"./result\";\n\nexport const DEFAULT_REPORT_LIMIT = 10;\n\nconst STASH_KEY = \"__a11yPulseOrphanedTabRoles\";\n\nexport type OrphanedTabRolesOptions = {\n\t/** Max elements to report in each of `failed`, `needsReview` and `passed`. Defaults to 10. */\n\treportLimit?: number;\n};\n\ntype Classified = TabRoleClassification & { index: number; element: CollectedTabRole };\n\nasync function toElements(\n\tadaptor: BrowserAdaptor,\n\tclassified: readonly Classified[],\n): Promise<OrphanedTabRolesElement[]> {\n\tconst tablists = [\n\t\t...new Set(\n\t\t\tclassified.flatMap(({ element }) =>\n\t\t\t\ttypeof element.tablist === \"number\" ? [element.tablist] : [],\n\t\t\t),\n\t\t),\n\t];\n\tconst selectors = await stashedElementSelectors(adaptor, STASH_KEY, [\n\t\t...classified.map(({ index }) => index),\n\t\t...tablists,\n\t]);\n\tconst tablistSelector = (index: number) =>\n\t\tselectors[classified.length + tablists.indexOf(index)]!;\n\n\treturn classified.map(({ element, finding }, i) => {\n\t\tconst reported: OrphanedTabRolesElement = {\n\t\t\tselector: selectors[i]!,\n\t\t\thtml: truncateHtml(element.html),\n\t\t\trole: element.role,\n\t\t\tfinding,\n\t\t};\n\n\t\tif (element.role === \"tab\") {\n\t\t\treported.tablist =\n\t\t\t\ttypeof element.tablist === \"number\" ? tablistSelector(element.tablist) : null;\n\t\t}\n\n\t\tif (element.controlsTargets?.length) {\n\t\t\treported.controls = element.controlsTargets;\n\t\t}\n\n\t\tif (finding === \"tab-used-as-link\" && element.href) {\n\t\t\treported.href = element.href;\n\t\t}\n\n\t\treturn reported;\n\t});\n}\n\n/**\n * Check that every tab on the page has a tab panel and every tab panel has a tab.\n */\nexport async function runOrphanedTabRolesAudit(\n\tadaptor: BrowserAdaptor,\n\toptions: OrphanedTabRolesOptions = {},\n): Promise<OrphanedTabRolesResult> {\n\tconst reportLimit = options.reportLimit ?? DEFAULT_REPORT_LIMIT;\n\n\tconst elements = await adaptor.evaluate(collectTabRolesScript, STASH_KEY);\n\n\ttry {\n\t\tconst classified: Classified[] = classifyTabRoles(elements).map((classification, index) => ({\n\t\t\t...classification,\n\t\t\tindex,\n\t\t\telement: elements[index]!,\n\t\t}));\n\t\tconst withVerdict = (verdict: TabRoleVerdict) =>\n\t\t\tclassified.filter((entry) => entry.verdict === verdict);\n\n\t\tconst failed = withVerdict(\"failed\");\n\t\tconst needsReview = withVerdict(\"needs-review\");\n\t\tconst passed = withVerdict(\"passed\");\n\n\t\treturn {\n\t\t\tfailed: await toElements(adaptor, failed.slice(0, reportLimit)),\n\t\t\tneedsReview: await toElements(adaptor, needsReview.slice(0, reportLimit)),\n\t\t\tpassed: await toElements(adaptor, passed.slice(0, reportLimit)),\n\t\t\tsummary: {\n\t\t\t\tchecked: classified.length,\n\t\t\t\tfailed: failed.length,\n\t\t\t\tneedsReview: needsReview.length,\n\t\t\t\tpassed: passed.length,\n\t\t\t},\n\t\t};\n\t} finally {\n\t\tawait releaseStashedElements(adaptor, STASH_KEY);\n\t}\n}\n","/**\n * IMPORTANT: Functions in this file are serialized and injected into the audited page, so they must\n * not reference any symbols outside their own scope.\n */\n\nexport type TabRole = \"tab\" | \"tabpanel\";\n\nexport type ControlsTarget = {\n\tid: string;\n\t/** An element with this id is in the document. */\n\tfound: boolean;\n\t/** The target's explicit role, or null when it has none or is not in the document. */\n\trole: string | null;\n\t/** The target's lowercase tag name, or null when it is not in the document. */\n\ttag: string | null;\n};\n\nexport type CollectedTabRole = {\n\trole: TabRole;\n\t/** Indices of the collected elements this element's `aria-controls` resolves to. */\n\tcontrols: number[];\n\t/** `aria-controls` lists at least one id, whether or not it resolves. */\n\thasControls: boolean;\n\t/** `aria-controls` resolves to an element that is not a tab panel. */\n\tcontrolsOther: boolean;\n\t/** Indices of the collected elements this element's `aria-labelledby` resolves to. */\n\tlabelledBy: number[];\n\t/** A link to another document, so activating it navigates instead of switching panels. */\n\tnavigates: boolean;\n\t/** Where that link goes, resolved against the page, or null when the element does not navigate. */\n\thref?: string | null;\n\t/** `aria-selected=\"true\"`. */\n\tselected?: boolean;\n\t/**\n\t * Identifies the tablist a tab belongs to: its nearest ancestor with role `tablist`, or the\n\t * tablist whose `aria-owns` names it. The number is the tablist's index in the stash, after\n\t * every tab and tab panel. Null outside a tablist.\n\t */\n\ttablist?: number | null;\n\t/** Every id in `aria-controls`, with what it resolves to. */\n\tcontrolsTargets?: ControlsTarget[];\n\t/** Shallow outerHTML. */\n\thtml: string;\n};\n\n/**\n * Collect every element whose role is `tab` or `tabpanel` in the main document, hidden ones\n * included, and resolve their `aria-controls` and `aria-labelledby` references. The collected\n * elements are left on `window[stashKey]`, in the same order, followed by the tablists the tabs\n * belong to.\n */\nexport function collectTabRolesScript(stashKey: string): CollectedTabRole[] {\n\tconst roleOf = (el: Element): string =>\n\t\t(el.getAttribute(\"role\") ?? \"\").trim().split(/\\s+/)[0]?.toLowerCase() ?? \"\";\n\n\tconst elements = Array.from(document.querySelectorAll(\"[role]\")).filter((el) => {\n\t\tconst role = roleOf(el);\n\n\t\treturn role === \"tab\" || role === \"tabpanel\";\n\t});\n\n\tconst idrefs = (el: Element, attribute: string): Element[] =>\n\t\t(el.getAttribute(attribute) ?? \"\")\n\t\t\t.split(/\\s+/)\n\t\t\t.filter(Boolean)\n\t\t\t.flatMap((id) => {\n\t\t\t\tconst target = document.getElementById(id);\n\n\t\t\t\treturn target ? [target] : [];\n\t\t\t});\n\n\t// Fragment-only and same-document hrefs are how Bootstrap-style tab anchors point at panels.\n\tconst navigationTarget = (el: Element): string | null => {\n\t\tconst link = el.closest(\"a[href]\") as HTMLAnchorElement | null;\n\n\t\tif (!link) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst href = link.getAttribute(\"href\") ?? \"\";\n\n\t\tif (href.startsWith(\"#\") || /^\\s*javascript:/i.test(href)) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst target = new URL(link.href, document.baseURI);\n\t\tconst here = new URL(location.href);\n\n\t\treturn target.origin !== here.origin ||\n\t\t\ttarget.pathname !== here.pathname ||\n\t\t\ttarget.search !== here.search\n\t\t\t? target.href\n\t\t\t: null;\n\t};\n\n\tconst tablists: Element[] = [];\n\tconst ownedBy = new Map<Element, Element>();\n\n\tfor (const list of Array.from(document.querySelectorAll(\"[role][aria-owns]\"))) {\n\t\tif (roleOf(list) === \"tablist\") {\n\t\t\tfor (const owned of idrefs(list, \"aria-owns\")) {\n\t\t\t\townedBy.set(owned, list);\n\t\t\t}\n\t\t}\n\t}\n\n\tconst tablistOf = (el: Element): number | null => {\n\t\tlet list: Element | null = null;\n\n\t\tfor (let node: Element | null = el; node && !list; node = node.parentElement) {\n\t\t\tlist = ownedBy.get(node) ?? null;\n\n\t\t\tif (!list && node !== el && roleOf(node) === \"tablist\") {\n\t\t\t\tlist = node;\n\t\t\t}\n\t\t}\n\n\t\tif (!list) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (!tablists.includes(list)) {\n\t\t\ttablists.push(list);\n\t\t}\n\n\t\treturn elements.length + tablists.indexOf(list);\n\t};\n\n\tconst controlsTargets = (el: Element): ControlsTarget[] =>\n\t\t(el.getAttribute(\"aria-controls\") ?? \"\")\n\t\t\t.split(/\\s+/)\n\t\t\t.filter(Boolean)\n\t\t\t.map((id) => {\n\t\t\t\tconst target = document.getElementById(id);\n\n\t\t\t\treturn {\n\t\t\t\t\tid,\n\t\t\t\t\tfound: target !== null,\n\t\t\t\t\trole: target ? roleOf(target) || null : null,\n\t\t\t\t\ttag: target ? target.tagName.toLowerCase() : null,\n\t\t\t\t};\n\t\t\t});\n\n\tconst collected = elements.map((el): CollectedTabRole => {\n\t\tconst controlled = idrefs(el, \"aria-controls\");\n\t\tconst href = navigationTarget(el);\n\t\tconst role = roleOf(el) as TabRole;\n\n\t\treturn {\n\t\t\trole,\n\t\t\tcontrols: controlled.map((target) => elements.indexOf(target)).filter((i) => i !== -1),\n\t\t\thasControls: Boolean(el.getAttribute(\"aria-controls\")?.trim()),\n\t\t\tcontrolsOther: controlled.some((target) => roleOf(target) !== \"tabpanel\"),\n\t\t\tlabelledBy: idrefs(el, \"aria-labelledby\")\n\t\t\t\t.map((target) => elements.indexOf(target))\n\t\t\t\t.filter((i) => i !== -1),\n\t\t\tnavigates: href !== null,\n\t\t\thref,\n\t\t\tselected: el.getAttribute(\"aria-selected\") === \"true\",\n\t\t\ttablist: role === \"tab\" ? tablistOf(el) : null,\n\t\t\tcontrolsTargets: controlsTargets(el),\n\t\t\thtml: (el.cloneNode(false) as Element).outerHTML,\n\t\t};\n\t});\n\n\t(window as unknown as Record<string, unknown>)[stashKey] = [...elements, ...tablists];\n\n\treturn collected;\n}\n","import type { CollectedTabRole } from \"./browser-scripts\";\n\n/**\n * - `tab-used-as-link`: a tab that links to another document and is associated with no panel.\n * - `tab-without-panel`: a tab with no panel it could control, because the page has none or every\n * one belongs to a tab in another tablist.\n * - `tabpanel-without-tab`: a tab panel no tab could control, because the page has no tabs or every\n * one belongs to another panel.\n * - `controls-not-a-tabpanel`: a tab whose `aria-controls` points at something other than a tab\n * panel.\n * - `association-unverifiable`: no association is declared, but one may be wired up some other way.\n */\nexport type TabRoleFinding =\n\t| \"tab-used-as-link\"\n\t| \"tab-without-panel\"\n\t| \"tabpanel-without-tab\"\n\t| \"controls-not-a-tabpanel\"\n\t| \"association-unverifiable\";\n\nexport type TabRoleVerdict = \"failed\" | \"needs-review\" | \"passed\";\n\nexport type TabRoleClassification = { verdict: TabRoleVerdict; finding: TabRoleFinding | null };\n\nconst PASSED: TabRoleClassification = { verdict: \"passed\", finding: null };\n\nfunction failed(finding: TabRoleFinding): TabRoleClassification {\n\treturn { verdict: \"failed\", finding };\n}\n\nfunction needsReview(finding: TabRoleFinding): TabRoleClassification {\n\treturn { verdict: \"needs-review\", finding };\n}\n\n/**\n * Classify each collected tab and tab panel by whether it is associated with a counterpart, in the\n * same order.\n */\nexport function classifyTabRoles(elements: readonly CollectedTabRole[]): TabRoleClassification[] {\n\tconst isTab = (i: number) => elements[i]?.role === \"tab\";\n\tconst isPanel = (i: number) => elements[i]?.role === \"tabpanel\";\n\n\t// An explicit association runs either way: the tab's `aria-controls` names the panel, or the\n\t// panel's `aria-labelledby` names the tab.\n\tconst partners = elements.map(() => new Set<number>());\n\n\telements.forEach((element, i) => {\n\t\tconst controlled = element.role === \"tab\" ? element.controls.filter(isPanel) : [];\n\t\tconst labellers = element.role === \"tabpanel\" ? element.labelledBy.filter(isTab) : [];\n\n\t\tfor (const panel of controlled) {\n\t\t\tpartners[i]!.add(panel);\n\t\t\tpartners[panel]!.add(i);\n\t\t}\n\n\t\tfor (const tab of labellers) {\n\t\t\tpartners[i]!.add(tab);\n\t\t\tpartners[tab]!.add(i);\n\t\t}\n\t});\n\n\tconst tabs = elements.flatMap((element, i) => (element.role === \"tab\" ? [i] : []));\n\tconst panels = elements.flatMap((element, i) => (element.role === \"tabpanel\" ? [i] : []));\n\tconst associated = (i: number) => partners[i]!.size > 0;\n\n\t// A tab whose `aria-controls` names a panel that is not mounted yet still has a panel of its own,\n\t// but one that names only other elements may be pointing at a wrapper around its panel.\n\tconst tabHasOwnPanel = (i: number) =>\n\t\tassociated(i) || (elements[i]!.hasControls && !elements[i]!.controlsOther);\n\n\t// Libraries that mount only the active panel leave the other tabs with nothing to point at:\n\t// React Aria puts `aria-controls` on the selected tab alone, Radix puts a dangling one on every\n\t// tab. Only that exact shape vouches for the unselected siblings: the selected tab paired with\n\t// one panel that is paired with it alone. A wired tab that is not selected, or one sharing\n\t// panels with other tabs, says nothing about whether its siblings were ever wired.\n\tconst pairedOneToOne = (i: number) => {\n\t\tconst [only, ...rest] = partners[i]!;\n\n\t\treturn only !== undefined && rest.length === 0 && partners[only]!.size === 1;\n\t};\n\tconst lazyTablists = new Set(\n\t\ttabs\n\t\t\t.filter((i) => elements[i]!.selected === true && pairedOneToOne(i))\n\t\t\t.flatMap((i) => {\n\t\t\t\tconst tablist = elements[i]!.tablist;\n\n\t\t\t\treturn typeof tablist === \"number\" ? [tablist] : [];\n\t\t\t}),\n\t);\n\tconst selectedSiblingVouches = (element: CollectedTabRole) =>\n\t\ttypeof element.tablist === \"number\" && lazyTablists.has(element.tablist);\n\n\treturn elements.map((element, i) => {\n\t\tif (associated(i)) {\n\t\t\treturn PASSED;\n\t\t}\n\n\t\tif (element.role === \"tabpanel\") {\n\t\t\treturn tabs.every(tabHasOwnPanel)\n\t\t\t\t? failed(\"tabpanel-without-tab\")\n\t\t\t\t: needsReview(\"association-unverifiable\");\n\t\t}\n\n\t\tif (element.navigates) {\n\t\t\treturn failed(\"tab-used-as-link\");\n\t\t}\n\n\t\tif (element.controlsOther) {\n\t\t\treturn needsReview(\"controls-not-a-tabpanel\");\n\t\t}\n\n\t\t// A leftover panel may be this tab's own, wired up wrongly, so that still goes to review.\n\t\tif (!element.selected && selectedSiblingVouches(element) && panels.every(associated)) {\n\t\t\treturn PASSED;\n\t\t}\n\n\t\t// A dangling `aria-controls` is how lazily mounted panels look, so only a page with no panels\n\t\t// at all fails such a tab.\n\t\tif (panels.length === 0 || (!element.hasControls && panels.every(associated))) {\n\t\t\treturn failed(\"tab-without-panel\");\n\t\t}\n\n\t\treturn needsReview(\"association-unverifiable\");\n\t});\n}\n"],"mappings":";AAAA;AAAA,EAEC;AAAA,EACA;AAAA,OACM;AACP,SAAS,oBAAoB;;;AC8CtB,SAAS,sBAAsB,UAAsC;AAC3E,QAAM,SAAS,CAAC,QACd,GAAG,aAAa,MAAM,KAAK,IAAI,KAAK,EAAE,MAAM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AE,IAAI,CAAC,gBAAgB,WAAW;AAAA,MAC3F,GAAG;AAAA,MACH;AAAA,MACA,SAAS,SAAS,KAAK;AAAA,IACxB,EAAE;AACF,UAAM,cAAc,CAAC,YACpB,WAAW,OAAO,CAAC,UAAU,MAAM,YAAY,OAAO;AAEvD,UAAMA,UAAS,YAAY,QAAQ;AACnC,UAAMC,eAAc,YAAY,cAAc;AAC9C,UAAM,SAAS,YAAY,QAAQ;AAEnC,WAAO;AAAA,MACN,QAAQ,MAAM,WAAW,SAASD,QAAO,MAAM,GAAG,WAAW,CAAC;AAAA,MAC9D,aAAa,MAAM,WAAW,SAASC,aAAY,MAAM,GAAG,WAAW,CAAC;AAAA,MACxE,QAAQ,MAAM,WAAW,SAAS,OAAO,MAAM,GAAG,WAAW,CAAC;AAAA,MAC9D,SAAS;AAAA,QACR,SAAS,WAAW;AAAA,QACpB,QAAQD,QAAO;AAAA,QACf,aAAaC,aAAY;AAAA,QACzB,QAAQ,OAAO;AAAA,MAChB;AAAA,IACD;AAAA,EACD,UAAE;AACD,UAAM,uBAAuB,SAAS,SAAS;AAAA,EAChD;AACD;","names":["failed","needsReview"]}
|
package/dist/result.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { TabRole } from "./browser-scripts";
|
|
1
|
+
import type { ControlsTarget, TabRole } from "./browser-scripts";
|
|
2
2
|
import type { TabRoleFinding } from "./classify";
|
|
3
3
|
export type OrphanedTabRolesElement = {
|
|
4
4
|
/** Selector for the element (see getSelector). */
|
|
@@ -8,6 +8,15 @@ export type OrphanedTabRolesElement = {
|
|
|
8
8
|
role: TabRole;
|
|
9
9
|
/** What is wrong with the element's association, or null when it has one. */
|
|
10
10
|
finding: TabRoleFinding | null;
|
|
11
|
+
/** Selector for the tablist a tab belongs to, or null when it is outside one. Tabs only. */
|
|
12
|
+
tablist?: string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Every id in the element's `aria-controls` with what it resolves to, so a dangling id or one
|
|
15
|
+
* that names something other than a tab panel can be seen. Present when `aria-controls` is set.
|
|
16
|
+
*/
|
|
17
|
+
controls?: ControlsTarget[];
|
|
18
|
+
/** Where the link goes. Present for `tab-used-as-link`. */
|
|
19
|
+
href?: string;
|
|
11
20
|
};
|
|
12
21
|
export type OrphanedTabRolesResult = {
|
|
13
22
|
/** Tabs and tab panels that provably have no counterpart. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a11y-pulse/orphaned-tab-roles-audit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "WCAG 1.3.1 and 4.1.2 audit that flags ARIA tabs with no tab panel and tab panels with no tab, including navigation links dressed up as tabs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"@a11y-pulse/browser-adaptor": "*"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"happy-dom": "^21.0.
|
|
58
|
-
"playwright-core": "^1.
|
|
59
|
-
"puppeteer": "^25.
|
|
57
|
+
"happy-dom": "^21.0.4",
|
|
58
|
+
"playwright-core": "^1.65.0",
|
|
59
|
+
"puppeteer": "^25.16.0"
|
|
60
60
|
}
|
|
61
61
|
}
|