a11y-test-mcp 1.0.7 → 1.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/build/constants.d.ts +6 -0
- package/build/constants.js +21 -0
- package/build/functions.js +45 -61
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_WCAG_TAGS = exports.ALLOWED_PREFIXES_OR_TAGS = exports.WCAG_TAG_MAP = void 0;
|
|
4
|
+
/** WCAG tag map */
|
|
5
|
+
exports.WCAG_TAG_MAP = {
|
|
6
|
+
'a': 'wcag2a',
|
|
7
|
+
'wcag20a': 'wcag2a',
|
|
8
|
+
'wcag2a': 'wcag2a',
|
|
9
|
+
'aa': 'wcag2aa',
|
|
10
|
+
'wcag20aa': 'wcag2aa',
|
|
11
|
+
'wcag2aa': 'wcag2aa',
|
|
12
|
+
'wcag21a': 'wcag21a',
|
|
13
|
+
'wcag21aa': 'wcag21aa',
|
|
14
|
+
'wcag22a': 'wcag22a',
|
|
15
|
+
'wcag22aa': 'wcag22aa',
|
|
16
|
+
// Add other known tags or aliases here
|
|
17
|
+
};
|
|
18
|
+
/** Allow prefixes or tags */
|
|
19
|
+
exports.ALLOWED_PREFIXES_OR_TAGS = ['wcag', 'best-practice', 'section508'];
|
|
20
|
+
/** default WCAG tags */
|
|
21
|
+
exports.DEFAULT_WCAG_TAGS = ["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"];
|
package/build/functions.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.convertTestResultToText = exports.execTest = void 0;
|
|
7
7
|
const playwright_1 = __importDefault(require("playwright"));
|
|
8
8
|
const playwright_2 = __importDefault(require("@axe-core/playwright"));
|
|
9
|
+
const constants_1 = require("./constants");
|
|
9
10
|
/**
|
|
10
11
|
* Enhance WCAG tag conversion
|
|
11
12
|
* @param {string[]} tags - Array of WCAG tags
|
|
@@ -14,33 +15,28 @@ const playwright_2 = __importDefault(require("@axe-core/playwright"));
|
|
|
14
15
|
const convertWcagTag = (tags) => {
|
|
15
16
|
return tags.map(tag => {
|
|
16
17
|
const lowerTag = tag.toLowerCase().replace(/[\s.]/g, '');
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
case 'wcag2aa':
|
|
23
|
-
case 'aa':
|
|
24
|
-
case 'wcag20aa':
|
|
25
|
-
return 'wcag2aa';
|
|
26
|
-
case 'wcag21a':
|
|
27
|
-
return 'wcag21a';
|
|
28
|
-
case 'wcag21aa':
|
|
29
|
-
return 'wcag21aa';
|
|
30
|
-
case 'wcag22a':
|
|
31
|
-
return 'wcag22a';
|
|
32
|
-
case 'wcag22aa':
|
|
33
|
-
return 'wcag22aa';
|
|
34
|
-
default:
|
|
35
|
-
if (lowerTag.startsWith('wcag') || ['best-practice', 'section508'].includes(lowerTag)) {
|
|
36
|
-
return lowerTag;
|
|
37
|
-
}
|
|
38
|
-
console.warn(`Unrecognized WCAG tag: ${tag}`);
|
|
39
|
-
return '';
|
|
18
|
+
if (lowerTag in constants_1.WCAG_TAG_MAP) {
|
|
19
|
+
return constants_1.WCAG_TAG_MAP[lowerTag];
|
|
20
|
+
}
|
|
21
|
+
if (constants_1.ALLOWED_PREFIXES_OR_TAGS.some(prefixOrTag => lowerTag.startsWith(prefixOrTag) || lowerTag === prefixOrTag)) {
|
|
22
|
+
return lowerTag;
|
|
40
23
|
}
|
|
41
|
-
|
|
42
|
-
return
|
|
43
|
-
});
|
|
24
|
+
console.warn(`Unrecognized WCAG tag: ${tag}`);
|
|
25
|
+
return '';
|
|
26
|
+
}).filter(tag => tag !== '');
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Formats a single accessibility violation into a human-readable string.
|
|
30
|
+
* Includes impact level, ID, description, node count, help URL, and details of each affected node.
|
|
31
|
+
* @param {ViolationSummary} v - The violation summary object containing details about the violation.
|
|
32
|
+
* @returns {string} A formatted string representing the violation, suitable for display in reports or logs.
|
|
33
|
+
*/
|
|
34
|
+
const formatViolation = (v) => {
|
|
35
|
+
const violationHeader = ` - [${String(v.impact?.toUpperCase() ?? 'N/A')}] ${v.id}: ${v.description} (Nodes: ${String(v.nodes.length)}, Help: ${v.helpUrl})`;
|
|
36
|
+
const violationNodes = v.nodes
|
|
37
|
+
.map((node, index) => ` Node ${String(index + 1)}: ${node.html}`)
|
|
38
|
+
.join('\n');
|
|
39
|
+
return `${violationHeader}\n${violationNodes}`;
|
|
44
40
|
};
|
|
45
41
|
/**
|
|
46
42
|
* Execute a11y test
|
|
@@ -49,14 +45,13 @@ const convertWcagTag = (tags) => {
|
|
|
49
45
|
* @returns {AccessibilityTestOutput[]} - Results of the accessibility tests
|
|
50
46
|
*/
|
|
51
47
|
const execTest = async (urls, wcagStandards) => {
|
|
52
|
-
const results = [];
|
|
53
48
|
const browser = await playwright_1.default.chromium.launch();
|
|
54
49
|
const context = await browser.newContext();
|
|
55
50
|
const tagsToUse = (wcagStandards && wcagStandards.length > 0)
|
|
56
51
|
? convertWcagTag(wcagStandards)
|
|
57
|
-
:
|
|
52
|
+
: constants_1.DEFAULT_WCAG_TAGS;
|
|
58
53
|
try {
|
|
59
|
-
|
|
54
|
+
const results = await Promise.all(urls.map(async (url) => {
|
|
60
55
|
let page = null;
|
|
61
56
|
try {
|
|
62
57
|
page = await context.newPage();
|
|
@@ -64,40 +59,38 @@ const execTest = async (urls, wcagStandards) => {
|
|
|
64
59
|
const axeBuilder = new playwright_2.default({ page });
|
|
65
60
|
axeBuilder.withTags(tagsToUse);
|
|
66
61
|
const axeResults = await axeBuilder.analyze();
|
|
67
|
-
// Summarize results, handling null impact
|
|
68
62
|
const summarizedViolations = axeResults.violations.map(v => ({
|
|
69
63
|
id: v.id,
|
|
70
|
-
// Handle null impact from axe-core
|
|
71
64
|
impact: v.impact === null ? undefined : v.impact,
|
|
72
65
|
description: v.description,
|
|
73
66
|
helpUrl: v.helpUrl,
|
|
74
67
|
nodes: v.nodes
|
|
75
68
|
}));
|
|
76
|
-
|
|
69
|
+
return {
|
|
77
70
|
url: url,
|
|
78
71
|
violations: summarizedViolations,
|
|
79
72
|
passesCount: axeResults.passes.length,
|
|
80
73
|
incompleteCount: axeResults.incomplete.length,
|
|
81
74
|
inapplicableCount: axeResults.inapplicable.length,
|
|
82
|
-
}
|
|
75
|
+
};
|
|
83
76
|
}
|
|
84
77
|
catch (error) {
|
|
85
|
-
|
|
78
|
+
return {
|
|
86
79
|
url: url,
|
|
87
80
|
error: `Failed to test: ${error instanceof Error ? error.message : String(error)}`,
|
|
88
|
-
}
|
|
81
|
+
};
|
|
89
82
|
}
|
|
90
83
|
finally {
|
|
91
84
|
if (page !== null) {
|
|
92
85
|
await page.close();
|
|
93
86
|
}
|
|
94
87
|
}
|
|
95
|
-
}
|
|
88
|
+
}));
|
|
89
|
+
return results;
|
|
96
90
|
}
|
|
97
91
|
finally {
|
|
98
92
|
await browser.close();
|
|
99
93
|
}
|
|
100
|
-
return results;
|
|
101
94
|
};
|
|
102
95
|
exports.execTest = execTest;
|
|
103
96
|
/**
|
|
@@ -106,34 +99,25 @@ exports.execTest = execTest;
|
|
|
106
99
|
* @returns {string} - Text representation of the results
|
|
107
100
|
*/
|
|
108
101
|
const convertTestResultToText = (structuredResults) => {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
let outputText = '';
|
|
103
|
+
for (const result of structuredResults) {
|
|
104
|
+
let resultText = `URL: ${result.url}\n`;
|
|
112
105
|
if (result.error) {
|
|
113
|
-
|
|
106
|
+
resultText += ` Error: ${result.error}\n`;
|
|
114
107
|
}
|
|
115
108
|
else {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
.map((node, index) => {
|
|
122
|
-
return ` Node ${String(index + 1)}: ${node.html}`;
|
|
123
|
-
})
|
|
124
|
-
.join('\n')
|
|
125
|
-
].join('\n');
|
|
126
|
-
});
|
|
127
|
-
if (resultViolationText !== undefined) {
|
|
128
|
-
resultTextList.push(...resultViolationText);
|
|
109
|
+
resultText += ` Violations: ${String(result.violations?.length ?? 0)}\n`;
|
|
110
|
+
if (result.violations && result.violations.length > 0) {
|
|
111
|
+
for (const violation of result.violations) {
|
|
112
|
+
resultText += `${formatViolation(violation)}\n`;
|
|
113
|
+
}
|
|
129
114
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
115
|
+
resultText += ` Passes: ${String(result.passesCount ?? 0)}\n`;
|
|
116
|
+
resultText += ` Incomplete: ${String(result.incompleteCount ?? 0)}\n`;
|
|
117
|
+
resultText += ` Inapplicable: ${String(result.inapplicableCount ?? 0)}\n`;
|
|
133
118
|
}
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
.trim();
|
|
119
|
+
outputText += resultText + '\n';
|
|
120
|
+
}
|
|
121
|
+
return outputText.trim();
|
|
138
122
|
};
|
|
139
123
|
exports.convertTestResultToText = convertTestResultToText;
|