Skip to content

Commit c0c9dec

Browse files
committed
Report violations for every callback of the MutationObserver
(Instead of for every node in every callback of the MutationObserver)
1 parent 12ec118 commit c0c9dec

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/AxeObserver.mjs

+12-13
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ export default class AxeObserver {
3131
)
3232
}
3333

34-
this._violationsCallback = violationsCallback
35-
3634
this.observe = this.observe.bind(this)
3735
this.disconnect = this.disconnect.bind(this)
3836

3937
this._alreadyReportedIncidents = new Set()
4038
this._mutationObserver = new window.MutationObserver(mutationRecords => {
41-
mutationRecords.forEach(mutationRecord => {
42-
this._auditNode(mutationRecord.target)
39+
const violations = []
40+
mutationRecords.forEach(async mutationRecord => {
41+
const violationsInCurrentNode = await this._getViolationsForNode(
42+
mutationRecord.target
43+
)
44+
violationsInCurrentNode.forEach(violations.push)
4345
})
46+
if (violations.length > 0) {
47+
violationsCallback(violations)
48+
}
4449
})
4550
}
4651
observe(targetNode) {
@@ -60,16 +65,16 @@ export default class AxeObserver {
6065
this._mutationObserver.disconnect()
6166
this.__alreadyReportedIncidents.clear()
6267
}
63-
async _auditNode(node) {
68+
async _getViolationsForNode(node) {
6469
const response = await sharedAuditQueue.run(async () => {
6570
// Since audits are scheduled asynchronously, it can happen that
6671
// the node is no longer connected. We cannot analyze it then.
6772
return node.isConnected ? axeCore.run(node) : null
6873
})
6974

70-
if (!response) return
75+
if (!response) return []
7176

72-
const violationsToReport = response.violations.filter(violation => {
77+
return response.violations.filter(violation => {
7378
const filteredNodes = violation.nodes.filter(node => {
7479
const key = node.target.toString() + violation.id
7580

@@ -87,11 +92,5 @@ export default class AxeObserver {
8792

8893
return filteredNodes.length > 0
8994
})
90-
91-
const hasViolationsToReport = violationsToReport.length > 0
92-
93-
if (hasViolationsToReport) {
94-
this._violationsCallback(violationsToReport)
95-
}
9695
}
9796
}

0 commit comments

Comments
 (0)