I need to check the accessibility of HTML code, herewith:
- I need the Node.js API (class of function), not CLI.
- I want to pass the HTML string as parameter, not the URI of file path.
Is should be something like:
import AccessibilityInspector from "package-which_i_need";
AccessibilityInspector.inspect({
rawHTML_Code: `<!doctypehtml><html lang=en><meta charset=utf-8><title>title</title><link href=style.css rel=stylesheet><script src=script.js></script>`,
standard: "WCAG2AAA"
}).
then((issues: Array<AccessibilityInspector.Issue>): void => {
// Log the issues
}).
catch((error: unknown) => {
console.error(error);
})
Below packages does not satisfy to above conditions:
- The pa11y accepts only URI as the first parameter, but not the HTML code.
- According the documentation, the access-sniff accepts only URIs too. Although the raw HTML could be also passed, "Pattern is too long" unclear error could occur. Also, the access-sniff has many vulnerabilities and not being maintained.
Other options?
pa11yhas two modes of operation: get contents from a web page by following the URL, or get contents from the providedbrowserandpageinstances directly, without making an HTTP request. The behavior is controlled byignoreUrlparameter, which isfalseby default.Get contents directly from
browserandpageWith the help of
puppeteer, you can createbrowserandpageinstances and provide them topa11y. You would need to setignoreUrltotrue:Get contents from a local server-hosted web page
Alternatively, you can spin up a local server that would respond to any request with the HTML code, effectively making your own temporary website with the said HTML code.
This can totally be implemented using the built-in
httpmodule, although it is more cumbersome that way. The below implementation usesexpress:Note that the server is attached to whatever port is available at the time, which is why we have to use
getPortfunction (or similar).