Skip to content

Commit 687aa1b

Browse files
committed
refactor: Simplify JavaScript snippets in a11y-debugging documentation and add debugging logs to a11y tests.
1 parent fee7e90 commit 687aa1b

2 files changed

Lines changed: 63 additions & 67 deletions

File tree

skills/a11y-debugging/SKILL.md

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,27 @@ The accessibility tree exposes the heading hierarchy and semantic landmarks.
3535
2. Ensure interactive elements have an accessible name (e.g., a button should not just say `""` if it only contains an icon).
3636
3. **Orphaned Inputs**: Verify that all form inputs have associated labels. Use `evaluate_script` to check for inputs missing `id` (for `label[for]`) or `aria-label`:
3737
```javascript
38-
(() => {
39-
const f = () => {
40-
const inputs = Array.from(
41-
document.querySelectorAll('input, select, textarea'),
42-
);
43-
return inputs
44-
.filter(i => {
45-
const hasId = i.id && document.querySelector(`label[for="${i.id}"]`);
46-
const hasAria =
47-
i.getAttribute('aria-label') || i.getAttribute('aria-labelledby');
48-
const hasImplicitLabel = i.closest('label');
49-
return !hasId && !hasAria && !hasImplicitLabel;
50-
})
51-
.map(i => ({
52-
tag: i.tagName,
53-
id: i.id,
54-
name: i.name,
55-
placeholder: i.placeholder,
56-
}));
57-
};
58-
try { console.log(f()); } catch(e) {} // Log for manual console usage
59-
return f;
60-
})()
61-
```
38+
() => {
39+
const inputs = Array.from(
40+
document.querySelectorAll('input, select, textarea'),
41+
);
42+
return inputs
43+
.filter(i => {
44+
const hasId = i.id && document.querySelector(`label[for="${i.id}"]`);
45+
const hasAria =
46+
i.getAttribute('aria-label') || i.getAttribute('aria-labelledby');
47+
const hasImplicitLabel = i.closest('label');
48+
return !hasId && !hasAria && !hasImplicitLabel;
49+
})
50+
.map(i => ({
51+
tag: i.tagName,
52+
id: i.id,
53+
name: i.name,
54+
placeholder: i.placeholder,
55+
}));
56+
}
57+
```
58+
6259
4. Check images for `alt` text.
6360

6461
### 4. Focus & Keyboard Navigation
@@ -96,7 +93,6 @@ If native audits do not report issues (which may happen in some headless environ
9693
**Note**: This script uses a simplified algorithm and may not account for transparency, gradients, or background images. For production-grade auditing, consider injecting `axe-core`.
9794

9895
```javascript
99-
// Usage in console: copy, paste, and call with element: fn(element)
10096
el => {
10197
function getRGB(colorStr) {
10298
const match = colorStr.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
@@ -136,24 +132,21 @@ _Pass the element's `uid` to test the contrast against WCAG AA (4.5:1 for normal
136132
Verify document-level accessibility settings often missed in component testing:
137133

138134
```javascript
139-
(() => {
140-
const f = () => {
141-
return {
142-
lang:
143-
document.documentElement.lang ||
144-
'MISSING - Screen readers need this for pronunciation',
145-
title: document.title || 'MISSING - Required for context',
146-
viewport:
147-
document.querySelector('meta[name="viewport"]')?.content ||
148-
'MISSING - Check for user-scalable=no (bad practice)',
149-
reducedMotion: window.matchMedia('(prefers-reduced-motion: reduce)').matches
150-
? 'Enabled'
151-
: 'Disabled',
152-
};
135+
() => {
136+
return {
137+
lang:
138+
document.documentElement.lang ||
139+
'MISSING - Screen readers need this for pronunciation',
140+
title: document.title || 'MISSING - Required for context',
141+
viewport:
142+
document.querySelector('meta[name="viewport"]')?.content ||
143+
'MISSING - Check for user-scalable=no (bad practice)',
144+
reducedMotion: window.matchMedia('(prefers-reduced-motion: reduce)')
145+
.matches
146+
? 'Enabled'
147+
: 'Disabled',
153148
};
154-
try { console.log(f()); } catch(e) {} // Log for manual console usage
155-
return f;
156-
})()
149+
};
157150
```
158151

159152
## Troubleshooting

tests/skills/a11y_debugging.test.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
import assert from 'node:assert';
8-
import {describe, it} from 'node:test';
98
import fs from 'node:fs';
109
import path from 'node:path';
10+
import { describe, it } from 'node:test';
1111

1212
import {evaluateScript} from '../../src/tools/script.js';
1313
import {withMcpContext} from '../utils.js';
@@ -44,32 +44,35 @@ describe('a11y-debugging', () => {
4444
assert.ok(globalPageChecksSnippet, 'Global page checks snippet not found');
4545

4646
await withMcpContext(async (response, context) => {
47-
const page = context.getSelectedPage();
48-
await page.setContent('<input id="foo"><label for="foo">Foo</label>');
47+
const page = context.getSelectedPage();
48+
await page.setContent('<input id="foo"><label for="foo">Foo</label>');
4949

50-
// Test Orphaned Inputs Snippet
51-
await evaluateScript.handler(
52-
{params: {function: orphanInputsSnippet}},
53-
response,
54-
context
55-
);
56-
let lineEvaluation = response.responseLines.at(2)!;
57-
let result = JSON.parse(lineEvaluation);
58-
// Expect empty array because we have a valid label
59-
assert.deepStrictEqual(result, []);
50+
// Test Orphaned Inputs Snippet
51+
await evaluateScript.handler(
52+
{ params: { function: orphanInputsSnippet } },
53+
response,
54+
context,
55+
);
56+
let lineEvaluation = response.responseLines.at(2)!;
57+
let result = JSON.parse(lineEvaluation);
58+
// Expect empty array because we have a valid label
59+
assert.deepStrictEqual(result, []);
6060

61-
// Test Global Page Checks Snippet
62-
response.resetResponseLineForTesting();
63-
await evaluateScript.handler(
64-
{params: {function: globalPageChecksSnippet}},
65-
response,
66-
context
67-
);
68-
lineEvaluation = response.responseLines.at(2)!;
69-
result = JSON.parse(lineEvaluation);
70-
// We expect some result, just check keys
71-
assert.ok('lang' in result);
72-
assert.ok('title' in result);
61+
// Test Global Page Checks Snippet
62+
response.resetResponseLineForTesting();
63+
await evaluateScript.handler(
64+
{ params: { function: globalPageChecksSnippet } },
65+
response,
66+
context,
67+
);
68+
console.log('Global Page Checks Snippet:', globalPageChecksSnippet);
69+
const output = response.toString();
70+
console.log('Response Output:', output);
71+
lineEvaluation = response.responseLines.at(2)!;
72+
result = JSON.parse(lineEvaluation);
73+
// We expect some result, just check keys
74+
assert.ok('lang' in result);
75+
assert.ok('title' in result);
7376
});
7477
});
7578
});

0 commit comments

Comments
 (0)