prunelle 0.8.0 → 0.9.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 +61 -44
- package/package.json +7 -2
- package/src/a11y/declaration.ts +6 -10
- package/src/a11y/index.ts +69 -125
- package/src/animate.ts +127 -0
- package/src/build/asides.ts +51 -0
- package/src/build/bundle.ts +88 -134
- package/src/build/client-entries.ts +6 -6
- package/src/build/client-outputs.ts +75 -0
- package/src/build/client-plugin.ts +9 -4
- package/src/build/ecoindex.ts +4 -2
- package/src/build/files.ts +18 -0
- package/src/build/graph.ts +14 -6
- package/src/build/index.ts +20 -10
- package/src/build/islands.ts +4 -1
- package/src/build/key-types.ts +31 -0
- package/src/build/manifest.ts +19 -1
- package/src/build/plugin-host.ts +2 -0
- package/src/build/prerender.ts +2 -2
- package/src/build/reach.ts +14 -1
- package/src/build/report.ts +1 -1
- package/src/build/route-types.ts +16 -0
- package/src/build/routes.ts +24 -15
- package/src/build/seed-ids.ts +65 -0
- package/src/build/seed-writes.ts +111 -0
- package/src/build/static-headers.ts +2 -2
- package/src/build/static.ts +1 -1
- package/src/cli/check-cache.ts +51 -0
- package/src/cli/check-consent.ts +95 -0
- package/src/cli/check-forms.ts +2 -2
- package/src/cli/check-links-patterns.ts +101 -0
- package/src/cli/check-links.ts +16 -100
- package/src/cli/check-regions.ts +185 -0
- package/src/cli/check-security.ts +50 -6
- package/src/cli/check.ts +40 -18
- package/src/cli/commands.ts +134 -14
- package/src/cli/docs.ts +21 -2
- package/src/cli/index.ts +1 -1
- package/src/cli/inspect.ts +2 -0
- package/src/cli/mcp-tools.ts +2 -2
- package/src/cli/new.ts +9 -5
- package/src/cli/upgrade-names.ts +14 -2
- package/src/cli/upgrade.ts +3 -0
- package/src/compiler/analyze.ts +2 -2
- package/src/compiler/cacheability.ts +24 -2
- package/src/compiler/compliance-contrast.ts +284 -0
- package/src/compiler/compliance-empty.ts +24 -0
- package/src/compiler/compliance-rgaa-content.ts +95 -0
- package/src/compiler/compliance-rgaa-criteria.ts +45 -0
- package/src/compiler/compliance-rgaa-pages.ts +140 -0
- package/src/compiler/compliance-rgaa.ts +19 -0
- package/src/compiler/compliance-third-party.ts +59 -0
- package/src/compiler/compliance-urls.ts +52 -0
- package/src/compiler/compliance.ts +70 -42
- package/src/compiler/cookie-inventory.ts +119 -0
- package/src/compiler/foreign-imports.ts +5 -3
- package/src/compiler/mistakes.ts +18 -9
- package/src/compiler/props-pattern.ts +211 -0
- package/src/compiler/props.ts +177 -346
- package/src/compiler/ssr-emit.ts +510 -0
- package/src/compiler/ssr-helpers.ts +170 -121
- package/src/compiler/ssr.ts +25 -440
- package/src/context.ts +14 -1
- package/src/dev/child.ts +1 -1
- package/src/dev/hot-islands.ts +71 -0
- package/src/dev/index.ts +31 -18
- package/src/dev/protocol.ts +1 -1
- package/src/dev/queue.ts +3 -2
- package/src/dev/reload.ts +1 -1
- package/src/dev/supervisor.ts +3 -1
- package/src/dev/watchers.ts +6 -2
- package/src/dom/action.ts +97 -14
- package/src/dom/extras.ts +2 -1
- package/src/dom/field-marks.ts +24 -0
- package/src/dom/hot.ts +9 -0
- package/src/dom/index.ts +25 -5
- package/src/dom/island-hot.ts +29 -0
- package/src/dom/island.ts +13 -2
- package/src/dom/prefetch.ts +132 -0
- package/src/dom/regions.ts +498 -0
- package/src/dom/swap.ts +245 -0
- package/src/dom/trusted.ts +22 -0
- package/src/dom/upload.ts +44 -0
- package/src/eco/declaration.ts +39 -40
- package/src/eco/index.ts +5 -5
- package/src/href.ts +31 -0
- package/src/index.ts +9 -3
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +7 -1
- package/src/plugins/daily.ts +27 -0
- package/src/plugins/index.ts +3 -3
- package/src/plugins/order.ts +1 -1
- package/src/plugins/public.ts +8 -5
- package/src/plugins/services.ts +88 -14
- package/src/plugins/types.ts +120 -9
- package/src/reactivity/hmr.ts +90 -0
- package/src/reactivity/index.ts +38 -18
- package/src/reactivity/resource.ts +115 -0
- package/src/reactivity/seed.ts +39 -0
- package/src/reactivity/server-seed.ts +194 -0
- package/src/reactivity/store.ts +55 -1
- package/src/rgpd/draft.ts +171 -0
- package/src/rgpd/forms.ts +85 -0
- package/src/rgpd/index.ts +57 -0
- package/src/server/action-flash.ts +1 -0
- package/src/server/action-registry.ts +6 -8
- package/src/server/action.ts +51 -19
- package/src/server/app-secret.ts +14 -8
- package/src/server/cache-invalidation.ts +96 -0
- package/src/server/compress.ts +3 -1
- package/src/server/consent.ts +16 -2
- package/src/server/cookie-inventory.ts +78 -0
- package/src/server/cookies.ts +50 -0
- package/src/server/csp-report.ts +86 -0
- package/src/server/discard.ts +13 -0
- package/src/server/document.ts +2 -2
- package/src/server/env.ts +1 -1
- package/src/server/errors.ts +9 -2
- package/src/server/field-errors.ts +91 -0
- package/src/server/form-schema.ts +16 -11
- package/src/server/health.ts +33 -12
- package/src/server/http.ts +25 -10
- package/src/server/index.ts +101 -298
- package/src/server/loader.ts +1 -0
- package/src/server/page.ts +3 -9
- package/src/server/plugin-host.ts +26 -8
- package/src/server/public.ts +14 -3
- package/src/server/rate-limit.ts +39 -10
- package/src/server/region.ts +3 -0
- package/src/server/request-handler.ts +370 -0
- package/src/server/request-log.ts +94 -0
- package/src/server/route-cache.ts +41 -6
- package/src/server/route-input.ts +54 -0
- package/src/server/route-table.ts +35 -19
- package/src/server/routing.ts +12 -11
- package/src/server/sanitize-html.ts +1 -1
- package/src/server/security.ts +38 -8
- package/src/server/serve-handlers.ts +9 -5
- package/src/server/shared-store.ts +314 -0
- package/src/server/start.ts +12 -5
- package/src/server/static-dir.ts +14 -10
- package/src/server/stream.ts +2 -1
- package/src/server/surface.ts +39 -0
- package/src/server/webhook.ts +48 -0
- package/src/server/workers.ts +168 -0
- package/src/ssr/consent-banner.ts +20 -16
- package/src/ssr/consent-texts.ts +40 -0
- package/src/ssr/context.ts +26 -3
- package/src/ssr/cookie-table.ts +75 -0
- package/src/ssr/index.ts +23 -13
- package/src/ssr/navigation.ts +161 -14
- package/src/test/browser.ts +50 -22
- package/src/test/build-reuse.ts +70 -0
- package/src/test/index.ts +45 -6
- package/src/test/mount.ts +53 -0
- package/src/test/outside.ts +94 -0
- package/templates/_shared/AGENTS.md +1 -1
- package/templates/_shared/Dockerfile +22 -0
- package/templates/_shared/dockerignore +9 -0
- package/templates/_shared/gitignore +2 -0
- package/templates/app/.env.example +3 -1
- package/templates/app/compose.yaml +32 -0
- package/templates/app/db/migrations/001_notes.sql +7 -0
- package/templates/app/db/schema.ts +11 -0
- package/templates/app/package.json +1 -0
- package/templates/app/prunelle.config.ts +6 -0
- package/templates/app/src/actions/notes.ts +26 -0
- package/templates/app/src/auth.ts +4 -2
- package/templates/app/src/db.ts +5 -0
- package/templates/app/src/routes/dashboard/index.tsx +26 -1
- package/templates/app/src/routes/privacy.tsx +6 -2
- package/templates/app/tests/app.test.ts +14 -3
- package/templates/plugin/AGENTS.md +11 -0
- package/templates/plugin/CLAUDE.md +1 -0
- package/templates/plugin/README.md +27 -0
- package/templates/plugin/gitignore +2 -0
- package/templates/plugin/llms.txt +19 -0
- package/templates/plugin/package.json +33 -0
- package/templates/plugin/src/index.ts +40 -0
- package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
- package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
- package/templates/plugin/tests/plugin.test.ts +42 -0
- package/templates/plugin/tsconfig.json +19 -0
- package/ts-plugin.cjs +3674 -2196
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
} from './cacheability-private'
|
|
19
19
|
import { EVENT_ATTR, jsxTagName, resolvePrunelleTag, unwrapTsExpr } from './shared'
|
|
20
20
|
|
|
21
|
-
const CACHE_HELP = 'write a duration such as \'30s\', \'5m\', \'1h\', \'1d\', a positive number of seconds, or { maxAge: \'1h\', staleWhileRevalidate?: \'1d\' }'
|
|
21
|
+
const CACHE_HELP = 'write a duration such as \'30s\', \'5m\', \'1h\', \'1d\', a positive number of seconds, or { maxAge: \'1h\', staleWhileRevalidate?: \'1d\', tags?: [\'posts\'] }'
|
|
22
22
|
const DYNAMIC_PROPS = new Set(['params', 'url', 'locals', 'cookies', 'headers', 'request', 'consent', 'ip'])
|
|
23
23
|
const DYNAMIC_IMPORTS = new Set(['bun:sqlite', 'node:sqlite', 'sqlite', 'bun:sql'])
|
|
24
24
|
const SYNC_IO_CALLEES = new Set(['readFileSync', 'readdirSync', 'statSync', 'lstatSync', 'existsSync', 'accessSync'])
|
|
@@ -46,12 +46,20 @@ function parseCacheObjectNode(node: Extract<Node, { type: 'ObjectExpression' }>)
|
|
|
46
46
|
let maxAgeMs: number | undefined
|
|
47
47
|
let swrMs: number | undefined
|
|
48
48
|
let swrKeyCount = 0
|
|
49
|
+
let tags: string[] = []
|
|
49
50
|
for (const prop of node.properties) {
|
|
50
51
|
if (prop.type !== 'Property' || prop.kind !== 'init')
|
|
51
52
|
return { problem: `cache object must use static properties: ${CACHE_HELP}` }
|
|
52
53
|
const key = propKeyName(prop)
|
|
53
54
|
if (key === undefined)
|
|
54
55
|
return { problem: `cache object has a dynamic key: ${CACHE_HELP}` }
|
|
56
|
+
if (key === 'tags') {
|
|
57
|
+
const names = staticTagNames(prop.value)
|
|
58
|
+
if (names === undefined)
|
|
59
|
+
return { problem: 'cache.tags must be a list of tag names written in the route, such as tags: [\'posts\']: invalidateCache({ tag }) and prunelle check read them from the source' }
|
|
60
|
+
tags = names
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
55
63
|
if (key !== 'maxAge' && key !== 'staleWhileRevalidate' && key !== 'swr')
|
|
56
64
|
return { problem: `unknown cache option "${key}": ${CACHE_HELP}` }
|
|
57
65
|
const parsed = staticStringOrNumber(prop.value)
|
|
@@ -72,7 +80,21 @@ function parseCacheObjectNode(node: Extract<Node, { type: 'ObjectExpression' }>)
|
|
|
72
80
|
return { problem: `cache object is missing "maxAge": ${CACHE_HELP}` }
|
|
73
81
|
if (swrKeyCount > 1)
|
|
74
82
|
return { problem: `specify either "staleWhileRevalidate" or "swr", not both: ${CACHE_HELP}` }
|
|
75
|
-
return { spec: buildRouteCacheSpec(maxAgeMs, swrMs ?? maxAgeMs) }
|
|
83
|
+
return { spec: buildRouteCacheSpec(maxAgeMs, swrMs ?? maxAgeMs, tags) }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function staticTagNames(node: Node): string[] | undefined {
|
|
87
|
+
const list = unwrapTsExpr(node)
|
|
88
|
+
if (list.type !== 'ArrayExpression')
|
|
89
|
+
return undefined
|
|
90
|
+
const names: string[] = []
|
|
91
|
+
for (const item of list.elements) {
|
|
92
|
+
const name = item === null || item.type === 'SpreadElement' ? undefined : staticStringOrNumber(item)
|
|
93
|
+
if (name?.ok !== true || typeof name.value !== 'string' || name.value === '')
|
|
94
|
+
return undefined
|
|
95
|
+
names.push(name.value)
|
|
96
|
+
}
|
|
97
|
+
return names
|
|
76
98
|
}
|
|
77
99
|
|
|
78
100
|
/** Parses and validates `export const cache = ...` (or `const cache = ...; export { cache }`) from a module AST. */
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// RGAA 3.2 read from the app's own stylesheets: a rule that sets both its text colour and its background, in the light and the dark scheme, with the custom properties its stylesheet declares on :root.
|
|
2
|
+
|
|
3
|
+
export type ContrastRule = 'rgaa/contrast'
|
|
4
|
+
|
|
5
|
+
export interface ContrastFinding {
|
|
6
|
+
offset: number
|
|
7
|
+
message: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Scheme = 'light' | 'dark'
|
|
11
|
+
type Rgb = readonly [number, number, number]
|
|
12
|
+
|
|
13
|
+
interface StyleRule {
|
|
14
|
+
selector: string
|
|
15
|
+
/** Set by `@media (prefers-color-scheme)` or a `[data-theme]`, `.dark` or `.light` selector; undefined in both schemes. */
|
|
16
|
+
scheme: Scheme | undefined
|
|
17
|
+
decls: Map<string, string>
|
|
18
|
+
offset: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Tokens {
|
|
22
|
+
base: Map<string, string>
|
|
23
|
+
light: Map<string, string>
|
|
24
|
+
dark: Map<string, string>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const COMMENT_RE = /\/\*[\s\S]*?\*\//g
|
|
28
|
+
const MEDIA_SCHEME_RE = /prefers-color-scheme\s*:\s*(light|dark)/
|
|
29
|
+
const SELECTOR_SCHEME_RE = /\[data-theme\s*=\s*["']?(light|dark)["']?\s*\]|\.(light|dark)\b/
|
|
30
|
+
// At-rules whose blocks hold no style rule.
|
|
31
|
+
const OPAQUE_AT_RULE_RE = /^@(?:keyframes|-webkit-keyframes|font-face|property|page|counter-style|font-feature-values)\b/
|
|
32
|
+
const HEX_RE = /^#([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i
|
|
33
|
+
const FUNCTION_COLOR_RE = /^(rgba?|hsla?)\(([^()]*)\)$/i
|
|
34
|
+
const FONT_SIZE_RE = /^([\d.]+)(px|rem|pt)$/
|
|
35
|
+
// The 17 colours of CSS 2.1 and their common aliases; a name outside them is not resolved, so its rule is skipped.
|
|
36
|
+
const NAMED: Record<string, Rgb> = {
|
|
37
|
+
black: [0, 0, 0], white: [255, 255, 255], gray: [128, 128, 128], grey: [128, 128, 128], silver: [192, 192, 192],
|
|
38
|
+
red: [255, 0, 0], maroon: [128, 0, 0], yellow: [255, 255, 0], olive: [128, 128, 0], lime: [0, 255, 0], green: [0, 128, 0],
|
|
39
|
+
aqua: [0, 255, 255], cyan: [0, 255, 255], teal: [0, 128, 128], blue: [0, 0, 255], navy: [0, 0, 128],
|
|
40
|
+
fuchsia: [255, 0, 255], magenta: [255, 0, 255], purple: [128, 0, 128], orange: [255, 165, 0],
|
|
41
|
+
}
|
|
42
|
+
const MAX_VAR_DEPTH = 10
|
|
43
|
+
|
|
44
|
+
function schemeOfSelector(selector: string): Scheme | undefined {
|
|
45
|
+
const match = SELECTOR_SCHEME_RE.exec(selector)
|
|
46
|
+
return match === null ? undefined : (match[1] ?? match[2]) as Scheme
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The index of the parenthesis or brace closing the one at `open`, strings skipped.
|
|
50
|
+
function closing(css: string, open: number): number {
|
|
51
|
+
const [opener, closer] = css[open] === '(' ? ['(', ')'] : ['{', '}']
|
|
52
|
+
let depth = 0
|
|
53
|
+
for (let i = open; i < css.length; i++) {
|
|
54
|
+
const ch = css[i]
|
|
55
|
+
if (ch === '"' || ch === '\'') {
|
|
56
|
+
i = css.indexOf(ch, i + 1)
|
|
57
|
+
if (i === -1)
|
|
58
|
+
return css.length
|
|
59
|
+
}
|
|
60
|
+
else if (ch === opener) {
|
|
61
|
+
depth++
|
|
62
|
+
}
|
|
63
|
+
else if (ch === closer && --depth === 0) {
|
|
64
|
+
return i
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return css.length
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The declarations at the top of a rule's body; nested rules are left out.
|
|
71
|
+
function declarationsOf(body: string): Map<string, string> {
|
|
72
|
+
const decls = new Map<string, string>()
|
|
73
|
+
let start = 0
|
|
74
|
+
for (let i = 0; i <= body.length; i++) {
|
|
75
|
+
const ch = body[i]
|
|
76
|
+
if (ch === '(' || ch === '{') {
|
|
77
|
+
const end = closing(body, i)
|
|
78
|
+
if (ch === '{')
|
|
79
|
+
start = end + 1
|
|
80
|
+
i = end
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
if (ch !== ';' && i !== body.length)
|
|
84
|
+
continue
|
|
85
|
+
const decl = body.slice(start, i)
|
|
86
|
+
const colon = decl.indexOf(':')
|
|
87
|
+
if (colon > 0)
|
|
88
|
+
decls.set(decl.slice(0, colon).trim().toLowerCase(), decl.slice(colon + 1).replace(/!important\s*$/i, '').trim())
|
|
89
|
+
start = i + 1
|
|
90
|
+
}
|
|
91
|
+
return decls
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function styleRules(source: string): StyleRule[] {
|
|
95
|
+
const css = source.replace(COMMENT_RE, match => ' '.repeat(match.length))
|
|
96
|
+
const rules: StyleRule[] = []
|
|
97
|
+
const walk = (from: number, to: number, scheme: Scheme | undefined): void => {
|
|
98
|
+
let start = from
|
|
99
|
+
for (let i = from; i < to; i++) {
|
|
100
|
+
const ch = css[i]
|
|
101
|
+
if (ch === ';') {
|
|
102
|
+
start = i + 1
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
if (ch !== '{')
|
|
106
|
+
continue
|
|
107
|
+
const end = closing(css, i)
|
|
108
|
+
const prelude = css.slice(start, i)
|
|
109
|
+
const selector = prelude.trim()
|
|
110
|
+
if (selector.startsWith('@')) {
|
|
111
|
+
if (!OPAQUE_AT_RULE_RE.test(selector))
|
|
112
|
+
walk(i + 1, end, (MEDIA_SCHEME_RE.exec(selector)?.[1] as Scheme | undefined) ?? scheme)
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
rules.push({ selector, scheme: schemeOfSelector(selector) ?? scheme, decls: declarationsOf(css.slice(i + 1, end)), offset: start + prelude.length - prelude.trimStart().length })
|
|
116
|
+
}
|
|
117
|
+
i = end
|
|
118
|
+
start = end + 1
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
walk(0, css.length, undefined)
|
|
122
|
+
return rules
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// `:root`, `html` or `:host`, alone or with a theme qualifier: the places a stylesheet declares its tokens.
|
|
126
|
+
function isRootSelector(selector: string): boolean {
|
|
127
|
+
return selector.split(',').every((part) => {
|
|
128
|
+
const bare = part.replace(SELECTOR_SCHEME_RE, '').trim()
|
|
129
|
+
return bare === '' || bare === ':root' || bare === 'html' || bare === ':host'
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function tokensOf(rules: readonly StyleRule[]): Tokens {
|
|
134
|
+
const tokens: Tokens = { base: new Map(), light: new Map(), dark: new Map() }
|
|
135
|
+
for (const rule of rules) {
|
|
136
|
+
if (!isRootSelector(rule.selector))
|
|
137
|
+
continue
|
|
138
|
+
const into = rule.scheme === undefined ? tokens.base : tokens[rule.scheme]
|
|
139
|
+
for (const [name, value] of rule.decls) {
|
|
140
|
+
if (name.startsWith('--'))
|
|
141
|
+
into.set(name, value)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return tokens
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// The arguments of a function call, split at its top-level commas.
|
|
148
|
+
function argsOf(inner: string): string[] {
|
|
149
|
+
const args: string[] = []
|
|
150
|
+
let start = 0
|
|
151
|
+
for (let i = 0; i < inner.length; i++) {
|
|
152
|
+
if (inner[i] === '(') {
|
|
153
|
+
i = closing(inner, i)
|
|
154
|
+
}
|
|
155
|
+
else if (inner[i] === ',') {
|
|
156
|
+
args.push(inner.slice(start, i).trim())
|
|
157
|
+
start = i + 1
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
args.push(inner.slice(start).trim())
|
|
161
|
+
return args
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// `value` with its `var()` and `light-dark()` replaced for `scheme`; undefined when a variable is not declared and has no fallback.
|
|
165
|
+
function substitute(value: string, scheme: Scheme, tokens: Tokens, depth = 0): string | undefined {
|
|
166
|
+
if (depth > MAX_VAR_DEPTH)
|
|
167
|
+
return undefined
|
|
168
|
+
const match = /\b(var|light-dark)\(/i.exec(value)
|
|
169
|
+
if (match === null)
|
|
170
|
+
return value
|
|
171
|
+
const open = match.index + match[0].length - 1
|
|
172
|
+
const end = closing(value, open)
|
|
173
|
+
const args = argsOf(value.slice(open + 1, end))
|
|
174
|
+
let replacement: string | undefined
|
|
175
|
+
if (match[1]!.toLowerCase() === 'var') {
|
|
176
|
+
const declared = tokens[scheme].get(args[0]!) ?? tokens.base.get(args[0]!)
|
|
177
|
+
replacement = declared ?? (args.length > 1 ? args.slice(1).join(',') : undefined)
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
replacement = scheme === 'light' ? args[0] : args[1]
|
|
181
|
+
}
|
|
182
|
+
if (replacement === undefined)
|
|
183
|
+
return undefined
|
|
184
|
+
return substitute(value.slice(0, match.index) + replacement + value.slice(end + 1), scheme, tokens, depth + 1)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function channel(raw: string, max: number): number | undefined {
|
|
188
|
+
const n = Number.parseFloat(raw)
|
|
189
|
+
if (Number.isNaN(n))
|
|
190
|
+
return undefined
|
|
191
|
+
return raw.endsWith('%') ? (n / 100) * max : n
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function hslToRgb(h: number, s: number, l: number): Rgb {
|
|
195
|
+
const k = (n: number): number => (n + h / 30) % 12
|
|
196
|
+
const a = s * Math.min(l, 1 - l)
|
|
197
|
+
const f = (n: number): number => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1))))
|
|
198
|
+
return [f(0), f(8), f(4)]
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** An opaque colour as sRGB channels; undefined for a translucent one, which shows what lies under it, and for any syntax not read here. */
|
|
202
|
+
export function parseColor(raw: string): Rgb | undefined {
|
|
203
|
+
const value = raw.trim().toLowerCase()
|
|
204
|
+
if (value in NAMED)
|
|
205
|
+
return NAMED[value]
|
|
206
|
+
const hex = HEX_RE.exec(value)?.[1]
|
|
207
|
+
if (hex !== undefined) {
|
|
208
|
+
const full = hex.length <= 4 ? [...hex].map(c => c + c).join('') : hex
|
|
209
|
+
if (full.length === 8 && full.slice(6) !== 'ff')
|
|
210
|
+
return undefined
|
|
211
|
+
return [Number.parseInt(full.slice(0, 2), 16), Number.parseInt(full.slice(2, 4), 16), Number.parseInt(full.slice(4, 6), 16)]
|
|
212
|
+
}
|
|
213
|
+
const fn = FUNCTION_COLOR_RE.exec(value)
|
|
214
|
+
if (fn === null)
|
|
215
|
+
return undefined
|
|
216
|
+
const parts = fn[2]!.split(/[\s,/]+/).filter(Boolean)
|
|
217
|
+
if (parts.length < 3 || parts.length > 4 || (parts.length === 4 && channel(parts[3]!, 1) !== 1))
|
|
218
|
+
return undefined
|
|
219
|
+
if (fn[1]!.startsWith('rgb')) {
|
|
220
|
+
const [r, g, b] = [channel(parts[0]!, 255), channel(parts[1]!, 255), channel(parts[2]!, 255)]
|
|
221
|
+
return r === undefined || g === undefined || b === undefined ? undefined : [Math.round(r), Math.round(g), Math.round(b)]
|
|
222
|
+
}
|
|
223
|
+
const [h, s, l] = [Number.parseFloat(parts[0]!), channel(parts[1]!, 1), channel(parts[2]!, 1)]
|
|
224
|
+
return Number.isNaN(h) || s === undefined || l === undefined ? undefined : hslToRgb(((h % 360) + 360) % 360, s, l)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function luminance(rgb: Rgb): number {
|
|
228
|
+
const [r, g, b] = rgb.map((c) => {
|
|
229
|
+
const s = c / 255
|
|
230
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4
|
|
231
|
+
})
|
|
232
|
+
return 0.2126 * r! + 0.7152 * g! + 0.0722 * b!
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The WCAG contrast ratio of two opaque colours, from 1 to 21. */
|
|
236
|
+
export function contrastRatio(a: Rgb, b: Rgb): number {
|
|
237
|
+
const [la, lb] = [luminance(a), luminance(b)]
|
|
238
|
+
return (Math.max(la, lb) + 0.05) / (Math.min(la, lb) + 0.05)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Text from 24px, or from 18.66px when bold, needs 3:1 instead of 4.5:1.
|
|
242
|
+
function isLargeText(decls: ReadonlyMap<string, string>): boolean {
|
|
243
|
+
const size = FONT_SIZE_RE.exec(decls.get('font-size') ?? '')
|
|
244
|
+
if (size === null)
|
|
245
|
+
return false
|
|
246
|
+
const px = Number.parseFloat(size[1]!) * (size[2] === 'rem' ? 16 : size[2] === 'pt' ? 4 / 3 : 1)
|
|
247
|
+
const weight = decls.get('font-weight') ?? ''
|
|
248
|
+
const bold = weight === 'bold' || weight === 'bolder' || Number.parseInt(weight, 10) >= 700
|
|
249
|
+
return px >= 24 || (bold && px >= 18.66)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const hexOf = (rgb: Rgb): string => `#${rgb.map(c => c.toString(16).padStart(2, '0')).join('')}`
|
|
253
|
+
|
|
254
|
+
/** Each rule of a stylesheet whose text colour and background, both set in the rule and both resolved, fall under the contrast RGAA 3.2 asks: 4.5:1, or 3:1 for large text. */
|
|
255
|
+
export function contrastFindings(code: string): ContrastFinding[] {
|
|
256
|
+
const rules = styleRules(code)
|
|
257
|
+
const tokens = tokensOf(rules)
|
|
258
|
+
const findings: ContrastFinding[] = []
|
|
259
|
+
for (const rule of rules) {
|
|
260
|
+
const color = rule.decls.get('color')
|
|
261
|
+
const background = rule.decls.get('background-color') ?? rule.decls.get('background')
|
|
262
|
+
if (color === undefined || background === undefined)
|
|
263
|
+
continue
|
|
264
|
+
const min = isLargeText(rule.decls) ? 3 : 4.5
|
|
265
|
+
const failing = new Map<string, Scheme[]>()
|
|
266
|
+
for (const scheme of rule.scheme === undefined ? ['light', 'dark'] as const : [rule.scheme]) {
|
|
267
|
+
const fg = parseColor(substitute(color, scheme, tokens) ?? '')
|
|
268
|
+
const bg = parseColor(substitute(background, scheme, tokens) ?? '')
|
|
269
|
+
if (fg === undefined || bg === undefined)
|
|
270
|
+
continue
|
|
271
|
+
const ratio = contrastRatio(fg, bg)
|
|
272
|
+
if (ratio >= min)
|
|
273
|
+
continue
|
|
274
|
+
// Floored, so a ratio just under the minimum never prints as the minimum.
|
|
275
|
+
const key = `${hexOf(fg)} on ${hexOf(bg)}, ${Math.floor(ratio * 100) / 100}:1`
|
|
276
|
+
failing.set(key, [...failing.get(key) ?? [], scheme])
|
|
277
|
+
}
|
|
278
|
+
for (const [key, schemes] of failing) {
|
|
279
|
+
const where = schemes.length === 2 || rule.scheme !== undefined ? '' : ` in the ${schemes[0]} scheme`
|
|
280
|
+
findings.push({ offset: rule.offset, message: `[rgaa/contrast] "${rule.selector}" sets ${key}${where}, under the ${min}:1 RGAA 3.2 asks of ${min === 3 ? 'large ' : ''}text: darken one or lighten the other until they reach ${min}:1.` })
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return findings
|
|
284
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Whether a page's JSX can render anything to read, for the notice pages `prunelle check` expects someone to have written.
|
|
2
|
+
|
|
3
|
+
import type { Program } from 'oxc-parser'
|
|
4
|
+
import { walk } from './ast'
|
|
5
|
+
import { isComponentTag, jsxTagName } from './shared'
|
|
6
|
+
|
|
7
|
+
/** True when the module has JSX but none of it holds text, an expression or a component, so the page renders nothing to read. A module with no JSX at all is not judged. */
|
|
8
|
+
export function rendersNoText(program: Program): boolean {
|
|
9
|
+
let hasJsx = false
|
|
10
|
+
let hasContent = false
|
|
11
|
+
walk(program, (node): undefined => {
|
|
12
|
+
if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
|
|
13
|
+
hasJsx = true
|
|
14
|
+
if (node.type === 'JSXElement' && isComponentTag(jsxTagName(node.openingElement.name)))
|
|
15
|
+
hasContent = true
|
|
16
|
+
for (const child of node.children) {
|
|
17
|
+
if (child.type === 'JSXText' ? child.value.trim() !== '' : child.type === 'JSXSpreadChild' || (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression'))
|
|
18
|
+
hasContent = true
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return undefined
|
|
22
|
+
})
|
|
23
|
+
return hasJsx && !hasContent
|
|
24
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// What an element says or carries, read from its children: a link's whole text (RGAA 6.1), a video's caption track (4.3), a table's caption (5.4).
|
|
2
|
+
|
|
3
|
+
import type { JSXChild, JSXElement } from 'oxc-parser'
|
|
4
|
+
import { isComponentTag, jsxTagName, staticLiteral, unwrapTsExpr } from './shared'
|
|
5
|
+
|
|
6
|
+
// Link texts that say nothing of where the link goes once read alone, as a screen reader's list of links reads them.
|
|
7
|
+
const GENERIC_LINK_TEXTS = new Set([
|
|
8
|
+
'ici', 'cliquez ici', 'cliquer ici', 'en savoir plus', 'savoir plus', 'lire la suite', 'la suite', 'suite', 'voir plus', 'voir', 'plus', 'plus de détails', 'détails', 'lien', 'ce lien',
|
|
9
|
+
'here', 'click here', 'read more', 'more', 'learn more', 'see more', 'more details', 'details', 'link', 'this link',
|
|
10
|
+
])
|
|
11
|
+
// Punctuation and arrows a generic text is often dressed in: "Lire la suite…", "En savoir plus →".
|
|
12
|
+
// Pieces join as the browser renders them, so `<span>Lire</span>la suite` reads "Lirela suite".
|
|
13
|
+
const DRESSING_RE = /[\s.,;:!?…»«›‹→←>()[\]-]+/g
|
|
14
|
+
const CAPTION_KINDS = new Set(['captions', 'subtitles'])
|
|
15
|
+
|
|
16
|
+
function textOf(children: readonly JSXChild[]): string | undefined {
|
|
17
|
+
let text = ''
|
|
18
|
+
for (const child of children) {
|
|
19
|
+
if (child.type === 'JSXText') {
|
|
20
|
+
text += child.value
|
|
21
|
+
}
|
|
22
|
+
else if (child.type === 'JSXFragment') {
|
|
23
|
+
const inner = textOf(child.children)
|
|
24
|
+
if (inner === undefined)
|
|
25
|
+
return undefined
|
|
26
|
+
text += inner
|
|
27
|
+
}
|
|
28
|
+
else if (child.type === 'JSXElement') {
|
|
29
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
30
|
+
// A component may render anything, and an image or a drawing adds its own name.
|
|
31
|
+
const inner = isComponentTag(tag) || tag === 'img' || tag === 'svg' ? undefined : textOf(child.children)
|
|
32
|
+
if (inner === undefined)
|
|
33
|
+
return undefined
|
|
34
|
+
text += inner
|
|
35
|
+
}
|
|
36
|
+
else if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression') {
|
|
37
|
+
const literal = staticLiteral(unwrapTsExpr(child.expression))
|
|
38
|
+
if (!literal.ok || typeof literal.value !== 'string')
|
|
39
|
+
return undefined
|
|
40
|
+
text += literal.value
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return text
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The text of a link when the whole of it is generic ("Lire la suite", "click here"); undefined when it names its target or is not known before render. */
|
|
47
|
+
export function genericLinkText(element: JSXElement): string | undefined {
|
|
48
|
+
const text = textOf(element.children)
|
|
49
|
+
if (text === undefined)
|
|
50
|
+
return undefined
|
|
51
|
+
return GENERIC_LINK_TEXTS.has(text.toLowerCase().replace(DRESSING_RE, ' ').trim()) ? text.replace(/\s+/g, ' ').trim() : undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Whether `children` may hold an element `matches` accepts: a component or an expression might render one. */
|
|
55
|
+
function mayHold(children: readonly JSXChild[], matches: (el: JSXElement, tag: string) => boolean): boolean {
|
|
56
|
+
for (const child of children) {
|
|
57
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression')
|
|
58
|
+
return true
|
|
59
|
+
if (child.type === 'JSXFragment' && mayHold(child.children, matches))
|
|
60
|
+
return true
|
|
61
|
+
if (child.type === 'JSXElement') {
|
|
62
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
63
|
+
if (isComponentTag(tag) || matches(child, tag))
|
|
64
|
+
return true
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A track whose kind is captions or subtitles, or is set by a spread or an expression known only at render.
|
|
71
|
+
function mayCaption(track: JSXElement): boolean {
|
|
72
|
+
for (const attr of track.openingElement.attributes) {
|
|
73
|
+
if (attr.type === 'JSXSpreadAttribute')
|
|
74
|
+
return true
|
|
75
|
+
if (attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'kind' || attr.value === null)
|
|
76
|
+
continue
|
|
77
|
+
if (attr.value.type === 'Literal')
|
|
78
|
+
return CAPTION_KINDS.has(String(attr.value.value).toLowerCase())
|
|
79
|
+
if (attr.value.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
|
|
80
|
+
return false
|
|
81
|
+
const literal = staticLiteral(unwrapTsExpr(attr.value.expression))
|
|
82
|
+
return !literal.ok || CAPTION_KINDS.has(String(literal.value).toLowerCase())
|
|
83
|
+
}
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A `<video>` none of whose children is, or may render, a `<track kind="captions">` or `kind="subtitles"`. */
|
|
88
|
+
export function videoWithoutCaptions(element: JSXElement): boolean {
|
|
89
|
+
return !mayHold(element.children, (el, tag) => tag === 'track' && mayCaption(el))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A `<table>` none of whose children is, or may render, a `<caption>`. */
|
|
93
|
+
export function tableWithoutCaption(element: JSXElement): boolean {
|
|
94
|
+
return !mayHold(element.children, (_el, tag) => tag === 'caption')
|
|
95
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The RGAA 4.1 criteria each static rule partly covers, so `prunelle check` can say how little a clean run proves.
|
|
2
|
+
|
|
3
|
+
import type { ContrastRule } from './compliance-contrast'
|
|
4
|
+
import type { RgaaJsxRule } from './compliance-rgaa'
|
|
5
|
+
import type { RgaaPageRule } from './compliance-rgaa-pages'
|
|
6
|
+
|
|
7
|
+
/** The criteria of the RGAA 4.1. */
|
|
8
|
+
export const RGAA_CRITERIA_TOTAL = 106
|
|
9
|
+
|
|
10
|
+
// Every rule has an entry, so a new rule cannot ship without saying which criteria it touches. `rgaa/autofocus` maps to none: no criterion names it.
|
|
11
|
+
const CRITERIA_BY_RULE: Record<RgaaJsxRule | RgaaPageRule | ContrastRule | 'rgaa/document-lang', readonly string[]> = {
|
|
12
|
+
'rgaa/img-alt': ['1.1', '1.2'],
|
|
13
|
+
'rgaa/form-label': ['11.1'],
|
|
14
|
+
'rgaa/accessible-name': ['1.1', '6.2', '11.9'],
|
|
15
|
+
'rgaa/duplicate-id': ['8.2'],
|
|
16
|
+
'rgaa/positive-tabindex': ['12.8'],
|
|
17
|
+
'rgaa/nested-control': ['8.2'],
|
|
18
|
+
'rgaa/link-as-button': ['7.1'],
|
|
19
|
+
'rgaa/unknown-role': ['7.1'],
|
|
20
|
+
'rgaa/unknown-aria': ['7.1'],
|
|
21
|
+
'rgaa/invalid-aria-value': ['7.1'],
|
|
22
|
+
'rgaa/skipped-heading': ['9.1'],
|
|
23
|
+
'rgaa/table-without-header': ['5.6'],
|
|
24
|
+
'rgaa/fieldset-without-legend': ['11.5', '11.6'],
|
|
25
|
+
'rgaa/click-without-key': ['7.3'],
|
|
26
|
+
'rgaa/autofocus': [],
|
|
27
|
+
'rgaa/meta-refresh': ['13.1'],
|
|
28
|
+
'rgaa/role-without-focus': ['7.3'],
|
|
29
|
+
'rgaa/mouse-only': ['7.3'],
|
|
30
|
+
'rgaa/frame-title': ['2.1'],
|
|
31
|
+
'rgaa/empty-heading': ['9.1'],
|
|
32
|
+
'rgaa/autocomplete': ['11.13'],
|
|
33
|
+
'rgaa/generic-link-text': ['6.1'],
|
|
34
|
+
'rgaa/video-captions': ['4.3'],
|
|
35
|
+
'rgaa/table-caption': ['5.4'],
|
|
36
|
+
'rgaa/document-lang': ['8.3'],
|
|
37
|
+
'rgaa/skip-link': ['12.7'],
|
|
38
|
+
'rgaa/page-title': ['8.5'],
|
|
39
|
+
'rgaa/contrast': ['3.2'],
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** How many distinct RGAA criteria at least one rule of `prunelle check` partly covers. */
|
|
43
|
+
export function rgaaCriteriaCovered(): number {
|
|
44
|
+
return new Set(Object.values(CRITERIA_BY_RULE).flat()).size
|
|
45
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// The RGAA criteria a page meets through its layouts as much as through itself: a link past the blocks every page repeats (12.7) and a title (8.5).
|
|
2
|
+
|
|
3
|
+
import type { JSXChild, JSXElement, Program } from 'oxc-parser'
|
|
4
|
+
import type { ModuleAnalysis } from './analyze'
|
|
5
|
+
import { basename } from 'node:path'
|
|
6
|
+
import { relPosix } from '../build/files'
|
|
7
|
+
import { ancestorFiles, fileToRoutePath } from '../build/routes'
|
|
8
|
+
import { walk } from './ast'
|
|
9
|
+
import { isComponentTag, jsxTagName, staticLiteral, unwrapTsExpr } from './shared'
|
|
10
|
+
|
|
11
|
+
export type RgaaPageRule = 'rgaa/skip-link' | 'rgaa/page-title'
|
|
12
|
+
|
|
13
|
+
export interface RgaaPageFinding {
|
|
14
|
+
rule: RgaaPageRule
|
|
15
|
+
/** From the project root. */
|
|
16
|
+
file: string
|
|
17
|
+
line: number
|
|
18
|
+
message: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Blocks a layout repeats on every page, which a keyboard user tabs through on each one unless a link jumps past them.
|
|
22
|
+
const REPEATED_BLOCKS = new Set(['header', 'nav'])
|
|
23
|
+
|
|
24
|
+
function hrefOf(el: JSXElement): string | undefined {
|
|
25
|
+
for (const attr of el.openingElement.attributes) {
|
|
26
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'href' || attr.value === null)
|
|
27
|
+
continue
|
|
28
|
+
if (attr.value.type === 'Literal')
|
|
29
|
+
return String(attr.value.value)
|
|
30
|
+
if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
31
|
+
const literal = staticLiteral(unwrapTsExpr(attr.value.expression))
|
|
32
|
+
return literal.ok && typeof literal.value === 'string' ? literal.value : undefined
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What a layout renders that 12.7 cares about: the offset of its first `<header>` or `<nav>`, and whether it links to an anchor of the page (`<a href="#main">`). */
|
|
39
|
+
function layoutBlocks(program: Program): { block: number | undefined, skipLink: boolean } {
|
|
40
|
+
let block: number | undefined
|
|
41
|
+
let skipLink = false
|
|
42
|
+
walk(program, (node) => {
|
|
43
|
+
if (node.type !== 'JSXElement')
|
|
44
|
+
return
|
|
45
|
+
const tag = jsxTagName(node.openingElement.name)
|
|
46
|
+
if (block === undefined && REPEATED_BLOCKS.has(tag))
|
|
47
|
+
block = node.start
|
|
48
|
+
const href = tag === 'a' ? hrefOf(node) : undefined
|
|
49
|
+
if (href !== undefined && href.length > 1 && href.startsWith('#'))
|
|
50
|
+
skipLink = true
|
|
51
|
+
})
|
|
52
|
+
return { block, skipLink }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// A <title> with text, or a component or an expression that may render one.
|
|
56
|
+
function mayHoldTitle(children: readonly JSXChild[]): boolean {
|
|
57
|
+
for (const child of children) {
|
|
58
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression')
|
|
59
|
+
return true
|
|
60
|
+
if (child.type === 'JSXFragment' && mayHoldTitle(child.children))
|
|
61
|
+
return true
|
|
62
|
+
if (child.type !== 'JSXElement')
|
|
63
|
+
continue
|
|
64
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
65
|
+
if (isComponentTag(tag) && tag !== 'Head')
|
|
66
|
+
return true
|
|
67
|
+
if (tag === 'title' && child.children.some(c => c.type !== 'JSXText' || c.value.trim() !== ''))
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether a module renders a `<Head>` holding a title, or something that may render one. */
|
|
74
|
+
function rendersTitle(program: Program): boolean {
|
|
75
|
+
let found = false
|
|
76
|
+
walk(program, (node) => {
|
|
77
|
+
if (found)
|
|
78
|
+
return false
|
|
79
|
+
if (node.type === 'JSXElement' && jsxTagName(node.openingElement.name) === 'Head' && mayHoldTitle(node.children))
|
|
80
|
+
found = true
|
|
81
|
+
return undefined
|
|
82
|
+
})
|
|
83
|
+
return found
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function lineAt(code: string, offset: number): number {
|
|
87
|
+
let line = 1
|
|
88
|
+
for (let i = code.indexOf('\n'); i !== -1 && i < offset; i = code.indexOf('\n', i + 1))
|
|
89
|
+
line++
|
|
90
|
+
return line
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface RgaaPageInput {
|
|
94
|
+
root: string
|
|
95
|
+
routesDir: string
|
|
96
|
+
routeFiles: readonly string[]
|
|
97
|
+
analyses: ReadonlyMap<string, ModuleAnalysis>
|
|
98
|
+
/** Whether a plugin (`@prunellejs/seo`) reports a page with no title itself. */
|
|
99
|
+
titleChecked: boolean
|
|
100
|
+
source: (file: string) => string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A layout that renders a header or a navigation with no skip link in it or above it, and a page that neither it nor its layouts give a title. A file not analysed counts as compliant, as nothing is known of it. */
|
|
104
|
+
export function rgaaPageFindings(input: RgaaPageInput): RgaaPageFinding[] {
|
|
105
|
+
const { root, routesDir, routeFiles, analyses, titleChecked, source } = input
|
|
106
|
+
const findings: RgaaPageFinding[] = []
|
|
107
|
+
const blocks = new Map<string, { block: number | undefined, skipLink: boolean }>()
|
|
108
|
+
const blocksOf = (file: string): { block: number | undefined, skipLink: boolean } => {
|
|
109
|
+
let found = blocks.get(file)
|
|
110
|
+
if (found === undefined) {
|
|
111
|
+
const program = analyses.get(file)?.program
|
|
112
|
+
found = program === undefined ? { block: undefined, skipLink: true } : layoutBlocks(program)
|
|
113
|
+
blocks.set(file, found)
|
|
114
|
+
}
|
|
115
|
+
return found
|
|
116
|
+
}
|
|
117
|
+
for (const file of routeFiles) {
|
|
118
|
+
const layouts = ancestorFiles(routesDir, file, ['_layout.tsx'])
|
|
119
|
+
if (basename(file) === '_layout.tsx') {
|
|
120
|
+
const { block } = blocksOf(file)
|
|
121
|
+
if (block !== undefined && !layouts.some(layout => blocksOf(layout).skipLink)) {
|
|
122
|
+
const rel = relPosix(root, file)
|
|
123
|
+
findings.push({ rule: 'rgaa/skip-link', file: rel, line: lineAt(source(file), block), message: `${rel} repeats a header or a navigation on every page with no link to jump past it, which a keyboard user tabs through on each page: add <a href="#main" class="skip-link">Aller au contenu</a> as its first element and id="main" on <main>.` })
|
|
124
|
+
}
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
const analysis = analyses.get(file)
|
|
128
|
+
if (titleChecked || basename(file).startsWith('_') || analysis === undefined || !analysis.exportedNames.includes('default'))
|
|
129
|
+
continue
|
|
130
|
+
const titled = [file, ...layouts].some((f) => {
|
|
131
|
+
const program = analyses.get(f)?.program
|
|
132
|
+
return program === undefined || rendersTitle(program)
|
|
133
|
+
})
|
|
134
|
+
if (!titled) {
|
|
135
|
+
const rel = relPosix(root, file)
|
|
136
|
+
findings.push({ rule: 'rgaa/page-title', file: rel, line: 1, message: `Route "${fileToRoutePath(routesDir, file)}" has no <title>, so a tab, a bookmark and a screen reader all name it by its URL: add <Head><title>...</title></Head> to ${rel}, or a default one to its _layout.tsx.` })
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return findings
|
|
140
|
+
}
|