prunelle 0.7.3 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/llms.txt +11 -9
  2. package/package.json +1 -1
  3. package/src/a11y/index.ts +9 -2
  4. package/src/build/index.ts +29 -30
  5. package/src/build/prerender.ts +29 -13
  6. package/src/build/routes.ts +19 -1
  7. package/src/build/static.ts +24 -2
  8. package/src/cli/check-forms.ts +16 -6
  9. package/src/cli/check-links.ts +12 -9
  10. package/src/cli/check-security.ts +87 -12
  11. package/src/cli/check.ts +1 -1
  12. package/src/cli/commands.ts +3 -3
  13. package/src/cli/upgrade.ts +36 -3
  14. package/src/compiler/compliance.ts +2 -0
  15. package/src/compiler/dom.ts +41 -15
  16. package/src/compiler/ssr-helpers.ts +45 -21
  17. package/src/compiler/ssr.ts +32 -16
  18. package/src/context.ts +2 -0
  19. package/src/dev/error-log.ts +1 -1
  20. package/src/dev/index.ts +1 -1
  21. package/src/dev/supervisor.ts +2 -2
  22. package/src/dom/attrs.ts +1 -1
  23. package/src/dom/island.ts +30 -4
  24. package/src/jsx-runtime.ts +1 -1
  25. package/src/plugins/services.ts +1 -0
  26. package/src/reactivity/index.ts +14 -2
  27. package/src/server/action.ts +26 -18
  28. package/src/server/assets.ts +22 -4
  29. package/src/server/consent.ts +7 -2
  30. package/src/server/cookies.ts +8 -1
  31. package/src/server/errors.ts +57 -26
  32. package/src/server/http.ts +1 -1
  33. package/src/server/index.ts +7 -7
  34. package/src/server/page.ts +4 -0
  35. package/src/server/rate-limit.ts +36 -12
  36. package/src/server/route-cache.ts +9 -10
  37. package/src/server/route-table.ts +38 -13
  38. package/src/server/router.ts +4 -4
  39. package/src/server/sanitize-html.ts +21 -7
  40. package/src/server/security.ts +13 -3
  41. package/src/server/serve-handlers.ts +3 -3
  42. package/src/server/start.ts +2 -2
  43. package/src/server/static-dir.ts +3 -10
  44. package/src/server/stream.ts +4 -4
  45. package/src/ssr/consent-banner.ts +3 -1
  46. package/src/ssr/context.ts +85 -2
  47. package/src/ssr/index.ts +16 -4
  48. package/src/ssr/navigation.ts +1 -1
  49. package/src/style.ts +9 -7
  50. package/src/unrenderable.ts +15 -5
package/src/ssr/index.ts CHANGED
@@ -10,7 +10,7 @@ import { extractFormSchema, lookupFieldConstraint } from '../server/form-schema'
10
10
  import { styleText } from '../style'
11
11
  import { assertRenderable } from '../unrenderable'
12
12
  import { currentFormFields, currentRenderContext, lookupFormAction, PRUNELLE_STREAM_SCRIPT, renderIsland, runWithFormFields } from './context'
13
- import { keepScrollTag } from './navigation'
13
+ import { KEEP_SCROLL_TAG, keepScrollTag } from './navigation'
14
14
  export { ssrStyleBlock } from './context'
15
15
 
16
16
  export const PRUNELLE_ISLAND_STYLE = 'pru-island,pru-slot{display:contents}'
@@ -122,8 +122,19 @@ export function raw(html: string): Html {
122
122
  return { __html: html, [RAW_HTML]: true } as unknown as Html
123
123
  }
124
124
 
