Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 790b421

Browse files
authored
feat: Support aXe-core 3.0 Shadow DOM selectors (#49)
1 parent 44a9858 commit 790b421

File tree

6 files changed

+134
-4
lines changed

6 files changed

+134
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ axeTestUrls(urls, program, {
9898
' Violation of %j with %d occurrences!\n') +
9999
' %s. Correct invalid elements at:\n' +
100100
(violation.nodes.map( node =>
101-
' - ' + node.target + '\n'
101+
' - ' + utils.selectorToString(node.target) + '\n'
102102
).join('')) +
103103
' For details, see: %s',
104104
violation.id,

lib/utils.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ module.exports.getAxeSource = function getAxeSource(axePath) {
6262
return fs.readFileSync(axePath, 'utf8')
6363
}
6464

65-
6665
module.exports.getAxeVersion = function getAxeVersion(source) {
6766
const match = source.match(/\.version\s*=\s'([^']+)'/)
6867
return (match ? match[1] : 'unknown version')
6968
}
7069

71-
7270
module.exports.splitList = function (val) {
7371
return (val.split(/[,;]/)).map(str => str.trim());
74-
}
72+
}
73+
74+
module.exports.selectorToString = function (selectors, separator) {
75+
separator = separator || ' ';
76+
return selectors
77+
.reduce((prev, curr) => (prev.concat(curr)), [])
78+
.join(separator);
79+
}

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"devDependencies": {
4646
"chai": "^4.0.0",
4747
"mocha": "^3.4.2",
48+
"node-static": "^0.7.10",
4849
"snyk": "^1.41.1"
4950
},
5051
"snyk": true

test/integrations.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* global mocha */
2+
'use strict';
3+
4+
var assert = require('chai').assert
5+
6+
var chromedriver = require('chromedriver')
7+
var chrome = require('selenium-webdriver/chrome')
8+
var http = require('http')
9+
var nodeStatic = require('node-static')
10+
var axeTestUrls = require('../lib/axe-test-urls')
11+
var {startDriver, stopDriver} = require('../lib/webdriver')
12+
13+
describe('integrations', function () {
14+
var program, urls, server;
15+
16+
before(function () {
17+
// Start a server
18+
var file = new nodeStatic.Server('.');
19+
server = http.createServer(function (request, response) {
20+
request.addListener('end', function () {
21+
file.serve(request, response);
22+
}).resume();
23+
})
24+
server.listen(8182);
25+
})
26+
27+
after(function () {
28+
server.close();
29+
})
30+
31+
beforeEach(function () {
32+
program = {
33+
browser: 'chrome-headless'
34+
}
35+
startDriver(program)
36+
urls = ['http://localhost:8182/test/testpage.html']
37+
})
38+
39+
afterEach(function (done) {
40+
stopDriver(program)
41+
42+
var service = chrome.getDefaultService()
43+
if (service.isRunning()) {
44+
service.stop().then(() => {
45+
// An unfortunately hacky way to clean up
46+
// the service. Stop will shut it down,
47+
// but it doesn't reset the local state
48+
service.address_ = null;
49+
chrome.setDefaultService(null);
50+
done()
51+
})
52+
} else {
53+
done()
54+
}
55+
})
56+
57+
it('finds results in light and shadow DOM', function () {
58+
var listResult
59+
return axeTestUrls(urls, program, {
60+
onTestComplete: function (results) {
61+
listResult = results.violations.find(result => result.id === 'list')
62+
assert.lengthOf(listResult.nodes, 2);
63+
assert.deepEqual(listResult.nodes[0].target, ['#list'])
64+
assert.deepEqual(listResult.nodes[1].target, [['#shadow-root', '#shadow-list']])
65+
}
66+
}).then(function () {
67+
assert.isDefined(listResult)
68+
})
69+
})
70+
})

test/testpage.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>List Item Test</title>
6+
</head>
7+
<body>
8+
<main>
9+
<h1>Page heading</h1>
10+
<ul id="list">
11+
<p>Item One</p>
12+
<p>Item Two</p>
13+
</ul>
14+
15+
<div id="shadow-root"></div>
16+
</main>
17+
<script>
18+
document.querySelector('#shadow-root')
19+
.attachShadow({ mode: 'open' })
20+
.innerHTML =
21+
' <ul id="shadow-list">' +
22+
' <p>Item One</p>' +
23+
' <p>Item Two</p>' +
24+
' </ul>';
25+
</script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)