Skip to content

Commit

Permalink
chore(rules): add rule split by locales
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 9, 2024
1 parent 4f1e2ca commit 87c25db
Show file tree
Hide file tree
Showing 72 changed files with 214,856 additions and 2,716 deletions.
6 changes: 4 additions & 2 deletions fast_htmlcs/HTMLCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ _global.HTMLCS = new (function () {
if (options.include && options.include.length > 0) {
// Included sniffs.
ruleSet.sniffs = options.include;
} else if (options.exclude) {
}

if (options.exclude) {
// Excluded sniffs.
for (const exclude of options.exclude) {
// @ts-ignore
Expand Down Expand Up @@ -423,7 +425,7 @@ _global.HTMLCS = new (function () {
standard,
sniffs.shift(),
function () {
_registerSniffs(standard, sniffs, callback, failCallback);
_registerSniffs(standard, sniffs, callback, failCallback);
},
failCallback
);
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/Standards/Section508/Sniffs/D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _global.HTMLCS_Section508_Sniffs_D = {

_global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1.testPresentationMarkup(
top
)
);
this.testHeadingOrder(top);

// Look for any script elements, and fire off another notice regarding
Expand Down
2 changes: 1 addition & 1 deletion kayle/bench/fast_axecore-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function launchBench() {
unit: "OPS/S",
value: 1000 / avg,
},
]),
])
);
}

Expand Down
2 changes: 1 addition & 1 deletion kayle/bench/fast_htmlcs-playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function launchBench() {
unit: "OPS/S",
value: 1000 / avg,
},
]),
])
);
}

