Skip to content

Commit

Permalink
feat(rules): add pre-compiled rules
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Mar 8, 2024
1 parent 7fce8d0 commit ce3c373
Show file tree
Hide file tree
Showing 37 changed files with 1,267 additions and 91 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ chrome-extension

# build custom extension
build-extension.js
build-rules.js

# data dir
_data
10 changes: 0 additions & 10 deletions fast_htmlcs/HTMLCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,6 @@ _global.HTMLCS = new (function () {
}
};

/**
* Returns all the messages for the last run.
*
* Return a copy of the array so the class variable doesn't get modified by
* future modification (eg. splicing).
*
* @returns {Array} Array of message objects.
*/
this.getMessages = () => this.messages;

/**
* Runs the sniffs in the loaded standard for the specified element.
*
Expand Down
1 change: 0 additions & 1 deletion kayle/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ build
_tests
./test-results
_data

./screenshot.png
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/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
94 changes: 94 additions & 0 deletions kayle/build-rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { chromium } from "playwright";
import { Standard, kayle } from "kayle";
import { writeFile } from "fs/promises";
import { format } from "prettier";

type Rule = {
ruleId: string;
description?: string;
help?: string;
helpUrl?: string;
tags?: string[];
actIds?: string[];
};

(async () => {
const browser = await chromium.launch({ headless: true });
const page = await browser.newPage();

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,
);

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(() => {
// @ts-ignore
for (const r of window.axe.getRules()) {
// @ts-ignore we need to get the rules description and urls.
window.pushAxeRule(r);
}
for (const k of Object.keys(window)) {
if (k.startsWith("HTMLCS_WCAG2AAA_Sniffs_Principle")) {
// @ts-ignore
window.pushHtmlcsRule({
ruleId: k,
});
}
}
});

const pConfig = {
singleQuote: true,
semi: false,
parser: "babel",
};

const DNE = "THIS FILE WAS CREATED DYNAMICALLY - DO NOT EDIT";

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

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

await browser.close();
})();
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
8 changes: 4 additions & 4 deletions kayle/lib/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function autoKayle(
cb?: ((result: Audit) => Promise<void>) | ((result: Audit) => void);
} = {},
ignoreSet?: Set<String>,
_results?: Audit[]
_results?: Audit[],
): Promise<Audit[]> {
if (!write) {
const { writeFile } = await import("fs/promises");
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function autoKayle(
if (o.store) {
await write(
`${o.store}/${encodeURIComponent(o.page.url())}`,
await o.page.content()
await o.page.content(),
);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export async function autoKayle(
origin: link,
},
ignoreSet,
_results
_results,
);
})
.catch((e) => {
Expand All @@ -94,7 +94,7 @@ export async function autoKayle(
console.error(e);
}
});
})
}),
);

return _results;
Expand Down
10 changes: 5 additions & 5 deletions kayle/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type BrowserContext = {
newCDPSession?(page: Partial<Page> | Frame): Partial<CDPSession>;
overridePermissions?(
origin: string,
permissions: Permission[]
permissions: Permission[],
): Promise<void>;
};

Expand Down Expand Up @@ -89,7 +89,7 @@ type Page = {
_routes?: { url: string }[];
route(
path: string,
intercept: (config: any, next: any) => Promise<void> | Promise<boolean>
intercept: (config: any, next: any) => Promise<void> | Promise<boolean>,
): Promise<void>;
setRequestInterception?(enable?: boolean): Promise<void>;
listenerCount?(name: string): number;
Expand All @@ -99,19 +99,19 @@ type Page = {
| Function
| {
default: Function;
}
},
): Promise<void>;
addInitScript?(script: { content?: string }): Promise<void>;
evaluateOnNewDocument?<
Params extends unknown[],
Func extends (...args: Params) => unknown = (...args: Params) => unknown
Func extends (...args: Params) => unknown = (...args: Params) => unknown,
>(
pageFunction: Func | string,
...args: Params
): Promise<{ identifier: string }>;
evaluate<
Params extends unknown[],
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>
Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
>(
pageFunction: Func | string,
...args: Params
Expand Down
2 changes: 2 additions & 0 deletions kayle/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ export {
type WaitForOptions,
} from "./config";
export { extractLinks, innateBuilder } from "./wasm";
export { htmlcsRules } from "./rules/htmlcs-rules";
export { axeRules } from "./rules/axe-rules";
16 changes: 8 additions & 8 deletions kayle/lib/kayle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const audit = async (config: RunnerConfig): Promise<Audit> => {
origin: config.origin,
language: config.language,
clip: config.clip,
}
},
);
};

Expand Down Expand Up @@ -61,7 +61,7 @@ const injectRunners = async (config: RunnerConfig) => {
return await Promise.allSettled([
config.page.evaluate(runnersJavascript["kayle"]),
...config.runners.map((r) =>
config.page.evaluate(getRunner(config.language, r))
config.page.evaluate(getRunner(config.language, r)),
),
]);
}
Expand All @@ -77,7 +77,7 @@ export const auditExtension = async (config: RunnerConfig): Promise<Audit> => {
}

window.addEventListener("kayle_receive", (event: CustomEvent) =>
resolve(event.detail.data)
resolve(event.detail.data),
);

window.dispatchEvent(
Expand All @@ -86,7 +86,7 @@ export const auditExtension = async (config: RunnerConfig): Promise<Audit> => {
name: "kayle",
options: runOptions,
},
})
}),
);
});
},
Expand All @@ -100,7 +100,7 @@ export const auditExtension = async (config: RunnerConfig): Promise<Audit> => {
origin: config.origin,
language: config.language,
clip: config.clip,
}
},
);
};

Expand All @@ -121,7 +121,7 @@ const auditPageInnate = async (config: RunnerConf, results: Audit) => {
const innateAudit: InnateIssue[] = await kayle_innate.audit(
html,
css,
config.clip
config.clip,
);

for (const innateIssue of innateAudit) {
Expand Down Expand Up @@ -163,7 +163,7 @@ const auditPageInnate = async (config: RunnerConf, results: Audit) => {
*/
export const kayle = async (
o: RunnerConf = {},
preventClose?: boolean
preventClose?: boolean,
): Promise<Audit> => {
const watcher = new Watcher();
const navigate = o.page.url() === "about:blank" && (o.origin || o.html);
Expand Down Expand Up @@ -212,7 +212,7 @@ export const kayle = async (
}

return item;
})
}),
);
}

Expand Down
Loading

0 comments on commit ce3c373

Please sign in to comment.