Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/tools/auditKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,16 @@ const auditKeyboard = defineTabTool({
}

const role = current.getAttribute('role') || current.tagName.toLowerCase();
const labelledBy = current.getAttribute('aria-labelledby');
// aria-labelledby is a list of IDs separated by ASCII whitespace (not e.g.  ),
// resolved in the current element's own tree.
const idRoot = current.getRootNode() as Document | ShadowRoot;
const labelledBy = (current.getAttribute('aria-labelledby') || '').split(/[ \t\n\f\r]+/)
.map(id => id && idRoot.getElementById(id)?.textContent?.trim())
.filter(Boolean)
.join(' ');
const text = current.textContent?.trim().slice(0, maxTextLength) || null;
const name = current.getAttribute('aria-label')
|| labelledBy && document.getElementById(labelledBy)?.textContent?.trim()
|| labelledBy
|| current.getAttribute('title')
|| (current instanceof HTMLInputElement || current instanceof HTMLTextAreaElement ? current.labels?.[0]?.textContent?.trim() : null)
|| text;
Expand Down
26 changes: 26 additions & 0 deletions tests/tools-auditKeyboard.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const canRun = !!process.env.MCP_TEST_BROWSER_NAME || existsSync(chromium.execut
const server = http.createServer((request, response) => {
const url = new URL(request.url || '/', 'http://localhost');
response.writeHead(200, { 'content-type': 'text/html' });
if (url.pathname === '/labelledby') {
response.end(`<span id="verb">Save</span><span id="noun">draft</span>
<button id="labelled" aria-labelledby=" verb\tnoun missing ">Fallback text</button>`);
return;
}
if (['/prior', '/target', '/forward'].includes(url.pathname)) {
response.end(`<button id="${url.pathname.slice(1)}">Other document</button>`);
return;
Expand Down Expand Up @@ -142,3 +147,24 @@ it.skipIf(!canRun).each(['replace', 'push', 'fragment', 'document', 'reload', 'l
}
}
);

it.skipIf(!canRun)('names stops from every aria-labelledby id reference', async () => {
directory = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'audit-keyboard-labelledby-'));
const config = await resolveCLIConfig({ browser, headless: true, isolated: true, outputDir: directory });
client = new Client({ name: 'keyboard-labelledby-test', version: '1' });
await client.connect(await wrapInProcess(new BrowserServerBackend(config, contextFactory(config))));
await ok('browser_navigate', { url: `${origin}/labelledby` });
await ok('audit_keyboard', {
maxTabs: 1,
activateSkipLink: false,
checkFocusTrap: false,
checkFocusVisibility: false,
checkFocusJumps: false,
checkTargetSize: false,
checkFocusObscured: false,
reportFile: 'keyboard.json',
});
const report = JSON.parse(await fs.promises.readFile(path.join(directory, 'keyboard.json'), 'utf8'));
const stop = report.stops.find((s: { id: string | null }) => s.id === 'labelled');
expect(stop?.name).toBe('Save draft');
});
Loading