@ngbracket/a11y-devtools 0.18.5 → 0.18.6

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/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to `@ngbracket/a11y-devtools` are documented here.
4
4
  This project adheres to [Semantic Versioning](https://semver.org/).
5
5
 
6
+ ## 0.18.6
7
+
8
+ ### Fixed
9
+
10
+ - A baseline no longer reports a known finding as new when the markup around
11
+ it was reordered. axe's selectors can contain positional parts such as
12
+ `li:nth-child(3)` and generated ids such as `#mat-input-3`. Adding an item
13
+ above the element, or another field earlier in the form, changed them, so
14
+ the finding failed to match its baseline entry and failed the CI gate.
15
+ Findings still unmatched after the exact comparison are now compared again
16
+ with those parts left out, for the same route, rule and owning component.
17
+ Only ids with at least two words before the number count as generated
18
+ (`#mat-input-3`, `#cdk-overlay-12`); `#h2`, `#col-12` and `#item-2024` are
19
+ compared as written, and so are ids with a number in the middle such as
20
+ `#mat-mdc-checkbox-2-input`. A positional part inside `:not()`, `:is()`,
21
+ `:where()` or `:has()` is kept. Duplicates
22
+ are still counted, so a component that gains a real extra instance of a
23
+ rule reports it as new. One case can't be told apart: if one instance is
24
+ fixed and another appears in the same component and rule in the same run,
25
+ and their selectors differ only in position or id counter, the count is
26
+ unchanged and neither is reported.
27
+
6
28
  ## 0.18.5
7
29
 
8
30
  ### Fixed
@@ -47,9 +47,11 @@ export declare function findingKey(page: string, finding: A11yFinding): string;
47
47
  export declare function findingsNotIn(findings: A11yFinding[], reference: A11yFinding[]): A11yFinding[];
48
48
  /**
49
49
  * Compare `current` with `baseline`. Matching is by {@link findingKey} with
50
- * counts, so two identical keys in the current run against one in the baseline
51
- * leaves one new. Pages are matched by label; a route the baseline never
52
- * scanned counts entirely as new (add it to the baseline when you add it to CI).
50
+ * counts, then by a looser key that ignores positional selector parts and id
51
+ * counters for whatever is still unpaired. Two identical keys in the current
52
+ * run against one in the baseline leaves one new. Pages are matched by label;
53
+ * a route the baseline never scanned counts entirely as new (add it to the
54
+ * baseline when you add it to CI).
53
55
  * Pages that failed to scan in the current run are skipped, so an outage
54
56
  * doesn't report everything on that page as fixed. Pages that failed in the
55
57
  * *baseline* are skipped too — there's nothing to compare against, so their
@@ -14,6 +14,57 @@ function normalizeTarget(target) {
14
14
  export function findingKey(page, finding) {
15
15
  return [page, finding.id, finding.component ?? '', normalizeTarget(finding.target)].join('\u0000');
16
16
  }
17
+ /**
18
+ * Positional parts of a selector: `:nth-child(3)`, `:nth-of-type(2)`,
19
+ * `:first-child`, `:last-of-type` and the like. They change when markup is
20
+ * reordered even though the element is the same.
21
+ */
22
+ const POSITIONAL = /:(?:nth-(?:last-)?(?:child|of-type)\([^)]*\)|(?:first|last|only)-(?:child|of-type))/g;
23
+ /** Selector functions whose argument is itself a selector. */
24
+ const SELECTOR_FUNCTION = /:(?:not|is|where|has)\(/g;
25
+ /**
26
+ * Remove {@link POSITIONAL} parts, except inside `:not()`, `:is()`, `:where()`
27
+ * and `:has()`: there a positional part is what tells `li:not(.x:first-child)`
28
+ * from `li:not(.x:last-child)`, so it stays.
29
+ */
30
+ function stripPositional(selector) {
31
+ let out = '';
32
+ let from = 0;
33
+ for (const match of selector.matchAll(SELECTOR_FUNCTION)) {
34
+ const open = match.index + match[0].length;
35
+ if (match.index < from)
36
+ continue; // nested inside a function already kept
37
+ let depth = 1;
38
+ let end = open;
39
+ while (end < selector.length && depth > 0) {
40
+ if (selector[end] === '(')
41
+ depth++;
42
+ else if (selector[end] === ')')
43
+ depth--;
44
+ end++;
45
+ }
46
+ out += selector.slice(from, match.index).replace(POSITIONAL, '') + selector.slice(match.index, end);
47
+ from = end;
48
+ }
49
+ return out + selector.slice(from).replace(POSITIONAL, '');
50
+ }
51
+ /**
52
+ * The counter at the end of a generated id: `#mat-input-3`, `#cdk-overlay-12`,
53
+ * `#pn_id_3`. Only ids with at least two words before the number count, so
54
+ * `#h2`, `#col-12` and `#item-2024`, where the number is part of the name,
55
+ * stay distinct.
56
+ */
57
+ const ID_COUNTER = /(#[A-Za-z][A-Za-z0-9]*[-_][\w-]*?[-_])\d+(?=[\s>+~.:#[]|$)/g;
58
+ /**
59
+ * A looser identity for the second matching pass: {@link findingKey} with the
60
+ * positional parts and id counters taken out of the selector. A markup
61
+ * reorder changes those, so without this pass a known finding came back as
62
+ * "new" and failed the CI gate.
63
+ */
64
+ function looseKey(page, finding) {
65
+ const target = stripPositional(normalizeTarget(finding.target)).replace(ID_COUNTER, '$1#');
66
+ return [page, finding.id, finding.component ?? '', target].join('\u0000');
67
+ }
17
68
  /**
18
69
  * The findings in `findings` that aren't also in `reference`, matched by
19
70
  * {@link findingKey} with counts. Used for a dark-mode pass, so it reports only
@@ -36,9 +87,11 @@ export function findingsNotIn(findings, reference) {
36
87
  }
37
88
  /**
38
89
  * Compare `current` with `baseline`. Matching is by {@link findingKey} with
39
- * counts, so two identical keys in the current run against one in the baseline
40
- * leaves one new. Pages are matched by label; a route the baseline never
41
- * scanned counts entirely as new (add it to the baseline when you add it to CI).
90
+ * counts, then by a looser key that ignores positional selector parts and id
91
+ * counters for whatever is still unpaired. Two identical keys in the current
92
+ * run against one in the baseline leaves one new. Pages are matched by label;
93
+ * a route the baseline never scanned counts entirely as new (add it to the
94
+ * baseline when you add it to CI).
42
95
  * Pages that failed to scan in the current run are skipped, so an outage
43
96
  * doesn't report everything on that page as fixed. Pages that failed in the
44
97
  * *baseline* are skipped too — there's nothing to compare against, so their
@@ -47,37 +100,53 @@ export function findingsNotIn(findings, reference) {
47
100
  export function diffAgainstBaseline(current, baseline) {
48
101
  const failed = new Set(current.pages.filter((p) => p.error).map((p) => p.label));
49
102
  const baselineFailed = new Set(baseline.pages.filter((p) => p.error).map((p) => p.label));
103
+ let before = baseline.pages
104
+ .filter((page) => !failed.has(page.label))
105
+ .flatMap((page) => page.findings.map((finding) => ({ page: page.label, finding })));
106
+ let after = current.pages
107
+ .filter((page) => !baselineFailed.has(page.label))
108
+ .flatMap((page) => page.findings.map((finding) => ({ page: page.label, finding })));
109
+ // Exact keys first, then the loose key for what's left, so a finding whose
110
+ // selector only moved (a reordered list, a shifted id counter) still pairs
111
+ // with its baseline entry. Both passes match with counts: a component that
112
+ // gains a real extra instance of a rule still reports it as added.
113
+ let unchanged = 0;
114
+ for (const key of [findingKey, looseKey]) {
115
+ const result = pair(after, before, key);
116
+ unchanged += result.matched;
117
+ after = result.unmatched;
118
+ before = result.leftover;
119
+ }
120
+ return { added: after, fixed: before, unchanged };
121
+ }
122
+ /**
123
+ * Pair each item of `current` with one of `baseline` that has the same key.
124
+ * Returns how many paired, the current items with no partner, and the baseline
125
+ * items nobody claimed.
126
+ */
127
+ function pair(current, baseline, key) {
50
128
  const remaining = new Map();
51
- for (const page of baseline.pages) {
52
- if (failed.has(page.label))
53
- continue;
54
- for (const finding of page.findings) {
55
- const key = findingKey(page.label, finding);
56
- const bucket = remaining.get(key);
57
- if (bucket)
58
- bucket.push({ page: page.label, finding });
59
- else
60
- remaining.set(key, [{ page: page.label, finding }]);
61
- }
129
+ for (const item of baseline) {
130
+ const k = key(item.page, item.finding);
131
+ const bucket = remaining.get(k);
132
+ if (bucket)
133
+ bucket.push(item);
134
+ else
135
+ remaining.set(k, [item]);
62
136
  }
63
- const added = [];
64
- let unchanged = 0;
65
- for (const page of current.pages) {
66
- if (baselineFailed.has(page.label))
67
- continue;
68
- for (const finding of page.findings) {
69
- const bucket = remaining.get(findingKey(page.label, finding));
70
- if (bucket && bucket.length > 0) {
71
- bucket.pop();
72
- unchanged++;
73
- }
74
- else {
75
- added.push({ page: page.label, finding });
76
- }
137
+ let matched = 0;
138
+ const unmatched = [];
139
+ for (const item of current) {
140
+ const bucket = remaining.get(key(item.page, item.finding));
141
+ if (bucket && bucket.length > 0) {
142
+ bucket.pop();
143
+ matched++;
144
+ }
145
+ else {
146
+ unmatched.push(item);
77
147
  }
78
148
  }
79
- const fixed = [...remaining.values()].flat();
80
- return { added, fixed, unchanged };
149
+ return { matched, unmatched, leftover: [...remaining.values()].flat() };
81
150
  }
82
151
  /**
83
152
  * Read a baseline from its JSON text (a previous `toJson` report). Throws a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngbracket/a11y-devtools",
3
- "version": "0.18.5",
3
+ "version": "0.18.6",
4
4
  "description": "Finds accessibility problems in Angular apps and names the component behind each one. A dev overlay, or headless reports for CI.",
5
5
  "license": "MIT",
6
6
  "author": "Duncan Faulkner",