prunelle 0.0.0-stage → 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 +148 -0
- package/package.json +53 -4
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +613 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +169 -0
- package/src/build/client-prune.ts +118 -0
- package/src/build/compile.ts +128 -0
- package/src/build/files.ts +156 -0
- package/src/build/first-load.ts +164 -0
- package/src/build/graph.ts +247 -0
- package/src/build/index.ts +614 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/islands.ts +641 -0
- package/src/build/manifest.ts +281 -0
- package/src/build/output.ts +84 -0
- package/src/build/plugin-host.ts +220 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +150 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +247 -0
- package/src/build/resolve.ts +50 -0
- package/src/build/routes.ts +338 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static.ts +163 -0
- 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 +367 -0
- package/src/cli/commands.ts +265 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +335 -0
- package/src/cli/new.ts +41 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +354 -0
- package/src/compiler/analyze.ts +618 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +127 -0
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +497 -0
- package/src/compiler/data-keys.ts +295 -0
- package/src/compiler/dom-attrs.ts +379 -0
- package/src/compiler/dom.ts +566 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +408 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +499 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props.ts +460 -0
- package/src/compiler/scope.ts +568 -0
- package/src/compiler/server-only.ts +326 -0
- package/src/compiler/shared.ts +369 -0
- package/src/compiler/signal-reads.ts +242 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +56 -0
- package/src/compiler/ssr.ts +491 -0
- package/src/context.ts +165 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/index.ts +208 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/protocol.ts +21 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +157 -0
- package/src/dev/supervisor.ts +187 -0
- package/src/dev/watchers.ts +215 -0
- package/src/dom/action.ts +164 -0
- package/src/dom/attrs.ts +72 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +562 -0
- package/src/dom/index.ts +289 -0
- package/src/dom/island.ts +284 -0
- package/src/dom/loop.ts +450 -0
- package/src/dom/revive.ts +36 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +86 -0
- package/src/plugins/context.ts +66 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +70 -0
- package/src/plugins/order.ts +111 -0
- package/src/plugins/public.ts +12 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +35 -0
- package/src/plugins/types.ts +242 -0
- package/src/reactivity/graph.ts +249 -0
- package/src/reactivity/index.ts +533 -0
- package/src/reactivity/selector.ts +51 -0
- package/src/reactivity/store.ts +208 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +498 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/assets.ts +136 -0
- package/src/server/compress.ts +560 -0
- package/src/server/consent.ts +253 -0
- package/src/server/cookies.ts +139 -0
- package/src/server/document.ts +271 -0
- package/src/server/env.ts +33 -0
- package/src/server/errors.ts +382 -0
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +508 -0
- package/src/server/index.ts +687 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +118 -0
- package/src/server/page.ts +107 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +256 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +19 -0
- package/src/server/rate-limit.ts +133 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-table.ts +82 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +377 -0
- package/src/server/security.ts +472 -0
- package/src/server/serve-handlers.ts +83 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +87 -0
- package/src/server/start.ts +99 -0
- package/src/server/static-dir.ts +167 -0
- package/src/server/stream.ts +192 -0
- package/src/ssr/context.ts +350 -0
- package/src/ssr/index.ts +596 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +171 -0
- package/src/style.ts +33 -0
- package/src/test/browser.ts +192 -0
- package/src/test/index.ts +191 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +3 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +13 -0
- package/templates/_shared/tsconfig.json +20 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +21 -0
- package/templates/app/prunelle.config.ts +11 -0
- package/templates/app/src/auth.ts +14 -0
- package/templates/app/src/routes/_layout.tsx +32 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +13 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +34 -0
- package/templates/app/src/styles/app.css +7 -0
- package/templates/app/tests/app.test.ts +32 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +20 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +10 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +27 -0
- package/templates/fullstack/src/routes/_layout.tsx +14 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +29 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +14 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +22 -0
- package/README.md +0 -3
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Tiny client-side action stub (~300 B gzip, 0 B when no island imports an action).
|
|
2
|
+
|
|
3
|
+
import type { ReadonlySignal } from '../reactivity/index'
|
|
4
|
+
import type { ActionResult, ServerAction } from '../server/action'
|
|
5
|
+
import { batch, signal } from '../reactivity/index'
|
|
6
|
+
|
|
7
|
+
export interface ClientAction<Input, Output> {
|
|
8
|
+
/** A FormData input is sent as multipart, so it can carry files. */
|
|
9
|
+
(input: Input | FormData): Promise<ActionResult<Output>>
|
|
10
|
+
readonly url: string
|
|
11
|
+
toString(): string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UseActionOptions<Output> {
|
|
15
|
+
/** Reset the submitted `<form>` element after a successful submission (default: false). */
|
|
16
|
+
reset?: boolean | undefined
|
|
17
|
+
/** Called with the typed action `data` when the action succeeds (`res.ok === true`). */
|
|
18
|
+
onSuccess?: ((data: Output, form?: HTMLFormElement) => void | Promise<void>) | undefined
|
|
19
|
+
/** Called when the action fails (`res.ok === false`). */
|
|
20
|
+
onError?: ((result: Extract<ActionResult<Output>, { ok: false }>) => void) | undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ActionForm<Input, Output> {
|
|
24
|
+
readonly pending: ReadonlySignal<boolean>
|
|
25
|
+
readonly error: ReadonlySignal<string>
|
|
26
|
+
readonly fieldErrors: ReadonlySignal<Record<string, string>>
|
|
27
|
+
readonly data: ReadonlySignal<Output | undefined>
|
|
28
|
+
/** Action URL for progressive `<form action={form.action} method="post">` (or pass the action directly). */
|
|
29
|
+
readonly action: string
|
|
30
|
+
/** Progressive `<form onSubmit={form.onSubmit}>` handler that submits `FormData` via fetch when JS is loaded. */
|
|
31
|
+
readonly onSubmit: (event: SubmitEvent & { currentTarget: HTMLFormElement }) => Promise<ActionResult<Output> | undefined>
|
|
32
|
+
/** Imperatively submit typed input or `FormData` while tracking `pending`, `error`, `fieldErrors`, and `data`. */
|
|
33
|
+
submit(input: Input | FormData, form?: HTMLFormElement): Promise<ActionResult<Output> | undefined>
|
|
34
|
+
/** Clears `error`, `fieldErrors`, and `data`. */
|
|
35
|
+
reset(): void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const errMsg = (e: unknown): string => e instanceof Error ? e.message : String(e)
|
|
39
|
+
const fail = (status: number, error: string) => ({ ok: false as const, status, error })
|
|
40
|
+
|
|
41
|
+
/** Reactive progressive-enhancement form controller for a Prunelle server `action()`. */
|
|
42
|
+
export function useAction<Input, Output>(
|
|
43
|
+
action: ClientAction<Input, Output> | ServerAction<Input, Output>,
|
|
44
|
+
options: UseActionOptions<Output> = {},
|
|
45
|
+
): ActionForm<Input, Output> {
|
|
46
|
+
const pending = signal(false)
|
|
47
|
+
const error = signal('')
|
|
48
|
+
const fieldErrors = signal<Record<string, string>>({})
|
|
49
|
+
const data = signal<Output | undefined>(undefined)
|
|
50
|
+
|
|
51
|
+
const set = (p: boolean, e = '', f: Record<string, string> = {}) => batch(() => {
|
|
52
|
+
pending.value = p
|
|
53
|
+
error.value = e
|
|
54
|
+
fieldErrors.value = f
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const submit = async (input: Input | FormData, formEl?: HTMLFormElement): Promise<ActionResult<Output> | undefined> => {
|
|
58
|
+
if (pending.peek())
|
|
59
|
+
return undefined
|
|
60
|
+
set(true)
|
|
61
|
+
try {
|
|
62
|
+
const res = await (action as ClientAction<Input, Output>)(input)
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
set(false, res.error, res.fieldErrors)
|
|
65
|
+
options.onError?.(res)
|
|
66
|
+
return res
|
|
67
|
+
}
|
|
68
|
+
batch(() => {
|
|
69
|
+
data.value = res.data
|
|
70
|
+
pending.value = false
|
|
71
|
+
})
|
|
72
|
+
if (options.reset && formEl)
|
|
73
|
+
formEl.reset()
|
|
74
|
+
await options.onSuccess?.(res.data, formEl)
|
|
75
|
+
return res
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
const failed = fail(0, errMsg(err))
|
|
79
|
+
set(false, failed.error)
|
|
80
|
+
options.onError?.(failed)
|
|
81
|
+
return failed
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
pending,
|
|
87
|
+
error,
|
|
88
|
+
fieldErrors,
|
|
89
|
+
data,
|
|
90
|
+
get 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
|
|
95
|
+
},
|
|
96
|
+
onSubmit(event) {
|
|
97
|
+
event.preventDefault()
|
|
98
|
+
const formEl = event.currentTarget
|
|
99
|
+
return submit(new FormData(formEl, event.submitter), formEl)
|
|
100
|
+
},
|
|
101
|
+
submit,
|
|
102
|
+
reset() {
|
|
103
|
+
batch(() => {
|
|
104
|
+
set(pending.peek())
|
|
105
|
+
data.value = undefined
|
|
106
|
+
})
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function actionStub<Input = unknown, Output = unknown>(url: string): ClientAction<Input, Output> {
|
|
112
|
+
const call = (async (input: Input | FormData): Promise<ActionResult<Output>> => {
|
|
113
|
+
const isForm = typeof FormData !== 'undefined' && input instanceof FormData
|
|
114
|
+
const headers: Record<string, string> = { 'accept': 'application/json', 'x-pru-action': '1' }
|
|
115
|
+
if (!isForm)
|
|
116
|
+
headers['content-type'] = 'application/json'
|
|
117
|
+
let res: Response
|
|
118
|
+
try {
|
|
119
|
+
res = await fetch(url, {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
headers,
|
|
122
|
+
body: isForm ? input : input === undefined ? '{}' : JSON.stringify(input),
|
|
123
|
+
redirect: 'manual',
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
return fail(0, errMsg(err))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (res.type === 'opaqueredirect')
|
|
131
|
+
return fail(0, 'Unexpected redirect')
|
|
132
|
+
|
|
133
|
+
const h = res.headers
|
|
134
|
+
const to = h.get('x-pru-redirect') || (res.status > 299 && res.status < 400 && h.get('location')) || (res.redirected && res.url)
|
|
135
|
+
if (to) {
|
|
136
|
+
if (typeof location !== 'undefined') {
|
|
137
|
+
location.assign(to)
|
|
138
|
+
return new Promise<never>(() => {})
|
|
139
|
+
}
|
|
140
|
+
return fail(res.status || 302, to)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const b = await res.json() as Record<string, unknown> | null
|
|
145
|
+
if (b && typeof b === 'object') {
|
|
146
|
+
if (b.ok === true)
|
|
147
|
+
return b as unknown as ActionResult<Output>
|
|
148
|
+
if (b.ok === false && typeof b.error === 'string')
|
|
149
|
+
return { ...b, status: typeof b.status === 'number' ? b.status : res.status } as ActionResult<Output>
|
|
150
|
+
const m = typeof b.error === 'string' ? b.error : typeof b.message === 'string' ? b.message : ''
|
|
151
|
+
if (m)
|
|
152
|
+
return fail(res.status, m)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch {}
|
|
156
|
+
return fail(res.status, res.statusText || `HTTP ${res.status}`)
|
|
157
|
+
}) as unknown as ClientAction<Input, Output>
|
|
158
|
+
|
|
159
|
+
Object.defineProperties(call, {
|
|
160
|
+
url: { value: url, enumerable: true },
|
|
161
|
+
toString: { value: () => url, enumerable: false },
|
|
162
|
+
})
|
|
163
|
+
return call
|
|
164
|
+
}
|
package/src/dom/attrs.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Attribute, class, property and style bindings, in a module of their own (`dom-attrs`): only the islands that bind one load it.
|
|
2
|
+
|
|
3
|
+
import { isBlockedAttr, isStringBoolAttr, isUnsafeUrlAttr } from '../attrs'
|
|
4
|
+
import { styleText } from '../style'
|
|
5
|
+
import { assertRenderable } from '../unrenderable'
|
|
6
|
+
|
|
7
|
+
export function setClass(el: Element, value: unknown): void {
|
|
8
|
+
if (process.env.NODE_ENV !== 'production')
|
|
9
|
+
assertRenderable(value, 'class')
|
|
10
|
+
const str = value == null || value === false ? '' : String(value)
|
|
11
|
+
if (typeof el.className === 'string') {
|
|
12
|
+
if (el.className !== str) {
|
|
13
|
+
if (str === '')
|
|
14
|
+
el.removeAttribute('class')
|
|
15
|
+
else
|
|
16
|
+
el.className = str
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
else if (str === '') {
|
|
20
|
+
el.removeAttribute('class')
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
el.setAttribute('class', str)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function setAttr(el: Element, name: string, value: unknown): void {
|
|
28
|
+
if (isBlockedAttr(name))
|
|
29
|
+
return
|
|
30
|
+
if (process.env.NODE_ENV !== 'production')
|
|
31
|
+
assertRenderable(value, name)
|
|
32
|
+
if (value == null) {
|
|
33
|
+
el.removeAttribute(name)
|
|
34
|
+
}
|
|
35
|
+
else if (typeof value === 'boolean') {
|
|
36
|
+
if (isStringBoolAttr(name))
|
|
37
|
+
el.setAttribute(name, value ? 'true' : 'false')
|
|
38
|
+
else if (value)
|
|
39
|
+
el.setAttribute(name, '')
|
|
40
|
+
else
|
|
41
|
+
el.removeAttribute(name)
|
|
42
|
+
}
|
|
43
|
+
else if (isUnsafeUrlAttr(name, value, el.tagName)) {
|
|
44
|
+
if (process.env.NODE_ENV !== 'production')
|
|
45
|
+
console.warn(`prunelle: dropped unsafe URL on <${el.tagName.toLowerCase()} ${name}>.`)
|
|
46
|
+
el.removeAttribute(name)
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
el.setAttribute(name, String(value))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function setProp(el: Element, name: string, value: unknown): void {
|
|
54
|
+
const next = name === 'value'
|
|
55
|
+
? (value == null ? '' : String(value))
|
|
56
|
+
: Boolean(value)
|
|
57
|
+
const rec = el as unknown as Record<string, unknown>
|
|
58
|
+
if (rec[name] !== next)
|
|
59
|
+
rec[name] = next
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function setStyle(el: HTMLElement, value: unknown): void {
|
|
63
|
+
if (value == null || value === false) {
|
|
64
|
+
el.removeAttribute('style')
|
|
65
|
+
}
|
|
66
|
+
else if (typeof value === 'string') {
|
|
67
|
+
el.style.cssText = value
|
|
68
|
+
}
|
|
69
|
+
else if (typeof value === 'object') {
|
|
70
|
+
el.style.cssText = styleText(value as Record<string, unknown>)
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/dom/claim.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Claiming the SSR nodes of a list or an insert while hydrating (`dom-claim`): only the islands that render one load it.
|
|
2
|
+
|
|
3
|
+
import { hydrateCursor, isHydrating, isRangeEnd, moveCursor, skipRange } from './index'
|
|
4
|
+
|
|
5
|
+
export function claimLoop(parent: Node, fn: () => void, skip = 0, startMarker?: Node | null): boolean {
|
|
6
|
+
if (!isHydrating())
|
|
7
|
+
return false
|
|
8
|
+
let cur: Node | null
|
|
9
|
+
if (startMarker !== undefined && startMarker !== null) {
|
|
10
|
+
cur = startMarker.nextSibling
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
cur = parent.firstChild
|
|
14
|
+
for (let i = 0; i < skip && cur !== null; i++) {
|
|
15
|
+
cur = cur.nextSibling
|
|
16
|
+
while (cur !== null && (cur as unknown as { __pruAnchor?: boolean }).__pruAnchor === true)
|
|
17
|
+
cur = cur.nextSibling
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const prev = cur === null || isRangeEnd(cur) ? moveCursor(false, null) : moveCursor(true, cur)
|
|
21
|
+
try {
|
|
22
|
+
fn()
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
moveCursor(...prev)
|
|
26
|
+
}
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The siblings from `from` up to, not including, `to`; undefined when there are none.
|
|
31
|
+
function between(from: Node | null, to: Node | null): Node[] | undefined {
|
|
32
|
+
const nodes: Node[] = []
|
|
33
|
+
for (let n = from; n !== null && n !== to; n = n.nextSibling)
|
|
34
|
+
nodes.push(n)
|
|
35
|
+
return nodes.length > 0 ? nodes : undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// `nodes` is the SSR range the render claimed through template() (a fragment hydrates to null, so the caller learns its nodes here).
|
|
39
|
+
export function claimInsert<T>(
|
|
40
|
+
parent: Node,
|
|
41
|
+
marker: Node | null,
|
|
42
|
+
fn: () => T,
|
|
43
|
+
startMarker?: Node | null,
|
|
44
|
+
): { result: T, marker: Node | null, claimed: Node | null, nodes: Node[] | undefined, consumed: boolean } {
|
|
45
|
+
if (!isHydrating())
|
|
46
|
+
return { result: fn(), marker, claimed: null, nodes: undefined, consumed: false }
|
|
47
|
+
|
|
48
|
+
let claimed: Node | null
|
|
49
|
+
if (startMarker !== undefined) {
|
|
50
|
+
const first = startMarker === null ? parent.firstChild : startMarker.nextSibling
|
|
51
|
+
claimed = first === marker || (first !== null && isRangeEnd(first)) ? null : first
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
claimed = marker === null ? parent.lastChild : marker.nodeType === 8 ? null : marker
|
|
55
|
+
}
|
|
56
|
+
const prev = claimed === null ? moveCursor(false, null) : moveCursor(true, claimed)
|
|
57
|
+
let result: T
|
|
58
|
+
let end: Node | null
|
|
59
|
+
try {
|
|
60
|
+
result = fn()
|
|
61
|
+
end = hydrateCursor()
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
moveCursor(...prev)
|
|
65
|
+
}
|
|
66
|
+
const consumed = claimed !== null && end !== claimed
|
|
67
|
+
if (startMarker !== undefined) {
|
|
68
|
+
const firstNow = startMarker === null ? parent.firstChild : startMarker.nextSibling
|
|
69
|
+
return { result, marker, claimed, nodes: between(firstNow, marker), consumed }
|
|
70
|
+
}
|
|
71
|
+
if (marker === null) {
|
|
72
|
+
const from = claimed !== null && claimed.parentNode === parent ? claimed : parent.lastChild
|
|
73
|
+
return { result, marker: null, claimed, nodes: claimed !== null ? between(from, end) : undefined, consumed }
|
|
74
|
+
}
|
|
75
|
+
if (claimed === null)
|
|
76
|
+
return { result, marker, claimed, nodes: undefined, consumed: false }
|
|
77
|
+
|
|
78
|
+
// The anchor goes after the claimed range; a text value or a nested insert moves no cursor and claims the one node.
|
|
79
|
+
const anchor = document.createComment('')
|
|
80
|
+
;(anchor as unknown as { __pruAnchor?: boolean }).__pruAnchor = true
|
|
81
|
+
const from = claimed.parentNode === parent ? claimed : null
|
|
82
|
+
parent.insertBefore(anchor, consumed ? end : from?.nextSibling ?? null)
|
|
83
|
+
return { result, marker: anchor, claimed, nodes: consumed && from !== null ? between(from, anchor) : undefined, consumed }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function eachStatic<T>(
|
|
87
|
+
parent: Node,
|
|
88
|
+
marker: Node | null,
|
|
89
|
+
items: readonly T[] | null | undefined,
|
|
90
|
+
render: (item: T, index: number) => Node,
|
|
91
|
+
skip = 0,
|
|
92
|
+
): void {
|
|
93
|
+
if (!items || items.length === 0)
|
|
94
|
+
return
|
|
95
|
+
let startMarker: Node | null | undefined
|
|
96
|
+
let insertBefore = marker
|
|
97
|
+
if (marker !== null && marker.nodeType === 8 && (marker as Comment).data === 'k[') {
|
|
98
|
+
const close = skipRange(marker)
|
|
99
|
+
if (close !== null) {
|
|
100
|
+
startMarker = marker
|
|
101
|
+
insertBefore = close
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (claimLoop(parent, () => {
|
|
105
|
+
for (let i = 0; i < items.length; i++) {
|
|
106
|
+
const node = render(items[i]!, i)
|
|
107
|
+
if (node != null && node.parentNode !== parent)
|
|
108
|
+
parent.insertBefore(node, insertBefore)
|
|
109
|
+
}
|
|
110
|
+
}, skip, startMarker)) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const f = document.createDocumentFragment()
|
|
114
|
+
for (let i = 0; i < items.length; i++) {
|
|
115
|
+
const node = render(items[i]!, i)
|
|
116
|
+
if (node != null)
|
|
117
|
+
f.appendChild(node)
|
|
118
|
+
}
|
|
119
|
+
parent.insertBefore(f, insertBefore)
|
|
120
|
+
}
|