UNPKG

kayle

Version:

Extremely fast and accurate accessibility engine built for any headless tool like playwright or puppeteer.

122 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.axeLocales = exports.axeRunner = void 0; const runAxeAudit = async (options) => { const axeOptions = { runOnly: undefined, rules: undefined, }; if (options.standard) { axeOptions.runOnly = a11yStandardToAxe(); } axeOptions.rules = a11yRulesToAxe(Array.isArray(options.rules) ? options.rules : [], Array.isArray(options.ignore) ? options.ignore : []); window.axe.configure({ allowedOrigins: window.origin === "null" ? [] : [window.origin], }); async function runAxeCore() { return new Promise(async (resolve) => { const result = await window.axe.run((options.rootElement && window.document.querySelector(options.rootElement)) || window.document, axeOptions); const issues = []; for (const item of result.violations) { processViolation(item, issues); } for (const item of result.incomplete) { processIncomplete(item, issues); } resolve(issues); }); } function a11yStandardToAxe() { if (options.standard === "WCAG2A") { return { type: "tags", values: ["wcag2a", "wcag21a", "best-practice"], }; } return { type: "tags", values: ["wcag2a", "wcag21a", "wcag2aa", "wcag21aa", "best-practice"], }; } function a11yRulesToAxe(rules, ignore) { const axeRuleIds = window.axe.getRules().reduce((allRules, rule) => { allRules[rule.ruleId.toLowerCase()] = true; return allRules; }, {}); const axeRules = {}; rules.forEach((rule) => { if (axeRuleIds[rule]) { axeRules[rule] = { enabled: true }; } }); ignore.forEach((rule) => { if (axeRuleIds[rule]) { axeRules[rule] = { enabled: false }; } }); return axeRules; } function processViolation(issue, issues) { return processIssue(issue, issues, "error"); } function processIncomplete(issue, issues) { return processIssue(issue, issues, "warning"); } function processIssue(axeIssue, issues, impact) { if (axeIssue.nodes && axeIssue.nodes.length) { for (const node of axeIssue.nodes) { issues.push({ type: impact, code: axeIssue.id, message: axeIssue.help, element: window.document.querySelector(selectorToString(node.target)), runnerExtras: { description: axeIssue.description, impact: axeIssue.impact, helpUrl: axeIssue.helpUrl, }, runner: "axe", }); } } } function selectorToString(selectors) { return selectors .reduce((selectorParts, selector) => selectorParts.concat(selector), []) .join(" "); } return await runAxeCore(); }; const axeRunner = { en: { scripts: [require.resolve("fast_axecore/axe.min.js")], run: runAxeAudit, }, }; exports.axeRunner = axeRunner; const axeLocales = [ "ar", "da", "de", "es", "eu", "fr", "he", "ja", "ko", "nl", "pl", "no_NB", "pt_BR", "zh_CN", ]; exports.axeLocales = axeLocales; for (const lang of axeLocales) { axeRunner[lang.replace("_", "-")] = { scripts: [require.resolve(`fast_axecore/axe.${lang}.min.js`)], run: runAxeAudit, }; } //# sourceMappingURL=axe.js.map