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.
Files changed (48) hide show
  1. package/llms.txt +12 -12
  2. package/package.json +1 -1
  3. package/src/build/client-entries.ts +3 -2
  4. package/src/build/compile.ts +2 -1
  5. package/src/build/index.ts +68 -29
  6. package/src/build/manifest.ts +18 -1
  7. package/src/build/output.ts +2 -2
  8. package/src/build/precompress.ts +15 -2
  9. package/src/build/prerender.ts +2 -2
  10. package/src/build/routes.ts +171 -10
  11. package/src/build/static.ts +71 -17
  12. package/src/build/status-pages.ts +127 -0
  13. package/src/cli/check-forms.ts +654 -0
  14. package/src/cli/check-links.ts +619 -0
  15. package/src/cli/check.ts +37 -4
  16. package/src/cli/commands.ts +5 -3
  17. package/src/cli/inspect.ts +21 -2
  18. package/src/cli/upgrade-names.ts +73 -13
  19. package/src/cli/upgrade.ts +137 -16
  20. package/src/compiler/compliance-rgaa.ts +502 -0
  21. package/src/compiler/compliance.ts +119 -220
  22. package/src/compiler/ssr.ts +121 -24
  23. package/src/dom/action.ts +4 -1
  24. package/src/dom/island.ts +5 -5
  25. package/src/plugins/config.ts +1 -1
  26. package/src/plugins/types.ts +13 -0
  27. package/src/server/action-protect.ts +186 -0
  28. package/src/server/action.ts +33 -10
  29. package/src/server/errors.ts +291 -62
  30. package/src/server/form-schema.ts +343 -0
  31. package/src/server/health.ts +244 -0
  32. package/src/server/http.ts +84 -17
  33. package/src/server/index.ts +162 -91
  34. package/src/server/param-matchers.ts +112 -0
  35. package/src/server/plugin-host.ts +40 -14
  36. package/src/server/public.ts +3 -3
  37. package/src/server/route-table.ts +5 -2
  38. package/src/server/router.ts +125 -40
  39. package/src/server/routing.ts +95 -57
  40. package/src/server/security.ts +171 -22
  41. package/src/server/serve-handlers.ts +21 -7
  42. package/src/ssr/context.ts +49 -0
  43. package/src/ssr/index.ts +120 -1
  44. package/src/test/browser.ts +6 -0
  45. package/src/test/index.ts +17 -3
  46. package/templates/_shared/src/routes/legal.tsx +5 -0
  47. package/templates/blog/prunelle.config.ts +1 -1
  48. package/templates/blog/src/routes/index.tsx +4 -0
@@ -1,19 +1,30 @@
1
1
  // French web law compliance checks (RGAA, RGPD, LCEN): static TSX/CSS warnings and route-level checks with explicit silencing.
2
2
 
3
- import type { JSXAttribute, JSXAttributeItem, JSXChild, JSXElement, Node, Program } from 'oxc-parser'
3
+ import type { JSXAttribute, JSXElement, Node, Program } from 'oxc-parser'
4
4
  import type { ModuleAnalysis } from './analyze'
5
+ import type { RgaaJsxRule, RgaaScope } from './compliance-rgaa'
5
6
  import type { CompilerWarning } from './mistakes'
6
7
  import { existsSync, readFileSync } from 'node:fs'
7
8
  import { basename } from 'node:path'
8
9
  import { visitorKeys } from 'oxc-parser'
9
10
  import { relPosix, walkFiles } from '../build/files'
10
11
  import { fileToRoutePath } from '../build/routes'
11
- import { cleanJsxText, formatDiagnostic, formatLoc, isComponentTag, jsxTagName, unwrapParens, unwrapTsExpr } from './shared'
12
+ import {
13
+ attrTargetKey,
14
+ checkRgaaElement,
15
+ cloneRgaaScope,
16
+ createRgaaScope,
17
+ findAttr,
18
+ hasNonEmptyAttr,
19
+ INTERACTIVE_ANCESTORS,
20
+ mergeRgaaScope,
21
+ RGAA_JSX_RULES,
22
+ staticStringValueOf,
23
+ } from './compliance-rgaa'
24
+ import { formatDiagnostic, formatLoc, isComponentTag, jsxTagName } from './shared'
12
25
 
13
26
  export type ComplianceRule =
14
- | 'rgaa/img-alt'
15
- | 'rgaa/form-label'
16
- | 'rgaa/accessible-name'
27
+ | RgaaJsxRule
17
28
  | 'rgpd/third-party-url'
