Skip to content

Commit 25f04c8

Browse files
authored
Add ignoreElements input to API (#69)
* Generate pa11yOpts within getConfig * Update function signatures to expect options object * Add ignoreElements to manifest * Update signature of getPa11yOpts * Pass explicit pa11yOpts into runPa11y
1 parent 11dfb29 commit 25f04c8

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

manifest.yml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ inputs:
99
- name: ignoreDirectories
1010
default: []
1111
description: Array of directories whose pages the plugin should ignore when checking for a11y issues. Defaults to [].
12+
- name: ignoreElements
13+
description: A CSS selector to ignore elements when testing. Accepts multiple comma-separated selectors.
1214
- name: wcagLevel
1315
default: 'WCAG2AA'
1416
description: The level of WCAG 2.1 against which to check site pages. Defaults to 'WCAGAA'; can also be 'WCAGA' or 'WCAGAAA'.

src/config.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@ const PA11Y_DEFAULT_WCAG_LEVEL = 'WCAG2AA'
99
const PA11Y_RUNNERS = ['axe']
1010
const PA11Y_USER_AGENT = 'netlify-plugin-a11y'
1111

12-
const getConfiguration = ({
12+
const getConfiguration = async ({
1313
constants: { PUBLISH_DIR },
14-
inputs: { checkPaths, ignoreDirectories, failWithIssues, wcagLevel },
14+
inputs: { checkPaths, ignoreDirectories, ignoreElements, failWithIssues, wcagLevel },
1515
}) => {
1616
return {
17-
publishDir: PUBLISH_DIR || process.env.PUBLISH_DIR,
1817
checkPaths: checkPaths || DEFAULT_CHECK_PATHS,
19-
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
2018
failWithIssues: failWithIssues !== undefined ? failWithIssues : DEFAULT_FAIL_WITH_ISSUES,
21-
wcagLevel: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL,
19+
ignoreDirectories: ignoreDirectories || DEFAULT_IGNORE_DIRECTORIES,
20+
pa11yOpts: await getPa11yOpts({ hideElements: ignoreElements, standard: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL }),
21+
publishDir: PUBLISH_DIR || process.env.PUBLISH_DIR,
2222
}
2323
}
2424

25-
const getPa11yOpts = async (wcagLevel) => {
25+
/**
26+
* Generates the options object used to configure Pa11y.
27+
* @param {Object} pa11yInputs
28+
* @param {String} [pa11yInputs.hideElements]
29+
* @param {'WCAG2A' | 'WCAG2AA' | 'WCAG2AAA'} [pa11yInputs.standard]
30+
* @returns
31+
*/
32+
const getPa11yOpts = async ({ hideElements, standard }) => {
2633
return {
2734
browser: await puppeteer.launch({ ignoreHTTPSErrors: true }),
35+
hideElements,
2836
runners: PA11Y_RUNNERS,
2937
userAgent: PA11Y_USER_AGENT,
30-
standard: wcagLevel || PA11Y_DEFAULT_WCAG_LEVEL,
38+
standard,
3139
}
3240
}
3341

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pico = require('picocolors')
77
module.exports = {
88
async onPostBuild({ constants, inputs, utils: { build } }) {
99
try {
10-
const { publishDir, checkPaths, ignoreDirectories, wcagLevel, failWithIssues } = getConfiguration({
10+
const { publishDir, checkPaths, ignoreDirectories, failWithIssues, pa11yOpts } = await getConfiguration({
1111
constants,
1212
inputs,
1313
})
@@ -23,7 +23,7 @@ module.exports = {
2323
build,
2424
htmlFilePaths,
2525
publishDir,
26-
wcagLevel,
26+
pa11yOpts,
2727
})
2828
const reportSummary =
2929
`${issueCount === 0 ? 'No' : issueCount} accessibility issues found!` +

src/pluginCore.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ const { extname, join } = require('path')
55
const { isDirectory, isFile } = require('path-type')
66
const { results: cliReporter } = require('./reporter')
77
const readdirp = require('readdirp')
8-
const { getPa11yOpts } = require('./config')
98
const { StaticServer, SERVER_ADDRESS } = require('./server')
109

1110
const EMPTY_ARRAY = []
1211
const ASTERISK = '*'
1312
const HTML_EXT = '.html'
1413
const GLOB_HTML = '*.html'
1514

16-
exports.runPa11y = async function ({ build, htmlFilePaths, publishDir, wcagLevel }) {
17-
const pa11yOpts = await getPa11yOpts(wcagLevel)
15+
exports.runPa11y = async function ({ build, htmlFilePaths, pa11yOpts, publishDir }) {
1816
let issueCount = 0
1917

2018
const staticServer = new StaticServer(publishDir).listen()

tests/runPa11y/this.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const path = require('path')
2+
const { getPa11yOpts } = require('../../src/config')
23
const filePath = path.relative(process.cwd(), path.join(__dirname, 'publishDir/index.html'))
34

45
// actual test
56
const pluginCore = require('../../src/pluginCore')
67
test('runPa11y works', async () => {
78
const results = await pluginCore.runPa11y({
8-
htmlFilePaths: [filePath],
99
build: { failBuild() {} },
10+
htmlFilePaths: [filePath],
11+
pa11yOpts: await getPa11yOpts({}),
1012
})
1113
expect(results).toMatchSnapshot()
1214
})

0 commit comments

Comments
 (0)