accented 0.0.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +14 -0
- package/README.md +44 -187
- package/dist/accented.d.ts +8 -8
- package/dist/accented.d.ts.map +1 -1
- package/dist/accented.js +37 -30
- package/dist/accented.js.map +1 -1
- package/dist/common/strings.d.ts +2 -0
- package/dist/common/strings.d.ts.map +1 -0
- package/dist/common/strings.js +2 -0
- package/dist/common/strings.js.map +1 -0
- package/dist/common/tokens.d.ts +7 -0
- package/dist/common/tokens.d.ts.map +1 -0
- package/dist/common/tokens.js +8 -0
- package/dist/common/tokens.js.map +1 -0
- package/dist/constants.d.ts +2 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -1
- package/dist/constants.js.map +1 -1
- package/dist/dom-updater.d.ts +1 -1
- package/dist/dom-updater.d.ts.map +1 -1
- package/dist/dom-updater.js +73 -31
- package/dist/dom-updater.js.map +1 -1
- package/dist/elements/accented-dialog.d.ts +18 -10
- package/dist/elements/accented-dialog.d.ts.map +1 -1
- package/dist/elements/accented-dialog.js +116 -95
- package/dist/elements/accented-dialog.js.map +1 -1
- package/dist/elements/accented-trigger.d.ts +14 -9
- package/dist/elements/accented-trigger.d.ts.map +1 -1
- package/dist/elements/accented-trigger.js +83 -24
- package/dist/elements/accented-trigger.js.map +1 -1
- package/dist/fullscreen-listener.d.ts +2 -0
- package/dist/fullscreen-listener.d.ts.map +1 -0
- package/dist/fullscreen-listener.js +17 -0
- package/dist/fullscreen-listener.js.map +1 -0
- package/dist/intersection-observer.d.ts +1 -1
- package/dist/intersection-observer.d.ts.map +1 -1
- package/dist/intersection-observer.js +12 -6
- package/dist/intersection-observer.js.map +1 -1
- package/dist/log-and-rethrow.d.ts +1 -1
- package/dist/log-and-rethrow.d.ts.map +1 -1
- package/dist/log-and-rethrow.js +2 -3
- package/dist/log-and-rethrow.js.map +1 -1
- package/dist/logger.d.ts +4 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +6 -3
- package/dist/logger.js.map +1 -1
- package/dist/register-elements.d.ts +1 -1
- package/dist/register-elements.d.ts.map +1 -1
- package/dist/register-elements.js +6 -7
- package/dist/register-elements.js.map +1 -1
- package/dist/resize-listener.d.ts +1 -1
- package/dist/resize-listener.d.ts.map +1 -1
- package/dist/resize-listener.js +3 -4
- package/dist/resize-listener.js.map +1 -1
- package/dist/scanner.d.ts +2 -2
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +76 -43
- package/dist/scanner.js.map +1 -1
- package/dist/scroll-listeners.d.ts +1 -1
- package/dist/scroll-listeners.d.ts.map +1 -1
- package/dist/scroll-listeners.js +3 -4
- package/dist/scroll-listeners.js.map +1 -1
- package/dist/state.d.ts +3 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +5 -3
- package/dist/state.js.map +1 -1
- package/dist/task-queue.d.ts +4 -4
- package/dist/task-queue.d.ts.map +1 -1
- package/dist/task-queue.js +3 -2
- package/dist/task-queue.js.map +1 -1
- package/dist/types.d.ts +140 -49
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/are-elements-with-issues-equal.d.ts +3 -0
- package/dist/utils/are-elements-with-issues-equal.d.ts.map +1 -0
- package/dist/utils/are-elements-with-issues-equal.js +5 -0
- package/dist/utils/are-elements-with-issues-equal.js.map +1 -0
- package/dist/utils/are-issue-sets-equal.d.ts +2 -2
- package/dist/utils/are-issue-sets-equal.d.ts.map +1 -1
- package/dist/utils/are-issue-sets-equal.js +3 -3
- package/dist/utils/are-issue-sets-equal.js.map +1 -1
- package/dist/utils/containing-blocks.d.ts +3 -0
- package/dist/utils/containing-blocks.d.ts.map +1 -0
- package/dist/utils/containing-blocks.js +46 -0
- package/dist/utils/containing-blocks.js.map +1 -0
- package/dist/utils/contains.d.ts +2 -0
- package/dist/utils/contains.d.ts.map +1 -0
- package/dist/utils/contains.js +19 -0
- package/dist/utils/contains.js.map +1 -0
- package/dist/utils/deduplicate-nodes.d.ts +2 -0
- package/dist/utils/deduplicate-nodes.d.ts.map +1 -0
- package/dist/utils/deduplicate-nodes.js +4 -0
- package/dist/utils/deduplicate-nodes.js.map +1 -0
- package/dist/utils/deep-merge.d.ts +1 -1
- package/dist/utils/deep-merge.d.ts.map +1 -1
- package/dist/utils/deep-merge.js +8 -5
- package/dist/utils/deep-merge.js.map +1 -1
- package/dist/utils/dom-helpers.d.ts +9 -0
- package/dist/utils/dom-helpers.d.ts.map +1 -0
- package/dist/utils/dom-helpers.js +34 -0
- package/dist/utils/dom-helpers.js.map +1 -0
- package/dist/utils/ensure-non-empty.d.ts +2 -0
- package/dist/utils/ensure-non-empty.d.ts.map +1 -0
- package/dist/utils/ensure-non-empty.js +7 -0
- package/dist/utils/ensure-non-empty.js.map +1 -0
- package/dist/utils/get-element-html.d.ts +1 -1
- package/dist/utils/get-element-html.d.ts.map +1 -1
- package/dist/utils/get-element-html.js +4 -2
- package/dist/utils/get-element-html.js.map +1 -1
- package/dist/utils/get-element-position.d.ts +10 -2
- package/dist/utils/get-element-position.d.ts.map +1 -1
- package/dist/utils/get-element-position.js +64 -16
- package/dist/utils/get-element-position.js.map +1 -1
- package/dist/utils/get-parent.d.ts +2 -0
- package/dist/utils/get-parent.d.ts.map +1 -0
- package/dist/utils/get-parent.js +12 -0
- package/dist/utils/get-parent.js.map +1 -0
- package/dist/utils/get-scan-context.d.ts +3 -0
- package/dist/utils/get-scan-context.d.ts.map +1 -0
- package/dist/utils/get-scan-context.js +28 -0
- package/dist/utils/get-scan-context.js.map +1 -0
- package/dist/utils/get-scrollable-ancestors.d.ts +1 -1
- package/dist/utils/get-scrollable-ancestors.d.ts.map +1 -1
- package/dist/utils/get-scrollable-ancestors.js +10 -6
- package/dist/utils/get-scrollable-ancestors.js.map +1 -1
- package/dist/utils/is-node-in-scan-context.d.ts +3 -0
- package/dist/utils/is-node-in-scan-context.d.ts.map +1 -0
- package/dist/utils/is-node-in-scan-context.js +26 -0
- package/dist/utils/is-node-in-scan-context.js.map +1 -0
- package/dist/utils/is-non-empty.d.ts +2 -0
- package/dist/utils/is-non-empty.d.ts.map +1 -0
- package/dist/utils/is-non-empty.js +4 -0
- package/dist/utils/is-non-empty.js.map +1 -0
- package/dist/utils/normalize-context.d.ts +3 -0
- package/dist/utils/normalize-context.d.ts.map +1 -0
- package/dist/utils/normalize-context.js +59 -0
- package/dist/utils/normalize-context.js.map +1 -0
- package/dist/utils/recalculate-positions.d.ts +1 -1
- package/dist/utils/recalculate-positions.d.ts.map +1 -1
- package/dist/utils/recalculate-positions.js +5 -5
- package/dist/utils/recalculate-positions.js.map +1 -1
- package/dist/utils/recalculate-scrollable-ancestors.d.ts +1 -1
- package/dist/utils/recalculate-scrollable-ancestors.d.ts.map +1 -1
- package/dist/utils/recalculate-scrollable-ancestors.js +4 -4
- package/dist/utils/recalculate-scrollable-ancestors.js.map +1 -1
- package/dist/utils/shadow-dom-aware-mutation-observer.d.ts +10 -0
- package/dist/utils/shadow-dom-aware-mutation-observer.d.ts.map +1 -0
- package/dist/utils/shadow-dom-aware-mutation-observer.js +61 -0
- package/dist/utils/shadow-dom-aware-mutation-observer.js.map +1 -0
- package/dist/utils/supports-anchor-positioning.d.ts +1 -1
- package/dist/utils/supports-anchor-positioning.d.ts.map +1 -1
- package/dist/utils/supports-anchor-positioning.js +15 -2
- package/dist/utils/supports-anchor-positioning.js.map +1 -1
- package/dist/utils/transform-violations.d.ts +2 -2
- package/dist/utils/transform-violations.d.ts.map +1 -1
- package/dist/utils/transform-violations.js +25 -10
- package/dist/utils/transform-violations.js.map +1 -1
- package/dist/utils/update-elements-with-issues.d.ts +11 -5
- package/dist/utils/update-elements-with-issues.d.ts.map +1 -1
- package/dist/utils/update-elements-with-issues.js +56 -24
- package/dist/utils/update-elements-with-issues.js.map +1 -1
- package/dist/validate-options.d.ts +2 -2
- package/dist/validate-options.d.ts.map +1 -1
- package/dist/validate-options.js +91 -4
- package/dist/validate-options.js.map +1 -1
- package/package.json +16 -8
- package/src/accented.test.ts +2 -2
- package/src/accented.ts +45 -34
- package/src/common/strings.ts +2 -0
- package/src/common/tokens.ts +10 -0
- package/src/constants.ts +2 -1
- package/src/dom-updater.ts +87 -34
- package/src/elements/accented-dialog.ts +163 -123
- package/src/elements/accented-trigger.ts +128 -50
- package/src/fullscreen-listener.ts +21 -0
- package/src/intersection-observer.ts +27 -16
- package/src/log-and-rethrow.ts +2 -3
- package/src/logger.ts +14 -4
- package/src/register-elements.ts +7 -7
- package/src/resize-listener.ts +15 -11
- package/src/scanner.ts +113 -57
- package/src/scroll-listeners.ts +27 -19
- package/src/state.ts +27 -16
- package/src/task-queue.test.ts +5 -4
- package/src/task-queue.ts +8 -6
- package/src/types.ts +179 -76
- package/src/utils/are-elements-with-issues-equal.ts +11 -0
- package/src/utils/are-issue-sets-equal.test.ts +6 -7
- package/src/utils/are-issue-sets-equal.ts +8 -6
- package/src/utils/containing-blocks.ts +60 -0
- package/src/utils/contains.test.ts +54 -0
- package/src/utils/contains.ts +19 -0
- package/src/utils/deduplicate-nodes.ts +3 -0
- package/src/utils/deep-merge.test.ts +8 -1
- package/src/utils/deep-merge.ts +14 -8
- package/src/utils/dom-helpers.ts +42 -0
- package/src/utils/ensure-non-empty.ts +6 -0
- package/src/utils/get-element-html.ts +4 -2
- package/src/utils/get-element-position.ts +84 -16
- package/src/utils/get-parent.ts +14 -0
- package/src/utils/get-scan-context.test.ts +85 -0
- package/src/utils/get-scan-context.ts +36 -0
- package/src/utils/get-scrollable-ancestors.ts +15 -7
- package/src/utils/is-node-in-scan-context.test.ts +70 -0
- package/src/utils/is-node-in-scan-context.ts +29 -0
- package/src/utils/is-non-empty.ts +3 -0
- package/src/utils/normalize-context.test.ts +105 -0
- package/src/utils/normalize-context.ts +65 -0
- package/src/utils/recalculate-positions.ts +5 -5
- package/src/utils/recalculate-scrollable-ancestors.ts +4 -4
- package/src/utils/shadow-dom-aware-mutation-observer.ts +75 -0
- package/src/utils/supports-anchor-positioning.ts +19 -3
- package/src/utils/transform-violations.test.ts +29 -25
- package/src/utils/transform-violations.ts +32 -12
- package/src/utils/update-elements-with-issues.test.ts +145 -53
- package/src/utils/update-elements-with-issues.ts +123 -54
- package/src/validate-options.ts +154 -14
package/src/scanner.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
import axe from 'axe-core';
|
|
2
|
-
import
|
|
2
|
+
import { getAccentedElementNames, issuesUrl } from './constants.js';
|
|
3
|
+
import { logAndRethrow } from './log-and-rethrow.js';
|
|
3
4
|
import { elementsWithIssues, enabled, extendedElementsWithIssues } from './state.js';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
import { TaskQueue } from './task-queue.js';
|
|
6
|
+
import type { AxeOptions, Callback, Context, Throttle } from './types.ts';
|
|
7
|
+
import { getScanContext } from './utils/get-scan-context.js';
|
|
8
|
+
import { recalculatePositions } from './utils/recalculate-positions.js';
|
|
9
|
+
import { recalculateScrollableAncestors } from './utils/recalculate-scrollable-ancestors.js';
|
|
10
|
+
import { createShadowDOMAwareMutationObserver } from './utils/shadow-dom-aware-mutation-observer.js';
|
|
11
|
+
import { supportsAnchorPositioning } from './utils/supports-anchor-positioning.js';
|
|
12
|
+
import { updateElementsWithIssues } from './utils/update-elements-with-issues.js';
|
|
13
|
+
|
|
14
|
+
export function createScanner(
|
|
15
|
+
name: string,
|
|
16
|
+
context: Context,
|
|
17
|
+
axeOptions: AxeOptions,
|
|
18
|
+
throttle: Required<Throttle>,
|
|
19
|
+
callback: Callback,
|
|
20
|
+
) {
|
|
13
21
|
const axeRunningWindowProp = `__${name}_axe_running__`;
|
|
14
|
-
const win
|
|
15
|
-
const taskQueue = new TaskQueue<Node>(async () => {
|
|
22
|
+
const win = window as unknown as Record<string, boolean>;
|
|
23
|
+
const taskQueue = new TaskQueue<Node>(async (nodes) => {
|
|
16
24
|
// We may see errors coming from axe-core when Accented is toggled off and on in qiuck succession,
|
|
17
25
|
// which I've seen happen with hot reloading of a React application.
|
|
18
26
|
// This window property serves as a circuit breaker for that particular case.
|
|
@@ -21,50 +29,91 @@ export default function createScanner(name: string, axeContext: AxeContext, axeO
|
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
try {
|
|
24
|
-
|
|
25
|
-
performance.mark('axe-start');
|
|
32
|
+
performance.mark('scan-start');
|
|
26
33
|
|
|
27
34
|
win[axeRunningWindowProp] = true;
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
const scanContext = getScanContext(nodes, context);
|
|
37
|
+
|
|
38
|
+
let result: axe.AxeResults | undefined;
|
|
30
39
|
|
|
31
40
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
result = await axe.run(scanContext, {
|
|
42
|
+
/**
|
|
43
|
+
* By default, axe-core doesn't include element refs
|
|
44
|
+
* in the violations array,
|
|
45
|
+
* and we need those element refs.
|
|
46
|
+
*/
|
|
35
47
|
elementRef: true,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Although axe-core can perform iframe scanning, I haven't succeeded in it,
|
|
51
|
+
* and the docs suggest that the axe-core script should be explicitly included
|
|
52
|
+
* in each of the iframed documents anyway.
|
|
53
|
+
* It seems preferable to disallow iframe scanning and not report issues in elements within iframes
|
|
54
|
+
* in the case that such issues are for some reason reported by axe-core.
|
|
55
|
+
* A consumer of Accented can instead scan the iframed document by calling Accented initialization from that document.
|
|
56
|
+
*/
|
|
42
57
|
iframes: false,
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The `preload` docs are not clear to me,
|
|
61
|
+
* but when it's set to `true` by default,
|
|
62
|
+
* axe-core tries to fetch cross-origin CSS,
|
|
63
|
+
* which fails in the absence of CORS headers.
|
|
64
|
+
* I'm not sure why axe-core needs to preload
|
|
65
|
+
* those resources in the first place,
|
|
66
|
+
* so disabling it seems to be the safe option.
|
|
67
|
+
*/
|
|
68
|
+
preload: false,
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* We're only interested in violations,
|
|
72
|
+
* not in passes or incomplete results.
|
|
73
|
+
*/
|
|
43
74
|
resultTypes: ['violations'],
|
|
44
|
-
|
|
75
|
+
|
|
76
|
+
...axeOptions,
|
|
45
77
|
});
|
|
46
78
|
} catch (error) {
|
|
47
79
|
console.error(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
`If you still think it’s a bug in Accented, file an issue at ${issuesUrl}.\n`,
|
|
51
|
-
error
|
|
80
|
+
`Accented: axe-core (the accessibility testing engine) threw an error. Check the \`axeOptions\` property (https://accented.dev/api#axeoptions) that you’re passing to Accented. If you still think it’s a bug in Accented, file an issue at ${issuesUrl}.\n`,
|
|
81
|
+
error,
|
|
52
82
|
);
|
|
53
|
-
result = { violations: [] };
|
|
54
83
|
}
|
|
55
84
|
win[axeRunningWindowProp] = false;
|
|
56
85
|
|
|
57
|
-
const
|
|
86
|
+
const scanMeasure = performance.measure('scan', 'scan-start');
|
|
87
|
+
const scanDuration = Math.round(scanMeasure.duration);
|
|
58
88
|
|
|
59
|
-
if (!enabled.value) {
|
|
89
|
+
if (!enabled.value || !result) {
|
|
60
90
|
return;
|
|
61
91
|
}
|
|
62
92
|
|
|
63
|
-
|
|
93
|
+
performance.mark('dom-update-start');
|
|
94
|
+
|
|
95
|
+
updateElementsWithIssues({
|
|
96
|
+
extendedElementsWithIssues,
|
|
97
|
+
scanContext,
|
|
98
|
+
violations: result.violations,
|
|
99
|
+
win: window,
|
|
100
|
+
name,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const domUpdateMeasure = performance.measure('dom-update', 'dom-update-start');
|
|
104
|
+
const domUpdateDuration = Math.round(domUpdateMeasure.duration);
|
|
64
105
|
|
|
65
106
|
callback({
|
|
107
|
+
// Assuming that the {include, exclude} shape of the context object will be used less often
|
|
108
|
+
// than other variants, we'll output just the `include` array in case nothing is excluded
|
|
109
|
+
// in the scan.
|
|
110
|
+
scanContext: scanContext.exclude.length > 0 ? scanContext : scanContext.include,
|
|
66
111
|
elementsWithIssues: elementsWithIssues.value,
|
|
67
|
-
|
|
112
|
+
performance: {
|
|
113
|
+
totalBlockingTime: scanDuration + domUpdateDuration,
|
|
114
|
+
scan: scanDuration,
|
|
115
|
+
domUpdate: domUpdateDuration,
|
|
116
|
+
},
|
|
68
117
|
});
|
|
69
118
|
} catch (error) {
|
|
70
119
|
win[axeRunningWindowProp] = false;
|
|
@@ -72,21 +121,24 @@ export default function createScanner(name: string, axeContext: AxeContext, axeO
|
|
|
72
121
|
}
|
|
73
122
|
}, throttle);
|
|
74
123
|
|
|
75
|
-
// TODO (https://github.com/pomerantsev/accented/issues/102):
|
|
76
|
-
// limit to what's in axeContext,
|
|
77
|
-
// if that's an element or array of elements (not a selector).
|
|
78
124
|
taskQueue.add(document);
|
|
79
125
|
|
|
80
|
-
const accentedElementNames =
|
|
81
|
-
const mutationObserver =
|
|
126
|
+
const accentedElementNames = getAccentedElementNames(name);
|
|
127
|
+
const mutationObserver = createShadowDOMAwareMutationObserver(name, (mutationList) => {
|
|
82
128
|
try {
|
|
83
129
|
// We're not interested in mutations that are caused exclusively by the custom elements
|
|
84
130
|
// introduced by Accented.
|
|
85
|
-
const listWithoutAccentedElements = mutationList.filter(mutationRecord => {
|
|
86
|
-
const onlyAccentedElementsAddedOrRemoved =
|
|
87
|
-
|
|
88
|
-
[...mutationRecord.
|
|
89
|
-
|
|
131
|
+
const listWithoutAccentedElements = mutationList.filter((mutationRecord) => {
|
|
132
|
+
const onlyAccentedElementsAddedOrRemoved =
|
|
133
|
+
mutationRecord.type === 'childList' &&
|
|
134
|
+
[...mutationRecord.addedNodes].every((node) =>
|
|
135
|
+
accentedElementNames.includes(node.nodeName.toLowerCase()),
|
|
136
|
+
) &&
|
|
137
|
+
[...mutationRecord.removedNodes].every((node) =>
|
|
138
|
+
accentedElementNames.includes(node.nodeName.toLowerCase()),
|
|
139
|
+
);
|
|
140
|
+
const accentedElementChanged =
|
|
141
|
+
mutationRecord.type === 'attributes' &&
|
|
90
142
|
accentedElementNames.includes(mutationRecord.target.nodeName.toLowerCase());
|
|
91
143
|
return !(onlyAccentedElementsAddedOrRemoved || accentedElementChanged);
|
|
92
144
|
});
|
|
@@ -106,31 +158,35 @@ export default function createScanner(name: string, axeContext: AxeContext, axeO
|
|
|
106
158
|
// If we simply exclude all mutations where attributeName = `data-${name}`,
|
|
107
159
|
// we may miss other mutations on those same elements caused by Accented,
|
|
108
160
|
// leading to extra runs of the mutation observer.
|
|
109
|
-
const elementsWithAccentedAttributeChanges = listWithoutAccentedElements.reduce(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
161
|
+
const elementsWithAccentedAttributeChanges = listWithoutAccentedElements.reduce(
|
|
162
|
+
(nodes, mutationRecord) => {
|
|
163
|
+
if (
|
|
164
|
+
mutationRecord.type === 'attributes' &&
|
|
165
|
+
mutationRecord.attributeName === `data-${name}`
|
|
166
|
+
) {
|
|
167
|
+
nodes.add(mutationRecord.target);
|
|
168
|
+
}
|
|
169
|
+
return nodes;
|
|
170
|
+
},
|
|
171
|
+
new Set<Node>(),
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
const filteredMutationList = listWithoutAccentedElements.filter((mutationRecord) => {
|
|
117
175
|
return !elementsWithAccentedAttributeChanges.has(mutationRecord.target);
|
|
118
176
|
});
|
|
119
177
|
|
|
120
|
-
|
|
178
|
+
const nodes = filteredMutationList.map((mutationRecord) => mutationRecord.target);
|
|
179
|
+
taskQueue.addMultiple(nodes);
|
|
121
180
|
} catch (error) {
|
|
122
181
|
logAndRethrow(error);
|
|
123
182
|
}
|
|
124
183
|
});
|
|
125
184
|
|
|
126
|
-
// TODO (https://github.com/pomerantsev/accented/issues/102):
|
|
127
|
-
// possibly limit the observer to what's in axeContext,
|
|
128
|
-
// if that's an element or array of elements (not a selector).
|
|
129
185
|
mutationObserver.observe(document, {
|
|
130
186
|
subtree: true,
|
|
131
187
|
childList: true,
|
|
132
188
|
attributes: true,
|
|
133
|
-
characterData: true
|
|
189
|
+
characterData: true,
|
|
134
190
|
});
|
|
135
191
|
|
|
136
192
|
return () => {
|
package/src/scroll-listeners.ts
CHANGED
|
@@ -1,37 +1,45 @@
|
|
|
1
1
|
import { effect } from '@preact/signals-core';
|
|
2
|
-
import
|
|
2
|
+
import { logAndRethrow } from './log-and-rethrow.js';
|
|
3
3
|
import { scrollableAncestors } from './state.js';
|
|
4
|
-
import
|
|
4
|
+
import { recalculatePositions } from './utils/recalculate-positions.js';
|
|
5
5
|
|
|
6
|
-
export
|
|
6
|
+
export function setupScrollListeners() {
|
|
7
7
|
const documentAbortController = new AbortController();
|
|
8
|
-
document.addEventListener(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
document.addEventListener(
|
|
9
|
+
'scroll',
|
|
10
|
+
() => {
|
|
11
|
+
try {
|
|
12
|
+
recalculatePositions();
|
|
13
|
+
} catch (error) {
|
|
14
|
+
logAndRethrow(error);
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
{ signal: documentAbortController.signal },
|
|
18
|
+
);
|
|
15
19
|
|
|
16
20
|
const disposeOfEffect = effect(() => {
|
|
17
21
|
// TODO: optimize performance, issue #81
|
|
18
22
|
const elementAbortController = new AbortController();
|
|
19
23
|
for (const scrollableAncestor of scrollableAncestors.value) {
|
|
20
|
-
scrollableAncestor.addEventListener(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
scrollableAncestor.addEventListener(
|
|
25
|
+
'scroll',
|
|
26
|
+
() => {
|
|
27
|
+
try {
|
|
28
|
+
recalculatePositions();
|
|
29
|
+
} catch (error) {
|
|
30
|
+
logAndRethrow(error);
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
{ signal: elementAbortController.signal },
|
|
34
|
+
);
|
|
27
35
|
}
|
|
28
36
|
return () => {
|
|
29
37
|
elementAbortController.abort();
|
|
30
|
-
}
|
|
38
|
+
};
|
|
31
39
|
});
|
|
32
40
|
|
|
33
41
|
return () => {
|
|
34
42
|
documentAbortController.abort();
|
|
35
43
|
disposeOfEffect();
|
|
36
44
|
};
|
|
37
|
-
}
|
|
45
|
+
}
|
package/src/state.ts
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { computed, signal } from '@preact/signals-core';
|
|
2
2
|
|
|
3
|
-
import type { ElementWithIssues, ExtendedElementWithIssues } from './types';
|
|
3
|
+
import type { ElementWithIssues, ExtendedElementWithIssues } from './types.ts';
|
|
4
4
|
|
|
5
5
|
export const enabled = signal(false);
|
|
6
6
|
|
|
7
7
|
export const extendedElementsWithIssues = signal<Array<ExtendedElementWithIssues>>([]);
|
|
8
8
|
|
|
9
|
-
export const elementsWithIssues = computed<Array<ElementWithIssues>>(() =>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
export const elementsWithIssues = computed<Array<ElementWithIssues>>(() =>
|
|
10
|
+
extendedElementsWithIssues.value.map((extendedElementWithIssues) => ({
|
|
11
|
+
element: extendedElementWithIssues.element,
|
|
12
|
+
rootNode: extendedElementWithIssues.rootNode,
|
|
13
|
+
issues: extendedElementWithIssues.issues.value,
|
|
14
|
+
})),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export const rootNodes = computed<Set<Node>>(
|
|
18
|
+
() =>
|
|
19
|
+
new Set(
|
|
20
|
+
(enabled.value ? [document as Node] : []).concat(
|
|
21
|
+
...extendedElementsWithIssues.value.map(
|
|
22
|
+
(extendedElementWithIssues) => extendedElementWithIssues.rootNode,
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
13
27
|
|
|
14
|
-
export const scrollableAncestors = computed<Set<
|
|
15
|
-
extendedElementsWithIssues.value.reduce(
|
|
16
|
-
(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
},
|
|
22
|
-
new Set<HTMLElement>()
|
|
23
|
-
)
|
|
28
|
+
export const scrollableAncestors = computed<Set<Element>>(() =>
|
|
29
|
+
extendedElementsWithIssues.value.reduce((scrollableAncestors, extendedElementWithIssues) => {
|
|
30
|
+
for (const scrollableAncestor of extendedElementWithIssues.scrollableAncestors.value) {
|
|
31
|
+
scrollableAncestors.add(scrollableAncestor);
|
|
32
|
+
}
|
|
33
|
+
return scrollableAncestors;
|
|
34
|
+
}, new Set<Element>()),
|
|
24
35
|
);
|
package/src/task-queue.test.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import assert from 'node:assert/strict';
|
|
2
|
-
import {mock, suite, test} from 'node:test';
|
|
2
|
+
import { mock, suite, test } from 'node:test';
|
|
3
3
|
|
|
4
|
-
import TaskQueue from './task-queue
|
|
4
|
+
import { TaskQueue } from './task-queue';
|
|
5
5
|
|
|
6
|
-
const wait = (duration: number) => new Promise(resolve => setTimeout(resolve, duration));
|
|
6
|
+
const wait = (duration: number) => new Promise((resolve) => setTimeout(resolve, duration));
|
|
7
7
|
|
|
8
|
-
const createAsyncCallback = (duration: number) =>
|
|
8
|
+
const createAsyncCallback = (duration: number) =>
|
|
9
|
+
mock.fn(() => new Promise((resolve) => setTimeout(resolve, duration)));
|
|
9
10
|
|
|
10
11
|
suite('TaskQueue', () => {
|
|
11
12
|
test('callback is not called after a TaskQueue is created, even after a timeout', async () => {
|
package/src/task-queue.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type { Throttle } from './types';
|
|
1
|
+
import type { Throttle } from './types.ts';
|
|
2
2
|
|
|
3
|
-
type TaskCallback = () => void;
|
|
3
|
+
type TaskCallback<T> = (items: Array<T>) => void;
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export class TaskQueue<T> {
|
|
6
6
|
#throttle: Throttle;
|
|
7
|
-
#asyncCallback: TaskCallback | null = null;
|
|
7
|
+
#asyncCallback: TaskCallback<T> | null = null;
|
|
8
8
|
|
|
9
9
|
#items = new Set<T>();
|
|
10
10
|
#inRunLoop = false;
|
|
11
11
|
|
|
12
|
-
constructor(asyncCallback: TaskCallback
|
|
12
|
+
constructor(asyncCallback: TaskCallback<T>, throttle: Required<Throttle>) {
|
|
13
13
|
this.#asyncCallback = asyncCallback;
|
|
14
14
|
this.#throttle = throttle;
|
|
15
15
|
}
|
|
@@ -33,10 +33,12 @@ export default class TaskQueue<T> {
|
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
const items = Array.from(this.#items);
|
|
37
|
+
|
|
36
38
|
this.#items.clear();
|
|
37
39
|
|
|
38
40
|
if (this.#asyncCallback) {
|
|
39
|
-
await this.#asyncCallback();
|
|
41
|
+
await this.#asyncCallback(items);
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
await new Promise((resolve) => setTimeout(resolve, this.#throttle.wait));
|