@@ -31,16 +31,21 @@ export default class AxeObserver {
31
31
)
32
32
}
33
33
34
- this . _violationsCallback = violationsCallback
35
-
36
34
this . observe = this . observe . bind ( this )
37
35
this . disconnect = this . disconnect . bind ( this )
38
36
39
37
this . _alreadyReportedIncidents = new Set ( )
40
38
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 )
43
45
} )
46
+ if ( violations . length > 0 ) {
47
+ violationsCallback ( violations )
48
+ }
44
49
} )
45
50
}
46
51
observe ( targetNode ) {
@@ -60,16 +65,16 @@ export default class AxeObserver {
60
65
this . _mutationObserver . disconnect ( )
61
66
this . __alreadyReportedIncidents . clear ( )
62
67
}
63
- async _auditNode ( node ) {
68
+ async _getViolationsForNode ( node ) {
64
69
const response = await sharedAuditQueue . run ( async ( ) => {
65
70
// Since audits are scheduled asynchronously, it can happen that
66
71
// the node is no longer connected. We cannot analyze it then.
67
72
return node . isConnected ? axeCore . run ( node ) : null
68
73
} )
69
74
70
- if ( ! response ) return
75
+ if ( ! response ) return [ ]
71
76
72
- const violationsToReport = response . violations . filter ( violation => {
77
+ return response . violations . filter ( violation => {
73
78
const filteredNodes = violation . nodes . filter ( node => {
74
79
const key = node . target . toString ( ) + violation . id
75
80
@@ -87,11 +92,5 @@ export default class AxeObserver {
87
92
88
93
return filteredNodes . length > 0
89
94
} )
90
-
91
- const hasViolationsToReport = violationsToReport . length > 0
92
-
93
- if ( hasViolationsToReport ) {
94
- this . _violationsCallback ( violationsToReport )
95
- }
96
95
}
97
96
}
0 commit comments