web: shared modal focus controller (M1/M2) — work in progress
This commit is contained in:
@@ -285,6 +285,31 @@ export async function launchChrome({ width = 1440, height = 900, chromePath } =
|
||||
{ timeout, what: `text ${JSON.stringify(text)}` },
|
||||
);
|
||||
},
|
||||
/**
|
||||
* Dispatch one real key event pair (Input.dispatchKeyEvent), so the page
|
||||
* sees the same trusted Tab/Escape/Enter a keyboard operator produces.
|
||||
*/
|
||||
async pressKey(key, { shift = false } = {}) {
|
||||
const keys = {
|
||||
Tab: { code: "Tab", windowsVirtualKeyCode: 9 },
|
||||
Escape: { code: "Escape", windowsVirtualKeyCode: 27 },
|
||||
Enter: { code: "Enter", windowsVirtualKeyCode: 13, text: "\r", unmodifiedText: "\r" },
|
||||
" ": { code: "Space", windowsVirtualKeyCode: 32, text: " ", unmodifiedText: " " },
|
||||
ArrowDown: { code: "ArrowDown", windowsVirtualKeyCode: 40 },
|
||||
ArrowUp: { code: "ArrowUp", windowsVirtualKeyCode: 38 },
|
||||
};
|
||||
const info = keys[key] ?? { code: key, windowsVirtualKeyCode: 0 };
|
||||
const base = {
|
||||
key,
|
||||
code: info.code,
|
||||
windowsVirtualKeyCode: info.windowsVirtualKeyCode,
|
||||
nativeVirtualKeyCode: info.windowsVirtualKeyCode,
|
||||
modifiers: shift ? 8 : 0,
|
||||
...(info.text ? { text: info.text, unmodifiedText: info.unmodifiedText } : {}),
|
||||
};
|
||||
await session("Input.dispatchKeyEvent", { type: "keyDown", ...base });
|
||||
await session("Input.dispatchKeyEvent", { type: "keyUp", ...base });
|
||||
},
|
||||
async text() {
|
||||
return api.evaluate("document.body.innerText");
|
||||
},
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
// Focused modal-focus probe: serves web/ on a free port against the live
|
||||
// Pipelines server, then drives the real page in headless Chrome with trusted
|
||||
// key events. It proves the shared controller's contract on the attention
|
||||
// sheet, the retry confirm and the changes viewer, plus one replaced/stacked
|
||||
// overlay (the artifact viewer over a stage pane).
|
||||
//
|
||||
// cd web && node test/modal-probe.mjs --out ../captures
|
||||
//
|
||||
// It is read-only: no run is retried/cancelled/decided, no chat or template is
|
||||
// created or changed. The one PNG is an open state focused from the keyboard.
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import net from "node:net";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { eventually, launchChrome } from "./browser.mjs";
|
||||
|
||||
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const pipelines = flag(process.argv, "--pipelines", process.env.PIPELINES_TARGET ?? "127.0.0.1:8080");
|
||||
const out = resolve(flag(process.argv, "--out", process.env.WEB_MODAL_OUT ?? resolve(ROOT, "../captures")));
|
||||
const port = flag(process.argv, "--port", process.env.WEB_MODAL_PORT);
|
||||
const settle = (ms = 120) => new Promise((resolvePromise) => setTimeout(resolvePromise, ms));
|
||||
|
||||
function flag(argv, name, fallback) {
|
||||
const index = argv.indexOf(name);
|
||||
return index !== -1 && argv[index + 1] ? argv[index + 1] : fallback;
|
||||
}
|
||||
|
||||
function freePort() {
|
||||
return new Promise((resolvePort) => {
|
||||
const server = net.createServer();
|
||||
server.listen(0, "127.0.0.1", () => {
|
||||
const { port: bound } = server.address();
|
||||
server.close(() => resolvePort(bound));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const FOCUSABLE = [
|
||||
"a[href]",
|
||||
"button:not([disabled])",
|
||||
"input:not([disabled])",
|
||||
"select:not([disabled])",
|
||||
"textarea:not([disabled])",
|
||||
"[tabindex]:not([tabindex='-1'])",
|
||||
].join(",");
|
||||
|
||||
const focusableList = (selector) => `(() => {
|
||||
const panel = document.querySelector(${JSON.stringify(selector)});
|
||||
if (!panel) return [];
|
||||
return [...panel.querySelectorAll(${JSON.stringify(FOCUSABLE)})].filter((node) =>
|
||||
!node.hidden &&
|
||||
node.getAttribute("aria-hidden") !== "true" &&
|
||||
!node.closest("[inert]") &&
|
||||
getComputedStyle(node).display !== "none" &&
|
||||
getComputedStyle(node).visibility !== "hidden"
|
||||
);
|
||||
})()`;
|
||||
|
||||
const activeId = () =>
|
||||
page.evaluate(`(() => {
|
||||
const active = document.activeElement;
|
||||
if (!active) return "";
|
||||
return active.id || active.getAttribute("data-change-index") || active.className || active.tagName;
|
||||
})()`);
|
||||
|
||||
const inertState = () =>
|
||||
page.evaluate(`(() => ({
|
||||
appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false,
|
||||
skipHidden: document.querySelector(".skip-link")?.getAttribute("aria-hidden") ?? null,
|
||||
skipTabindex: document.querySelector(".skip-link")?.getAttribute("tabindex") ?? null,
|
||||
skipInert: document.querySelector(".skip-link")?.hasAttribute("inert") ?? false,
|
||||
}))()`);
|
||||
|
||||
const insideDialog = (selector) =>
|
||||
page.evaluate(`(() => {
|
||||
const panel = document.querySelector(${JSON.stringify(selector)});
|
||||
return Boolean(panel && panel.contains(document.activeElement));
|
||||
})()`);
|
||||
|
||||
async function wrapCheck(selector, label) {
|
||||
const count = await page.evaluate(`${focusableList(selector)}.length`);
|
||||
if (!count) throw new Error(`${label}: no focusable control in the dialog`);
|
||||
await page.evaluate(`(() => {
|
||||
const list = ${focusableList(selector)};
|
||||
list[list.length - 1].focus();
|
||||
})()`);
|
||||
await page.pressKey("Tab");
|
||||
const afterTab = await page.evaluate(`(() => {
|
||||
const list = ${focusableList(selector)};
|
||||
return list.indexOf(document.activeElement);
|
||||
})()`);
|
||||
if (afterTab !== 0) throw new Error(`${label}: Tab from the last control landed on index ${afterTab}`);
|
||||
await page.pressKey("Tab", { shift: true });
|
||||
const afterShift = await page.evaluate(`(() => {
|
||||
const list = ${focusableList(selector)};
|
||||
return list.indexOf(document.activeElement);
|
||||
})()`);
|
||||
const last = count - 1;
|
||||
if (afterShift !== last) {
|
||||
throw new Error(`${label}: Shift+Tab from the first control landed on index ${afterShift}, expected ${last}`);
|
||||
}
|
||||
console.log(` ${label}: ${count} controls; Tab last->first and Shift+Tab first->last`);
|
||||
}
|
||||
|
||||
async function assertInert(label) {
|
||||
const state = await inertState();
|
||||
if (!state.appRoot) throw new Error(`${label}: .app-root is not inert while the dialog is open`);
|
||||
return state;
|
||||
}
|
||||
|
||||
async function assertRestored(label, prior) {
|
||||
const state = await inertState();
|
||||
if (state.appRoot) throw new Error(`${label}: .app-root stayed inert after close`);
|
||||
if (state.skipHidden !== prior.skipHidden || state.skipTabindex !== prior.skipTabindex) {
|
||||
throw new Error(`${label}: skip-link state was not restored exactly: ${JSON.stringify({ before: prior, after: state })}`);
|
||||
}
|
||||
console.log(` ${label}: app root and skip link restored exactly`);
|
||||
}
|
||||
|
||||
async function openRun(id) {
|
||||
const detail = `.run-detail .run-identity`;
|
||||
await eventually(() => page.evaluate(`document.querySelector(${JSON.stringify(detail)})?.textContent?.includes("run ${id} ") ?? false`), {
|
||||
timeout: 25000,
|
||||
what: `run ${id} detail`,
|
||||
});
|
||||
}
|
||||
|
||||
let serve = null;
|
||||
let browser = null;
|
||||
let page = null;
|
||||
|
||||
try {
|
||||
await mkdir(out, { recursive: true });
|
||||
const webPort = port ? Number(port) : await freePort();
|
||||
console.log(`• serving web/ on 127.0.0.1:${webPort} (proxy /connect -> ${pipelines})`);
|
||||
serve = spawn(process.execPath, ["serve.mjs", "--port", String(webPort), "--pipelines", pipelines], {
|
||||
cwd: ROOT,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
let serveLog = "";
|
||||
serve.stdout.on("data", (chunk) => (serveLog += chunk.toString()));
|
||||
serve.stderr.on("data", (chunk) => (serveLog += chunk.toString()));
|
||||
await eventually(
|
||||
async () => {
|
||||
if (serve.exitCode != null) throw new Error(`serve.mjs exited: ${serveLog.trim().slice(-400)}`);
|
||||
const response = await fetch(`http://127.0.0.1:${webPort}/`).catch(() => null);
|
||||
return response?.ok;
|
||||
},
|
||||
{ what: "the page to be served" },
|
||||
);
|
||||
|
||||
console.log("• launching headless Chrome");
|
||||
browser = await launchChrome({ width: 1440, height: 900 });
|
||||
page = await browser.openPage();
|
||||
await page.addInitScript(`(() => {
|
||||
localStorage.setItem("pipelines.web.serverUrl.v2", "ws://127.0.0.1:${webPort}/connect");
|
||||
localStorage.removeItem("pipelines.web.attention.v1");
|
||||
localStorage.removeItem("pipelines.web.workspace.v1");
|
||||
})()`);
|
||||
const pageUrl = `http://127.0.0.1:${webPort}/`;
|
||||
await page.goto(pageUrl);
|
||||
await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 30000 });
|
||||
const runsClicked = await page.evaluate(`(() => {
|
||||
const all = [...document.querySelectorAll('[data-destination="runs"]')];
|
||||
const control = all.find((node) => node.offsetParent !== null) ?? all[0];
|
||||
if (!control) return false;
|
||||
control.click();
|
||||
return true;
|
||||
})()`);
|
||||
if (!runsClicked) throw new Error("no Runs destination control");
|
||||
await page.waitForSelector(".list.runs .run-row", { timeout: 30000 });
|
||||
|
||||
const prior = await inertState();
|
||||
console.log(`• attention sheet (prior app inert=${prior.appRoot}, skip aria-hidden=${prior.skipHidden})`);
|
||||
await page.waitForSelector("#attentionBell", { timeout: 15000 });
|
||||
const bellCount = await page.evaluate(
|
||||
`Number(document.querySelector("#attentionBell .attention-badge")?.dataset.count ?? 0)`,
|
||||
);
|
||||
if (!(bellCount > 0)) throw new Error("the live server has no attention entries to open");
|
||||
await page.evaluate(`document.querySelector("#attentionBell").focus()`);
|
||||
await page.pressKey("Enter");
|
||||
await page.waitForSelector(".attention-sheet", { timeout: 10000 });
|
||||
await settle();
|
||||
await assertInert("attention open");
|
||||
if (!(await insideDialog(".attention-sheet"))) {
|
||||
throw new Error("attention sheet: focus is outside the panel after open");
|
||||
}
|
||||
if (await page.evaluate(`!!document.querySelector(".attention-sheet [inert]")`)) {
|
||||
throw new Error("attention sheet: its own controls are inert");
|
||||
}
|
||||
const capture = `${out}/86-modal-focus-attention.png`;
|
||||
await page.screenshot(capture);
|
||||
console.log(` capture ${capture}`);
|
||||
await wrapCheck(".attention-sheet", "attention sheet");
|
||||
await page.pressKey("Escape");
|
||||
await eventually(() => page.evaluate(`!document.querySelector(".attention-sheet")`), {
|
||||
timeout: 5000,
|
||||
what: "the attention sheet to close",
|
||||
});
|
||||
const afterAttention = await activeId();
|
||||
if (afterAttention !== "attentionBell") {
|
||||
throw new Error(`attention sheet: Escape returned focus to ${afterAttention || "body"}, not #attentionBell`);
|
||||
}
|
||||
await assertRestored("attention close", prior);
|
||||
|
||||
console.log("• retry confirm (run 440)");
|
||||
await page.goto(`${pageUrl}runs/440`);
|
||||
await page.waitForSelector(".run-detail .run-actions .retry-run", { timeout: 25000 });
|
||||
await openRun(440);
|
||||
await page.evaluate(`document.querySelector(".run-detail .run-actions .retry-run").focus()`);
|
||||
await page.pressKey("Enter");
|
||||
await page.waitForSelector(".confirm-dialog.confirm-retry", { timeout: 10000 });
|
||||
await settle();
|
||||
await assertInert("retry confirm open");
|
||||
if (!(await insideDialog(".confirm-dialog.confirm-retry"))) {
|
||||
throw new Error("retry confirm: focus is outside the panel after open");
|
||||
}
|
||||
await wrapCheck(".confirm-dialog.confirm-retry", "retry confirm");
|
||||
await page.pressKey("Escape");
|
||||
await eventually(() => page.evaluate(`!document.querySelector(".confirm-dialog.confirm-retry")`), {
|
||||
timeout: 5000,
|
||||
what: "the retry confirm to close",
|
||||
});
|
||||
await settle();
|
||||
const afterConfirm = await activeId();
|
||||
if (!String(afterConfirm).includes("retry-run")) {
|
||||
throw new Error(`retry confirm: Escape returned focus to ${afterConfirm || "body"}, not .retry-run`);
|
||||
}
|
||||
await assertRestored("retry confirm close", prior);
|
||||
|
||||
console.log("• changes viewer (run 438)");
|
||||
await page.goto(`${pageUrl}runs/438`);
|
||||
await page.waitForSelector("#detailTitle", { timeout: 25000 });
|
||||
await openRun(438);
|
||||
const expanded = await page.evaluate(`(() => {
|
||||
const toggle = [...document.querySelectorAll(".run-detail .collapsible .section-toggle")]
|
||||
.find((node) => /\\d+ files? changed/.test(node.querySelector(".section-title")?.textContent ?? ""));
|
||||
if (!toggle) return false;
|
||||
toggle.click();
|
||||
return true;
|
||||
})()`);
|
||||
if (!expanded) throw new Error("changes viewer: no Changes section on run 438");
|
||||
await page.waitForSelector(".run-detail .changes-list .change-row", { timeout: 15000 });
|
||||
await page.evaluate(`(() => {
|
||||
const row = document.querySelector(".run-detail .changes-list .change-row");
|
||||
row.scrollIntoView({ block: "center" });
|
||||
row.focus();
|
||||
})()`);
|
||||
await page.pressKey("Enter");
|
||||
await page.waitForSelector(".change-viewer", { timeout: 10000 });
|
||||
await settle();
|
||||
await assertInert("changes viewer open");
|
||||
if (!(await insideDialog(".change-viewer"))) {
|
||||
throw new Error("changes viewer: focus is outside the panel after open");
|
||||
}
|
||||
await wrapCheck(".change-viewer", "changes viewer");
|
||||
await page.pressKey("Escape");
|
||||
await eventually(() => page.evaluate(`!document.querySelector(".change-viewer")`), {
|
||||
timeout: 5000,
|
||||
what: "the changes viewer to close",
|
||||
});
|
||||
const afterChanges = await activeId();
|
||||
if (afterChanges !== "0") {
|
||||
throw new Error(`changes viewer: Escape returned focus to ${afterChanges || "body"}, not change row 0`);
|
||||
}
|
||||
await assertRestored("changes viewer close", prior);
|
||||
|
||||
console.log("• stacking: artifact viewer over the stage pane (run 440)");
|
||||
await page.goto(`${pageUrl}runs/440`);
|
||||
await page.waitForSelector("#detailTitle", { timeout: 25000 });
|
||||
await openRun(440);
|
||||
const timelineClicked = await page.evaluate(`(() => {
|
||||
const tab = [...document.querySelectorAll(".rail-tab, .strip-tab")]
|
||||
.find((node) => node.offsetParent !== null && node.textContent.trim() === "Timeline");
|
||||
if (!tab) return false;
|
||||
tab.click();
|
||||
return true;
|
||||
})()`);
|
||||
if (!timelineClicked) throw new Error("stacking: no Timeline tab");
|
||||
await page.waitForSelector(".run-detail .timeline-row", { timeout: 15000 });
|
||||
await page.evaluate(`document.querySelector(".run-detail .timeline-row").focus()`);
|
||||
await page.pressKey("Enter");
|
||||
await page.waitForSelector(".stage-pane", { timeout: 10000 });
|
||||
const openedArtifacts = await page.evaluate(`(() => {
|
||||
const toggle = [...document.querySelectorAll(".stage-pane .section-toggle")]
|
||||
.find((node) => node.querySelector(".section-title")?.textContent?.trim().startsWith("Artifacts"));
|
||||
if (!toggle) return false;
|
||||
toggle.click();
|
||||
return true;
|
||||
})()`);
|
||||
if (!openedArtifacts) throw new Error("stacking: no Artifacts section in the stage pane");
|
||||
await page.waitForSelector(".stage-pane .artifact-grid button.artifact-tile", { timeout: 10000 });
|
||||
await page.evaluate(`(() => {
|
||||
const tiles = [...document.querySelectorAll(".stage-pane .artifact-grid button.artifact-tile")];
|
||||
const tile = tiles.find((node) => node.querySelector("img")) ?? tiles[0];
|
||||
tile.scrollIntoView({ block: "center" });
|
||||
tile.focus();
|
||||
})()`);
|
||||
await page.pressKey("Enter");
|
||||
await page.waitForSelector(".artifact-viewer", { timeout: 10000 });
|
||||
await settle();
|
||||
const stack = await page.evaluate(`(() => ({
|
||||
lower: document.querySelector(".stage-pane")?.hasAttribute("inert") ?? false,
|
||||
upper: document.querySelector(".artifact-viewer")?.hasAttribute("inert") ?? false,
|
||||
activeInsideUpper: document.querySelector(".artifact-viewer")?.contains(document.activeElement) ?? false,
|
||||
appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false,
|
||||
}))()`);
|
||||
if (!stack.lower) throw new Error("stacking: the lower stage pane is still tabbable while the viewer is open");
|
||||
if (stack.upper) throw new Error("stacking: the top artifact viewer is inert");
|
||||
if (!stack.activeInsideUpper) throw new Error("stacking: focus is not inside the top artifact viewer");
|
||||
if (!stack.appRoot) throw new Error("stacking: the app root is not inert under both dialogs");
|
||||
await page.pressKey("Escape");
|
||||
await eventually(() => page.evaluate(`!document.querySelector(".artifact-viewer")`), {
|
||||
timeout: 5000,
|
||||
what: "the artifact viewer to close",
|
||||
});
|
||||
const lowerRestored = await page.evaluate(`(() => ({
|
||||
lowerInert: document.querySelector(".stage-pane")?.hasAttribute("inert") ?? false,
|
||||
activeInsideLower: document.querySelector(".stage-pane")?.contains(document.activeElement) ?? false,
|
||||
appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false,
|
||||
}))()`);
|
||||
if (lowerRestored.lowerInert) throw new Error("stacking: the stage pane stayed inert after the viewer closed");
|
||||
if (!lowerRestored.activeInsideLower) throw new Error("stacking: focus did not return into the stage pane");
|
||||
if (!lowerRestored.appRoot) throw new Error("stacking: the app root lost inert while the stage pane remained open");
|
||||
await page.pressKey("Escape");
|
||||
await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), {
|
||||
timeout: 5000,
|
||||
what: "the stage pane to close",
|
||||
});
|
||||
const afterStack = await page.evaluate(`(() => ({
|
||||
activeRow: document.activeElement?.getAttribute("data-stage-key") ?? "",
|
||||
appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false,
|
||||
}))()`);
|
||||
if (!afterStack.activeRow) throw new Error("stacking: focus was not returned to the timeline row");
|
||||
if (afterStack.appRoot) throw new Error("stacking: the app root stayed inert after the last close");
|
||||
console.log(` artifact viewer -> stage pane -> timeline row (${afterStack.activeRow}); app root restored`);
|
||||
|
||||
console.log("\nMODAL PROBE PASS");
|
||||
} finally {
|
||||
await page?.close().catch(() => {});
|
||||
await browser?.close().catch(() => {});
|
||||
if (serve) {
|
||||
serve.kill("SIGTERM");
|
||||
await eventually(async () => serve.exitCode != null || serve.signalCode != null, {
|
||||
timeout: 5000,
|
||||
what: "serve.mjs exit",
|
||||
}).catch(() => {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
// Focused unit tests for the shared modal controller. A tiny DOM stub gives the
|
||||
// controller the same surface it uses in the browser (attributes, children,
|
||||
// focus and document listeners) without a test dependency or a live server.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
createModalController,
|
||||
focusableElements,
|
||||
initialFocusIndex,
|
||||
isFocusable,
|
||||
wrapFocusIndex,
|
||||
} from "../src/modal.js";
|
||||
|
||||
class FakeElement {
|
||||
constructor(tag = "div", document_ = null) {
|
||||
this.tagName = tag.toUpperCase();
|
||||
this.ownerDocument = document_;
|
||||
this.nodeType = 1;
|
||||
this.attributes = new Map();
|
||||
this.children = [];
|
||||
this.parentElement = null;
|
||||
this.isConnected = false;
|
||||
this.hidden = false;
|
||||
this.disabled = false;
|
||||
this.textContent = "";
|
||||
}
|
||||
|
||||
getAttribute(name) {
|
||||
return this.attributes.has(name) ? this.attributes.get(name) : null;
|
||||
}
|
||||
|
||||
setAttribute(name, value) {
|
||||
this.attributes.set(name, String(value));
|
||||
}
|
||||
|
||||
hasAttribute(name) {
|
||||
return this.attributes.has(name);
|
||||
}
|
||||
|
||||
removeAttribute(name) {
|
||||
this.attributes.delete(name);
|
||||
}
|
||||
|
||||
append(...nodes) {
|
||||
for (const node of nodes) {
|
||||
if (node.parentElement) node.remove();
|
||||
node.parentElement = this;
|
||||
node.isConnected = true;
|
||||
this.children.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
replaceChildren(...nodes) {
|
||||
for (const child of this.children) {
|
||||
child.parentElement = null;
|
||||
child.isConnected = false;
|
||||
}
|
||||
this.children = [];
|
||||
this.append(...nodes);
|
||||
}
|
||||
|
||||
replaceWith(next) {
|
||||
const parent = this.parentElement;
|
||||
if (!parent) return;
|
||||
const index = parent.children.indexOf(this);
|
||||
if (index === -1) return;
|
||||
parent.children[index] = next;
|
||||
next.parentElement = parent;
|
||||
next.isConnected = true;
|
||||
this.parentElement = null;
|
||||
this.isConnected = false;
|
||||
}
|
||||
|
||||
remove() {
|
||||
const parent = this.parentElement;
|
||||
if (!parent) return;
|
||||
const index = parent.children.indexOf(this);
|
||||
if (index !== -1) parent.children.splice(index, 1);
|
||||
this.parentElement = null;
|
||||
this.isConnected = false;
|
||||
}
|
||||
|
||||
contains(node) {
|
||||
if (node === this) return true;
|
||||
return this.children.some((child) => child.contains(node));
|
||||
}
|
||||
|
||||
querySelectorAll() {
|
||||
const found = [];
|
||||
const walk = (element) => {
|
||||
for (const child of element.children) {
|
||||
found.push(child);
|
||||
walk(child);
|
||||
}
|
||||
};
|
||||
walk(this);
|
||||
return found;
|
||||
}
|
||||
|
||||
querySelector(selector) {
|
||||
return this.querySelectorAll(selector)[0] ?? null;
|
||||
}
|
||||
|
||||
matches(selector) {
|
||||
if (selector === "a[href]") return this.tagName === "A" && this.hasAttribute("href");
|
||||
if (selector === "button") return this.tagName === "BUTTON";
|
||||
if (selector === "[tabindex]") return this.hasAttribute("tabindex");
|
||||
return false;
|
||||
}
|
||||
|
||||
focus() {
|
||||
this.ownerDocument.activeElement = this;
|
||||
}
|
||||
}
|
||||
|
||||
class FakeDocument {
|
||||
constructor() {
|
||||
this.body = new FakeElement("body", this);
|
||||
this.body.isConnected = true;
|
||||
this.activeElement = this.body;
|
||||
this.byId = new Map();
|
||||
this.bySelector = new Map();
|
||||
this.listeners = new Map();
|
||||
this.defaultView = { getComputedStyle: () => ({ display: "block", visibility: "visible" }) };
|
||||
}
|
||||
|
||||
createElement(tag) {
|
||||
return new FakeElement(tag, this);
|
||||
}
|
||||
|
||||
register(selector, element) {
|
||||
if (selector.startsWith("#")) this.byId.set(selector.slice(1), element);
|
||||
this.bySelector.set(selector, element);
|
||||
return element;
|
||||
}
|
||||
|
||||
getElementById(id) {
|
||||
return this.byId.get(id) ?? null;
|
||||
}
|
||||
|
||||
querySelector(selector) {
|
||||
return this.bySelector.get(selector) ?? null;
|
||||
}
|
||||
|
||||
querySelectorAll(selector) {
|
||||
const element = this.bySelector.get(selector);
|
||||
return element ? [element] : [];
|
||||
}
|
||||
|
||||
addEventListener(type, listener, capture = false) {
|
||||
const key = `${type}:${capture ? "capture" : "bubble"}`;
|
||||
const list = this.listeners.get(key) ?? [];
|
||||
list.push(listener);
|
||||
this.listeners.set(key, list);
|
||||
}
|
||||
|
||||
removeEventListener(type, listener, capture = false) {
|
||||
const key = `${type}:${capture ? "capture" : "bubble"}`;
|
||||
const list = this.listeners.get(key) ?? [];
|
||||
this.listeners.set(
|
||||
key,
|
||||
list.filter((candidate) => candidate !== listener),
|
||||
);
|
||||
}
|
||||
|
||||
listenerCount(type, capture = false) {
|
||||
return (this.listeners.get(`${type}:${capture ? "capture" : "bubble"}`) ?? []).length;
|
||||
}
|
||||
|
||||
dispatch(type, event, capture = false) {
|
||||
for (const listener of [...(this.listeners.get(`${type}:${capture ? "capture" : "bubble"}`) ?? [])]) {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function harness({ fallback = false } = {}) {
|
||||
const document_ = new FakeDocument();
|
||||
const root = document_.register(".app-root", new FakeElement("div", document_));
|
||||
document_.body.append(root);
|
||||
const skip = document_.register(".skip-link", new FakeElement("a", document_));
|
||||
skip.setAttribute("href", "#main");
|
||||
document_.body.append(skip);
|
||||
const layer = new FakeElement("div", document_);
|
||||
document_.body.append(layer);
|
||||
const title = document_.register("#detailTitle", new FakeElement("h1", document_));
|
||||
title.setAttribute("tabindex", "-1");
|
||||
root.append(title);
|
||||
const pane = document_.register("#pane-main", new FakeElement("div", document_));
|
||||
pane.setAttribute("tabindex", "-1");
|
||||
root.append(pane);
|
||||
|
||||
const controller = createModalController({
|
||||
document: document_,
|
||||
layer,
|
||||
background: root,
|
||||
skipLinks: [skip],
|
||||
supportsInert: () => !fallback,
|
||||
fallbackSelectors: ["#detailTitle", "#pane-main"],
|
||||
});
|
||||
return { document_, root, skip, layer, title, controller };
|
||||
}
|
||||
|
||||
function button(document_, text, props = {}) {
|
||||
const node = new FakeElement("button", document_);
|
||||
node.textContent = text;
|
||||
node.tagName = "BUTTON";
|
||||
for (const [key, value] of Object.entries(props)) {
|
||||
if (key === "disabled") node.disabled = value;
|
||||
else if (key === "hidden") node.hidden = value;
|
||||
else node.setAttribute(key, value);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
function panelWith(document_, ...controls) {
|
||||
const panel = new FakeElement("div", document_);
|
||||
panel.setAttribute("role", "dialog");
|
||||
panel.append(...controls);
|
||||
return panel;
|
||||
}
|
||||
|
||||
test("focusable discovery keeps DOM order and excludes every unreachable form", () => {
|
||||
const document_ = new FakeDocument();
|
||||
const panel = new FakeElement("div", document_);
|
||||
const first = button(document_, "first");
|
||||
const disabled = button(document_, "disabled", { disabled: true });
|
||||
const hidden = button(document_, "hidden", { hidden: true });
|
||||
const ariaHidden = button(document_, "aria hidden", { "aria-hidden": "true" });
|
||||
const inert = button(document_, "inert", { inert: "" });
|
||||
const negative = button(document_, "negative", { tabindex: "-1" });
|
||||
const nested = new FakeElement("div", document_);
|
||||
nested.setAttribute("aria-hidden", "true");
|
||||
nested.append(button(document_, "nested"));
|
||||
const last = button(document_, "last");
|
||||
panel.append(first, disabled, hidden, ariaHidden, inert, negative, nested, last);
|
||||
|
||||
assert.deepEqual(
|
||||
focusableElements(panel).map((node) => node.textContent),
|
||||
["first", "last"],
|
||||
);
|
||||
assert.equal(isFocusable(first, { root: panel }), true);
|
||||
assert.equal(isFocusable(disabled, { root: panel }), false);
|
||||
assert.equal(isFocusable(negative, { root: panel }), false);
|
||||
});
|
||||
|
||||
test("Tab and Shift+Tab arithmetic wraps in both directions", () => {
|
||||
assert.equal(wrapFocusIndex(2, 1, 3), 0);
|
||||
assert.equal(wrapFocusIndex(0, -1, 3), 2);
|
||||
assert.equal(wrapFocusIndex(1, 1, 3), 2);
|
||||
assert.equal(wrapFocusIndex(1, -1, 3), 0);
|
||||
assert.equal(wrapFocusIndex(-1, 1, 3), 0);
|
||||
assert.equal(wrapFocusIndex(-1, -1, 3), 2);
|
||||
assert.equal(wrapFocusIndex(0, 1, 0), -1);
|
||||
assert.equal(initialFocusIndex(0), -1);
|
||||
assert.equal(initialFocusIndex(2), 0);
|
||||
});
|
||||
|
||||
test("a panel with no focusable control focuses the panel itself", () => {
|
||||
const { document_, layer, controller } = harness();
|
||||
const panel = panelWith(document_, button(document_, "disabled", { disabled: true }));
|
||||
controller.open("empty", panel);
|
||||
assert.equal(document_.activeElement, panel);
|
||||
assert.equal(panel.getAttribute("tabindex"), "-1");
|
||||
assert.equal(layer.children.includes(panel), true);
|
||||
controller.close("empty");
|
||||
assert.equal(document_.activeElement, document_.getElementById("detailTitle"));
|
||||
});
|
||||
|
||||
test("opening the same id twice keeps one trigger and one set of listeners", () => {
|
||||
const { document_, title, controller } = harness();
|
||||
const trigger = button(document_, "open");
|
||||
title.append(trigger);
|
||||
trigger.focus();
|
||||
const first = panelWith(document_, button(document_, "a"));
|
||||
controller.open("same", first, { onClose: () => {} });
|
||||
assert.equal(document_.listenerCount("keydown"), 1);
|
||||
const second = panelWith(document_, button(document_, "b"));
|
||||
const other = button(document_, "other");
|
||||
title.append(other);
|
||||
other.focus();
|
||||
controller.open("same", second, { onClose: () => {} });
|
||||
assert.equal(document_.listenerCount("keydown"), 1);
|
||||
assert.equal(controller.stackIds().length, 1);
|
||||
controller.close("same");
|
||||
assert.equal(document_.activeElement, trigger, "the original opener is still the return target");
|
||||
assert.equal(controller.listenerState().keydown, false);
|
||||
});
|
||||
|
||||
test("a same-id re-render replaces the panel and keeps the focused control", () => {
|
||||
const { document_, title, controller } = harness();
|
||||
const trigger = button(document_, "trigger");
|
||||
title.append(trigger);
|
||||
trigger.focus();
|
||||
const firstPanel = panelWith(document_, button(document_, "one"), button(document_, "two"));
|
||||
controller.open("same", firstPanel, { onClose: () => {} });
|
||||
const second = firstPanel.children[1];
|
||||
second.focus();
|
||||
const replacement = panelWith(document_, button(document_, "one again"), button(document_, "two again"));
|
||||
controller.open("same", replacement, { onClose: () => {} });
|
||||
assert.equal(controller.stackIds().length, 1);
|
||||
assert.equal(document_.activeElement.textContent, "two again");
|
||||
controller.close("same");
|
||||
assert.equal(document_.activeElement, trigger);
|
||||
});
|
||||
|
||||
test("a second overlay inerts the first and closing it restores the first", () => {
|
||||
const { document_, root, skip, title, controller } = harness();
|
||||
const firstTrigger = button(document_, "first trigger");
|
||||
title.append(firstTrigger);
|
||||
firstTrigger.focus();
|
||||
const first = panelWith(document_, button(document_, "first control"));
|
||||
controller.open("first", first);
|
||||
|
||||
assert.equal(root.getAttribute("inert"), "");
|
||||
assert.equal(skip.getAttribute("aria-hidden"), "true");
|
||||
|
||||
const secondTrigger = button(document_, "second trigger");
|
||||
title.append(secondTrigger);
|
||||
secondTrigger.focus();
|
||||
const second = panelWith(document_, button(document_, "second control"));
|
||||
controller.open("second", second);
|
||||
assert.equal(first.getAttribute("inert"), "");
|
||||
assert.equal(second.hasAttribute("inert"), false);
|
||||
assert.deepEqual(controller.stackIds(), ["first", "second"]);
|
||||
|
||||
controller.close("second");
|
||||
assert.equal(first.hasAttribute("inert"), false, "the lower panel is interactive again");
|
||||
assert.equal(root.getAttribute("inert"), "");
|
||||
assert.equal(controller.isOpen("first"), true);
|
||||
|
||||
controller.close("first");
|
||||
assert.equal(root.hasAttribute("inert"), false);
|
||||
assert.equal(skip.hasAttribute("aria-hidden"), false);
|
||||
assert.equal(controller.listenerState().keydown, false);
|
||||
});
|
||||
|
||||
test("native inert and aria-hidden/tabindex restore the exact prior values", () => {
|
||||
const { document_, root, skip, controller } = harness();
|
||||
root.setAttribute("inert", "kept");
|
||||
skip.setAttribute("aria-hidden", "false");
|
||||
const trigger = button(document_, "trigger");
|
||||
root.append(trigger);
|
||||
trigger.focus();
|
||||
controller.open("exact", panelWith(document_, button(document_, "ok")));
|
||||
controller.close("exact");
|
||||
assert.equal(root.getAttribute("inert"), "kept");
|
||||
assert.equal(skip.getAttribute("aria-hidden"), "false");
|
||||
});
|
||||
|
||||
test("the no-inert fallback hides the background, removes its tab stops and restores them", () => {
|
||||
const { document_, root, skip, controller } = harness({ fallback: true });
|
||||
const first = button(document_, "one", { tabindex: "3" });
|
||||
const second = button(document_, "two");
|
||||
const trigger = button(document_, "trigger");
|
||||
root.append(first, second, trigger);
|
||||
trigger.focus();
|
||||
|
||||
controller.open("fallback", panelWith(document_, button(document_, "inside")));
|
||||
assert.equal(root.getAttribute("aria-hidden"), "true");
|
||||
assert.equal(skip.getAttribute("aria-hidden"), "true");
|
||||
assert.equal(first.getAttribute("tabindex"), "-1");
|
||||
assert.equal(second.getAttribute("tabindex"), "-1");
|
||||
assert.equal(document_.listenerCount("keydown"), 1);
|
||||
assert.equal(document_.listenerCount("focusin", true), 1);
|
||||
|
||||
controller.close("fallback");
|
||||
assert.equal(root.hasAttribute("aria-hidden"), false);
|
||||
assert.equal(skip.hasAttribute("aria-hidden"), false);
|
||||
assert.equal(first.getAttribute("tabindex"), "3");
|
||||
assert.equal(second.hasAttribute("tabindex"), false);
|
||||
assert.equal(document_.listenerCount("keydown"), 0);
|
||||
assert.equal(document_.listenerCount("focusin", true), 0);
|
||||
});
|
||||
|
||||
test("Tab from the last control wraps to the first; Shift+Tab wraps back", () => {
|
||||
const { document_, title, controller } = harness();
|
||||
const trigger = button(document_, "trigger");
|
||||
title.append(trigger);
|
||||
trigger.focus();
|
||||
const first = button(document_, "first");
|
||||
const last = button(document_, "last");
|
||||
controller.open("trap", panelWith(document_, first, last));
|
||||
|
||||
last.focus();
|
||||
const tab = {
|
||||
key: "Tab",
|
||||
shiftKey: false,
|
||||
defaultPrevented: false,
|
||||
preventDefault() {
|
||||
this.defaultPrevented = true;
|
||||
},
|
||||
stopPropagation() {},
|
||||
};
|
||||
document_.dispatch("keydown", tab);
|
||||
assert.equal(document_.activeElement, first);
|
||||
|
||||
const shiftTab = {
|
||||
key: "Tab",
|
||||
shiftKey: true,
|
||||
defaultPrevented: false,
|
||||
preventDefault() {
|
||||
this.defaultPrevented = true;
|
||||
},
|
||||
stopPropagation() {},
|
||||
};
|
||||
document_.dispatch("keydown", shiftTab);
|
||||
assert.equal(document_.activeElement, last);
|
||||
});
|
||||
|
||||
test("Escape closes through the supplied callback and returns focus", () => {
|
||||
const { document_, title, controller } = harness();
|
||||
const trigger = button(document_, "trigger");
|
||||
title.append(trigger);
|
||||
trigger.focus();
|
||||
let closed = 0;
|
||||
controller.open("escape", panelWith(document_, button(document_, "ok")), {
|
||||
onClose: () => {
|
||||
closed += 1;
|
||||
controller.close("escape");
|
||||
},
|
||||
});
|
||||
const escape = {
|
||||
key: "Escape",
|
||||
defaultPrevented: false,
|
||||
preventDefault() {},
|
||||
stopPropagation() {},
|
||||
};
|
||||
document_.dispatch("keydown", escape);
|
||||
assert.equal(closed, 1);
|
||||
assert.equal(controller.isOpen("escape"), false);
|
||||
assert.equal(document_.activeElement, trigger);
|
||||
});
|
||||
Reference in New Issue
Block a user