Skip to content

Commit 4c4b48d

Browse files
committed
Replace the options parameter by exporting the axeCoreInstance
1 parent 1f42096 commit 4c4b48d

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

README.MD

+14-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ Important: When adding Agnostic Axe to your project, be sure to only import it i
2323

2424
## API Details
2525

26-
The `AxeObserver` constructor takes two parameters:
26+
The `AxeObserver` constructor accepts one parameter:
2727

2828
- `violationsCallback` (required). A function that is invoked with an array of violations, as reported by [axe-core](https://github.com/dequelabs/axe-core). To log violations to the console, simply pass the `logViolations` function exported by this module.
29-
- `options` (optional). An object with that supports the following configuration keys:
30-
- `axeCoreConfiguration` (optional). [A configuration object for axe-core](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#api-name-axeconfigure). Overwrites the default configuration used by agnostic axe.
31-
- `axeCoreInstanceCallback` (optional). A callback that is invoked with the [axe-core](https://github.com/dequelabs/axe-core) instance.
3229

3330
The `AxeObserver.observe` method takes one parameter:
3431

@@ -50,6 +47,19 @@ To stop observing changes, invoke the `disconnect` method.
5047
MyAxeObserver.disconnect()
5148
```
5249

50+
### Interacting with the axe-core API
51+
52+
The instance of axe-core used by agnostic-axe is exported by this module. Import it to interact with the [axe-core API](https://github.com/dequelabs/axe-core/blob/develop/doc/API.md).
53+
54+
```js
55+
import('https://unpkg.com/agnostic-axe@3').then(
56+
({ axeCoreInstance AxeObserver, logViolations }) => {
57+
axeCoreInstance.registerPlugin(myPlugin)
58+
// ...
59+
}
60+
)
61+
```
62+
5363
## Comparison with react-axe
5464

5565
Unlike framework specific implementations of [axe-core](https://github.com/dequelabs/axe-core), such as [react-axe](https://github.com/dequelabs/react-axe), agnostic axe uses a [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to listen for changes directly in the DOM. This has two advantages:

src/AxeObserver.mjs

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import axeCore from 'axe-core'
22
import AuditQueue from './AuditQueue.mjs'
33

4+
// Apply default axeCore config
5+
axeCore.configure({
6+
reporter: 'v2',
7+
checks: [
8+
{
9+
id: 'color-contrast',
10+
options: {
11+
// Prevent axe from automatically scrolling
12+
noScroll: true
13+
}
14+
}
15+
]
16+
})
17+
18+
export const axeCore
19+
420
// The AxeObserver class takes a violationsCallback, which is invoked with an
521
// array of observed violations.
622
export default class AxeObserver {
723
constructor(
8-
violationsCallback,
9-
{
10-
axeCoreConfiguration = {
11-
reporter: 'v2',
12-
checks: [
13-
{
14-
id: 'color-contrast',
15-
options: {
16-
// Prevent axe from automatically scrolling
17-
noScroll: true
18-
}
19-
}
20-
]
21-
},
22-
axeCoreInstanceCallback
23-
} = {}
24+
violationsCallback
2425
) {
2526
if (typeof violationsCallback !== 'function') {
2627
throw new Error(
@@ -43,14 +44,6 @@ export default class AxeObserver {
4344

4445
// AuditQueue sequentially runs audits when the browser is idle.
4546
this._auditQueue = new AuditQueue()
46-
47-
// Allow for registering plugins etc
48-
if (typeof axeCoreInstanceCallback === 'function') {
49-
axeCoreInstanceCallback(axeCore)
50-
}
51-
52-
// Configure axe
53-
axeCore.configure(axeCoreConfiguration)
5447
}
5548
observe(targetNode) {
5649
if (!targetNode) {

src/index.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import _AxeObserver from './AxeObserver.mjs'
1+
import _AxeObserver, {
2+
axeCoreInstance as _axeCoreInstance
3+
} from './AxeObserver.mjs'
24
import _logViolations from './logViolations.mjs'
35

46
export const AxeObserver = _AxeObserver
7+
export const axeCoreInstance = _axeCoreInstance
58
export const logViolations = _logViolations

src/logViolations.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axeCore from 'axe-core'
1+
import { axeCoreInstance } from './AxeObserver'
22

33
const boldCourier = 'font-weight:bold;font-family:Courier;'
44
const critical = 'color:red;font-weight:bold;'
@@ -64,7 +64,9 @@ function logHtml(node) {
6464
}
6565

6666
function logFailureMessage(node, key) {
67-
var message = axeCore._audit.data.failureSummaries[key].failureMessage(
67+
var message = axeCoreInstance._audit.data.failureSummaries[
68+
key
69+
].failureMessage(
6870
node[key].map(function(check) {
6971
return check.message || ''
7072
})

0 commit comments

Comments
 (0)