18
29
  | 'lcen/legal-notice'
19
30
  | 'rgaa/accessibility-statement'
@@ -28,15 +39,12 @@ export interface ComplianceConfig {
28
39
  }
29
40
 
30
41
  const COMPLIANCE_RULES = new Set<ComplianceRule>([
31
- 'rgaa/img-alt',
32
- 'rgaa/form-label',
33
- 'rgaa/accessible-name',
42
+ ...RGAA_JSX_RULES,
34
43
  'rgpd/third-party-url',
35
44
  'lcen/legal-notice',
36
45
  'rgaa/accessibility-statement',
37
46
  ])
38
47
 
39
- const NON_LABELLED_INPUT_TYPES = new Set(['hidden', 'submit', 'reset', 'button', 'image'])
40
48
  const THIRD_PARTY_LINK_RELS = new Set(['stylesheet', 'preload', 'preconnect', 'dns-prefetch'])
41
49
  const LOCAL_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0.0.0.0'])
42
50
  const DEFAULT_LEGAL_PATHS = new Set(['/mentions-legales', '/legal', '/legal-notice', '/imprint', '/impressum', '/cgu', '/cgv'])
@@ -175,147 +183,18 @@ export function shouldEmitWarning(warning: CompilerWarning, compliance?: Complia
175
183
  return true
176
184
  }
177
185
 
