prunelle 0.1.1 → 0.2.0
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/llms.txt +12 -12
- package/package.json +1 -1
- package/src/build/client-entries.ts +3 -2
- package/src/build/compile.ts +2 -1
- package/src/build/index.ts +68 -29
- package/src/build/manifest.ts +18 -1
- package/src/build/output.ts +2 -2
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +2 -2
- package/src/build/routes.ts +171 -10
- package/src/build/static.ts +71 -17
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +37 -4
- package/src/cli/commands.ts +5 -3
- package/src/cli/inspect.ts +21 -2
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +119 -220
- package/src/compiler/ssr.ts +121 -24
- package/src/dom/action.ts +4 -1
- package/src/dom/island.ts +5 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/types.ts +13 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action.ts +33 -10
- package/src/server/errors.ts +291 -62
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +84 -17
- package/src/server/index.ts +162 -91
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +40 -14
- package/src/server/public.ts +3 -3
- package/src/server/route-table.ts +5 -2
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +95 -57
- package/src/server/security.ts +171 -22
- package/src/server/serve-handlers.ts +21 -7
- package/src/ssr/context.ts +49 -0
- package/src/ssr/index.ts +120 -1
- package/src/test/browser.ts +6 -0
- package/src/test/index.ts +17 -3
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
// Static RGAA JSX accessibility checks: WAI-ARIA, headings, controls, tables, fieldsets, and focus.
|
|
2
|
+
|
|
3
|
+
import type { JSXAttribute, JSXAttributeItem, JSXChild, JSXElement, Node } from 'oxc-parser'
|
|
4
|
+
import type { CompilerWarning } from './mistakes'
|
|
5
|
+
import { cleanJsxText, isComponentTag, jsxTagName, staticLiteral, unwrapParens, unwrapTsExpr } from './shared'
|
|
6
|
+
|
|
7
|
+
export const RGAA_JSX_RULES = [
|
|
8
|
+
'rgaa/img-alt',
|
|
9
|
+
'rgaa/form-label',
|
|
10
|
+
'rgaa/accessible-name',
|
|
11
|
+
'rgaa/duplicate-id',
|
|
12
|
+
'rgaa/positive-tabindex',
|
|
13
|
+
'rgaa/nested-control',
|
|
14
|
+
'rgaa/link-as-button',
|
|
15
|
+
'rgaa/unknown-role',
|
|
16
|
+
'rgaa/unknown-aria',
|
|
17
|
+
'rgaa/invalid-aria-value',
|
|
18
|
+
'rgaa/skipped-heading',
|
|
19
|
+
'rgaa/table-without-header',
|
|
20
|
+
'rgaa/fieldset-without-legend',
|
|
21
|
+
'rgaa/click-without-key',
|
|
22
|
+
'rgaa/autofocus',
|
|
23
|
+
'rgaa/meta-refresh',
|
|
24
|
+
] as const
|
|
25
|
+
|
|
26
|
+
export type RgaaJsxRule = (typeof RGAA_JSX_RULES)[number]
|
|
27
|
+
|
|
28
|
+
const NON_LABELLED_INPUT_TYPES = new Set(['hidden', 'submit', 'reset', 'button', 'image'])
|
|
29
|
+
export const INTERACTIVE_ANCESTORS = new Set(['a', 'button', 'summary'])
|
|
30
|
+
const INTERACTIVE_DESCENDANTS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary'])
|
|
31
|
+
const NATIVE_CLICKABLE_TAGS = new Set(['a', 'button', 'input', 'select', 'textarea', 'summary', 'label', 'option', 'dialog', 'area'])
|
|
32
|
+
const TABLE_STRUCTURAL_TAGS = new Set(['thead', 'tbody', 'tfoot', 'tr'])
|
|
33
|
+
const HEADING_LEVELS: Record<string, number> = { h1: 1, h2: 2, h3: 3, h4: 4, h5: 5, h6: 6 }
|
|
34
|
+
const PRESENTATIONAL_ROLES = new Set(['presentation', 'none'])
|
|
35
|
+
const INT_RE = /^-?\d+$/
|
|
36
|
+
const NUM_RE = /^-?(?:\d+\.?\d*|\.\d+)$/
|
|
37
|
+
const TABINDEX_NUM_RE = /^[+-]?\d+(?:\.\d+)?$/
|
|
38
|
+
|
|
39
|
+
// Concrete WAI-ARIA 1.2 & 1.3 roles (abstract roles excluded).
|
|
40
|
+
const VALID_ARIA_ROLES = new Set((
|
|
41
|
+
'alert alertdialog application article associationlist associationlistitemkey associationlistitemvalue '
|
|
42
|
+
+ 'banner blockquote button caption cell checkbox code columnheader combobox comment complementary contentinfo '
|
|
43
|
+
+ 'definition deletion dialog directory document emphasis feed figure form generic '
|
|
44
|
+
+ 'graphics-document graphics-object graphics-symbol grid gridcell group heading image img insertion '
|
|
45
|
+
+ 'link list listbox listitem log main mark marquee math menu menubar menuitem menuitemcheckbox menuitemradio '
|
|
46
|
+
+ 'meter navigation none note option paragraph presentation progressbar radio radiogroup region row rowgroup rowheader '
|
|
47
|
+
+ 'scrollbar search searchbox separator slider spinbutton status strong subscript suggestion superscript switch '
|
|
48
|
+
+ 'tab table tablist tabpanel term textbox time timer toolbar tooltip tree treegrid treeitem'
|
|
49
|
+
).split(' '))
|
|
50
|
+
|
|
51
|
+
// Valid WAI-ARIA 1.1 / 1.2 / 1.3 attributes.
|
|
52
|
+
const VALID_ARIA_ATTRS = new Set((
|
|
53
|
+
'aria-activedescendant aria-atomic aria-autocomplete aria-braillelabel aria-brailleroledescription '
|
|
54
|
+
+ 'aria-busy aria-checked aria-colcount aria-colindex aria-colindextext aria-colspan aria-controls '
|
|
55
|
+
+ 'aria-current aria-describedby aria-description aria-details aria-disabled aria-dropeffect '
|
|
56
|
+
+ 'aria-errormessage aria-expanded aria-flowto aria-grabbed aria-haspopup aria-hidden aria-invalid '
|
|
57
|
+
+ 'aria-keyshortcuts aria-label aria-labelledby aria-level aria-live aria-modal aria-multiline '
|
|
58
|
+
+ 'aria-multiselectable aria-orientation aria-owns aria-placeholder aria-posinset aria-pressed '
|
|
59
|
+
+ 'aria-readonly aria-relevant aria-required aria-roledescription aria-rowcount aria-rowindex '
|
|
60
|
+
+ 'aria-rowindextext aria-rowspan aria-selected aria-setsize aria-sort aria-valuemax aria-valuemin '
|
|
61
|
+
+ 'aria-valuenow aria-valuetext'
|
|
62
|
+
).split(' '))
|
|
63
|
+
|
|
64
|
+
const ARIA_BOOL_ATTRS = new Set((
|
|
65
|
+
'aria-atomic aria-busy aria-disabled aria-expanded aria-grabbed aria-hidden '
|
|
66
|
+
+ 'aria-modal aria-multiline aria-multiselectable aria-readonly aria-required aria-selected'
|
|
67
|
+
).split(' '))
|
|
68
|
+
const ARIA_TRISTATE_ATTRS = new Set(['aria-checked', 'aria-pressed'])
|
|
69
|
+
const ARIA_INT_ATTRS = new Set((
|
|
70
|
+
'aria-level aria-posinset aria-setsize aria-colcount aria-rowcount '
|
|
71
|
+
+ 'aria-colindex aria-rowindex aria-colspan aria-rowspan'
|
|
72
|
+
).split(' '))
|
|
73
|
+
const ARIA_NUM_ATTRS = new Set(['aria-valuemin', 'aria-valuemax', 'aria-valuenow'])
|
|
74
|
+
const ARIA_RELEVANT_TOKENS = new Set(['additions', 'removals', 'text', 'all'])
|
|
75
|
+
const ARIA_ENUM_ATTRS: Record<string, ReadonlySet<string>> = {
|
|
76
|
+
'aria-live': new Set(['off', 'polite', 'assertive']),
|
|
77
|
+
'aria-orientation': new Set(['horizontal', 'vertical']),
|
|
78
|
+
'aria-sort': new Set(['none', 'ascending', 'descending', 'other']),
|
|
79
|
+
'aria-current': new Set(['page', 'step', 'location', 'date', 'time', 'true', 'false']),
|
|
80
|
+
'aria-haspopup': new Set(['false', 'true', 'menu', 'listbox', 'tree', 'grid', 'dialog']),
|
|
81
|
+
'aria-invalid': new Set(['grammar', 'false', 'spelling', 'true']),
|
|
82
|
+
'aria-autocomplete': new Set(['inline', 'list', 'both', 'none']),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface RgaaScope {
|
|
86
|
+
seenIds: Set<string>
|
|
87
|
+
lastHeading: number
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface RgaaElementContext {
|
|
91
|
+
filename: string
|
|
92
|
+
inLabel: boolean
|
|
93
|
+
interactiveAncestor: string | undefined
|
|
94
|
+
labelTargets: ReadonlySet<string>
|
|
95
|
+
scope: RgaaScope
|
|
96
|
+
isIgnored: (rule: RgaaJsxRule, elementStart: number, attrStart?: number) => boolean
|
|
97
|
+
warnings: CompilerWarning[]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function createRgaaScope(): RgaaScope {
|
|
101
|
+
return { seenIds: new Set(), lastHeading: 0 }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function cloneRgaaScope(scope: RgaaScope): RgaaScope {
|
|
105
|
+
return { seenIds: new Set(scope.seenIds), lastHeading: scope.lastHeading }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function mergeRgaaScope(target: RgaaScope, branch: RgaaScope): void {
|
|
109
|
+
for (const id of branch.seenIds)
|
|
110
|
+
target.seenIds.add(id)
|
|
111
|
+
if (branch.lastHeading > target.lastHeading)
|
|
112
|
+
target.lastHeading = branch.lastHeading
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function findAttr(attributes: readonly JSXAttributeItem[], name: string): JSXAttribute | undefined {
|
|
116
|
+
for (const attr of attributes) {
|
|
117
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name === name)
|
|
118
|
+
return attr
|
|
119
|
+
}
|
|
120
|
+
return undefined
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function findAttrLower(attributes: readonly JSXAttributeItem[], lowerName: string): JSXAttribute | undefined {
|
|
124
|
+
for (const attr of attributes) {
|
|
125
|
+
if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name.toLowerCase() === lowerName)
|
|
126
|
+
return attr
|
|
127
|
+
}
|
|
128
|
+
return undefined
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function hasSpread(attributes: readonly JSXAttributeItem[]): boolean {
|
|
132
|
+
return attributes.some(a => a.type === 'JSXSpreadAttribute')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function staticStringValueOf(value: JSXAttribute['value']): string | undefined {
|
|
136
|
+
if (value === null)
|
|
137
|
+
return undefined
|
|
138
|
+
if (value.type === 'Literal' && typeof value.value === 'string')
|
|
139
|
+
return value.value
|
|
140
|
+
if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
|
|
141
|
+
const expr = unwrapTsExpr(value.expression)
|
|
142
|
+
if (expr.type === 'Literal' && typeof expr.value === 'string')
|
|
143
|
+
return expr.value
|
|
144
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
|
|
145
|
+
return expr.quasis[0]?.value.cooked ?? undefined
|
|
146
|
+
}
|
|
147
|
+
return undefined
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function attrTargetKey(attr: JSXAttribute | undefined): string | undefined {
|
|
151
|
+
if (attr === undefined || attr.value === null)
|
|
152
|
+
return undefined
|
|
153
|
+
const str = staticStringValueOf(attr.value)
|
|
154
|
+
if (str !== undefined && str.trim() !== '')
|
|
155
|
+
return `s:${str}`
|
|
156
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
157
|
+
const expr = unwrapTsExpr(attr.value.expression)
|
|
158
|
+
if (expr.type === 'Identifier')
|
|
159
|
+
return `i:${expr.name}`
|
|
160
|
+
}
|
|
161
|
+
return undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function hasNonEmptyAttr(attributes: readonly JSXAttributeItem[], name: string): boolean {
|
|
165
|
+
const attr = findAttr(attributes, name)
|
|
166
|
+
if (attr === undefined || attr.value === null)
|
|
167
|
+
return false
|
|
168
|
+
const str = staticStringValueOf(attr.value)
|
|
169
|
+
if (str !== undefined)
|
|
170
|
+
return str.trim() !== ''
|
|
171
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
172
|
+
const expr = unwrapParens(attr.value.expression)
|
|
173
|
+
if (expr.type === 'Literal' && (expr.value === null || expr.value === false || expr.value === ''))
|
|
174
|
+
return false
|
|
175
|
+
if (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
176
|
+
return false
|
|
177
|
+
return true
|
|
178
|
+
}
|
|
179
|
+
return false
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isExplicitlyFalsyExpr(value: JSXAttribute['value']): boolean {
|
|
183
|
+
if (value === null || value.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression')
|
|
184
|
+
return false
|
|
185
|
+
const expr = unwrapTsExpr(value.expression)
|
|
186
|
+
if (expr.type === 'Literal' && (expr.value === false || expr.value === null))
|
|
187
|
+
return true
|
|
188
|
+
return expr.type === 'Identifier' && expr.name === 'undefined'
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function hasLabelAttr(attributes: readonly JSXAttributeItem[]): boolean {
|
|
192
|
+
return hasNonEmptyAttr(attributes, 'aria-label')
|
|
193
|
+
|| hasNonEmptyAttr(attributes, 'aria-labelledby')
|
|
194
|
+
|| hasNonEmptyAttr(attributes, 'title')
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function isAriaHidden(attributes: readonly JSXAttributeItem[]): boolean {
|
|
198
|
+
const attr = findAttr(attributes, 'aria-hidden')
|
|
199
|
+
if (attr === undefined)
|
|
200
|
+
return false
|
|
201
|
+
if (attr.value === null)
|
|
202
|
+
return true
|
|
203
|
+
if (attr.value.type === 'Literal')
|
|
204
|
+
return attr.value.value === 'true'
|
|
205
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
206
|
+
const expr = unwrapParens(attr.value.expression)
|
|
207
|
+
return expr.type === 'Literal' && (expr.value === true || expr.value === 'true')
|
|
208
|
+
}
|
|
209
|
+
return false
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isPresentational(attributes: readonly JSXAttributeItem[]): boolean {
|
|
213
|
+
const raw = staticStringValueOf(findAttr(attributes, 'role')?.value ?? null)
|
|
214
|
+
return raw !== undefined && raw.trim().split(/\s+/).some(token => PRESENTATIONAL_ROLES.has(token))
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function hasMeaningfulExpression(expr: Node): boolean {
|
|
218
|
+
const inner = unwrapParens(expr)
|
|
219
|
+
if (inner.type === 'Literal')
|
|
220
|
+
return typeof inner.value === 'string' ? inner.value.trim() !== '' : typeof inner.value === 'number'
|
|
221
|
+
return !(inner.type === 'Identifier' && inner.name === 'undefined')
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function hasAccessibleName(children: readonly JSXChild[]): boolean {
|
|
225
|
+
for (const child of children) {
|
|
226
|
+
if (child.type === 'JSXText') {
|
|
227
|
+
if (cleanJsxText(child.value).trim() !== '')
|
|
228
|
+
return true
|
|
229
|
+
}
|
|
230
|
+
else if (child.type === 'JSXExpressionContainer') {
|
|
231
|
+
if (child.expression.type !== 'JSXEmptyExpression' && hasMeaningfulExpression(child.expression))
|
|
232
|
+
return true
|
|
233
|
+
}
|
|
234
|
+
else if (child.type === 'JSXFragment') {
|
|
235
|
+
if (hasAccessibleName(child.children))
|
|
236
|
+
return true
|
|
237
|
+
}
|
|
238
|
+
else if (child.type === 'JSXElement') {
|
|
239
|
+
const attrs = child.openingElement.attributes
|
|
240
|
+
if (isAriaHidden(attrs))
|
|
241
|
+
continue
|
|
242
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
243
|
+
if (hasSpread(attrs) || hasLabelAttr(attrs))
|
|
244
|
+
return true
|
|
245
|
+
if (tag === 'img') {
|
|
246
|
+
if (hasNonEmptyAttr(attrs, 'alt'))
|
|
247
|
+
return true
|
|
248
|
+
continue
|
|
249
|
+
}
|
|
250
|
+
if (tag === 'svg') {
|
|
251
|
+
if (child.children.some(c => c.type === 'JSXElement' && jsxTagName(c.openingElement.name) === 'title' && hasAccessibleName(c.children)))
|
|
252
|
+
return true
|
|
253
|
+
continue
|
|
254
|
+
}
|
|
255
|
+
if (isComponentTag(tag)) {
|
|
256
|
+
if (hasNonEmptyAttr(attrs, 'label') || hasAccessibleName(child.children))
|
|
257
|
+
return true
|
|
258
|
+
continue
|
|
259
|
+
}
|
|
260
|
+
if (hasAccessibleName(child.children))
|
|
261
|
+
return true
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return false
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
type StaticAttrVal =
|
|
268
|
+
| { kind: 'bool', value: boolean }
|
|
269
|
+
| { kind: 'number', value: number }
|
|
270
|
+
| { kind: 'string', value: string }
|
|
271
|
+
|
|
272
|
+
function extractStaticAttrValue(attr: JSXAttribute): StaticAttrVal | undefined {
|
|
273
|
+
if (attr.value === null)
|
|
274
|
+
return { kind: 'bool', value: true }
|
|
275
|
+
if (attr.value.type === 'Literal' && typeof attr.value.value === 'string')
|
|
276
|
+
return { kind: 'string', value: attr.value.value }
|
|
277
|
+
if (attr.value.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
|
|
278
|
+
return undefined
|
|
279
|
+
const expr = unwrapTsExpr(attr.value.expression)
|
|
280
|
+
if (expr.type === 'Identifier' && expr.name === 'undefined')
|
|
281
|
+
return undefined
|
|
282
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
|
|
283
|
+
return { kind: 'string', value: expr.quasis[0]?.value.cooked ?? '' }
|
|
284
|
+
if (expr.type === 'UnaryExpression' && expr.operator === '+' && expr.argument.type === 'Literal' && typeof expr.argument.value === 'number')
|
|
285
|
+
return { kind: 'number', value: expr.argument.value }
|
|
286
|
+
const lit = staticLiteral(expr)
|
|
287
|
+
if (!lit.ok || lit.value === null)
|
|
288
|
+
return undefined
|
|
289
|
+
if (typeof lit.value === 'boolean')
|
|
290
|
+
return { kind: 'bool', value: lit.value }
|
|
291
|
+
if (typeof lit.value === 'number')
|
|
292
|
+
return { kind: 'number', value: lit.value }
|
|
293
|
+
return { kind: 'string', value: lit.value }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function validateAriaValue(name: string, val: StaticAttrVal): string | undefined {
|
|
297
|
+
if (ARIA_BOOL_ATTRS.has(name))
|
|
298
|
+
return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false')) ? undefined : 'expected "true" or "false"'
|
|
299
|
+
if (ARIA_TRISTATE_ATTRS.has(name))
|
|
300
|
+
return val.kind === 'bool' || (val.kind === 'string' && (val.value === 'true' || val.value === 'false' || val.value === 'mixed')) ? undefined : 'expected "true", "false", or "mixed"'
|
|
301
|
+
const enumSet = ARIA_ENUM_ATTRS[name]
|
|
302
|
+
if (enumSet !== undefined) {
|
|
303
|
+
const token = val.kind === 'bool' ? String(val.value) : val.kind === 'string' ? val.value : undefined
|
|
304
|
+
return token !== undefined && enumSet.has(token) ? undefined : `expected one of ${[...enumSet].join(', ')}`
|
|
305
|
+
}
|
|
306
|
+
if (name === 'aria-relevant') {
|
|
307
|
+
const tokens = val.kind === 'string' ? val.value.trim().split(/\s+/).filter(Boolean) : []
|
|
308
|
+
return tokens.length > 0 && tokens.every(t => ARIA_RELEVANT_TOKENS.has(t)) ? undefined : 'expected space-separated tokens from additions, removals, text, all'
|
|
309
|
+
}
|
|
310
|
+
if (ARIA_INT_ATTRS.has(name))
|
|
311
|
+
return (val.kind === 'number' && Number.isInteger(val.value)) || (val.kind === 'string' && INT_RE.test(val.value.trim())) ? undefined : 'expected an integer'
|
|
312
|
+
if (ARIA_NUM_ATTRS.has(name))
|
|
313
|
+
return (val.kind === 'number' && Number.isFinite(val.value)) || (val.kind === 'string' && NUM_RE.test(val.value.trim())) ? undefined : 'expected a number'
|
|
314
|
+
return undefined
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function emitWarning(ctx: RgaaElementContext, rule: RgaaJsxRule, elementStart: number, message: string, attrStart?: number): void {
|
|
318
|
+
if (!ctx.isIgnored(rule, elementStart, attrStart))
|
|
319
|
+
ctx.warnings.push({ rule, start: attrStart ?? elementStart, message })
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function checkRoleAndAria(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
323
|
+
const attrs = element.openingElement.attributes
|
|
324
|
+
const roleAttr = findAttr(attrs, 'role')
|
|
325
|
+
if (roleAttr !== undefined) {
|
|
326
|
+
const raw = staticStringValueOf(roleAttr.value)
|
|
327
|
+
if (raw !== undefined) {
|
|
328
|
+
const tokens = raw.trim().split(/\s+/).filter(Boolean)
|
|
329
|
+
const invalid = tokens.filter(t => !VALID_ARIA_ROLES.has(t))
|
|
330
|
+
if (tokens.length === 0 || invalid.length > 0) {
|
|
331
|
+
const detail = tokens.length === 0 ? 'an empty role' : `unknown WAI-ARIA role "${invalid[0]}"`
|
|
332
|
+
emitWarning(ctx, 'rgaa/unknown-role', element.start, `[rgaa/unknown-role] <${tag} role="${raw}"> in ${ctx.filename} uses ${detail}: use a valid WAI-ARIA role or remove the attribute.`, roleAttr.start)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
for (const attr of attrs) {
|
|
337
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier' || !attr.name.name.startsWith('aria-'))
|
|
338
|
+
continue
|
|
339
|
+
const name = attr.name.name
|
|
340
|
+
if (!VALID_ARIA_ATTRS.has(name)) {
|
|
341
|
+
emitWarning(ctx, 'rgaa/unknown-aria', element.start, `[rgaa/unknown-aria] <${tag} ${name}> in ${ctx.filename} is not a valid WAI-ARIA attribute: check the spelling or remove it.`, attr.start)
|
|
342
|
+
continue
|
|
343
|
+
}
|
|
344
|
+
const val = extractStaticAttrValue(attr)
|
|
345
|
+
if (val === undefined)
|
|
346
|
+
continue
|
|
347
|
+
const err = validateAriaValue(name, val)
|
|
348
|
+
if (err !== undefined) {
|
|
349
|
+
const shown = val.kind === 'string' ? `"${val.value}"` : String(val.value)
|
|
350
|
+
emitWarning(ctx, 'rgaa/invalid-aria-value', element.start, `[rgaa/invalid-aria-value] <${tag} ${name}=${shown}> in ${ctx.filename} has an invalid value (${err}).`, attr.start)
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function checkExprForChild(expr: Node, checkElement: (el: JSXElement) => boolean, checkFragment: (children: readonly JSXChild[]) => boolean): boolean {
|
|
356
|
+
const inner = unwrapTsExpr(expr)
|
|
357
|
+
if (inner.type === 'Literal' || (inner.type === 'Identifier' && inner.name === 'undefined'))
|
|
358
|
+
return false
|
|
359
|
+
if (inner.type === 'JSXElement')
|
|
360
|
+
return checkElement(inner)
|
|
361
|
+
if (inner.type === 'JSXFragment')
|
|
362
|
+
return checkFragment(inner.children)
|
|
363
|
+
if (inner.type === 'LogicalExpression') {
|
|
364
|
+
return inner.operator === '&&'
|
|
365
|
+
? checkExprForChild(inner.right, checkElement, checkFragment)
|
|
366
|
+
: checkExprForChild(inner.left, checkElement, checkFragment) || checkExprForChild(inner.right, checkElement, checkFragment)
|
|
367
|
+
}
|
|
368
|
+
if (inner.type === 'ConditionalExpression')
|
|
369
|
+
return checkExprForChild(inner.consequent, checkElement, checkFragment) || checkExprForChild(inner.alternate, checkElement, checkFragment)
|
|
370
|
+
return true
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function tableMayHaveHeader(children: readonly JSXChild[]): boolean {
|
|
374
|
+
const checkEl = (el: JSXElement): boolean => {
|
|
375
|
+
const tag = jsxTagName(el.openingElement.name)
|
|
376
|
+
if (tag === 'th' || isComponentTag(tag))
|
|
377
|
+
return true
|
|
378
|
+
return TABLE_STRUCTURAL_TAGS.has(tag) && tableMayHaveHeader(el.children)
|
|
379
|
+
}
|
|
380
|
+
for (const child of children) {
|
|
381
|
+
if (child.type === 'JSXElement' && checkEl(child))
|
|
382
|
+
return true
|
|
383
|
+
if (child.type === 'JSXFragment' && tableMayHaveHeader(child.children))
|
|
384
|
+
return true
|
|
385
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, tableMayHaveHeader))
|
|
386
|
+
return true
|
|
387
|
+
}
|
|
388
|
+
return false
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function fieldsetMayHaveLegend(children: readonly JSXChild[], isDirect: boolean): boolean {
|
|
392
|
+
const checkEl = (el: JSXElement): boolean => {
|
|
393
|
+
const tag = jsxTagName(el.openingElement.name)
|
|
394
|
+
if (tag === 'legend')
|
|
395
|
+
return true
|
|
396
|
+
if (tag === 'fieldset')
|
|
397
|
+
return false
|
|
398
|
+
return (isDirect && isComponentTag(tag)) || fieldsetMayHaveLegend(el.children, false)
|
|
399
|
+
}
|
|
400
|
+
for (const child of children) {
|
|
401
|
+
if (child.type === 'JSXElement' && checkEl(child))
|
|
402
|
+
return true
|
|
403
|
+
if (child.type === 'JSXFragment' && fieldsetMayHaveLegend(child.children, isDirect))
|
|
404
|
+
return true
|
|
405
|
+
if (isDirect && child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression' && checkExprForChild(child.expression, checkEl, cs => fieldsetMayHaveLegend(cs, true)))
|
|
406
|
+
return true
|
|
407
|
+
}
|
|
408
|
+
return false
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function checkFocusAndInteraction(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
|
|
412
|
+
const attrs = element.openingElement.attributes
|
|
413
|
+
|
|
414
|
+
const tabAttr = findAttrLower(attrs, 'tabindex')
|
|
415
|
+
if (tabAttr !== undefined) {
|
|
416
|
+
const val = extractStaticAttrValue(tabAttr)
|
|
417
|
+
const num = val?.kind === 'number' ? val.value : val?.kind === 'string' && TABINDEX_NUM_RE.test(val.value.trim()) ? Number(val.value.trim()) : undefined
|
|
418
|
+
if (num !== undefined && Number.isFinite(num) && num > 0) {
|
|
419
|
+
const attrName = tabAttr.name.type === 'JSXIdentifier' ? tabAttr.name.name : 'tabindex'
|
|
420
|
+
emitWarning(ctx, 'rgaa/positive-tabindex', element.start, `[rgaa/positive-tabindex] <${tag} ${attrName}={${num}}> in ${ctx.filename} uses a positive tabindex: use 0, -1, or reorder the DOM to preserve natural keyboard order.`, tabAttr.start)
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const autoFocusAttr = findAttrLower(attrs, 'autofocus')
|
|
425
|
+
if (autoFocusAttr !== undefined && !isExplicitlyFalsyExpr(autoFocusAttr.value)) {
|
|
426
|
+
const attrName = autoFocusAttr.name.type === 'JSXIdentifier' ? autoFocusAttr.name.name : 'autofocus'
|
|
427
|
+
emitWarning(ctx, 'rgaa/autofocus', element.start, `[rgaa/autofocus] <${tag} ${attrName}> in ${ctx.filename} steals focus on load and can disorient keyboard and screen-reader users: remove ${attrName} or manage focus explicitly.`, autoFocusAttr.start)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (ctx.interactiveAncestor !== undefined && INTERACTIVE_DESCENDANTS.has(tag)) {
|
|
431
|
+
const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
|
|
432
|
+
if (inputType !== 'hidden')
|
|
433
|
+
emitWarning(ctx, 'rgaa/nested-control', element.start, `[rgaa/nested-control] <${tag}> in ${ctx.filename} is nested inside <${ctx.interactiveAncestor}>: interactive controls must not be nested; place them as siblings.`)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const onClickAttr = findAttr(attrs, 'onClick')
|
|
437
|
+
const hasClick = onClickAttr !== undefined && !isExplicitlyFalsyExpr(onClickAttr.value)
|
|
438
|
+
if (tag === 'a' && !spread && hasClick && !hasNonEmptyAttr(attrs, 'href'))
|
|
439
|
+
emitWarning(ctx, 'rgaa/link-as-button', element.start, `[rgaa/link-as-button] <a> with onClick and no href in ${ctx.filename}: use <button type="button"> for actions, or add a valid href for navigation.`)
|
|
440
|
+
|
|
441
|
+
if (hasClick && !spread && !NATIVE_CLICKABLE_TAGS.has(tag) && !isPresentational(attrs)) {
|
|
442
|
+
const hasKey = findAttr(attrs, 'onKeyDown') !== undefined || findAttr(attrs, 'onKeyUp') !== undefined || findAttr(attrs, 'onKeyPress') !== undefined
|
|
443
|
+
if (!hasKey)
|
|
444
|
+
emitWarning(ctx, 'rgaa/click-without-key', element.start, `[rgaa/click-without-key] <${tag} onClick={...}> in ${ctx.filename} is not keyboard-accessible: use <button type="button">, or add onKeyDown/onKeyUp with an appropriate role and tabIndex.`, onClickAttr.start)
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function checkStructureAndIdentity(element: JSXElement, tag: string, spread: boolean, ctx: RgaaElementContext): void {
|
|
449
|
+
const attrs = element.openingElement.attributes
|
|
450
|
+
|
|
451
|
+
const idAttr = findAttr(attrs, 'id')
|
|
452
|
+
const id = idAttr !== undefined ? staticStringValueOf(idAttr.value) : undefined
|
|
453
|
+
if (id !== undefined && id !== '') {
|
|
454
|
+
if (ctx.scope.seenIds.has(id))
|
|
455
|
+
emitWarning(ctx, 'rgaa/duplicate-id', element.start, `[rgaa/duplicate-id] Duplicate id="${id}" on <${tag}> in ${ctx.filename}: every id in a document must be unique.`, idAttr!.start)
|
|
456
|
+
else
|
|
457
|
+
ctx.scope.seenIds.add(id)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const headingLevel = HEADING_LEVELS[tag]
|
|
461
|
+
if (headingLevel !== undefined) {
|
|
462
|
+
if (ctx.scope.lastHeading > 0 && headingLevel > ctx.scope.lastHeading + 1)
|
|
463
|
+
emitWarning(ctx, 'rgaa/skipped-heading', element.start, `[rgaa/skipped-heading] <h${headingLevel}> in ${ctx.filename} skips heading level(s) after <h${ctx.scope.lastHeading}>: use <h${ctx.scope.lastHeading + 1}> to keep the heading hierarchy continuous.`)
|
|
464
|
+
ctx.scope.lastHeading = headingLevel
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (tag === 'table' && !isPresentational(attrs) && !(element.children.length === 0 && spread) && !tableMayHaveHeader(element.children))
|
|
468
|
+
emitWarning(ctx, 'rgaa/table-without-header', element.start, `[rgaa/table-without-header] <table> in ${ctx.filename} has no <th> header cell: add <th> headers or role="presentation" if used for layout.`)
|
|
469
|
+
|
|
470
|
+
if (tag === 'fieldset' && !(element.children.length === 0 && spread) && !fieldsetMayHaveLegend(element.children, true))
|
|
471
|
+
emitWarning(ctx, 'rgaa/fieldset-without-legend', element.start, `[rgaa/fieldset-without-legend] <fieldset> in ${ctx.filename} has no <legend>: add a <legend> child to label the form group.`)
|
|
472
|
+
|
|
473
|
+
if (tag === 'meta') {
|
|
474
|
+
const httpEquivAttr = findAttrLower(attrs, 'http-equiv') ?? findAttrLower(attrs, 'httpequiv')
|
|
475
|
+
if (httpEquivAttr !== undefined && staticStringValueOf(httpEquivAttr.value)?.trim().toLowerCase() === 'refresh')
|
|
476
|
+
emitWarning(ctx, 'rgaa/meta-refresh', element.start, `[rgaa/meta-refresh] <meta http-equiv="refresh"> in ${ctx.filename} redirects or reloads without user control: use an HTTP redirect or an explicit user action.`, httpEquivAttr.start)
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Runs all static RGAA JSX compliance checks on an intrinsic HTML element. */
|
|
481
|
+
export function checkRgaaElement(element: JSXElement, tag: string, ctx: RgaaElementContext): void {
|
|
482
|
+
const attrs = element.openingElement.attributes
|
|
483
|
+
const spread = hasSpread(attrs)
|
|
484
|
+
|
|
485
|
+
if (tag === 'img' && !spread && findAttr(attrs, 'alt') === undefined)
|
|
486
|
+
emitWarning(ctx, 'rgaa/img-alt', element.start, `[rgaa/img-alt] <img> in ${ctx.filename} is missing an alt attribute: add alt="description" or alt="" for decorative images.`)
|
|
487
|
+
|
|
488
|
+
if ((tag === 'input' || tag === 'select' || tag === 'textarea') && !spread && !ctx.inLabel && !hasLabelAttr(attrs)) {
|
|
489
|
+
const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
|
|
490
|
+
const idKey = attrTargetKey(findAttr(attrs, 'id'))
|
|
491
|
+
const linked = idKey !== undefined && ctx.labelTargets.has(idKey)
|
|
492
|
+
if ((inputType === undefined || !NON_LABELLED_INPUT_TYPES.has(inputType)) && !linked)
|
|
493
|
+
emitWarning(ctx, 'rgaa/form-label', element.start, `[rgaa/form-label] <${tag}> in ${ctx.filename} has no associated label: wrap it in <label>, link <label for="id">, or add aria-label="..." / aria-labelledby="...".`)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if ((tag === 'button' || tag === 'a') && !spread && !hasLabelAttr(attrs) && !hasAccessibleName(element.children))
|
|
497
|
+
emitWarning(ctx, 'rgaa/accessible-name', element.start, `[rgaa/accessible-name] <${tag}> in ${ctx.filename} has no accessible name: add visible text or aria-label="...".`)
|
|
498
|
+
|
|
499
|
+
checkRoleAndAria(element, tag, ctx)
|
|
500
|
+
checkFocusAndInteraction(element, tag, spread, ctx)
|
|
501
|
+
checkStructureAndIdentity(element, tag, spread, ctx)
|
|
502
|
+
}
|