@bevelo/testing 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +992 -0
- package/dist/index.d.cts +311 -0
- package/dist/index.d.ts +311 -0
- package/dist/index.js +965 -0
- package/package.json +39 -3
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,965 @@
|
|
|
1
|
+
import { accessibleName, accessibleRole, flush } from "@bevelo/core";
|
|
2
|
+
// src/aria.ts
|
|
3
|
+
/** Every attribute whose value is an ID reference (or a list of them). */
|
|
4
|
+
const REFERENCE_ATTRIBUTES = [
|
|
5
|
+
"aria-activedescendant",
|
|
6
|
+
"aria-controls",
|
|
7
|
+
"aria-describedby",
|
|
8
|
+
"aria-details",
|
|
9
|
+
"aria-errormessage",
|
|
10
|
+
"aria-flowto",
|
|
11
|
+
"aria-labelledby",
|
|
12
|
+
"aria-owns"
|
|
13
|
+
];
|
|
14
|
+
/** Raised when checkAria finds any problem; carries the full report. */
|
|
15
|
+
var AriaError = class extends Error {
|
|
16
|
+
report;
|
|
17
|
+
constructor(report) {
|
|
18
|
+
const listed = report.problems.map((problem) => ` - ${problem.message}`).join("\n");
|
|
19
|
+
super(`checkAria found ${report.problems.length} problem(s):\n${listed}`);
|
|
20
|
+
this.name = "AriaError";
|
|
21
|
+
this.report = report;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function problem(code, message, element) {
|
|
25
|
+
return element === void 0 ? {
|
|
26
|
+
code,
|
|
27
|
+
message
|
|
28
|
+
} : {
|
|
29
|
+
code,
|
|
30
|
+
message,
|
|
31
|
+
element
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function describeElement$1(element) {
|
|
35
|
+
const tag = element.tagName.toLowerCase();
|
|
36
|
+
const id = element.getAttribute("id");
|
|
37
|
+
const role = element.getAttribute("role");
|
|
38
|
+
if (id !== null && id !== "") return `<${tag} id="${id}">`;
|
|
39
|
+
if (role !== null && role !== "") return `<${tag} role="${role}">`;
|
|
40
|
+
return `<${tag}>`;
|
|
41
|
+
}
|
|
42
|
+
/** The root itself plus every descendant, so a check on a component's root
|
|
43
|
+
* element does not quietly skip the root. */
|
|
44
|
+
function elementsIn(root) {
|
|
45
|
+
const found = [...root.querySelectorAll("*")];
|
|
46
|
+
if (root instanceof Element) found.unshift(root);
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
function matchesIn(root, selector) {
|
|
50
|
+
const found = [...root.querySelectorAll(selector)];
|
|
51
|
+
if (root instanceof Element && root.matches(selector)) found.unshift(root);
|
|
52
|
+
return found;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Resolves an id inside the tree the element lives in.
|
|
56
|
+
*
|
|
57
|
+
* The document lookup alone fails for a detached fixture — a test can check the
|
|
58
|
+
* markup before it is appended — so a detached root's own subtree is searched
|
|
59
|
+
* too. A connected element uses getElementById, which is also the only way to
|
|
60
|
+
* see an id in another component's tree.
|
|
61
|
+
*/
|
|
62
|
+
function elementById(element, id) {
|
|
63
|
+
const rootNode = element.getRootNode();
|
|
64
|
+
if (rootNode instanceof Document) return rootNode.getElementById(id) ?? void 0;
|
|
65
|
+
if (rootNode instanceof Element && rootNode.id === id) return rootNode;
|
|
66
|
+
const scope = rootNode instanceof Element || rootNode instanceof DocumentFragment ? rootNode : void 0;
|
|
67
|
+
if (scope === void 0) return;
|
|
68
|
+
for (const candidate of scope.querySelectorAll("[id]")) if (candidate.id === id) return candidate;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Whether a keyboard user can reach the element.
|
|
72
|
+
*
|
|
73
|
+
* `tabindex="-1"` is focusable by script but not reachable from the keyboard,
|
|
74
|
+
* which is the set an aria-hidden check protects; `inert` removes the
|
|
75
|
+
* subtree from focus entirely, which is why the carousel can mark a slide
|
|
76
|
+
* aria-hidden without tripping this check.
|
|
77
|
+
*/
|
|
78
|
+
function isKeyboardFocusable(element) {
|
|
79
|
+
if (!(element instanceof HTMLElement)) return false;
|
|
80
|
+
if (element.hasAttribute("disabled")) return false;
|
|
81
|
+
if (element.closest("[inert]") !== null) return false;
|
|
82
|
+
const tabindex = element.getAttribute("tabindex");
|
|
83
|
+
if (tabindex !== null) {
|
|
84
|
+
const value = Number(tabindex);
|
|
85
|
+
return Number.isInteger(value) && value >= 0;
|
|
86
|
+
}
|
|
87
|
+
if (element.isContentEditable === true) return true;
|
|
88
|
+
const tag = element.tagName.toLowerCase();
|
|
89
|
+
if (tag === "a" || tag === "area") return element.hasAttribute("href");
|
|
90
|
+
if (tag === "input") return (element.getAttribute("type") ?? "text").toLowerCase() !== "hidden";
|
|
91
|
+
return tag === "button" || tag === "select" || tag === "textarea" || tag === "summary";
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Checks a rendered subtree against the ARIA contract and the caller's
|
|
95
|
+
* expectations, and throws with every problem listed rather than the first.
|
|
96
|
+
*
|
|
97
|
+
* The invariants are always checked:
|
|
98
|
+
* - ids inside the root are unique;
|
|
99
|
+
* - every ID reference an ARIA attribute names resolves in the document;
|
|
100
|
+
* - no keyboard-focusable element sits inside an aria-hidden subtree;
|
|
101
|
+
* - when `options.tabStop` is given, exactly one of its matches is a tab stop.
|
|
102
|
+
*/
|
|
103
|
+
function checkAria(root, expectations = [], options = {}) {
|
|
104
|
+
const problems = [];
|
|
105
|
+
const elements = elementsIn(root);
|
|
106
|
+
const ids = /* @__PURE__ */ new Map();
|
|
107
|
+
for (const element of elements) {
|
|
108
|
+
const id = element.getAttribute("id");
|
|
109
|
+
if (id === null || id === "") continue;
|
|
110
|
+
const first = ids.get(id);
|
|
111
|
+
if (first !== void 0) {
|
|
112
|
+
problems.push(problem("duplicate-id", `duplicate id "${id}" on ${describeElement$1(first)} and ${describeElement$1(element)}`, element));
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
ids.set(id, element);
|
|
116
|
+
}
|
|
117
|
+
for (const element of elements) for (const attribute of REFERENCE_ATTRIBUTES) {
|
|
118
|
+
const value = element.getAttribute(attribute);
|
|
119
|
+
if (value === null) continue;
|
|
120
|
+
const tokens = value.trim().split(/\s+/u).filter((token) => token !== "");
|
|
121
|
+
if (tokens.length === 0) {
|
|
122
|
+
problems.push(problem("empty-reference", `${describeElement$1(element)} has an empty ${attribute}`, element));
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
for (const token of tokens) if (elementById(element, token) === void 0) problems.push(problem("dangling-reference", `${describeElement$1(element)} ${attribute}="${token}" does not resolve to an element`, element));
|
|
126
|
+
}
|
|
127
|
+
for (const element of elements) {
|
|
128
|
+
if (!isKeyboardFocusable(element)) continue;
|
|
129
|
+
if (element.getAttribute("aria-hidden") === "true") {
|
|
130
|
+
problems.push(problem("hidden-focusable", `${describeElement$1(element)} is focusable and marked aria-hidden`, element));
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
let ancestor = element.parentElement;
|
|
134
|
+
while (ancestor !== null) {
|
|
135
|
+
if (ancestor.getAttribute("aria-hidden") === "true") {
|
|
136
|
+
problems.push(problem("hidden-focusable", `${describeElement$1(element)} is focusable but ${describeElement$1(ancestor)} is aria-hidden`, element));
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
if (ancestor === root) break;
|
|
140
|
+
ancestor = ancestor.parentElement;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (options.tabStop !== void 0) {
|
|
144
|
+
const group = matchesIn(root, options.tabStop);
|
|
145
|
+
if (group.length === 0) problems.push(problem("tab-stop-count", `tabStop selector "${options.tabStop}" matched no elements`));
|
|
146
|
+
else {
|
|
147
|
+
const stops = group.filter((element) => element.getAttribute("tabindex") === "0");
|
|
148
|
+
if (stops.length !== 1) problems.push(problem("tab-stop-count", `expected exactly one roving tab stop among "${options.tabStop}", found ${stops.length}`));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const expectation of expectations) {
|
|
152
|
+
const matches = matchesIn(root, expectation.selector);
|
|
153
|
+
if (matches.length === 0) {
|
|
154
|
+
problems.push(problem("missing-element", `no element matches "${expectation.selector}"`));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (expectation.count !== void 0 && matches.length !== expectation.count) problems.push(problem("count-mismatch", `expected ${expectation.count} match(es) for "${expectation.selector}", found ${matches.length}`));
|
|
158
|
+
for (const element of matches) {
|
|
159
|
+
if (expectation.role !== void 0) {
|
|
160
|
+
const actual = accessibleRole(element);
|
|
161
|
+
if (actual !== expectation.role) problems.push(problem("role-mismatch", `${describeElement$1(element)} should have role "${expectation.role}", found "${actual ?? "none"}"`, element));
|
|
162
|
+
}
|
|
163
|
+
if (expectation.name !== void 0) {
|
|
164
|
+
const actual = accessibleName(element);
|
|
165
|
+
if (actual !== expectation.name) problems.push(problem("name-mismatch", `${describeElement$1(element)} should be named "${expectation.name}", found "${actual}"`, element));
|
|
166
|
+
}
|
|
167
|
+
for (const [attribute, expected] of Object.entries(expectation.attrs ?? {})) {
|
|
168
|
+
const actual = element.getAttribute(attribute);
|
|
169
|
+
if (expected === null || expected === false) {
|
|
170
|
+
if (actual !== null) problems.push(problem("attribute-mismatch", `${describeElement$1(element)} should not have ${attribute} (found "${actual}")`, element));
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (expected === true) {
|
|
174
|
+
if (actual === null) problems.push(problem("attribute-mismatch", `${describeElement$1(element)} should have ${attribute}`, element));
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (actual !== expected) problems.push(problem("attribute-mismatch", `${describeElement$1(element)} should have ${attribute}="${expected}", found "${actual ?? ""}"`, element));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const report = {
|
|
182
|
+
ok: problems.length === 0,
|
|
183
|
+
problems
|
|
184
|
+
};
|
|
185
|
+
if (!report.ok) throw new AriaError(report);
|
|
186
|
+
return report;
|
|
187
|
+
}
|
|
188
|
+
// src/mount.ts
|
|
189
|
+
function describeAvailable(elements, withNames) {
|
|
190
|
+
if (elements.length === 0) return "none";
|
|
191
|
+
return elements.map((element) => {
|
|
192
|
+
const role = accessibleRole(element) ?? "none";
|
|
193
|
+
return withNames ? `${role} "${accessibleName(element)}"` : role;
|
|
194
|
+
}).join(", ");
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Creates a component, appends it to a container, settles the first render and
|
|
198
|
+
* returns queries scoped to the component's root.
|
|
199
|
+
*
|
|
200
|
+
* The wrapper div is removed by dispose(); a caller-provided container is left
|
|
201
|
+
* in place, because the test owns it.
|
|
202
|
+
*/
|
|
203
|
+
function mount(factory, props, options = {}) {
|
|
204
|
+
const container = options.container ?? document.createElement("div");
|
|
205
|
+
const ownsContainer = options.container === void 0;
|
|
206
|
+
if (ownsContainer) document.body.append(container);
|
|
207
|
+
const instance = factory(props);
|
|
208
|
+
const root = instance.element;
|
|
209
|
+
container.append(root);
|
|
210
|
+
flush();
|
|
211
|
+
let disposed = false;
|
|
212
|
+
const guard = (action) => {
|
|
213
|
+
if (disposed) throw new Error(`mount: cannot ${action} after dispose()`);
|
|
214
|
+
};
|
|
215
|
+
const resolve = (target) => {
|
|
216
|
+
if (target instanceof Element) return target;
|
|
217
|
+
if (typeof target === "string") {
|
|
218
|
+
const found = root.matches(target) ? root : root.querySelector(target);
|
|
219
|
+
if (!(found instanceof Element)) throw new Error(`mount: no element matches "${target}"`);
|
|
220
|
+
return found;
|
|
221
|
+
}
|
|
222
|
+
const active = document.activeElement;
|
|
223
|
+
if (active instanceof HTMLElement && root.contains(active)) return active;
|
|
224
|
+
return root;
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
root,
|
|
228
|
+
instance,
|
|
229
|
+
get api() {
|
|
230
|
+
return instance.api;
|
|
231
|
+
},
|
|
232
|
+
update(next) {
|
|
233
|
+
guard("update()");
|
|
234
|
+
instance.update(next);
|
|
235
|
+
flush();
|
|
236
|
+
},
|
|
237
|
+
flush() {
|
|
238
|
+
flush();
|
|
239
|
+
},
|
|
240
|
+
dispose() {
|
|
241
|
+
if (disposed) return;
|
|
242
|
+
disposed = true;
|
|
243
|
+
instance.dispose();
|
|
244
|
+
root.remove();
|
|
245
|
+
if (ownsContainer) container.remove();
|
|
246
|
+
},
|
|
247
|
+
query(selector) {
|
|
248
|
+
const found = root.querySelector(selector);
|
|
249
|
+
return found instanceof HTMLElement ? found : null;
|
|
250
|
+
},
|
|
251
|
+
queryAll(selector) {
|
|
252
|
+
return [...root.querySelectorAll(selector)].filter((element) => element instanceof HTMLElement);
|
|
253
|
+
},
|
|
254
|
+
get(role, name) {
|
|
255
|
+
const candidates = [root, ...root.querySelectorAll("*")].filter((element) => element instanceof HTMLElement && accessibleRole(element) === role);
|
|
256
|
+
if (candidates.length === 0) {
|
|
257
|
+
const roles = new Set([root, ...root.querySelectorAll("*")].map((element) => accessibleRole(element)).filter((value) => value !== void 0));
|
|
258
|
+
throw new Error(`mount.get: no element has role "${role}". Roles present: ${roles.size === 0 ? "none" : [...roles].join(", ")}`);
|
|
259
|
+
}
|
|
260
|
+
if (name === void 0) {
|
|
261
|
+
const [first] = candidates;
|
|
262
|
+
if (first === void 0) throw new Error(`mount.get: no element has role "${role}"`);
|
|
263
|
+
return first;
|
|
264
|
+
}
|
|
265
|
+
const named = candidates.find((element) => accessibleName(element) === name);
|
|
266
|
+
if (named === void 0) throw new Error(`mount.get: no "${role}" named "${name}". Available: ${describeAvailable(candidates, true)}`);
|
|
267
|
+
return named;
|
|
268
|
+
},
|
|
269
|
+
press(key, target) {
|
|
270
|
+
guard("press()");
|
|
271
|
+
const event = new KeyboardEvent("keydown", {
|
|
272
|
+
key,
|
|
273
|
+
bubbles: true,
|
|
274
|
+
cancelable: true,
|
|
275
|
+
composed: true
|
|
276
|
+
});
|
|
277
|
+
resolve(target).dispatchEvent(event);
|
|
278
|
+
return event;
|
|
279
|
+
},
|
|
280
|
+
click(target) {
|
|
281
|
+
guard("click()");
|
|
282
|
+
const element = resolve(target);
|
|
283
|
+
if (!(element instanceof HTMLElement)) throw new Error("mount.click: the target is not an HTML element");
|
|
284
|
+
element.click();
|
|
285
|
+
},
|
|
286
|
+
html() {
|
|
287
|
+
return root.outerHTML;
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
// src/direction.ts
|
|
292
|
+
/**
|
|
293
|
+
* The items a horizontal group moves between, by role.
|
|
294
|
+
*
|
|
295
|
+
* Roles, not `[data-value]`: several components put `data-value` on their panels
|
|
296
|
+
* and on their root as well (a tabpanel names the tab it belongs to), and the
|
|
297
|
+
* panels are focusable, so a data-attribute selector would walk into them. A
|
|
298
|
+
* header host whose group renders buttons rather than items passes its own
|
|
299
|
+
* selector.
|
|
300
|
+
*/
|
|
301
|
+
const DEFAULT_SELECTOR = [
|
|
302
|
+
"[role=\"tab\"]",
|
|
303
|
+
"[role=\"radio\"]",
|
|
304
|
+
"[role=\"option\"]",
|
|
305
|
+
"[role=\"menuitem\"]",
|
|
306
|
+
"[role=\"menuitemcheckbox\"]",
|
|
307
|
+
"[role=\"menuitemradio\"]",
|
|
308
|
+
"[role=\"treeitem\"]",
|
|
309
|
+
"[role=\"slider\"]",
|
|
310
|
+
"[role=\"switch\"]",
|
|
311
|
+
"[role=\"checkbox\"]",
|
|
312
|
+
"[aria-pressed]"
|
|
313
|
+
].join(", ");
|
|
314
|
+
function defaultRead(mounted) {
|
|
315
|
+
const active = document.activeElement;
|
|
316
|
+
if (!(active instanceof HTMLElement) || !mounted.root.contains(active)) return "";
|
|
317
|
+
return active.getAttribute("data-value") ?? (active.textContent ?? "").trim();
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Focuses the first item, walks to the middle using the component's own "forward"
|
|
321
|
+
* key, then presses `key` and reports where it started and what it landed on.
|
|
322
|
+
*
|
|
323
|
+
* Two details matter. The walk uses the public keyboard contract rather than
|
|
324
|
+
* `focus()` on an item, because a group keeps its own idea of where the movement
|
|
325
|
+
* starts from (a radio group moves from its checked item, not from whatever has
|
|
326
|
+
* DOM focus). And starting in the middle matters: a clamped group at its first
|
|
327
|
+
* item cannot move backwards, so one end would make a correct RTL component look
|
|
328
|
+
* like it ignored the direction.
|
|
329
|
+
*/
|
|
330
|
+
function pressIn(factory, props, dir, key, forward, mirrored, options) {
|
|
331
|
+
const html = document.documentElement;
|
|
332
|
+
const previous = html.getAttribute("dir");
|
|
333
|
+
html.setAttribute("dir", dir);
|
|
334
|
+
const mounted = mount(factory, props);
|
|
335
|
+
try {
|
|
336
|
+
const all = [...mounted.root.matches(options.selector) ? [mounted.root] : [], ...mounted.root.querySelectorAll(options.selector)];
|
|
337
|
+
const items = all.filter((candidate) => !all.some((other) => other !== candidate && candidate.contains(other)));
|
|
338
|
+
if (items.length === 0) throw new Error(`checkDirection: no item matches "${options.selector}"`);
|
|
339
|
+
const fire = (pressed) => {
|
|
340
|
+
(document.activeElement instanceof Element ? document.activeElement : mounted.root).dispatchEvent(new KeyboardEvent("keydown", {
|
|
341
|
+
key: pressed,
|
|
342
|
+
bubbles: true,
|
|
343
|
+
cancelable: true,
|
|
344
|
+
composed: true
|
|
345
|
+
}));
|
|
346
|
+
};
|
|
347
|
+
items[0].focus();
|
|
348
|
+
const forwardKey = dir === "ltr" ? forward : mirrored;
|
|
349
|
+
for (let step = 0; step < Math.floor((items.length - 1) / 2); step += 1) fire(forwardKey);
|
|
350
|
+
const before = options.read(mounted);
|
|
351
|
+
fire(key);
|
|
352
|
+
return {
|
|
353
|
+
before,
|
|
354
|
+
after: options.read(mounted)
|
|
355
|
+
};
|
|
356
|
+
} finally {
|
|
357
|
+
mounted.dispose();
|
|
358
|
+
if (previous === null) html.removeAttribute("dir");
|
|
359
|
+
else html.setAttribute("dir", previous);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Mounts a component left to right and right to left and presses the two
|
|
364
|
+
* horizontal arrows, checking the direction contract:
|
|
365
|
+
*
|
|
366
|
+
* - the mirrored key in RTL lands where the forward key lands in LTR, and
|
|
367
|
+
* - the forward key in RTL does **not** land there (it moves the other way, or
|
|
368
|
+
* stays put at the end of a clamped group).
|
|
369
|
+
*
|
|
370
|
+
* Throws one error with every problem when the component ignores `dir`, when the
|
|
371
|
+
* arrows do nothing at all (there is nothing to check), and when the two
|
|
372
|
+
* directions agree.
|
|
373
|
+
*/
|
|
374
|
+
function checkDirection(factory, props = {}, options = {}) {
|
|
375
|
+
const selector = options.selector ?? DEFAULT_SELECTOR;
|
|
376
|
+
const key = options.key ?? "ArrowRight";
|
|
377
|
+
const mirrored = options.mirrored ?? "ArrowLeft";
|
|
378
|
+
const read = options.read ?? defaultRead;
|
|
379
|
+
const run = (direction, pressed) => pressIn(factory, props, direction, pressed, key, mirrored, {
|
|
380
|
+
selector,
|
|
381
|
+
read
|
|
382
|
+
});
|
|
383
|
+
const forward = {
|
|
384
|
+
ltr: run("ltr", key),
|
|
385
|
+
rtl: run("rtl", key)
|
|
386
|
+
};
|
|
387
|
+
const crossed = {
|
|
388
|
+
ltr: run("ltr", mirrored),
|
|
389
|
+
rtl: run("rtl", mirrored)
|
|
390
|
+
};
|
|
391
|
+
const problems = [];
|
|
392
|
+
if (forward.ltr.before === forward.ltr.after) problems.push(`"${key}" moved nothing in LTR, so there is nothing to compare`);
|
|
393
|
+
if (forward.rtl.after === forward.ltr.after) problems.push(`"${key}" lands on "${forward.rtl.after}" in both directions: the component ignores dir=rtl`);
|
|
394
|
+
if (crossed.rtl.after !== forward.ltr.after) problems.push(`"${mirrored}" in RTL lands on "${crossed.rtl.after}", but "${key}" in LTR lands on "${forward.ltr.after}"`);
|
|
395
|
+
if (crossed.ltr.after !== forward.rtl.after) problems.push(`"${mirrored}" in LTR lands on "${crossed.ltr.after}", but "${key}" in RTL lands on "${forward.rtl.after}"`);
|
|
396
|
+
if (problems.length > 0) throw new Error(`checkDirection found ${problems.length} problem(s):\n - ${problems.join("\n - ")}`);
|
|
397
|
+
return {
|
|
398
|
+
forward: {
|
|
399
|
+
ltr: forward.ltr.after,
|
|
400
|
+
rtl: forward.rtl.after
|
|
401
|
+
},
|
|
402
|
+
mirrored: {
|
|
403
|
+
ltr: crossed.ltr.after,
|
|
404
|
+
rtl: crossed.rtl.after
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
// src/pattern.ts
|
|
409
|
+
/** Raised at the first step whose expectation or focus precondition fails. */
|
|
410
|
+
var PatternError = class extends Error {
|
|
411
|
+
/** 1-based number of the failing step. */
|
|
412
|
+
step;
|
|
413
|
+
key;
|
|
414
|
+
constructor(step, key, detail) {
|
|
415
|
+
super(`checkPattern failed at step ${step} (key "${key}"): ${detail}`);
|
|
416
|
+
this.name = "PatternError";
|
|
417
|
+
this.step = step;
|
|
418
|
+
this.key = key;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
function normalizeText(text) {
|
|
422
|
+
return text.replace(/\s+/gu, " ").trim();
|
|
423
|
+
}
|
|
424
|
+
function describeElement(element) {
|
|
425
|
+
const tag = element.tagName.toLowerCase();
|
|
426
|
+
const role = element.getAttribute("role");
|
|
427
|
+
return role === null ? `<${tag}>` : `<${tag} role="${role}">`;
|
|
428
|
+
}
|
|
429
|
+
function booleanMismatch(element, attribute, expected, onValues = ["true"]) {
|
|
430
|
+
const actual = element.getAttribute(attribute);
|
|
431
|
+
if ((actual !== null && onValues.includes(actual)) === expected) return;
|
|
432
|
+
return expected ? `expected ${describeElement(element)} to have ${attribute}, found "${actual ?? "absent"}"` : `expected ${describeElement(element)} not to have ${attribute}, found "${actual ?? "absent"}"`;
|
|
433
|
+
}
|
|
434
|
+
function checkExpectation(expectation) {
|
|
435
|
+
const focused = document.activeElement;
|
|
436
|
+
if (!(focused instanceof HTMLElement)) return "expected an element to be focused, found none";
|
|
437
|
+
if (expectation.role !== void 0) {
|
|
438
|
+
const actual = accessibleRole(focused);
|
|
439
|
+
if (actual !== expectation.role) return `expected focused element to have role "${expectation.role}", found ${describeElement(focused)} with role "${actual ?? "none"}"`;
|
|
440
|
+
}
|
|
441
|
+
if (expectation.name !== void 0) {
|
|
442
|
+
const actual = accessibleName(focused);
|
|
443
|
+
if (actual !== expectation.name) return `expected focused element to be named "${expectation.name}", found "${actual}"`;
|
|
444
|
+
}
|
|
445
|
+
if (expectation.selected !== void 0) {
|
|
446
|
+
const mismatch = booleanMismatch(focused, "aria-selected", expectation.selected);
|
|
447
|
+
if (mismatch !== void 0) return mismatch;
|
|
448
|
+
}
|
|
449
|
+
if (expectation.expanded !== void 0) {
|
|
450
|
+
const mismatch = booleanMismatch(focused, "aria-expanded", expectation.expanded);
|
|
451
|
+
if (mismatch !== void 0) return mismatch;
|
|
452
|
+
}
|
|
453
|
+
if (expectation.checked !== void 0) {
|
|
454
|
+
if (expectation.checked === "mixed") {
|
|
455
|
+
if (focused.getAttribute("aria-checked") !== "mixed") return `expected focused element to be aria-checked="mixed", found "${focused.getAttribute("aria-checked") ?? "absent"}"`;
|
|
456
|
+
} else {
|
|
457
|
+
const mismatch = booleanMismatch(focused, "aria-checked", expectation.checked);
|
|
458
|
+
if (mismatch !== void 0) return mismatch;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (expectation.pressed !== void 0) {
|
|
462
|
+
if (expectation.pressed === "mixed") {
|
|
463
|
+
if (focused.getAttribute("aria-pressed") !== "mixed") return `expected focused element to be aria-pressed="mixed", found "${focused.getAttribute("aria-pressed") ?? "absent"}"`;
|
|
464
|
+
} else {
|
|
465
|
+
const mismatch = booleanMismatch(focused, "aria-pressed", expectation.pressed);
|
|
466
|
+
if (mismatch !== void 0) return mismatch;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (expectation.disabled !== void 0) {
|
|
470
|
+
if ((focused.hasAttribute("disabled") || focused.getAttribute("aria-disabled") === "true") !== expectation.disabled) return `expected focused element to be ${expectation.disabled ? "" : "not "}disabled`;
|
|
471
|
+
}
|
|
472
|
+
if (expectation.text !== void 0) {
|
|
473
|
+
const actual = normalizeText(focused.textContent ?? "");
|
|
474
|
+
const expected = normalizeText(expectation.text);
|
|
475
|
+
if (actual !== expected) return `expected focused element text "${expected}", found "${actual}"`;
|
|
476
|
+
}
|
|
477
|
+
for (const [attribute, expected] of Object.entries(expectation.attrs ?? {})) {
|
|
478
|
+
const actual = focused.getAttribute(attribute);
|
|
479
|
+
if (expected === null || expected === false) {
|
|
480
|
+
if (actual !== null) return `expected focused element to lack ${attribute}, found "${actual}"`;
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (expected === true) {
|
|
484
|
+
if (actual === null) return `expected focused element to have ${attribute}`;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (actual !== expected) return `expected focused element to have ${attribute}="${expected}", found "${actual ?? "absent"}"`;
|
|
488
|
+
}
|
|
489
|
+
if (expectation.activeDescendant !== void 0) {
|
|
490
|
+
const id = focused.getAttribute("aria-activedescendant");
|
|
491
|
+
if (id === null || id === "") return "expected the focused element to name an aria-activedescendant, found none";
|
|
492
|
+
const active = focused.ownerDocument.getElementById(id);
|
|
493
|
+
if (active === null) return `aria-activedescendant="${id}" does not resolve to an element`;
|
|
494
|
+
const row = expectation.activeDescendant;
|
|
495
|
+
if (row.role !== void 0) {
|
|
496
|
+
const actual = accessibleRole(active);
|
|
497
|
+
if (actual !== row.role) return `expected the active row to have role "${row.role}", found "${actual ?? "none"}"`;
|
|
498
|
+
}
|
|
499
|
+
if (row.name !== void 0) {
|
|
500
|
+
const actual = accessibleName(active);
|
|
501
|
+
if (actual !== normalizeText(row.name)) return `expected the active row to be named "${row.name}", found "${actual}"`;
|
|
502
|
+
}
|
|
503
|
+
if (row.text !== void 0) {
|
|
504
|
+
const actual = normalizeText(active.textContent ?? "");
|
|
505
|
+
const expected = normalizeText(row.text);
|
|
506
|
+
if (actual !== expected) return `expected the active row text "${expected}", found "${actual}"`;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function isWithin(root, element) {
|
|
511
|
+
return root instanceof Element ? root === element || root.contains(element) : true;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Runs an APG keyboard sequence against a rendered subtree.
|
|
515
|
+
*
|
|
516
|
+
* Each step dispatches keydown to the focused element (or `step.target`),
|
|
517
|
+
* settles the render, then checks `step.expect` against the element that ends
|
|
518
|
+
* up focused. A missing focus is a precondition failure, not a silent pass:
|
|
519
|
+
* dispatching a key to the body would test nothing.
|
|
520
|
+
*/
|
|
521
|
+
function checkPattern(root, steps) {
|
|
522
|
+
const keys = [];
|
|
523
|
+
for (const [index, step] of steps.entries()) {
|
|
524
|
+
const stepNumber = index + 1;
|
|
525
|
+
keys.push(step.key);
|
|
526
|
+
const target = step.target !== void 0 ? step.target : document.activeElement;
|
|
527
|
+
const element = target instanceof Element ? target : typeof target === "string" ? root instanceof Element && root.matches(target) ? root : root.querySelector(target) : null;
|
|
528
|
+
const focusedByDefault = step.target === void 0;
|
|
529
|
+
if (!(element instanceof Element) || focusedByDefault && (element === document.body || element === document.documentElement)) {
|
|
530
|
+
const detail = typeof target === "string" ? `no element matches "${target}"` : "nothing is focused; focus an element or pass target";
|
|
531
|
+
throw new PatternError(stepNumber, step.key, detail);
|
|
532
|
+
}
|
|
533
|
+
if (!isWithin(root, element)) throw new PatternError(stepNumber, step.key, `"${describeElement(element)}" is outside the root passed to checkPattern`);
|
|
534
|
+
const event = new KeyboardEvent("keydown", {
|
|
535
|
+
...step.init ?? {},
|
|
536
|
+
key: step.key,
|
|
537
|
+
bubbles: true,
|
|
538
|
+
cancelable: true,
|
|
539
|
+
composed: true
|
|
540
|
+
});
|
|
541
|
+
element.dispatchEvent(event);
|
|
542
|
+
flush();
|
|
543
|
+
if (step.expect !== void 0) {
|
|
544
|
+
const detail = checkExpectation(step.expect);
|
|
545
|
+
if (detail !== void 0) throw new PatternError(stepNumber, step.key, detail);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
steps: steps.length,
|
|
550
|
+
keys
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
// src/markup.ts
|
|
554
|
+
/** Raised by parseMarkup, with the offset where the markup stopped making sense. */
|
|
555
|
+
var MarkupParseError = class extends Error {
|
|
556
|
+
/** Zero-based offset into the input. */
|
|
557
|
+
index;
|
|
558
|
+
constructor(message, index) {
|
|
559
|
+
super(`parseMarkup: ${message} at index ${index}`);
|
|
560
|
+
this.name = "MarkupParseError";
|
|
561
|
+
this.index = index;
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
565
|
+
"area",
|
|
566
|
+
"base",
|
|
567
|
+
"br",
|
|
568
|
+
"col",
|
|
569
|
+
"embed",
|
|
570
|
+
"hr",
|
|
571
|
+
"img",
|
|
572
|
+
"input",
|
|
573
|
+
"link",
|
|
574
|
+
"meta",
|
|
575
|
+
"param",
|
|
576
|
+
"source",
|
|
577
|
+
"track",
|
|
578
|
+
"wbr"
|
|
579
|
+
]);
|
|
580
|
+
const TAG_PATTERN = /^[A-Za-z][A-Za-z0-9._:-]*$/u;
|
|
581
|
+
const ATTRIBUTE_PATTERN = /^[A-Za-z_:][A-Za-z0-9._:-]*$/u;
|
|
582
|
+
const NAMED_ENTITIES = {
|
|
583
|
+
amp: "&",
|
|
584
|
+
apos: "'",
|
|
585
|
+
gt: ">",
|
|
586
|
+
lt: "<",
|
|
587
|
+
nbsp: "\xA0",
|
|
588
|
+
quot: "\""
|
|
589
|
+
};
|
|
590
|
+
function escapeText(value) {
|
|
591
|
+
return value.replace(/[&<>]/gu, (character) => {
|
|
592
|
+
switch (character) {
|
|
593
|
+
case "&": return "&";
|
|
594
|
+
case "<": return "<";
|
|
595
|
+
default: return ">";
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
function escapeAttribute(value) {
|
|
600
|
+
return value.replace(/[&<>"]/gu, (character) => {
|
|
601
|
+
switch (character) {
|
|
602
|
+
case "&": return "&";
|
|
603
|
+
case "<": return "<";
|
|
604
|
+
case ">": return ">";
|
|
605
|
+
default: return """;
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
function renderNode(node, out) {
|
|
610
|
+
if (!TAG_PATTERN.test(node.tag)) throw new TypeError(`renderMarkup: invalid element name "${node.tag}"`);
|
|
611
|
+
out.push("<", node.tag);
|
|
612
|
+
for (const [name, value] of Object.entries(node.attrs ?? {})) {
|
|
613
|
+
if (!ATTRIBUTE_PATTERN.test(name)) throw new TypeError(`renderMarkup: invalid attribute name "${name}" on <${node.tag}>`);
|
|
614
|
+
if (value === void 0 || value === null || value === false) continue;
|
|
615
|
+
if (value === true) {
|
|
616
|
+
out.push(" ", name, "=\"\"");
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
out.push(" ", name, "=\"", escapeAttribute(String(value)), "\"");
|
|
620
|
+
}
|
|
621
|
+
const children = node.children ?? [];
|
|
622
|
+
const hasChildren = children.some((child) => child !== null && child !== void 0 && child !== false);
|
|
623
|
+
if (VOID_ELEMENTS.has(node.tag.toLowerCase())) {
|
|
624
|
+
if (hasChildren) throw new TypeError(`renderMarkup: void element <${node.tag}> cannot have children`);
|
|
625
|
+
out.push(">");
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
out.push(">");
|
|
629
|
+
for (const child of children) {
|
|
630
|
+
if (typeof child === "string") {
|
|
631
|
+
out.push(escapeText(child));
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (typeof child === "number") {
|
|
635
|
+
out.push(escapeText(String(child)));
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
if (typeof child === "object" && child !== null) renderNode(child, out);
|
|
639
|
+
}
|
|
640
|
+
out.push("</", node.tag, ">");
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Serialises a description tree (or a parsed tree) to HTML.
|
|
644
|
+
*
|
|
645
|
+
* Attributes are written in object order, booleans as `name=""`, and false,
|
|
646
|
+
* null and undefined are omitted — the same rules the renderer applies. Void
|
|
647
|
+
* elements get no closing tag and reject children, because a description that
|
|
648
|
+
* puts children inside <br> has no HTML representation.
|
|
649
|
+
*/
|
|
650
|
+
function renderMarkup(description) {
|
|
651
|
+
const out = [];
|
|
652
|
+
renderNode(description, out);
|
|
653
|
+
return out.join("");
|
|
654
|
+
}
|
|
655
|
+
function decodeEntities(value) {
|
|
656
|
+
return value.replace(/&(#(?:x[0-9A-Fa-f]+|[0-9]+)|[A-Za-z][A-Za-z0-9]*);/gu, (whole, body) => {
|
|
657
|
+
if (body.startsWith("#")) {
|
|
658
|
+
const hexadecimal = body.charAt(1) === "x" || body.charAt(1) === "X";
|
|
659
|
+
const digits = body.slice(hexadecimal ? 2 : 1);
|
|
660
|
+
const code = Number.parseInt(digits, hexadecimal ? 16 : 10);
|
|
661
|
+
if (!Number.isFinite(code) || code <= 0 || code > 1114111) return whole;
|
|
662
|
+
return String.fromCodePoint(code);
|
|
663
|
+
}
|
|
664
|
+
return NAMED_ENTITIES[body] ?? whole;
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
function makeNode(tag, attrs, children) {
|
|
668
|
+
const node = { tag };
|
|
669
|
+
if (Object.keys(attrs).length > 0) node.attrs = attrs;
|
|
670
|
+
if (children.length > 0) node.children = [...children];
|
|
671
|
+
return node;
|
|
672
|
+
}
|
|
673
|
+
var MarkupParser = class {
|
|
674
|
+
source;
|
|
675
|
+
index = 0;
|
|
676
|
+
constructor(source) {
|
|
677
|
+
this.source = source;
|
|
678
|
+
}
|
|
679
|
+
parse() {
|
|
680
|
+
this.skipWhitespace();
|
|
681
|
+
if (this.atEnd()) this.fail("expected an element, found end of markup");
|
|
682
|
+
const node = this.parseElement();
|
|
683
|
+
this.skipWhitespace();
|
|
684
|
+
if (!this.atEnd()) this.fail("unexpected content after the root element");
|
|
685
|
+
return node;
|
|
686
|
+
}
|
|
687
|
+
atEnd() {
|
|
688
|
+
return this.index >= this.source.length;
|
|
689
|
+
}
|
|
690
|
+
charAt(offset = 0) {
|
|
691
|
+
return this.source.charAt(this.index + offset);
|
|
692
|
+
}
|
|
693
|
+
fail(message) {
|
|
694
|
+
throw new MarkupParseError(message, this.index);
|
|
695
|
+
}
|
|
696
|
+
skipWhitespace() {
|
|
697
|
+
while (!this.atEnd() && /\s/u.test(this.charAt())) this.index += 1;
|
|
698
|
+
}
|
|
699
|
+
readName(kind) {
|
|
700
|
+
const match = this.source.slice(this.index).match(/^[A-Za-z_:][A-Za-z0-9._:-]*/u);
|
|
701
|
+
if (match === null) this.fail(`expected ${kind}`);
|
|
702
|
+
const name = match[0] ?? "";
|
|
703
|
+
this.index += name.length;
|
|
704
|
+
return name;
|
|
705
|
+
}
|
|
706
|
+
parseElement() {
|
|
707
|
+
if (this.charAt() !== "<") this.fail("expected \"<\"");
|
|
708
|
+
this.index += 1;
|
|
709
|
+
if (this.charAt() === "/") this.fail("unexpected closing tag");
|
|
710
|
+
const tag = this.readName("an element name");
|
|
711
|
+
const attrs = {};
|
|
712
|
+
let selfClosing = false;
|
|
713
|
+
for (;;) {
|
|
714
|
+
this.skipWhitespace();
|
|
715
|
+
if (this.atEnd()) this.fail(`unterminated <${tag}>`);
|
|
716
|
+
const character = this.charAt();
|
|
717
|
+
if (character === ">") {
|
|
718
|
+
this.index += 1;
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
if (character === "/" && this.charAt(1) === ">") {
|
|
722
|
+
this.index += 2;
|
|
723
|
+
selfClosing = true;
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
const name = this.readName("an attribute name");
|
|
727
|
+
this.skipWhitespace();
|
|
728
|
+
let value = "";
|
|
729
|
+
if (this.charAt() === "=") {
|
|
730
|
+
this.index += 1;
|
|
731
|
+
this.skipWhitespace();
|
|
732
|
+
value = this.readAttributeValue(tag, name);
|
|
733
|
+
}
|
|
734
|
+
attrs[name] = value;
|
|
735
|
+
}
|
|
736
|
+
const isVoid = VOID_ELEMENTS.has(tag.toLowerCase());
|
|
737
|
+
if (selfClosing && !isVoid) this.fail(`self-closing <${tag}> is not valid HTML`);
|
|
738
|
+
if (isVoid || selfClosing) return makeNode(tag, attrs, []);
|
|
739
|
+
const children = [];
|
|
740
|
+
for (;;) {
|
|
741
|
+
if (this.atEnd()) this.fail(`unclosed <${tag}>`);
|
|
742
|
+
if (this.charAt() === "<" && this.charAt(1) === "/") {
|
|
743
|
+
this.index += 2;
|
|
744
|
+
const closing = this.readName("a closing tag name");
|
|
745
|
+
this.skipWhitespace();
|
|
746
|
+
if (this.charAt() !== ">") this.fail(`expected ">" after </${closing}>`);
|
|
747
|
+
this.index += 1;
|
|
748
|
+
if (closing.toLowerCase() !== tag.toLowerCase()) this.fail(`closing tag </${closing}> does not match <${tag}>`);
|
|
749
|
+
return makeNode(tag, attrs, children);
|
|
750
|
+
}
|
|
751
|
+
if (this.charAt() === "<") {
|
|
752
|
+
children.push(this.parseElement());
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
children.push(this.readText());
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
readAttributeValue(tag, name) {
|
|
759
|
+
const quote = this.charAt();
|
|
760
|
+
if (quote === "\"" || quote === "'") {
|
|
761
|
+
this.index += 1;
|
|
762
|
+
const start = this.index;
|
|
763
|
+
while (!this.atEnd() && this.charAt() !== quote) this.index += 1;
|
|
764
|
+
if (this.atEnd()) this.fail(`unterminated value for attribute "${name}" on <${tag}>`);
|
|
765
|
+
const raw = this.source.slice(start, this.index);
|
|
766
|
+
this.index += 1;
|
|
767
|
+
return decodeEntities(raw);
|
|
768
|
+
}
|
|
769
|
+
const start = this.index;
|
|
770
|
+
while (!this.atEnd()) {
|
|
771
|
+
const character = this.charAt();
|
|
772
|
+
if (/\s/u.test(character) || character === ">") break;
|
|
773
|
+
this.index += 1;
|
|
774
|
+
}
|
|
775
|
+
return decodeEntities(this.source.slice(start, this.index));
|
|
776
|
+
}
|
|
777
|
+
readText() {
|
|
778
|
+
const start = this.index;
|
|
779
|
+
while (!this.atEnd() && this.charAt() !== "<") this.index += 1;
|
|
780
|
+
return decodeEntities(this.source.slice(start, this.index));
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
/**
|
|
784
|
+
* Parses markup produced by renderMarkup (or a hand-written fixture in the same
|
|
785
|
+
* subset) into a tree that can be compared structurally and rendered again.
|
|
786
|
+
*
|
|
787
|
+
* Text outside the root element is ignored only while it is whitespace; a
|
|
788
|
+
* second root is an error, because that usually means a fixture was pasted
|
|
789
|
+
* wrong rather than that the test wanted a fragment.
|
|
790
|
+
*/
|
|
791
|
+
function parseMarkup(html) {
|
|
792
|
+
return new MarkupParser(html).parse();
|
|
793
|
+
}
|
|
794
|
+
// src/server.ts
|
|
795
|
+
/** Raised at the first fact that does not hold, with the failing phase named. */
|
|
796
|
+
var ServerError = class extends Error {
|
|
797
|
+
phase;
|
|
798
|
+
constructor(phase, detail) {
|
|
799
|
+
super(`checkServer failed in ${phase}: ${detail}`);
|
|
800
|
+
this.name = "ServerError";
|
|
801
|
+
this.phase = phase;
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
function collectParts(markup) {
|
|
805
|
+
const parts = /* @__PURE__ */ new Set();
|
|
806
|
+
for (const match of markup.matchAll(/data-part="([^"]+)"/gu)) parts.add(match[1] ?? "");
|
|
807
|
+
for (const match of markup.matchAll(/data-slot="([^"]+)"/gu)) parts.add(match[1] ?? "");
|
|
808
|
+
return [...parts].sort();
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Checks the server contract of one component.
|
|
812
|
+
*
|
|
813
|
+
* `connect` is the host's half of hydration: given the server markup inside a
|
|
814
|
+
* container element, do whatever the framework needs to upgrade it (for strict
|
|
815
|
+
* elements, nothing — insertion is enough) and return the live host. The check
|
|
816
|
+
* asserts the hydrate marker is gone afterwards and the host holds one tree,
|
|
817
|
+
* then runs `verify` against the adopted host if the caller passed one.
|
|
818
|
+
*/
|
|
819
|
+
function checkServer(component, props = {}, options = {}) {
|
|
820
|
+
let markup;
|
|
821
|
+
try {
|
|
822
|
+
markup = component.render(props, { hydrate: true });
|
|
823
|
+
} catch (error) {
|
|
824
|
+
throw new ServerError("render", error instanceof Error ? error.message : String(error));
|
|
825
|
+
}
|
|
826
|
+
if (markup.trim() === "") throw new ServerError("empty", "the server render produced no markup");
|
|
827
|
+
try {
|
|
828
|
+
parseMarkup(markup);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
throw new ServerError("parse", error instanceof Error ? error.message : String(error));
|
|
831
|
+
}
|
|
832
|
+
const parts = collectParts(markup);
|
|
833
|
+
if (parts.length === 0) throw new ServerError("render", "the server markup carries no part markers");
|
|
834
|
+
for (const expected of options.expectParts ?? []) if (!parts.includes(expected)) throw new ServerError("render", `part "${expected}" is missing from the server markup (found: ${parts.join(", ")})`);
|
|
835
|
+
const container = document.createElement("div");
|
|
836
|
+
container.innerHTML = markup;
|
|
837
|
+
document.body.append(container);
|
|
838
|
+
const host = options.connect?.(container) ?? container.firstElementChild;
|
|
839
|
+
flush();
|
|
840
|
+
try {
|
|
841
|
+
if (!(host instanceof HTMLElement)) throw new ServerError("hydrate", "no element survived insertion into the document");
|
|
842
|
+
if (host.hasAttribute("hydrate")) throw new ServerError("hydrate", "the hydrate marker was never consumed");
|
|
843
|
+
const roots = host.children.length;
|
|
844
|
+
if (roots > 1) throw new ServerError("hydrate", `the host holds ${roots} trees after hydration: the server markup was duplicated, not adopted`);
|
|
845
|
+
options.verify?.(host);
|
|
846
|
+
} finally {
|
|
847
|
+
container.remove();
|
|
848
|
+
}
|
|
849
|
+
return {
|
|
850
|
+
markup,
|
|
851
|
+
parts,
|
|
852
|
+
adopted: true
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
/** Serializes a server render through the markup tree, normalizing attribute
|
|
856
|
+
* order for stable snapshots. */
|
|
857
|
+
function serverMarkup(component, props = {}) {
|
|
858
|
+
return renderMarkup(parseMarkup(component.render(props, { hydrate: true })));
|
|
859
|
+
}
|
|
860
|
+
// src/patterns.ts
|
|
861
|
+
function requireItems(spec, family) {
|
|
862
|
+
if (spec.items.length < 3) throw new Error(`${family}Pattern needs at least three items to test boundaries; got ${spec.items.length}`);
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* The movement contract every APG listbox, menu, tablist, radio group and
|
|
866
|
+
* toolbar shares: the forward arrow moves one item, the backward arrow moves
|
|
867
|
+
* one back, and Home/End jump to the edges.
|
|
868
|
+
*
|
|
869
|
+
* Edge behavior has no default because the APG does not give it one: radio
|
|
870
|
+
* groups wrap, listboxes clamp, tablists may do either. The test has to state
|
|
871
|
+
* which contract the component claims — passing nothing would hide the
|
|
872
|
+
* decision the pattern leaves open.
|
|
873
|
+
*/
|
|
874
|
+
function rovingSteps(spec, options) {
|
|
875
|
+
requireItems(spec, "roving");
|
|
876
|
+
const horizontal = spec.orientation === "horizontal";
|
|
877
|
+
const forward = horizontal ? "ArrowRight" : "ArrowDown";
|
|
878
|
+
const backward = horizontal ? "ArrowLeft" : "ArrowUp";
|
|
879
|
+
const first = spec.items[0];
|
|
880
|
+
const second = spec.items[1];
|
|
881
|
+
const last = spec.items[spec.items.length - 1];
|
|
882
|
+
const steps = [
|
|
883
|
+
{
|
|
884
|
+
key: forward,
|
|
885
|
+
expect: {
|
|
886
|
+
role: spec.role,
|
|
887
|
+
name: second
|
|
888
|
+
}
|
|
889
|
+
},
|
|
890
|
+
{
|
|
891
|
+
key: backward,
|
|
892
|
+
expect: {
|
|
893
|
+
role: spec.role,
|
|
894
|
+
name: first
|
|
895
|
+
}
|
|
896
|
+
},
|
|
897
|
+
{
|
|
898
|
+
key: "End",
|
|
899
|
+
expect: {
|
|
900
|
+
role: spec.role,
|
|
901
|
+
name: last
|
|
902
|
+
}
|
|
903
|
+
},
|
|
904
|
+
{
|
|
905
|
+
key: "Home",
|
|
906
|
+
expect: {
|
|
907
|
+
role: spec.role,
|
|
908
|
+
name: first
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
];
|
|
912
|
+
if (options.wraps === true) steps.push({
|
|
913
|
+
key: backward,
|
|
914
|
+
expect: {
|
|
915
|
+
role: spec.role,
|
|
916
|
+
name: last
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
else steps.push({
|
|
920
|
+
key: backward,
|
|
921
|
+
expect: {
|
|
922
|
+
role: spec.role,
|
|
923
|
+
name: first
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
return steps;
|
|
927
|
+
}
|
|
928
|
+
/**
|
|
929
|
+
* The listbox pattern adds selection to movement: arrows move the active
|
|
930
|
+
* option, and the active option is the selected one in a single-select listbox
|
|
931
|
+
* that follows focus (the APG's recommended model).
|
|
932
|
+
*/
|
|
933
|
+
function listboxSteps(spec, options) {
|
|
934
|
+
return rovingSteps({
|
|
935
|
+
...spec,
|
|
936
|
+
role: "option"
|
|
937
|
+
}, options).map((step) => step.expect === void 0 ? step : {
|
|
938
|
+
...step,
|
|
939
|
+
expect: {
|
|
940
|
+
...step.expect,
|
|
941
|
+
selected: true
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* The tablist pattern: arrows move across tabs, and a tab is selected when it
|
|
947
|
+
* is activated. Manual-activation lists assert only focus here; automatic ones
|
|
948
|
+
* pass `automatic: true` and get the selection assertion on movement.
|
|
949
|
+
*/
|
|
950
|
+
function tabsSteps(spec, options) {
|
|
951
|
+
const base = rovingSteps({
|
|
952
|
+
...spec,
|
|
953
|
+
role: "tab",
|
|
954
|
+
orientation: "horizontal"
|
|
955
|
+
}, { wraps: options.wraps });
|
|
956
|
+
if (options.automatic !== true) return base;
|
|
957
|
+
return base.map((step) => step.expect === void 0 ? step : {
|
|
958
|
+
...step,
|
|
959
|
+
expect: {
|
|
960
|
+
...step.expect,
|
|
961
|
+
selected: true
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
export { AriaError, MarkupParseError, PatternError, ServerError, accessibleName, accessibleRole, checkAria, checkDirection, checkPattern, checkServer, listboxSteps, mount, parseMarkup, renderMarkup, rovingSteps, serverMarkup, tabsSteps };
|