Skip to content

Commit 4b0f346

Browse files
author
Cameron Cundiff
committed
Run auditor on mutation events
- Re-run the audit when the DOM changes (via AJAX, or websockets, for example). - Treat the console message as a warning rather than a log message. - Reference the actual DOM node in the console warning, which makes it highlightable and clickable in developer tools. - Does not rerun on visibility changes (yet).
1 parent f93ac7c commit 4b0f346

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ run in the background automatically. If there are any accessibility issues on
2020
that page, accesslint.js will log the error to the console, and post to a server
2121
endpoint that you can optionally configure.
2222

23+
The audit will run once on page load, and **again for each DOM change event.**
24+
This feature gives you feedback on new content introduced via AJAX, for example.
25+
2326
accesslint.js runs assertions from the
2427
[aXe-core](https://github.com/dequelabs/axe-core) accessibility library wherever
2528
you include the script. It the logs the violations the browser's Javascript

src/auditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { axe } from "axe-core/axe.min.js";
22
import report from "./reporter";
33

4-
export default function () {
4+
export default function(target) {
55
const options = {
66
"rules": {
77
"color-contrast": { enabled: false },
88
}
99
};
1010

11-
window.axe.a11yCheck(document, options, (results) => {
11+
window.axe.a11yCheck(target.parentNode, options, (results) => {
1212
report(results);
1313
});
1414
}

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@ import auditor from "./auditor.js";
33
(function() {
44
var load = function() {
55
window.removeEventListener("load", load, false);
6-
auditor();
6+
auditor(document);
7+
};
8+
9+
var observer = new MutationObserver(function(mutations) {
10+
mutations.forEach(function(mutation) {
11+
auditor(mutation.target);
12+
});
13+
});
14+
15+
var config = {
16+
attributes: true,
17+
childList: true,
18+
characterData: true,
19+
subtree: true
720
};
821

922
window.addEventListener("load", load);
23+
observer.observe(document, config);
1024
})();

src/reporter.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ import request from "browser-request";
22

33
const url = "/access_lint/errors";
44

5-
export default function (message) {
5+
export default function(message) {
66
var violations = message.violations.map(function(violation) {
77
return {
88
description: violation.description,
99
help: violation.help,
10-
helpUrl: violation.helpUrl,
11-
id: violation.id,
1210
impact: violation.impact,
1311
nodes: violation.nodes.map(function(n) {
14-
return {
15-
target: n.target,
16-
html: n.html
17-
};
12+
return document.querySelectorAll(n.target);
1813
}),
19-
tags: violation.tags
2014
};
2115
});
2216

@@ -31,7 +25,7 @@ export default function (message) {
3125
}
3226
}
3327
}, function() {});
34-
}
3528

36-
console.log("AccessLint warnings: ", violations);
29+
console.warn(violations);
30+
}
3731
}

0 commit comments

Comments
 (0)