Expand Down
2 changes: 1 addition & 1 deletion kayle/builder/build-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ window.addEventListener("kayle_send", async (event) => {

writeFileSync(
`${ext}/content-script.js`,
`${extensionRunner}\n${extensionAxe}\n${extensionHtmlcs}\n${extensionRawEnd}`,
`${extensionRunner}\n${extensionAxe}\n${extensionHtmlcs}\n${extensionRawEnd}`
);

const extensionManifest = `{
Expand Down
163 changes: 101 additions & 62 deletions kayle/builder/build-rules.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,130 @@
import { chromium } from "playwright";
import { Standard, kayle } from "kayle";
import { writeFile } from "fs/promises";
import { Standard, kayle, htmlcsLocales, axeLocales } from "kayle";
import { writeFile, mkdir } from "fs/promises";
import { existsSync } from "node:fs";

import { format } from "prettier";
import { htmlcsRuleMap } from "./htmlcs-rule-map";
import { processParams } from "./build-htmlcs-params";
import type { Rule } from "./build-types";

(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();
page.on("console", (msg) => console.log("PAGE LOG:", msg.text()));

const fast_htmlcs_rules: Rule[] = [];
const fast_axe_rules: Rule[] = [];

// inject the scripts
await kayle(
{
page,
browser,
runners: ["htmlcs", "axe"],
html: "<html><body>><h1>Build Rules list</h1></body></html>",
standard: Standard.WCAG2AA,
origin: "https://www.example.com",
},
true,
);

const paramList = await processParams();
const pConfig = {
singleQuote: true,
semi: false,
parser: "babel",
};

await page.evaluate((o) => {
window.paramList = o;
}, paramList);
const runBuildRules = async (language: string) => {
const page = await browser.newPage();

await page.addScriptTag({
content: `window.htmlcsRuleMap = ${htmlcsRuleMap.toString()};`,
});
const fast_htmlcs_rules: Rule[] = [];
const fast_axe_rules: Rule[] = [];

await page.exposeFunction("pushHtmlcsRule", (t: Rule) =>
fast_htmlcs_rules.push(t),
);
// inject the scripts
await kayle(
{
page,
browser,
runners: ["htmlcs", "axe"],
html: "<html><body><h1>Build Rules list</h1></body></html>",
standard: Standard.WCAG2AA,
origin: "https://www.example.com",
language,
},
true
);

await page.exposeFunction("pushAxeRule", (t: Rule) => fast_axe_rules.push(t));
const paramList = await processParams();

await page.evaluate(() => {
for (const r of window.axe.getRules()) {
window.pushAxeRule(r);
}
for (const k of window.paramList) {
if (k && k.length >= 4) {
window.pushHtmlcsRule(window.htmlcsRuleMap(k));
await page.evaluate((o) => {
window.paramList = o;
}, paramList);

await page.addScriptTag({
content: `window.htmlcsRuleMap = ${htmlcsRuleMap.toString()};`,
});

await page.exposeFunction("pushHtmlcsRule", (t: Rule) =>
fast_htmlcs_rules.push(t)
);

await page.exposeFunction("pushAxeRule", (t: Rule) =>
fast_axe_rules.push(t)
);

await page.evaluate(() => {
for (const r of window.axe.getRules()) {
window.pushAxeRule(r);
}
for (const k of window.paramList) {
if (k && k.length >= 4) {
window.pushHtmlcsRule(window.htmlcsRuleMap(k));
}
}
});

if (!existsSync(`./lib/rules/${language}`)) {
await mkdir(`./lib/rules/${language}`);
}
});

const pConfig = {
singleQuote: true,
semi: false,
parser: "babel",
};
const DNE = "THIS FILE WAS CREATED DYNAMICALLY - DO NOT EDIT";

const DNE = "THIS FILE WAS CREATED DYNAMICALLY - DO NOT EDIT";
await writeFile(
`./lib/rules/${language}/htmlcs-rules.ts`,
Buffer.from(
await format(
`/* ${DNE} */\nexport const htmlcsRules = ${JSON.stringify(
fast_htmlcs_rules.filter((r) => r.description)
)};`,
pConfig
)
),
"utf8"
);

await writeFile(
"./lib/rules/htmlcs-rules.ts",
Buffer.from(
await format(
`/* ${DNE} */\nexport const htmlcsRules = ${JSON.stringify(
fast_htmlcs_rules.filter((r) => r.description),
)};`,
pConfig,
await writeFile(
`./lib/rules/${language}/axe-rules.ts`,
Buffer.from(
await format(
`/* ${DNE} */\nexport const axeRules = ${JSON.stringify(
fast_axe_rules
)};`,
pConfig
)
),
),
"utf8",
"utf8"
);

await page.close({
runBeforeUnload: true,
});
};

const localesList: string[] = Array.from(
new Set(htmlcsLocales.concat(axeLocales))
);

localesList.push("en");

await Promise.all(localesList.map((r) => runBuildRules(r)));

await writeFile(
"./lib/rules/axe-rules.ts",
`./lib/rules/index.ts`,
Buffer.from(
await format(
`/* ${DNE} */\nexport const axeRules = ${JSON.stringify(
fast_axe_rules,
)};`,
pConfig,
),
localesList
.map((l) => {
return `export { axeRules as axeRules${l.toUpperCase()} } from "./${l}/axe-rules";
export { htmlcsRules as htmlcsRules${l.toUpperCase()} } from "./${l}/htmlcs-rules";`;
})
.join(""),
pConfig
)
),
"utf8",
"utf8"
);

await browser.close();
Expand Down
23 changes: 15 additions & 8 deletions kayle/builder/htmlcs-rule-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Rule, ParamList } from "./build-types";
import type { ParamList } from "./build-types";

// Hand stich the rules to map from htmlcs.
// We need to map the ruleId to the translation file key.
Expand Down Expand Up @@ -26,21 +26,28 @@ export const htmlcsRuleMap = (rule: ParamList) => {
const inlineTranslation =
rule[2] && rule[2].startsWith("HTMLCS.getTranslation");

const description =
(inlineTranslation && eval(rule[2])) ||
rule[2] ||
window.HTMLCS.getTranslation(`${rule[4]}_${rule[3]}`) ||
window.HTMLCS.getTranslation(`${rule[4]}${rule[3]}`) ||
"";
let description = "";

const baseRule = `${rule[4]}${directRuleAssignments.includes(rule[3]) ? "." : "_"}${rule[3]}`;
try {
description =
(inlineTranslation && eval(rule[2])) ||
rule[2] ||
window.HTMLCS.getTranslation(`${rule[4]}_${rule[3]}`) ||
window.HTMLCS.getTranslation(`${rule[4]}${rule[3]}`) ||
"";
} catch (_e) {}

const baseRule = `${rule[4]}${
directRuleAssignments.includes(rule[3]) ? "." : "_"
}${rule[3]}`;
const baseRuleId =
section508 || !description ? rule[3] || baseRule : baseRule;

const inlineRule = inlineTranslation
? rule[2].replace("HTMLCS.getTranslation(", "").replace(")", "")
: "";

// FIXME: we need to get the BASE GUIDELINES upfront like WCAG2AA.Principle1.Guideline1_4 to concat the id with.
const ruleId = inlineTranslation
? inlineRule.substring(1, inlineRule.length - 1)
: baseRuleId;
Expand Down
22 changes: 11 additions & 11 deletions kayle/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export const actions = [
target.dispatchEvent(
new Event("input", {
bubbles: true,
}),
})
);

return Promise.resolve();
},
selector,
value,
value
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand Down Expand Up @@ -91,7 +91,7 @@ export const actions = [
target.dispatchEvent(
new Event("input", {
bubbles: true,
}),
})
);
return Promise.resolve();
}, selector);
Expand All @@ -117,12 +117,12 @@ export const actions = [
target.dispatchEvent(
new Event("change", {
bubbles: true,
}),
})
);
return Promise.resolve();
},
selector,
checked,
checked
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand Down Expand Up @@ -176,7 +176,7 @@ export const actions = [
{},
property,
expectedValue,
negated,
negated
);
},
},
Expand Down Expand Up @@ -216,7 +216,7 @@ export const actions = [
polling: 200,
},
selector,
state,
state
);
},
},
Expand Down Expand Up @@ -244,11 +244,11 @@ export const actions = [
},
{
once: true,
},
}
);
},
selector,
eventType,
eventType
);

await page.waitForFunction(
Expand All @@ -263,7 +263,7 @@ export const actions = [
},
{
polling: 200,
},
}
);
} catch (error) {
throw new Error(`${failedActionElement} "${selector}"`);
Expand All @@ -283,7 +283,7 @@ export const actions = [
*/
export async function runAction(browser, page, options, act, customActions?) {
const action = (customActions ?? actions).find((item) =>
item.match.test(act),
item.match.test(act)
);

if (!action) {
Expand Down
Loading

0 comments on commit 87c25db

Please sign in to comment.