Skip to content

Commit

Permalink
chore(rules): fix htmlcs wcag rules
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 10, 2024
1 parent 8f5e537 commit 9f96ebb
Show file tree
Hide file tree
Showing 39 changed files with 21,225 additions and 20,894 deletions.
10 changes: 6 additions & 4 deletions kayle/builder/build-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Rule } from "./build-types";
const browser = await chromium.launch({ headless: true });

const pConfig = {
singleQuote: true,
singleQuote: false,
semi: false,
parser: "babel",
};
Expand Down Expand Up @@ -46,8 +46,8 @@ import type { Rule } from "./build-types";
content: `window.htmlcsRuleMap = ${htmlcsRuleMap.toString()};`,
});

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

await page.exposeFunction("pushAxeRule", (t: Rule) =>
Expand Down Expand Up @@ -76,7 +76,9 @@ import type { Rule } from "./build-types";
Buffer.from(
await format(
`/* ${DNE} */\nexport const htmlcsRules = ${JSON.stringify(
fast_htmlcs_rules.filter((r) => r.description).sort((a, b) => a.ruleId.localeCompare(b.ruleId)),
fast_htmlcs_rules
.filter((r) => r.description)
.sort((a, b) => a.ruleId.localeCompare(b.ruleId)),
)};`,
pConfig,
),
Expand Down
4 changes: 2 additions & 2 deletions kayle/builder/build-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ declare global {
interface Window {
HTMLCS: any;
axe: any;
pushHtmlcsRule: (r: Rule) => void;
pushHtmlcsRule: (r: Rule[]) => void;
pushAxeRule: (r: Rule) => void;
htmlcsRuleMap: (t: ParamList) => Rule;
htmlcsRuleMap: (t: ParamList) => Rule[];
paramList: ParamList[];
}
}
Expand Down
38 changes: 31 additions & 7 deletions kayle/builder/htmlcs-rule-map.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { ParamList } from "./build-types";
import type { ParamList, Rule } from "./build-types";

// Hand stich the rules to map from htmlcs.
// We need to map the ruleId to the translation file key.

// map the rules to a detailed object
// the descriptions handling is not perfect and needs to handle split cases.
export const htmlcsRuleMap = (rule: ParamList) => {
const rules: Rule[] = [];

const concatWcagLink = (l: string) =>
`https://www.w3.org/TR/WCAG20-TECHS/${l}`;

Expand Down Expand Up @@ -36,6 +38,10 @@ export const htmlcsRuleMap = (rule: ParamList) => {
"";
} catch (_e) {}

if (description.startsWith("HTMLCS.getTranslation(")) {
description = eval(description);
}

const baseRule = `${rule[4]}${
directRuleAssignments.includes(rule[3]) ? "." : "_"
}${rule[3]}`;
Expand All @@ -46,18 +52,36 @@ export const htmlcsRuleMap = (rule: ParamList) => {
? rule[2].replace("HTMLCS.getTranslation(", "").replace(")", "")
: "";

const ruleId = inlineTranslation
const _ruleId = inlineTranslation
? inlineRule.substring(1, inlineRule.length - 1)
: baseRuleId;

return {
ruleId: rule[5]
? `${rule[5].replace("_Guideline", ".Guideline")}.${ruleId}`
: ruleId,
const ruleId = rule[5]
? `${rule[5].replace("_Guideline", ".Guideline")}.${_ruleId}`
: _ruleId;

const pattern = /(.{3})_or_(.{3})/;

const [, leftPart, rightPart] = ruleId.match(pattern) || [];

const ruleObj = {
ruleId,
description,
helpUrl: section508
? []
: helpLinks.map((r) => concatWcagLink(r.split(".")[0])),
ruleType: rule[0],
ruleType: rule[0] as "error" | "warning" | "notice",
};

// Check if both parts were found
if (leftPart && rightPart) {
ruleObj.ruleId = ruleId.replace(pattern, leftPart);
rules.push(ruleObj);
ruleObj.ruleId = ruleId.replace(pattern, rightPart);
rules.push(ruleObj);
} else {
rules.push(ruleObj);
}

return rules;
};
Loading

0 comments on commit 9f96ebb

Please sign in to comment.