125
+ /** HTML strips one leading newline after `<textarea>` and `<pre>`; double it so the parsed text keeps it. */
126
+ export function ssrLeadNl(html: string): string {
127
+ return html.startsWith('\n') || html.startsWith('\r') ? `\n${html}` : html
128
+ }
129
+
125
130
  export function ssrAttr(name: string, value: unknown, tag?: string): string {
126
- if (value == null || isBlockedAttr(name))
131
+ if (value == null || isBlockedAttr(name) || name === 'indeterminate')
132
+ return ''
133
+ if (name === 'checked' || name === 'selected' || name === 'muted') {
134
+ assertRenderable(value, name)
135
+ return value ? ` ${name}` : ''
136
+ }
137
+ if (name === 'value' && typeof value === 'boolean')
127
138
  return ''
128
139
  if (typeof value === 'boolean') {
129
140
  if (isStringBoolAttr(name))
@@ -165,7 +176,7 @@ export function ssrSpread(props: Record<string, unknown> | null | undefined, tag
165
176
  if (rawKey === 'dangerouslySetInnerHTML')
166
177
  throw new TypeError(SPREAD_DANGEROUS_HTML)
167
178
  const name = rawKey === 'className' ? 'class' : rawKey === 'htmlFor' ? 'for' : rawKey
168
- if (isBlockedAttr(name))
179
+ if (isBlockedAttr(name) || ((tag === 'textarea' || tag === 'select') && name === 'value'))
169
180
  continue
170
181
  if (trackForm && value !== undefined) {
171
182
  const bit = FORM_ATTR_BITS[name.toLowerCase()]
@@ -620,5 +631,6 @@ export function renderDocument(options: DocumentOptions): string {
620
631
  }
621
632
  }
622
633
 
623
- return `<!DOCTYPE html><html${attrStr}><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">${headExtra}</head><body>${body}${keepScrollTag(body)}</body></html>`
634
+ const scrollTag = keepScrollTag(body) || (hasSuspense ? KEEP_SCROLL_TAG : '')
635
+ return `<!DOCTYPE html><html${attrStr}><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">${headExtra}</head><body>${body}${scrollTag}</body></html>`
624
636
  }
@@ -8,7 +8,7 @@ export const NO_PREFETCH_ATTR = 'data-pru-no-prefetch'
8
8
  export type Eagerness = 'conservative' | 'moderate' | 'eager'
9
9
 
10
10
  export interface NavigationConfig {
11
- /** Fades one page into the next with a cross-document view transition (`@view-transition { navigation: auto }`), off under `prefers-reduced-motion`. On by default, `false` leaves the browser's plain repaint. */
11
+ /** Fades one page into the next with a cross-document view transition (`@view-transition { navigation: auto }`), off under `prefers-reduced-motion`. On by default, `false` leaves the browser's plain repaint, for `@prunellejs/spa` swaps too. */
12
12
  viewTransitions?: boolean | undefined
13
13
  /** When the browser fetches the page a same-origin link points at, through Speculation Rules: `'conservative'` (the default) on press, `'moderate'` on hover (and, on phones, as a link scrolls into view), `'eager'` as soon as it can, `false` never. A link, or an ancestor of it, with `data-pru-no-prefetch` is never fetched; neither is a `download` or `rel="nofollow"` link, a prunelle route (actions among them) or another origin's page. */
14
14
  prefetch?: Eagerness | false | undefined
package/src/style.ts CHANGED
@@ -3,14 +3,16 @@
3
3
  // Properties that take a bare number; any other number gets `px`.
4
4
  const UNITLESS = new Set([
5
5
  'animation-iteration-count', 'aspect-ratio', 'border-image-outset', 'border-image-slice', 'border-image-width',
6
- 'column-count', 'columns', 'fill-opacity', 'flex', 'flex-grow', 'flex-shrink', 'flood-opacity', 'font-weight',
7
- 'grid-area', 'grid-column', 'grid-column-end', 'grid-column-start', 'grid-row', 'grid-row-end', 'grid-row-start',
8
- 'line-clamp', '-webkit-line-clamp', 'line-height', 'opacity', 'order', 'orphans', 'scale', 'stop-opacity',
9
- 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'tab-size',
10
- 'widows', 'z-index', 'zoom',
6
+ 'box-flex', 'box-flex-group', 'box-ordinal-group', 'column-count', 'columns', 'fill-opacity', 'flex',
7
+ 'flex-grow', 'flex-negative', 'flex-order', 'flex-positive', 'flex-shrink', 'flood-opacity', 'font-weight',
8
+ 'grid-area', 'grid-column', 'grid-column-end', 'grid-column-span', 'grid-column-start', 'grid-row',
9
+ 'grid-row-end', 'grid-row-span', 'grid-row-start', 'line-clamp', 'line-height', 'opacity', 'order', 'orphans',
10
+ 'scale', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity',
11
+ 'stroke-width', 'tab-size', 'widows', 'z-index', 'zoom',
11
12
  ])
12
13
  const UPPER = /[A-Z]/g
13
14
  const MS_PREFIX = /^ms[A-Z]/
15
+ const VENDOR = /^-(?:webkit|moz|ms|o)-/
14
16
 
15
17
  // `backgroundColor` becomes `background-color` and `msTransform` `-ms-transform`; a custom property keeps its case.
16
18
  function cssName(key: string): string {
@@ -20,7 +22,7 @@ function cssName(key: string): string {
20
22
  return MS_PREFIX.test(key) ? `-${name}` : name
21
23
  }
22
24
 
23
- const PROPERTY_NAME = /^-{0,2}[a-z][\w-]*$/i
25
+ const PROPERTY_NAME = /^(?:--[\w-\u00A0-\uFFFF]+|-?[a-z][\w-]*)$/i
24
26
 
25
27
  const NL = /[\n\r\f]/
26
28
  const URL_FN = /(?:^|[^\w-])url$/i
@@ -89,7 +91,7 @@ export function styleText(style: Record<string, unknown>): string {
89
91
  if (typeof value === 'number' ? !Number.isFinite(value) : !isOneValue(String(value)))
90
92
  continue
91
93
  const name = cssName(key)
92
- const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name)
94
+ const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name.replace(VENDOR, ''))
93
95
  out += `${name}:${value}${px ? 'px' : ''};`
94
96
  }
95
97
  return out
@@ -6,7 +6,7 @@ function hasOwnToString(value: object): boolean {
6
6
  return typeof toString === 'function' && toString !== Function.prototype.toString && toString !== Object.prototype.toString && toString !== Array.prototype.toString
7
7
  }
8
8
 
9
- function describe(value: unknown, asClass: boolean): [string, string] | undefined {
9
+ function describe(value: unknown, place: string): [string, string] | undefined {
10
10
  if (typeof value === 'function')
11
11
  return hasOwnToString(value) ? undefined : ['a function', 'call it, or pass it to an event prop']
12
12
  if (typeof value !== 'object' || value === null)
@@ -17,19 +17,29 @@ function describe(value: unknown, asClass: boolean): [string, string] | undefine
17
17
  return ['a signal', 'read its .value']
18
18
  if ('__html' in value)
19
19
  return ['a { __html } object', 'pass the string to innerHTML, or wrap trusted HTML in raw()']
20
- if (asClass && Array.isArray(value))
21
- return ['an array', 'join the names into one string']
20
+ if (Array.isArray(value))
21
+ return place === 'class' ? ['an array', 'join the names into one string'] : undefined
22
+ if (value instanceof Date)
23
+ return ['a Date', 'format it with .toISOString() or Intl.DateTimeFormat']
24
+ if (value instanceof Set)
25
+ return ['a Set', 'spread it with [...set] and render each item']
26
+ if (value instanceof Map)
27
+ return ['a Map', 'iterate its entries and render each item']
22
28
  if (hasOwnToString(value))
23
29
  return undefined
30
+ if (place === 'text' && typeof Node !== 'undefined' && value instanceof Node)
31
+ return undefined
24
32
  const proto = Object.getPrototypeOf(value)
25
33
  if (proto === Object.prototype || proto === null)
26
34
  return ['a plain object', 'render one of its fields']
27
- return undefined
35
+ const name = (proto as { constructor?: { name?: unknown } })?.constructor?.name
36
+ const label = typeof name === 'string' && name !== '' ? `a ${name} instance` : 'an object'
37
+ return [label, 'render one of its fields or define toString()']
28
38
  }
29
39
 
30
40
  /** Throws when `value` would print as `[object Object]`, `[object Promise]` or a function's source; `place` is `text` or an attribute name. */
31
41
  export function assertRenderable(value: unknown, place: string): void {
32
- const found = describe(value, place === 'class')
42
+ const found = describe(value, place)
33
43
  if (found !== undefined)
34
44
  throw new TypeError(`prunelle: cannot render ${found[0]} as ${place === 'text' ? 'text' : `the ${place} attribute`}; ${found[1]}.`)
35
45
  }