178
- function findAttr(attributes: readonly JSXAttributeItem[], name: string): JSXAttribute | undefined {
179
- for (const attr of attributes) {
180
- if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name === name)
181
- return attr
182
- }
183
- return undefined
184
- }
185
-
186
- function hasSpread(attributes: readonly JSXAttributeItem[]): boolean {
187
- return attributes.some(a => a.type === 'JSXSpreadAttribute')
188
- }
189
-
190
- function staticStringValueOf(value: JSXAttribute['value']): string | undefined {
191
- if (value === null)
192
- return undefined
193
- if (value.type === 'Literal' && typeof value.value === 'string')
194
- return value.value
195
- if (value.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
196
- const expr = unwrapTsExpr(value.expression)
197
- if (expr.type === 'Literal' && typeof expr.value === 'string')
198
- return expr.value
199
- if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0)
200
- return expr.quasis[0]?.value.cooked ?? undefined
201
- }
202
- return undefined
203
- }
204
-
205
- function attrTargetKey(attr: JSXAttribute | undefined): string | undefined {
206
- if (attr === undefined || attr.value === null)
207
- return undefined
208
- const str = staticStringValueOf(attr.value)
209
- if (str !== undefined && str.trim() !== '')
210
- return `s:${str}`
211
- if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
212
- const expr = unwrapTsExpr(attr.value.expression)
213
- if (expr.type === 'Identifier')
214
- return `i:${expr.name}`
215
- }
216
- return undefined
217
- }
218
-
219
- function hasNonEmptyAttr(attributes: readonly JSXAttributeItem[], name: string): boolean {
220
- const attr = findAttr(attributes, name)
221
- if (attr === undefined || attr.value === null)
222
- return false
223
- const str = staticStringValueOf(attr.value)
224
- if (str !== undefined)
225
- return str.trim() !== ''
226
- if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
227
- const expr = unwrapParens(attr.value.expression)
228
- if (expr.type === 'Literal' && (expr.value === null || expr.value === false || expr.value === ''))
229
- return false
230
- if (expr.type === 'Identifier' && expr.name === 'undefined')
231
- return false
232
- return true
233
- }
234
- return false
235
- }
236
-
237
- function hasLabelAttr(attributes: readonly JSXAttributeItem[]): boolean {
238
- return hasNonEmptyAttr(attributes, 'aria-label')
239
- || hasNonEmptyAttr(attributes, 'aria-labelledby')
240
- || hasNonEmptyAttr(attributes, 'title')
241
- }
242
-
243
- function isAriaHidden(attributes: readonly JSXAttributeItem[]): boolean {
244
- const attr = findAttr(attributes, 'aria-hidden')
245
- if (attr === undefined)
246
- return false
247
- if (attr.value === null)
248
- return true
249
- if (attr.value.type === 'Literal')
250
- return attr.value.value === 'true'
251
- if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
252
- const expr = unwrapParens(attr.value.expression)
253
- return expr.type === 'Literal' && (expr.value === true || expr.value === 'true')
254
- }
255
- return false
256
- }
257
-
258
- function hasMeaningfulExpression(expr: Node): boolean {
259
- const inner = unwrapParens(expr)
260
- if (inner.type === 'Literal')
261
- return typeof inner.value === 'string' ? inner.value.trim() !== '' : typeof inner.value === 'number'
262
- if (inner.type === 'Identifier' && inner.name === 'undefined')
263
- return false
264
- return true
265
- }
266
-
267
- function hasAccessibleName(children: readonly JSXChild[]): boolean {
268
- for (const child of children) {
269
- if (child.type === 'JSXText') {
270
- if (cleanJsxText(child.value).trim() !== '')
271
- return true
272
- }
273
- else if (child.type === 'JSXExpressionContainer') {
274
- if (child.expression.type !== 'JSXEmptyExpression' && hasMeaningfulExpression(child.expression))
275
- return true
276
- }
277
- else if (child.type === 'JSXFragment') {
278
- if (hasAccessibleName(child.children))
279
- return true
280
- }
281
- else if (child.type === 'JSXElement') {
282
- const attrs = child.openingElement.attributes
283
- if (isAriaHidden(attrs))
284
- continue
285
- const tag = jsxTagName(child.openingElement.name)
286
- if (hasSpread(attrs) || hasLabelAttr(attrs))
287
- return true
288
- if (tag === 'img') {
289
- if (hasNonEmptyAttr(attrs, 'alt'))
290
- return true
291
- continue
292
- }
293
- if (tag === 'svg') {
294
- const hasTitle = child.children.some(c =>
295
- c.type === 'JSXElement' && jsxTagName(c.openingElement.name) === 'title' && hasAccessibleName(c.children),
296
- )
297
- if (hasTitle)
298
- return true
299
- continue
300
- }
301
- if (isComponentTag(tag)) {
302
- if (hasNonEmptyAttr(attrs, 'label') || hasAccessibleName(child.children))
303
- return true
304
- continue
305
- }
306
- if (hasAccessibleName(child.children))
307
- return true
308
- }
309
- }
310
- return false
186
+ interface AncestorState {
187
+ labelDepth: number
188
+ consentGateDepth: number
189
+ interactiveAncestor: string | undefined
311
190
  }
312
191
 
313
- function walkAst(value: unknown, enter: (node: Node) => void, exit?: (node: Node) => void): void {
192
+ function walkAst(value: unknown, enter: (node: Node) => void): void {
314
193
  if (value === null || typeof value !== 'object')
315
194
  return
316
195
  if (Array.isArray(value)) {
317
196
  for (let i = 0; i < value.length; i++)
318
- walkAst(value[i], enter, exit)
197
+ walkAst(value[i], enter)
319
198
  return
320
199
  }
321
200
  const node = value as Node
@@ -325,9 +204,8 @@ function walkAst(value: unknown, enter: (node: Node) => void, exit?: (node: Node
325
204
  const keys = visitorKeys[node.type]
326
205
  if (keys !== undefined) {
327
206
  for (let i = 0; i < keys.length; i++)
328
- walkAst((node as unknown as Record<string, unknown>)[keys[i]!], enter, exit)
207
+ walkAst((node as unknown as Record<string, unknown>)[keys[i]!], enter)
329
208
  }
330
- exit?.(node)
331
209
  }
332
210
 
333
211
  function collectLabelTargets(program: Program): Set<string> {
@@ -386,52 +264,14 @@ function checkThirdPartyElement(
386
264
  })
387
265
  }
388
266
 
389
- function checkJsxElementCompliance(
390
- element: JSXElement,
391
- filename: string,
392
- inLabel: boolean,
393
- inConsentGate: boolean,
394
- labelTargets: ReadonlySet<string>,
395
- ignores: IgnoreMap,
396
- warnings: CompilerWarning[],
397
- ): void {
398
- const tag = jsxTagName(element.openingElement.name)
399
- if (tag === '' || isComponentTag(tag))
400
- return
401
- const attrs = element.openingElement.attributes
402
- const spread = hasSpread(attrs)
403
-
404
- if (tag === 'img' && !spread && findAttr(attrs, 'alt') === undefined && !isIgnoredAt(ignores, 'rgaa/img-alt', element.start)) {
405
- warnings.push({
406
- rule: 'rgaa/img-alt',
407
- start: element.start,
408
- message: `[rgaa/img-alt] <img> in ${filename} is missing an alt attribute: add alt="description" or alt="" for decorative images.`,
409
- })
410
- }
411
-
412
- if ((tag === 'input' || tag === 'select' || tag === 'textarea') && !spread && !inLabel && !hasLabelAttr(attrs)) {
413
- const inputType = tag === 'input' ? staticStringValueOf(findAttr(attrs, 'type')?.value ?? null)?.toLowerCase() : undefined
414
- const idKey = attrTargetKey(findAttr(attrs, 'id'))
415
- const linked = idKey !== undefined && labelTargets.has(idKey)
416
- if ((inputType === undefined || !NON_LABELLED_INPUT_TYPES.has(inputType)) && !linked && !isIgnoredAt(ignores, 'rgaa/form-label', element.start)) {
417
- warnings.push({
418
- rule: 'rgaa/form-label',
419
- start: element.start,
420
- message: `[rgaa/form-label] <${tag}> in ${filename} has no associated label: wrap it in <label>, link <label for="id">, or add aria-label="..." / aria-labelledby="...".`,
421
- })
422
- }
423
- }
424
-
425
- if ((tag === 'button' || tag === 'a') && !spread && !hasLabelAttr(attrs) && !hasAccessibleName(element.children) && !isIgnoredAt(ignores, 'rgaa/accessible-name', element.start)) {
426
- warnings.push({
427
- rule: 'rgaa/accessible-name',
428
- start: element.start,
429
- message: `[rgaa/accessible-name] <${tag}> in ${filename} has no accessible name: add visible text or aria-label="...".`,
430
- })
431
- }
432
-
433
- if (!inConsentGate)
434
- checkThirdPartyElement(element, tag, filename, ignores, warnings)
267
+ function definitelyExits(stmt: Node): boolean {
268
+ if (stmt.type === 'ReturnStatement' || stmt.type === 'ThrowStatement')
269
+ return true
270
+ if (stmt.type === 'BlockStatement')
271
+ return stmt.body.length > 0 && definitelyExits(stmt.body[stmt.body.length - 1]!)
272
+ if (stmt.type === 'IfStatement')
273
+ return stmt.alternate !== null && definitelyExits(stmt.consequent) && definitelyExits(stmt.alternate)
274
+ return false
435
275
  }
436
276
 
437
277
  /** Runs RGAA & RGPD static JSX compliance checks on `program`, appending warnings (never throwing). */
@@ -444,36 +284,95 @@ export function checkCompliance(
444
284
  ): void {
445
285
  const ignores = parsePrunelleIgnores(code)
446
286
  const labelTargets = collectLabelTargets(program)
447
- let labelDepth = 0
448
- let consentGateDepth = 0
449
- const labelFlags: boolean[] = []
450
- const consentFlags: boolean[] = []
451
-
452
- walkAst(
453
- program,
454
- (node) => {
455
- if (node.type !== 'JSXElement')
456
- return
287
+ const isIgnored = (rule: RgaaJsxRule, elementStart: number, attrStart?: number): boolean =>
288
+ isIgnoredAt(ignores, rule, elementStart) || (attrStart !== undefined && isIgnoredAt(ignores, rule, attrStart))
289
+
290
+ const visitChildren = (node: Node, ancestors: AncestorState, scope: RgaaScope): void => {
291
+ const keys = visitorKeys[node.type]
292
+ if (keys === undefined)
293
+ return
294
+ const rec = node as unknown as Record<string, unknown>
295
+ for (let i = 0; i < keys.length; i++)
296
+ visit(rec[keys[i]!], ancestors, scope)
297
+ }
298
+
299
+ const visit = (value: unknown, ancestors: AncestorState, scope: RgaaScope): void => {
300
+ if (value === null || typeof value !== 'object')
301
+ return
302
+ if (Array.isArray(value)) {
303
+ for (let i = 0; i < value.length; i++)
304
+ visit(value[i], ancestors, scope)
305
+ return
306
+ }
307
+ const node = value as Node
308
+ if (typeof node.type !== 'string')
309
+ return
310
+
311
+ if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
312
+ visitChildren(node, ancestors, createRgaaScope())
313
+ return
314
+ }
315
+
316
+ if (node.type === 'ConditionalExpression') {
317
+ visit(node.test, ancestors, scope)
318
+ const branchA = cloneRgaaScope(scope)
319
+ const branchB = cloneRgaaScope(scope)
320
+ visit(node.consequent, ancestors, branchA)
321
+ visit(node.alternate, ancestors, branchB)
322
+ mergeRgaaScope(scope, branchA)
323
+ mergeRgaaScope(scope, branchB)
324
+ return
325
+ }
326
+
327
+ if (node.type === 'IfStatement') {
328
+ visit(node.test, ancestors, scope)
329
+ const branchA = cloneRgaaScope(scope)
330
+ visit(node.consequent, ancestors, branchA)
331
+ const branchB = cloneRgaaScope(scope)
332
+ if (node.alternate !== null)
333
+ visit(node.alternate, ancestors, branchB)
334
+ if (!definitelyExits(node.consequent))
335
+ mergeRgaaScope(scope, branchA)
336
+ if (node.alternate !== null && !definitelyExits(node.alternate))
337
+ mergeRgaaScope(scope, branchB)
338
+ return
339
+ }
340
+
341
+ if (node.type === 'JSXElement') {
457
342
  const tag = jsxTagName(node.openingElement.name)
458
- checkJsxElementCompliance(node, filename, labelDepth > 0, consentGateDepth > 0, labelTargets, ignores, warnings)
459
- const opensLabel = tag === 'label' || (isComponentTag(tag) && hasNonEmptyAttr(node.openingElement.attributes, 'label'))
343
+ const attrs = node.openingElement.attributes
344
+ if (tag !== '' && !isComponentTag(tag)) {
345
+ checkRgaaElement(node, tag, {
346
+ filename,
347
+ inLabel: ancestors.labelDepth > 0,
348
+ interactiveAncestor: ancestors.interactiveAncestor,
349
+ labelTargets,
350
+ scope,
351
+ isIgnored,
352
+ warnings,
353
+ })
354
+ if (ancestors.consentGateDepth === 0)
355
+ checkThirdPartyElement(node, tag, filename, ignores, warnings)
356
+ }
357
+ visit(attrs, ancestors, scope)
358
+ const opensLabel = tag === 'label' || (isComponentTag(tag) && hasNonEmptyAttr(attrs, 'label'))
460
359
  const opensConsentGate = resolveTag(tag) === 'ConsentGate'
461
- labelFlags.push(opensLabel)
462
- consentFlags.push(opensConsentGate)
463
- if (opensLabel)
464
- labelDepth++
465
- if (opensConsentGate)
466
- consentGateDepth++
467
- },
468
- (node) => {
469
- if (node.type !== 'JSXElement')
470
- return
471
- if (labelFlags.pop() === true)
472
- labelDepth--
473
- if (consentFlags.pop() === true)
474
- consentGateDepth--
475
- },
476
- )
360
+ const nextInteractive = INTERACTIVE_ANCESTORS.has(tag) ? tag : ancestors.interactiveAncestor
361
+ const childAncestors = opensLabel || opensConsentGate || nextInteractive !== ancestors.interactiveAncestor
362
+ ? {
363
+ labelDepth: ancestors.labelDepth + (opensLabel ? 1 : 0),
364
+ consentGateDepth: ancestors.consentGateDepth + (opensConsentGate ? 1 : 0),
365
+ interactiveAncestor: nextInteractive,
366
+ }
367
+ : ancestors
368
+ visit(node.children, childAncestors, scope)
369
+ return
370
+ }
371
+
372
+ visitChildren(node, ancestors, scope)
373
+ }
374
+
375
+ visit(program, { labelDepth: 0, consentGateDepth: 0, interactiveAncestor: undefined }, createRgaaScope())
477
376
  }
478
377
 
479
378
  /** Scans a CSS file for third-party `@import` or `url(...)` origins (RGPD), respecting `/* prunelle-ignore *\/` and config. */
@@ -34,11 +34,86 @@ import {
34
34
  emitSsrElementAttrs,
35
35
  precomputeSsrSubtreeFlags,
36
36
  serializeComponentProps,
37
+ staticLiteralOrTemplate,
37
38
  StringParts,
38
39
  } from './ssr-helpers'
39
40
  import { emitIsland } from './ssr-island'
40
41
  import { emitCatch, emitSuspense } from './ssr-prunelle-tags'
41
42
 
43
+ const FORM_CONTROL_TAGS = new Set(['input', 'select', 'textarea'])
44
+ const FORM_ATTR_BITS: Record<string, number> = {
45
+ type: 1,
46
+ required: 2,
47
+ minlength: 4,
48
+ maxlength: 8,
49
+ min: 16,
50
+ max: 32,
51
+ step: 64,
52
+ pattern: 128,
53
+ }
54
+
55
+ function findDynamicFormAction(node: JSXElement): Node | undefined {
56
+ for (const attr of node.openingElement.attributes) {
57
+ if (attr.type !== 'JSXAttribute' || jsxTagName(attr.name) !== 'action')
58
+ continue
59
+ if (attr.value?.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
60
+ return undefined
61
+ const expr = attr.value.expression
62
+ if (staticLiteralOrTemplate(expr).ok)
63
+ return undefined
64
+ if (expr.type === 'ConditionalExpression' && staticLiteralOrTemplate(expr.consequent).ok && staticLiteralOrTemplate(expr.alternate).ok)
65
+ return undefined
66
+ return expr
67
+ }
68
+ return undefined
69
+ }
70
+
71
+ function emitFormControlAttrs(
72
+ node: JSXElement,
73
+ tag: 'input' | 'select' | 'textarea',
74
+ parts: StringParts,
75
+ use: (name: string) => string,
76
+ compileSubExpression: (node: Node) => string,
77
+ ): void {
78
+ const attrs = node.openingElement.attributes
79
+ if (attrs.some(a => a.type === 'JSXSpreadAttribute'))
80
+ return
81
+ let nameExpr: string | undefined
82
+ let typeExpr: string | undefined
83
+ let present = 0
84
+ for (const attr of attrs) {
85
+ if (attr.type !== 'JSXAttribute')
86
+ continue
87
+ const attrName = jsxTagName(attr.name).toLowerCase()
88
+ const bit = FORM_ATTR_BITS[attrName]
89
+ if (bit !== undefined)
90
+ present |= bit
91
+ if (attrName === 'name') {
92
+ if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
93
+ nameExpr = JSON.stringify(attr.value.value)
94
+ }
95
+ else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
96
+ const lit = staticLiteralOrTemplate(attr.value.expression)
97
+ nameExpr = lit.ok && typeof lit.value === 'string' ? JSON.stringify(lit.value) : compileSubExpression(attr.value.expression)
98
+ }
99
+ }
100
+ else if (tag === 'input' && attrName === 'type') {
101
+ if (attr.value?.type === 'Literal' && typeof attr.value.value === 'string') {
102
+ typeExpr = JSON.stringify(attr.value.value.toLowerCase())
103
+ }
104
+ else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
105
+ const lit = staticLiteralOrTemplate(attr.value.expression)
106
+ if (lit.ok && typeof lit.value === 'string')
107
+ typeExpr = JSON.stringify(lit.value.toLowerCase())
108
+ }
109
+ }
110
+ }
111
+ if (nameExpr === undefined)
112
+ return
113
+ const typeArg = typeExpr !== undefined ? `, ${typeExpr}` : ''
114
+ parts.pushExpr(`${use('ssrFormAttrs')}(${JSON.stringify(tag)}, ${nameExpr}, ${present}${typeArg})`)
115
+ }
116
+
42
117
  export interface IslandBinding {
43
118
  id: string
44
119
  chunk: string
@@ -58,6 +133,33 @@ export interface CompileResult {
58
133
  analysis: ModuleAnalysis
59
134
  }
60
135
 
136
+ function emitActionFormExpr(
137
+ formAction: Node,
138
+ isAsyncForm: boolean,
139
+ nextId: () => number,
140
+ use: (name: string) => string,
141
+ compileSubExpression: (node: Node) => string,
142
+ emitBody: (bufVar: string, parts: StringParts, flush: () => void, pushStmt: (stmt: string) => void, override: { node: Node, ident: string }) => void,
143
+ ): string {
144
+ const faVar = `_fa${nextId()}`
145
+ const innerBuf = `_o${nextId()}`
146
+ const innerStmts: string[] = []
147
+ const innerParts = new StringParts()
148
+ const flushInner = () => {
149
+ if (!innerParts.isEmpty()) {
150
+ innerStmts.push(`${innerBuf} += ${innerParts.toExpr()};`)
151
+ innerParts.clear()
152
+ }
153
+ }
154
+ emitBody(innerBuf, innerParts, flushInner, s => innerStmts.push(s), { node: formAction, ident: faVar })
155
+ if (innerStmts.length > 0)
156
+ flushInner()
157
+ const body = innerStmts.length === 0 ? innerParts.toExpr() : `{ let ${innerBuf} = ""; ${innerStmts.join(' ')} return ${innerBuf}; }`
158
+ return isAsyncForm
159
+ ? `await ${use('ssrFormAsync')}(${compileSubExpression(formAction)}, async (${faVar}) => ${body})`
160
+ : `${use('ssrForm')}(${compileSubExpression(formAction)}, (${faVar}) => ${body})`
161
+ }
162
+
61
163
  function markerTest(markers: NonNullable<CompileSsrOptions['emitMarkers']>): (offset: number) => boolean {
62
164
  return typeof markers === 'boolean' ? () => markers : offset => markers.some(([start, end]) => offset >= start && offset < end)
63
165
  }
@@ -125,7 +227,7 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
125
227
  }
126
228
 
127
229
  // Emits JSX directly into a string accumulator variable `_out` (zero closures on nested `.map()` loops!)
128
- function emitIntoBuffer(node: JSXElement | JSXFragment, bufVar: string, parts: StringParts, flushParts: () => void, pushStmt: (stmt: string) => void): void {
230
+ function emitIntoBuffer(node: JSXElement | JSXFragment, bufVar: string, parts: StringParts, flushParts: () => void, pushStmt: (stmt: string) => void, actionOverride?: { node: Node, ident: string }): void {
129
231
  if (node.type === 'JSXFragment') {
130
232
  emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, undefined, markersAt(node.start), true)
131
233
  return
@@ -137,21 +239,21 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
137
239
  return
138
240
  }
139
241
 
242
+ if (tag === 'form' && actionOverride === undefined) {
243
+ const formAction = findDynamicFormAction(node)
244
+ if (formAction !== undefined) {
245
+ parts.pushExpr(emitActionFormExpr(formAction, directAwaitNodes.has(node), () => loopCounter++, use, compileSubExpression, (b, p, f, ps, ov) => emitIntoBuffer(node, b, p, f, ps, ov)))
246
+ return
247
+ }
248
+ }
249
+
140
250
  parts.pushStatic(`<${tag}`)
141
251
  const isTextarea = tag === 'textarea'
142
252
  const isSelect = tag === 'select'
143
- const { innerHtmlExpr, textareaValue, nextSelectValue } = emitSsrElementAttrs({
144
- node,
145
- tag,
146
- parts,
147
- activeSelectValue,
148
- flushParts,
149
- pushStmt,
150
- nextId: () => loopCounter++,
151
- use,
152
- compileSubExpression,
153
- })
154
-
253
+ const compileAttrExpr = actionOverride !== undefined ? (n: Node) => (n === actionOverride.node ? actionOverride.ident : compileSubExpression(n)) : compileSubExpression
254
+ const { innerHtmlExpr, textareaValue, nextSelectValue } = emitSsrElementAttrs({ node, tag, parts, activeSelectValue, flushParts, pushStmt, nextId: () => loopCounter++, use, compileSubExpression: compileAttrExpr })
255
+ if (FORM_CONTROL_TAGS.has(tag))
256
+ emitFormControlAttrs(node, tag as 'input' | 'select' | 'textarea', parts, use, compileSubExpression)
155
257
  parts.pushStatic('>')
156
258
  if (VOID_ELEMENTS.has(tag))
157
259
  return
@@ -161,28 +263,23 @@ export function compileSsr(filename: string, code: string, options: CompileSsrOp
161
263
  activeSelectValue = nextSelectValue
162
264
 
163
265
  try {
164
- if (innerHtmlExpr !== undefined) {
266
+ if (innerHtmlExpr !== undefined)
165
267
  parts.pushExpr(innerHtmlExpr)
166
- }
167
- else if (isTextarea && textareaValue !== undefined && filterMeaningfulChildren(node.children).length === 0) {
168
- if (textareaValue.staticText !== undefined)
169
- parts.pushStatic(escapeStaticText(textareaValue.staticText))
170
- else if (textareaValue.expr !== undefined)
171
- parts.pushExpr(`${use('esc')}(${textareaValue.expr})`)
172
- }
268
+ else if (isTextarea && textareaValue !== undefined && filterMeaningfulChildren(node.children).length === 0)
269
+ textareaValue.staticText !== undefined ? parts.pushStatic(escapeStaticText(textareaValue.staticText)) : textareaValue.expr !== undefined && parts.pushExpr(`${use('esc')}(${textareaValue.expr})`)
173
270
  else if (tag === 'style') {
174
271
  const styleExpr = compileChildrenListToStringExpr(node.children, false, false, 'style')
175
272
  if (styleExpr !== '""')
176
273
  parts.pushExpr(`${use('ssrStyleBlock')}(${styleExpr})`)
177
274
  }
178
- else {
275
+ else
179
276
  emitChildrenIntoBuffer(node.children, bufVar, parts, flushParts, pushStmt, RAW_TEXT_ELEMENTS.has(tag) ? tag as 'style' | 'script' : undefined)
180
- }
181
277
  }
182
278
  finally {
183
279
  activeSelectValue = prevSelectValue
184
280
  }
185
- parts.pushStatic(`</${tag}>`)
281
+ if (actionOverride === undefined)
282
+ parts.pushStatic(`</${tag}>`)
186
283
  }
187
284
 
188
285
  function emitChildrenIntoBuffer(
package/src/dom/action.ts CHANGED
@@ -88,7 +88,10 @@ export function useAction<Input, Output>(
88
88
  fieldErrors,
89
89
  data,
90
90
  get action() {
91
- return action.url || String(action)
91
+ const u = action.url || String(action)
92
+ if ('__pruAction' in action)
93
+ (globalThis as { __pruAct?: (url: string, act: unknown) => void }).__pruAct?.(u, action)
94
+ return u
92
95
  },
93
96
  onSubmit(event) {
94
97
  event.preventDefault()
package/src/dom/island.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  // Native <pru-island> Custom Element: the build defines which strategies an app uses, so the loader drops the code of the others.
2
2
 
3
- import { hydrate, mount } from './index'
4
-
3
+ import type { hydrate, mount } from './index'
5
4
 
6
5
  declare const PRUNELLE_VISIBLE: boolean | undefined
7
6
  declare const PRUNELLE_IDLE: boolean | undefined
@@ -14,7 +13,8 @@ declare const PRUNELLE_REVIVE: string | undefined
14
13
  // Only props holding a Date, Map, Set or another tagged value load the revivers: plain JSON parses with no extra file.
15
14
  const reviver = (): Promise<typeof import('./revive')> => typeof PRUNELLE_REVIVE === 'undefined' ? import('./revive') : import(PRUNELLE_REVIVE)
16
15
 
17
- type IslandModule = { default: (props: Record<string, unknown>) => Node | DocumentFragment | null }
16
+ // An island chunk also re-exports the DOM runtime it imports (`writeClientEntries`), `mount` only in an app with a client-only island.
17
+ type IslandModule = { default: (props: Record<string, unknown>) => Node | DocumentFragment | null, hydrate: typeof hydrate, mount?: typeof mount }
18
18
 
19
19
  /** Finds this island's own `<script type="application/json" data-pru-props>` direct child. */
20
20
  function findOwnPropsScript(root: Element): Element | null {
@@ -262,8 +262,8 @@ export function defineIslandElement(): void {
262
262
  const idPrefix = this.getAttribute('i') ?? `c${clientIslandIdSeq++}-`
263
263
  const render = () => loaded.default(props)
264
264
  dispose = (typeof PRUNELLE_CLIENT_ONLY === 'undefined' || PRUNELLE_CLIENT_ONLY) && clientOnly
265
- ? mount(this, render, idPrefix)
266
- : hydrate(this, render, idPrefix)
265
+ ? loaded.mount!(this, render, idPrefix)
266
+ : loaded.hydrate(this, render, idPrefix)
267
267
  if (!this.isConnected || token !== this._t) {
268
268
  dispose()
269
269
  return
@@ -15,7 +15,7 @@ export function pickRuntimeConfig(config: PrunelleConfig | undefined): ServerRun
15
15
  }
16
16
 
17
17
  // Typed as records so that a key added to the config types without being listed here fails the typecheck.
18
- const RUNTIME_KEYS: Record<keyof ServerRuntimeConfig, true> = { site: true, lang: true, assetPrefix: true, security: true, trustedOrigins: true, trustProxy: true, maxRequestBodySize: true, navigation: true, streamTimeout: true, consent: true, compress: true, onError: true, plugins: true }
18
+ const RUNTIME_KEYS: Record<keyof ServerRuntimeConfig, true> = { site: true, lang: true, assetPrefix: true, security: true, trustedOrigins: true, trustProxy: true, maxRequestBodySize: true, navigation: true, streamTimeout: true, consent: true, compress: true, healthCheck: true, onError: true, plugins: true }
19
19
  const BUILD_KEYS: Record<Exclude<keyof PrunelleConfig, keyof ServerRuntimeConfig>, true> = { port: true, routesDir: true, outDir: true, publicDir: true, compliance: true }
20
20
  const RUNTIME_CONFIG_KEYS = Object.keys(RUNTIME_KEYS) as (keyof ServerRuntimeConfig)[]
21
21
  const CONFIG_KEYS = [...RUNTIME_CONFIG_KEYS, ...Object.keys(BUILD_KEYS)]