skip to Main Content

I need to check the accessibility of HTML code, herewith:

  1. I need the Node.js API (class of function), not CLI.
  2. 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?

2

Answers


  1. Run an accessibility test against a file (absolute paths only, not
    relative):

    pa11y ./path/to/your/file.html
    

    You can do:

    const { exec } = require("child_process");
    
    function run() {
        exec("pa11y ./path/to/your/file.html", (error, stdout, stderr) => {
        if (error) {
            console.log(`[ERROR] openCashDrawer: ${error.message}`);
            return;
        }
        
        if (stderr) {
            console.log(`[STDERROR] openCashDrawer: ${stderr}`);
            return;
        }
    
        console.log(`openCashDrawer: ${stdout}`); // Output response from the terminal
        });
    }
    
    Login or Signup to reply.
  2. There seems to be a way to do that with puppeteer. The gist of it is to render the HTML in a browser and read the page from there, — although the browser is programmatic here.

    import puppeteer from 'puppeteer'
    import pa11y from 'pa11y'
    
    // Here comes your raw HTML code
    const htmlCode = '<html><body>Hello world!</body></html>'
    
    async function main() {
      const browser = await puppeteer.launch()
      const page = await browser.newPage()
    
      await page.setContent(htmlCode, {
        waitUntil: 'domcontentloaded',
      })
    
      const url = page.url()
      const a11y = await pa11y(url, { browser, page })
    
      console.log(a11y)
    }
    
    main()
    

    References:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search