@ecopages/dev-toolbar 0.2.0-rc.7 → 0.2.0-rc.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecopages/dev-toolbar",
3
- "version": "0.2.0-rc.7",
3
+ "version": "0.2.0-rc.9",
4
4
  "description": "Development toolbar for Ecopages",
5
5
  "keywords": [
6
6
  "ecopages",
@@ -45,47 +45,13 @@ function attachDomTarget(doc, element, targetSelector) {
45
45
  }
46
46
  return { targetSelector, domPath, matchIndex };
47
47
  }
48
- export function toA11yIssueView(issue) {
49
- const { element: _element, ...view } = issue;
50
- return view;
48
+ function isOutsideDevToolbar(element) {
49
+ return !element.closest('eco-dev-toolbar');
51
50
  }
52
- /**
53
- * Re-resolves the DOM node for a cached audit row.
54
- *
55
- * @remarks Prefers {@link A11yIssueView.domPath} because selectors can match multiple nodes (for example duplicate ids).
56
- */
57
- export function resolveA11yIssueElement(doc, issue) {
58
- if (issue.domPath.length > 0) {
59
- const fromPath = resolveDomPath(doc, issue.domPath);
60
- if (fromPath instanceof Element && !fromPath.closest('eco-dev-toolbar')) {
61
- return fromPath;
62
- }
63
- }
64
- const selectors = issue.targetSelector
65
- .split(',')
66
- .map((selector) => selector.trim())
67
- .filter((selector) => selector.length > 0);
68
- const candidates = [issue.targetSelector, ...selectors];
69
- for (const selector of candidates) {
70
- try {
71
- const matches = [...doc.querySelectorAll(selector)].filter((candidate) => !candidate.closest('eco-dev-toolbar'));
72
- if (matches.length === 0) {
73
- continue;
74
- }
75
- const matchIndex = issue.matchIndex ?? 0;
76
- return matches[matchIndex] ?? matches[0] ?? null;
77
- }
78
- catch {
79
- continue;
80
- }
81
- }
82
- return null;
83
- }
84
- export function runBuiltinChecks(doc) {
51
+ function checkMissingImageAlts(doc) {
85
52
  const issues = [];
86
- const ids = new Map();
87
53
  for (const image of doc.querySelectorAll('img')) {
88
- if (image.closest('eco-dev-toolbar')) {
54
+ if (!isOutsideDevToolbar(image)) {
89
55
  continue;
90
56
  }
91
57
  if (!image.getAttribute('alt')?.trim()) {
@@ -100,8 +66,12 @@ export function runBuiltinChecks(doc) {
100
66
  });
101
67
  }
102
68
  }
69
+ return issues;
70
+ }
71
+ function checkUnlabeledFormControls(doc) {
72
+ const issues = [];
103
73
  for (const input of doc.querySelectorAll('input, textarea, select')) {
104
- if (input.closest('eco-dev-toolbar')) {
74
+ if (!isOutsideDevToolbar(input)) {
105
75
  continue;
106
76
  }
107
77
  const type = input instanceof HTMLInputElement ? input.type : 'text';
@@ -121,9 +91,13 @@ export function runBuiltinChecks(doc) {
121
91
  });
122
92
  }
123
93
  }
94
+ return issues;
95
+ }
96
+ function checkHeadingOrder(doc) {
97
+ const issues = [];
124
98
  let previousHeading = 0;
125
99
  for (const heading of doc.querySelectorAll('h1, h2, h3, h4, h5, h6')) {
126
- if (heading.closest('eco-dev-toolbar')) {
100
+ if (!isOutsideDevToolbar(heading)) {
127
101
  continue;
128
102
  }
129
103
  const level = Number.parseInt(heading.tagName.slice(1), 10);
@@ -140,8 +114,13 @@ export function runBuiltinChecks(doc) {
140
114
  }
141
115
  previousHeading = level;
142
116
  }
117
+ return issues;
118
+ }
119
+ function checkDuplicateIds(doc) {
120
+ const issues = [];
121
+ const ids = new Map();
143
122
  for (const element of doc.querySelectorAll('[id]')) {
144
- if (element.closest('eco-dev-toolbar')) {
123
+ if (!isOutsideDevToolbar(element)) {
145
124
  continue;
146
125
  }
147
126
  const id = element.id;
@@ -166,6 +145,50 @@ export function runBuiltinChecks(doc) {
166
145
  }
167
146
  return issues;
168
147
  }
148
+ export function toA11yIssueView(issue) {
149
+ const { element: _element, ...view } = issue;
150
+ return view;
151
+ }
152
+ /**
153
+ * Re-resolves the DOM node for a cached audit row.
154
+ *
155
+ * @remarks Prefers {@link A11yIssueView.domPath} because selectors can match multiple nodes (for example duplicate ids).
156
+ */
157
+ export function resolveA11yIssueElement(doc, issue) {
158
+ if (issue.domPath.length > 0) {
159
+ const fromPath = resolveDomPath(doc, issue.domPath);
160
+ if (fromPath instanceof Element && !fromPath.closest('eco-dev-toolbar')) {
161
+ return fromPath;
162
+ }
163
+ }
164
+ const selectors = issue.targetSelector
165
+ .split(',')
166
+ .map((selector) => selector.trim())
167
+ .filter((selector) => selector.length > 0);
168
+ const candidates = [issue.targetSelector, ...selectors];
169
+ for (const selector of candidates) {
170
+ try {
171
+ const matches = [...doc.querySelectorAll(selector)].filter((candidate) => !candidate.closest('eco-dev-toolbar'));
172
+ if (matches.length === 0) {
173
+ continue;
174
+ }
175
+ const matchIndex = issue.matchIndex ?? 0;
176
+ return matches[matchIndex] ?? matches[0] ?? null;
177
+ }
178
+ catch {
179
+ continue;
180
+ }
181
+ }
182
+ return null;
183
+ }
184
+ export function runBuiltinChecks(doc) {
185
+ return [
186
+ ...checkMissingImageAlts(doc),
187
+ ...checkUnlabeledFormControls(doc),
188
+ ...checkHeadingOrder(doc),
189
+ ...checkDuplicateIds(doc),
190
+ ];
191
+ }
169
192
  function mapAxeImpact(impact) {
170
193
  if (impact === 'critical' || impact === 'serious') {
171
194
  return 'error';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,77 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { runBuiltinChecks } from './run-a11y-checks.js';
3
+ function createDocument(html) {
4
+ const template = document.createElement('template');
5
+ template.innerHTML = html.trim();
6
+ const doc = document.implementation.createHTMLDocument('a11y-test');
7
+ doc.body.replaceChildren(...Array.from(template.content.childNodes).map((node) => node.cloneNode(true)));
8
+ return doc;
9
+ }
10
+ function issueSnapshot(issues) {
11
+ return issues.map((issue) => ({
12
+ id: issue.id,
13
+ message: issue.message,
14
+ severity: issue.severity,
15
+ source: issue.source,
16
+ }));
17
+ }
18
+ describe('runBuiltinChecks', () => {
19
+ it('reports builtin issues in document order', () => {
20
+ const doc = createDocument(`
21
+ <img src="/hero.png" />
22
+ <input />
23
+ <h1>Title</h1>
24
+ <h3>Section</h3>
25
+ `);
26
+ const issues = runBuiltinChecks(doc);
27
+ expect(issueSnapshot(issues)).toEqual([
28
+ {
29
+ id: 'img-alt',
30
+ message: 'Image is missing alt text',
31
+ severity: 'error',
32
+ source: 'builtin',
33
+ },
34
+ {
35
+ id: 'form-label',
36
+ message: 'Form control is missing an accessible label',
37
+ severity: 'error',
38
+ source: 'builtin',
39
+ },
40
+ {
41
+ id: 'heading-order',
42
+ message: 'Heading level skips from h1 to h3',
43
+ severity: 'warning',
44
+ source: 'builtin',
45
+ },
46
+ ]);
47
+ });
48
+ it('ignores hidden inputs and honors aria labels', () => {
49
+ const doc = createDocument(`
50
+ <input type="hidden" name="token" value="secret" />
51
+ <input aria-label="Search" />
52
+ `);
53
+ const issues = runBuiltinChecks(doc);
54
+ expect(issues).toEqual([]);
55
+ });
56
+ it('reports duplicate ids', () => {
57
+ const doc = createDocument(`
58
+ <div id="panel"></div>
59
+ <section id="panel"></section>
60
+ `);
61
+ const issues = runBuiltinChecks(doc);
62
+ expect(issues).toHaveLength(1);
63
+ expect(issues[0]?.id).toBe('duplicate-id');
64
+ expect(issues[0]?.message).toBe('Duplicate id "panel"');
65
+ });
66
+ it('skips nodes inside eco-dev-toolbar', () => {
67
+ const doc = createDocument(`
68
+ <eco-dev-toolbar>
69
+ <img src="/logo.png" />
70
+ <input />
71
+ </eco-dev-toolbar>
72
+ <img src="/content.png" alt="Decorative" />
73
+ `);
74
+ const issues = runBuiltinChecks(doc);
75
+ expect(issues).toEqual([]);
76
+ });
77
+ });