Skip to content

Commit 6135bca

Browse files
committed
Merge pull request #8 from crawlkit/better-output
feat: provide context in output
2 parents bd0a371 + a5d9432 commit 6135bca

6 files changed

Lines changed: 119 additions & 7 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"mocha": "^2.3.4"
4141
},
4242
"dependencies": {
43-
"accessibility-developer-tools": "^2.10.0"
43+
"accessibility-developer-tools": "^2.10.0",
44+
"html-context": "^1.0.5"
4445
},
4546
"peerDependencies": {
4647
"crawlkit": ">= 1.1.0"

src/index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,54 @@ class A11yDeveloperToolsRunner {
77
// ah, the things your eyes have seen now...
88
path.join(__dirname, 'amd.undefine.js'),
99
require.resolve(path.join('accessibility-developer-tools', 'dist', 'js', 'axs_testing.js')),
10+
require.resolve(path.join('html-context', 'dist', 'index.js')),
1011
path.join(__dirname, 'amd.redefine.js'),
1112
];
1213
}
1314

1415
getRunnable() {
15-
/* global axs:false */
16+
/* global axs:false, htmlContext:false */
1617
/* eslint-disable no-var, vars-on-top */
1718
return function axsRunner() {
1819
var configuration = new axs.AuditConfiguration();
1920
configuration.showUnsupportedRulesWarning = false;
2021
var results = axs.Audit.run(configuration);
21-
window.callPhantom(null, axs.Audit.auditResults(results));
22+
23+
/**
24+
* @param line {!String} The error line, something like: "Warning: ERROR_CODE (Description) failed on the following element:\nhtml\nSee https://link for more information
25+
*/
26+
function parseError(line) {
27+
var lines = line.split('\n');
28+
29+
var matches = lines.shift().match(/^(?:Warning|Error): ([A-Z0-9_]+) \((.*)\) failed on the following element:$/);
30+
var urlMatches = lines.pop().match(/^See (.*) for more information\.$/);
31+
32+
var ret = {
33+
code: matches[1],
34+
elements: lines.map(function selectorToContext(selector) {
35+
return {
36+
selector: selector,
37+
context: htmlContext(document.querySelector(selector), { maxLength: 255 }),
38+
};
39+
}),
40+
msg: matches[2],
41+
helpUrl: urlMatches[1],
42+
};
43+
return ret;
44+
}
45+
46+
function reformat(res) {
47+
if (!res) {
48+
return res;
49+
}
50+
51+
return {
52+
errors: res.errors_.map(parseError),
53+
warnings: res.warnings_.map(parseError),
54+
};
55+
}
56+
57+
window.callPhantom(null, reformat(axs.Audit.auditResults(results)));
2258
};
2359
}
2460
}

test/fixtures/results/amd.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"errors": [
3+
],
4+
"warnings": [
5+
{
6+
"code": "AX_TITLE_01",
7+
"msg": "The web page should have a title that describes topic or purpose",
8+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_title_01",
9+
"elements": [
10+
{
11+
"selector": "html",
12+
"context": "<html lang=\"en\"><head><script src=\"js/almond.js\"></script>\n </head><body><a href=\"\" onclick=\"window.location.href = 'http://www.google.com';\">no href</a>\n\n</body></html>"
13+
}
14+
]
15+
}
16+
]
17+
}

test/fixtures/results/index.json

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
11
{
2-
"errors_": [],
3-
"warnings_": [
4-
"Warning: AX_TITLE_01 (The web page should have a title that describes topic or purpose) failed on the following element:\nhtml\nSee https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_title_01 for more information."
2+
"errors": [
3+
{
4+
"code": "AX_TEXT_01",
5+
"msg": "Controls and media elements should have labels",
6+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_text_01",
7+
"elements": [
8+
{
9+
"selector": "#no-label",
10+
"context": "<input id=\"no-label\">"
11+
}
12+
]
13+
}
14+
],
15+
"warnings": [
16+
{
17+
"code": "AX_FOCUS_01",
18+
"msg": "These elements are focusable but either invisible or obscured by another element",
19+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_focus_01",
20+
"elements": [
21+
{
22+
"selector": "A:nth-of-type(2)",
23+
"context": "<a href=\"#no-link-text\"></a>"
24+
}
25+
]
26+
},
27+
{
28+
"code": "AX_TEXT_02",
29+
"msg": "Images should have a text alternative or presentational role",
30+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_text_02",
31+
"elements": [
32+
{
33+
"selector": "IMG",
34+
"context": "<img src=\"no-alt\">"
35+
}
36+
]
37+
},
38+
{
39+
"code": "AX_TEXT_04",
40+
"msg": "The purpose of each link should be clear from the link text",
41+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_text_04",
42+
"elements": [
43+
{
44+
"selector": "A:nth-of-type(2)",
45+
"context": "<a href=\"#no-link-text\"></a>"
46+
}
47+
]
48+
},
49+
{
50+
"code": "AX_TITLE_01",
51+
"msg": "The web page should have a title that describes topic or purpose",
52+
"helpUrl": "https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_title_01",
53+
"elements": [
54+
{
55+
"selector": "html",
56+
"context": "<html lang=\"en\"><head></head><body><a href=\"\" onclick=\"window.location.href = 'http://www.google.com';\">no href</a>\n <a href=\"#no-link-text\"></a>\n <img src=\"no-alt\">\n <input id=\"no-label\">\n\n</body></html>"
57+
}
58+
]
59+
}
560
]
661
}

test/fixtures/website/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<a href="" onclick="window.location.href = 'http://www.google.com';">no href</a>
4+
<a href="#no-link-text"></a>
5+
<img src="no-alt">
6+
<input id="no-label">
47
</html>

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Google Chrome Accessibility Developer Tools runner', function main() {
6161
results[amdUrl] = {
6262
runners: {
6363
'a11y-dev-tools': {
64-
result: require(path.join(__dirname, 'fixtures/results/index.json')),
64+
result: require(path.join(__dirname, 'fixtures/results/amd.json')),
6565
},
6666
},
6767
};

0 commit comments

Comments
 (0)