prunelle 0.0.0-stage → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +245 -0
- package/package.json +62 -4
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +43 -0
- package/src/build/bundle.ts +661 -0
- package/src/build/byte-size.ts +11 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +175 -0
- package/src/build/client-prune.ts +168 -0
- package/src/build/compile.ts +130 -0
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +171 -0
- package/src/build/first-load.ts +235 -0
- package/src/build/graph.ts +237 -0
- package/src/build/index.ts +678 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +452 -0
- package/src/build/manifest.ts +204 -0
- package/src/build/output.ts +81 -0
- package/src/build/plugin-host.ts +294 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +288 -0
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +290 -0
- package/src/build/resolve.ts +52 -0
- package/src/build/routes.ts +342 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +209 -0
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +409 -0
- package/src/cli/commands.ts +372 -0
- package/src/cli/docs.ts +403 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +406 -0
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +52 -0
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +367 -0
- package/src/compiler/analyze.ts +651 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +250 -0
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +653 -0
- package/src/compiler/data-keys.ts +302 -0
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +452 -0
- package/src/compiler/dom.ts +641 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +587 -0
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +520 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +511 -0
- package/src/compiler/scope.ts +635 -0
- package/src/compiler/server-only.ts +365 -0
- package/src/compiler/shared.ts +397 -0
- package/src/compiler/signal-reads.ts +246 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +69 -0
- package/src/compiler/ssr.ts +525 -0
- package/src/context.ts +199 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +268 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +24 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +159 -0
- package/src/dev/supervisor.ts +212 -0
- package/src/dev/watchers.ts +276 -0
- package/src/dom/action.ts +187 -0
- package/src/dom/attrs.ts +84 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +579 -0
- package/src/dom/index.ts +329 -0
- package/src/dom/island.ts +318 -0
- package/src/dom/loop.ts +484 -0
- package/src/dom/revive.ts +36 -0
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +92 -0
- package/src/plugins/context.ts +109 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +90 -0
- package/src/plugins/order.ts +148 -0
- package/src/plugins/public.ts +15 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +119 -0
- package/src/plugins/types.ts +278 -0
- package/src/reactivity/graph.ts +259 -0
- package/src/reactivity/index.ts +541 -0
- package/src/reactivity/selector.ts +78 -0
- package/src/reactivity/store.ts +252 -0
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +511 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +168 -0
- package/src/server/compress.ts +646 -0
- package/src/server/consent.ts +290 -0
- package/src/server/cookies.ts +153 -0
- package/src/server/document.ts +286 -0
- package/src/server/env.ts +47 -0
- package/src/server/errors.ts +396 -0
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +588 -0
- package/src/server/index.ts +615 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +131 -0
- package/src/server/page.ts +122 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +267 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +20 -0
- package/src/server/rate-limit.ts +199 -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-cache.ts +541 -0
- package/src/server/route-table.ts +424 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +388 -0
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +550 -0
- package/src/server/serve-handlers.ts +93 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +104 -0
- package/src/server/start.ts +143 -0
- package/src/server/static-dir.ts +246 -0
- package/src/server/stream.ts +233 -0
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +399 -0
- package/src/ssr/index.ts +624 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +192 -0
- package/src/style.ts +96 -0
- package/src/test/browser.ts +201 -0
- package/src/test/index.ts +262 -0
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -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 +8 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +22 -0
- package/templates/_shared/tsconfig.json +25 -0
- package/templates/app/.env.example +8 -0
- package/templates/app/package.json +22 -0
- package/templates/app/prunelle.config.ts +13 -0
- package/templates/app/src/auth.ts +12 -0
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +40 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +47 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +53 -0
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +14 -0
- package/templates/app/tests/app.test.ts +73 -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 +25 -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 +15 -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 +28 -0
- package/templates/fullstack/src/routes/_layout.tsx +18 -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 +39 -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 +18 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +32 -0
- package/ts-plugin.cjs +4277 -0
- package/README.md +0 -3
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
// Element attribute, event, spread, ref, component callee, and raw-text compilation helpers for the DOM compiler.
|
|
2
|
+
|
|
3
|
+
import type { JSXElement, Node, Program } from 'oxc-parser'
|
|
4
|
+
import { walk } from './ast'
|
|
5
|
+
import { DELEGATED_EVENTS } from './emit'
|
|
6
|
+
import { isStaticPropExpr } from './props'
|
|
7
|
+
import {
|
|
8
|
+
cleanJsxText,
|
|
9
|
+
collectPatternNames,
|
|
10
|
+
DOM_PROPS,
|
|
11
|
+
escapeRawText,
|
|
12
|
+
escapeStaticAttr,
|
|
13
|
+
EVENT_ATTR,
|
|
14
|
+
filterMeaningfulChildren,
|
|
15
|
+
isBlockedAttr,
|
|
16
|
+
isStringBoolAttr,
|
|
17
|
+
isUnsafeUrlAttr,
|
|
18
|
+
jsxTagName,
|
|
19
|
+
unwrapTsExpr,
|
|
20
|
+
} from './shared'
|
|
21
|
+
import { formatStaticAttrValue, formatStaticStyleExpr, staticLiteralOrTemplate } from './ssr-helpers'
|
|
22
|
+
|
|
23
|
+
const SIMPLE_CALLEE_RE = /^[A-Za-z_$][\w$.]*$/
|
|
24
|
+
|
|
25
|
+
function isPlainInitObject(node: Node): boolean {
|
|
26
|
+
const cur = unwrapTsExpr(node)
|
|
27
|
+
if (cur.type !== 'ObjectExpression')
|
|
28
|
+
return false
|
|
29
|
+
for (const prop of cur.properties) {
|
|
30
|
+
if (prop.type !== 'Property' || prop.kind !== 'init')
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Names provably never holding a reactive `store()` proxy (local plain-object `const`s or known primitives). */
|
|
37
|
+
export function collectStaticStyleNames(program: Program, primitiveVars: ReadonlySet<string>): Set<string> {
|
|
38
|
+
const safe = new Set<string>(primitiveVars)
|
|
39
|
+
const unsafe = new Set<string>()
|
|
40
|
+
const mark = (pat: Node | null | undefined): void => {
|
|
41
|
+
if (pat != null)
|
|
42
|
+
collectPatternNames(pat, unsafe)
|
|
43
|
+
}
|
|
44
|
+
walk(program, (n) => {
|
|
45
|
+
if (n.type === 'VariableDeclaration') {
|
|
46
|
+
for (const d of n.declarations) {
|
|
47
|
+
if (n.kind === 'const' && d.id.type === 'Identifier' && d.init !== null && isPlainInitObject(d.init))
|
|
48
|
+
safe.add(d.id.name)
|
|
49
|
+
else if (n.kind !== 'const' || d.id.type !== 'Identifier' || !primitiveVars.has(d.id.name))
|
|
50
|
+
mark(d.id)
|
|
51
|
+
}
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
if (n.type === 'ImportSpecifier' || n.type === 'ImportDefaultSpecifier' || n.type === 'ImportNamespaceSpecifier')
|
|
55
|
+
mark(n.local)
|
|
56
|
+
else if (n.type === 'CatchClause')
|
|
57
|
+
mark(n.param)
|
|
58
|
+
else if (n.type === 'FunctionDeclaration' || n.type === 'ClassDeclaration')
|
|
59
|
+
mark(n.id)
|
|
60
|
+
if (n.type === 'FunctionDeclaration' || n.type === 'FunctionExpression' || n.type === 'ArrowFunctionExpression') {
|
|
61
|
+
for (const p of n.params)
|
|
62
|
+
mark(p)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
for (const name of unsafe)
|
|
66
|
+
safe.delete(name)
|
|
67
|
+
return safe
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** True when a DOM expression cannot subscribe to any signal, prop getter, or reactive store, so `__effect()` can be omitted. */
|
|
71
|
+
export function isStaticDomExpr(
|
|
72
|
+
rawNode: Node,
|
|
73
|
+
scopeRewrites: ReadonlyMap<string, string>,
|
|
74
|
+
isStyle = false,
|
|
75
|
+
staticStyleNames?: ReadonlySet<string>,
|
|
76
|
+
): boolean {
|
|
77
|
+
if (!isStaticPropExpr(rawNode, scopeRewrites))
|
|
78
|
+
return false
|
|
79
|
+
if (!isStyle)
|
|
80
|
+
return true
|
|
81
|
+
const node = unwrapTsExpr(rawNode)
|
|
82
|
+
if (node.type === 'Identifier')
|
|
83
|
+
return staticStyleNames?.has(node.name) === true
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function wrapDomOp(stmt: string, isStatic: boolean, use: (name: string) => string): string {
|
|
88
|
+
return isStatic ? `${stmt};` : `${use('effect')}(() => ${stmt});`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function formatSetTextOp(
|
|
92
|
+
targetVar: string,
|
|
93
|
+
exprCode: string,
|
|
94
|
+
expr: Node,
|
|
95
|
+
scopeRewrites: ReadonlyMap<string, string>,
|
|
96
|
+
use: (name: string) => string,
|
|
97
|
+
): string {
|
|
98
|
+
return wrapDomOp(`${use('setText')}(${targetVar}, ${exprCode})`, isStaticDomExpr(expr, scopeRewrites), use)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Rewrites a JSX component tag's root identifier through `scopeRewrites` (`<Icon />` -> `__p.Icon`, `<ui.Icon />` -> `__p.ui.Icon`). */
|
|
102
|
+
export function resolveComponentCallee(tag: string, scopeRewrites: ReadonlyMap<string, string>): string {
|
|
103
|
+
const dot = tag.indexOf('.')
|
|
104
|
+
const root = dot === -1 ? tag : tag.slice(0, dot)
|
|
105
|
+
const rewritten = scopeRewrites.get(root)
|
|
106
|
+
if (rewritten === undefined)
|
|
107
|
+
return tag
|
|
108
|
+
const safeRoot = SIMPLE_CALLEE_RE.test(rewritten) ? rewritten : `(${rewritten})`
|
|
109
|
+
return dot === -1 ? safeRoot : `${safeRoot}${tag.slice(dot)}`
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface ElementAttrCompileContext {
|
|
113
|
+
el: JSXElement
|
|
114
|
+
tag: string
|
|
115
|
+
elVar: string
|
|
116
|
+
scopeRewrites: ReadonlyMap<string, string>
|
|
117
|
+
staticStyleNames?: ReadonlySet<string> | undefined
|
|
118
|
+
ops: string[]
|
|
119
|
+
postChildOps: string[]
|
|
120
|
+
delegatedEvents: Set<string>
|
|
121
|
+
use: (name: string) => string
|
|
122
|
+
freshVar: (prefix?: string) => string
|
|
123
|
+
compileExpression: (node: Node, rewrites: ReadonlyMap<string, string>) => string
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function emitDomExprAttr(
|
|
127
|
+
ctx: ElementAttrCompileContext,
|
|
128
|
+
name: string,
|
|
129
|
+
expr: Node,
|
|
130
|
+
isSelect: boolean,
|
|
131
|
+
): string {
|
|
132
|
+
const { tag, elVar, scopeRewrites, staticStyleNames, ops, postChildOps, use, compileExpression } = ctx
|
|
133
|
+
|
|
134
|
+
if (name === 'style') {
|
|
135
|
+
const staticStyle = formatStaticStyleExpr(expr)
|
|
136
|
+
if (staticStyle !== undefined)
|
|
137
|
+
return staticStyle
|
|
138
|
+
const exprCode = compileExpression(expr, scopeRewrites)
|
|
139
|
+
ops.push(wrapDomOp(`${use('setStyle')}(${elVar}, ${exprCode})`, isStaticDomExpr(expr, scopeRewrites, true, staticStyleNames), use))
|
|
140
|
+
return ''
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!DOM_PROPS.has(name)) {
|
|
144
|
+
const unwrapped = unwrapTsExpr(expr)
|
|
145
|
+
const lit = staticLiteralOrTemplate(unwrapped)
|
|
146
|
+
if (lit.ok)
|
|
147
|
+
return formatStaticAttrValue(name, lit.value, tag)
|
|
148
|
+
if (unwrapped.type === 'Identifier' && unwrapped.name === 'undefined' && !scopeRewrites.has('undefined'))
|
|
149
|
+
return ''
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const exprCode = compileExpression(expr, scopeRewrites)
|
|
153
|
+
const isStatic = isStaticDomExpr(expr, scopeRewrites)
|
|
154
|
+
if (name === 'class') {
|
|
155
|
+
ops.push(wrapDomOp(`${use('setClass')}(${elVar}, ${exprCode})`, isStatic, use))
|
|
156
|
+
}
|
|
157
|
+
else if (DOM_PROPS.has(name)) {
|
|
158
|
+
const targetOps = isSelect && name === 'value' ? postChildOps : ops
|
|
159
|
+
targetOps.push(wrapDomOp(`${use('setProp')}(${elVar}, ${JSON.stringify(name)}, ${exprCode})`, isStatic, use))
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
ops.push(wrapDomOp(`${use('setAttr')}(${elVar}, ${JSON.stringify(name)}, ${exprCode})`, isStatic, use))
|
|
163
|
+
}
|
|
164
|
+
return ''
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function emitInnerHtmlOp(ctx: ElementAttrCompileContext, attr: Extract<Node, { type: 'JSXAttribute' }>, targetOps: string[]): void {
|
|
168
|
+
const { elVar, scopeRewrites, use, compileExpression } = ctx
|
|
169
|
+
if (attr.value?.type === 'Literal') {
|
|
170
|
+
targetOps.push(`${use('setInnerHtml')}(${elVar}, ${JSON.stringify(String(attr.value.value))});`)
|
|
171
|
+
}
|
|
172
|
+
else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
173
|
+
const expr = attr.value.expression
|
|
174
|
+
targetOps.push(wrapDomOp(`${use('setInnerHtml')}(${elVar}, ${compileExpression(expr, scopeRewrites)})`, isStaticDomExpr(expr, scopeRewrites), use))
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function emitRefOp(ctx: ElementAttrCompileContext, expr: Node, targetOps: string[]): void {
|
|
179
|
+
const { elVar, scopeRewrites, compileExpression } = ctx
|
|
180
|
+
const refCode = compileExpression(expr, scopeRewrites)
|
|
181
|
+
const unwrapped = unwrapTsExpr(expr)
|
|
182
|
+
if (unwrapped.type === 'ArrowFunctionExpression' || unwrapped.type === 'FunctionExpression')
|
|
183
|
+
targetOps.push(`(${refCode})(${elVar});`)
|
|
184
|
+
else
|
|
185
|
+
targetOps.push(`(${refCode})?.(${elVar});`)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function scanSpreadSpecialIndices(attrs: readonly JSXElement['openingElement']['attributes'][number][]): {
|
|
189
|
+
lastSpreadIdx: number
|
|
190
|
+
lastInnerHtmlIdx: number
|
|
191
|
+
lastRefIdx: number
|
|
192
|
+
} {
|
|
193
|
+
let lastSpreadIdx = -1
|
|
194
|
+
let lastInnerHtmlIdx = -1
|
|
195
|
+
let lastRefIdx = -1
|
|
196
|
+
for (let i = 0; i < attrs.length; i++) {
|
|
197
|
+
const attr = attrs[i]!
|
|
198
|
+
if (attr.type === 'JSXSpreadAttribute') {
|
|
199
|
+
lastSpreadIdx = i
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
const rawName = jsxTagName(attr.name)
|
|
203
|
+
if (rawName === 'innerHTML')
|
|
204
|
+
lastInnerHtmlIdx = i
|
|
205
|
+
else if (rawName === 'ref')
|
|
206
|
+
lastRefIdx = i
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { lastSpreadIdx, lastInnerHtmlIdx, lastRefIdx }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function compileSpreadAttributes(ctx: ElementAttrCompileContext, isSelect: boolean): string {
|
|
213
|
+
const { el, elVar, scopeRewrites, ops, postChildOps, use, freshVar, compileExpression } = ctx
|
|
214
|
+
const attrs = el.openingElement.attributes
|
|
215
|
+
const entries: string[] = []
|
|
216
|
+
const preSpreadOps: string[] = []
|
|
217
|
+
let soleSpreadExpr: string | undefined
|
|
218
|
+
const { lastSpreadIdx, lastInnerHtmlIdx, lastRefIdx } = scanSpreadSpecialIndices(attrs)
|
|
219
|
+
|
|
220
|
+
let innerHtmlAttr: Extract<Node, { type: 'JSXAttribute' }> | undefined
|
|
221
|
+
let innerHtmlAfterSpread = false
|
|
222
|
+
let postSpreadRef: Node | undefined
|
|
223
|
+
|
|
224
|
+
for (let i = 0; i < attrs.length; i++) {
|
|
225
|
+
const attr = attrs[i]!
|
|
226
|
+
if (attr.type === 'JSXSpreadAttribute') {
|
|
227
|
+
const argCode = compileExpression(attr.argument, scopeRewrites)
|
|
228
|
+
if (attrs.length === 1)
|
|
229
|
+
soleSpreadExpr = argCode
|
|
230
|
+
entries.push(`...(${argCode})`)
|
|
231
|
+
continue
|
|
232
|
+
}
|
|
233
|
+
const rawName = jsxTagName(attr.name)
|
|
234
|
+
if (rawName === 'key' || rawName === '$hydrate')
|
|
235
|
+
continue
|
|
236
|
+
|
|
237
|
+
if (rawName === 'innerHTML') {
|
|
238
|
+
if (i === lastInnerHtmlIdx) {
|
|
239
|
+
innerHtmlAttr = attr
|
|
240
|
+
innerHtmlAfterSpread = i > lastSpreadIdx
|
|
241
|
+
}
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (rawName === 'ref') {
|
|
246
|
+
if (i !== lastRefIdx && lastRefIdx > lastSpreadIdx)
|
|
247
|
+
continue
|
|
248
|
+
if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
249
|
+
const expr = attr.value.expression
|
|
250
|
+
const unwrapped = unwrapTsExpr(expr)
|
|
251
|
+
const isInlineFn = unwrapped.type === 'ArrowFunctionExpression' || unwrapped.type === 'FunctionExpression'
|
|
252
|
+
if (i > lastSpreadIdx && (isInlineFn || isStaticPropExpr(expr, scopeRewrites))) {
|
|
253
|
+
entries.push('ref: undefined')
|
|
254
|
+
postSpreadRef = expr
|
|
255
|
+
}
|
|
256
|
+
else if (isInlineFn) {
|
|
257
|
+
const refVar = freshVar('_r')
|
|
258
|
+
preSpreadOps.push(`const ${refVar} = ${compileExpression(expr, scopeRewrites)};`)
|
|
259
|
+
entries.push(`ref: ${refVar}`)
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
entries.push(`ref: ${compileExpression(expr, scopeRewrites)}`)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
|
|
269
|
+
const isEvent = EVENT_ATTR.test(rawName)
|
|
270
|
+
if (!isEvent && isBlockedAttr(name))
|
|
271
|
+
continue
|
|
272
|
+
|
|
273
|
+
const propName = isEvent ? rawName : name
|
|
274
|
+
const propKey = /^[A-Za-z_$][\w$]*$/.test(propName) ? propName : JSON.stringify(propName)
|
|
275
|
+
if (attr.value === null)
|
|
276
|
+
entries.push(`${propKey}: true`)
|
|
277
|
+
else if (attr.value.type === 'Literal')
|
|
278
|
+
entries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
|
|
279
|
+
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
|
|
280
|
+
entries.push(`${propKey}: ${compileExpression(attr.value.expression, scopeRewrites)}`)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const targetOps = isSelect ? postChildOps : ops
|
|
284
|
+
for (const op of preSpreadOps)
|
|
285
|
+
targetOps.push(op)
|
|
286
|
+
if (innerHtmlAttr !== undefined && !innerHtmlAfterSpread)
|
|
287
|
+
emitInnerHtmlOp(ctx, innerHtmlAttr, targetOps)
|
|
288
|
+
if (soleSpreadExpr !== undefined)
|
|
289
|
+
targetOps.push(`${use('spread')}(${elVar}, () => (${soleSpreadExpr}));`)
|
|
290
|
+
else if (entries.length > 0)
|
|
291
|
+
targetOps.push(`${use('spread')}(${elVar}, () => ({ ${entries.join(', ')} }));`)
|
|
292
|
+
if (innerHtmlAttr !== undefined && innerHtmlAfterSpread)
|
|
293
|
+
emitInnerHtmlOp(ctx, innerHtmlAttr, targetOps)
|
|
294
|
+
if (postSpreadRef !== undefined)
|
|
295
|
+
emitRefOp(ctx, postSpreadRef, targetOps)
|
|
296
|
+
return ''
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Compiles an HTML/SVG element's attributes, returning the static attribute HTML fragment and pushing DOM ops. */
|
|
300
|
+
export function compileElementAttributes(ctx: ElementAttrCompileContext): string {
|
|
301
|
+
const { el, tag, elVar, scopeRewrites, ops, postChildOps, delegatedEvents, use, freshVar, compileExpression } = ctx
|
|
302
|
+
const attrs = el.openingElement.attributes
|
|
303
|
+
const hasSpread = attrs.some(a => a.type === 'JSXSpreadAttribute')
|
|
304
|
+
const isFormValueEl = tag === 'textarea' || tag === 'select'
|
|
305
|
+
const isSelect = tag === 'select'
|
|
306
|
+
|
|
307
|
+
if (hasSpread)
|
|
308
|
+
return compileSpreadAttributes(ctx, isSelect)
|
|
309
|
+
|
|
310
|
+
let html = ''
|
|
311
|
+
let lastInnerHtml: Extract<Node, { type: 'JSXAttribute' }> | undefined
|
|
312
|
+
for (const attr of attrs) {
|
|
313
|
+
if (attr.type === 'JSXSpreadAttribute')
|
|
314
|
+
continue
|
|
315
|
+
const rawName = jsxTagName(attr.name)
|
|
316
|
+
if (rawName === 'key' || rawName === '$hydrate')
|
|
317
|
+
continue
|
|
318
|
+
|
|
319
|
+
if (rawName === 'ref') {
|
|
320
|
+
if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
|
|
321
|
+
emitRefOp(ctx, attr.value.expression, ops)
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (rawName === 'innerHTML') {
|
|
326
|
+
lastInnerHtml = attr
|
|
327
|
+
continue
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const eventMatch = EVENT_ATTR.exec(rawName)
|
|
331
|
+
if (eventMatch !== null) {
|
|
332
|
+
if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
333
|
+
const expr = attr.value.expression
|
|
334
|
+
const eventName = eventMatch[1]!.toLowerCase()
|
|
335
|
+
const handlerCode = compileExpression(expr, scopeRewrites)
|
|
336
|
+
const isStaticHandler = isStaticPropExpr(expr, scopeRewrites)
|
|
337
|
+
if (DELEGATED_EVENTS.has(eventName)) {
|
|
338
|
+
delegatedEvents.add(eventName)
|
|
339
|
+
if (isStaticHandler)
|
|
340
|
+
ops.push(`${elVar}.$$${eventName} = ${handlerCode};`)
|
|
341
|
+
else
|
|
342
|
+
ops.push(`${use('effect')}(() => { ${elVar}.$$${eventName} = ${handlerCode}; });`)
|
|
343
|
+
}
|
|
344
|
+
else if (isStaticHandler) {
|
|
345
|
+
ops.push(`${use('on')}(${elVar}, ${JSON.stringify(eventName)}, ${handlerCode});`)
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
const hVar = freshVar('_h')
|
|
349
|
+
ops.push(`let ${hVar}; ${use('effect')}(() => { ${hVar} = ${handlerCode}; }); ${use('on')}(${elVar}, ${JSON.stringify(eventName)}, (_ev) => typeof ${hVar} === "function" && ${hVar}(_ev));`)
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
continue
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
|
|
356
|
+
if (isBlockedAttr(name))
|
|
357
|
+
continue
|
|
358
|
+
|
|
359
|
+
if (attr.value === null) {
|
|
360
|
+
html += isStringBoolAttr(name) ? ` ${name}="true"` : ` ${name}`
|
|
361
|
+
}
|
|
362
|
+
else if (attr.value.type === 'Literal') {
|
|
363
|
+
// HTML ignores the `value` attribute on `<textarea>` and `<select>`, so set the DOM property instead (and after children for `<select>`).
|
|
364
|
+
if (isFormValueEl && name === 'value') {
|
|
365
|
+
const targetOps = isSelect ? postChildOps : ops
|
|
366
|
+
targetOps.push(`${use('setProp')}(${elVar}, "value", ${JSON.stringify(String(attr.value.value))});`)
|
|
367
|
+
}
|
|
368
|
+
else if (!isUnsafeUrlAttr(name, attr.value.value, tag)) {
|
|
369
|
+
html += ` ${name}="${escapeStaticAttr(String(attr.value.value))}"`
|
|
370
|
+
}
|
|
371
|
+
else if (process.env.NODE_ENV !== 'production') {
|
|
372
|
+
console.warn(`prunelle: dropped unsafe URL on <${tag} ${name}>.`)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
|
|
376
|
+
html += emitDomExprAttr(ctx, name, attr.value.expression, isSelect)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (lastInnerHtml !== undefined)
|
|
380
|
+
emitInnerHtmlOp(ctx, lastInnerHtml, ops)
|
|
381
|
+
return html
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Compiles `<style>` / `<script>` children without comment markers (HTML parsers treat `<!---->` inside raw text as literal text).
|
|
386
|
+
* Static raw text folds directly into the template; dynamic interpolation updates `el.textContent` directly inside an effect.
|
|
387
|
+
*/
|
|
388
|
+
export function compileRawTextChildren(
|
|
389
|
+
el: JSXElement,
|
|
390
|
+
rawTextTag: 'style' | 'script',
|
|
391
|
+
elVar: string,
|
|
392
|
+
scopeRewrites: ReadonlyMap<string, string>,
|
|
393
|
+
ops: string[],
|
|
394
|
+
use: (name: string) => string,
|
|
395
|
+
compileExpression: (node: Node, rewrites: ReadonlyMap<string, string>) => string,
|
|
396
|
+
): string {
|
|
397
|
+
const meaningful = filterMeaningfulChildren(el.children)
|
|
398
|
+
if (meaningful.length === 0)
|
|
399
|
+
return ''
|
|
400
|
+
|
|
401
|
+
const chunks: ({ static: true, text: string } | { static: false, expr: string })[] = []
|
|
402
|
+
let hasDynamic = false
|
|
403
|
+
let allStaticExprs = true
|
|
404
|
+
|
|
405
|
+
for (const child of meaningful) {
|
|
406
|
+
if (child.type === 'JSXText') {
|
|
407
|
+
const text = cleanJsxText(child.value)
|
|
408
|
+
if (text !== '')
|
|
409
|
+
chunks.push({ static: true, text })
|
|
410
|
+
continue
|
|
411
|
+
}
|
|
412
|
+
if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression') {
|
|
413
|
+
const expr = child.expression
|
|
414
|
+
if (expr.type === 'Literal' && typeof expr.value === 'string') {
|
|
415
|
+
if (expr.value !== '')
|
|
416
|
+
chunks.push({ static: true, text: expr.value })
|
|
417
|
+
continue
|
|
418
|
+
}
|
|
419
|
+
if (expr.type === 'TemplateLiteral' && expr.expressions.length === 0 && expr.quasis.length === 1) {
|
|
420
|
+
const cooked = expr.quasis[0]?.value.cooked
|
|
421
|
+
if (typeof cooked === 'string') {
|
|
422
|
+
if (cooked !== '')
|
|
423
|
+
chunks.push({ static: true, text: cooked })
|
|
424
|
+
continue
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
hasDynamic = true
|
|
428
|
+
if (!isStaticDomExpr(expr, scopeRewrites))
|
|
429
|
+
allStaticExprs = false
|
|
430
|
+
chunks.push({ static: false, expr: compileExpression(expr, scopeRewrites) })
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!hasDynamic) {
|
|
435
|
+
let staticHtml = ''
|
|
436
|
+
for (const c of chunks) {
|
|
437
|
+
if (c.static)
|
|
438
|
+
staticHtml += escapeRawText(c.text, rawTextTag)
|
|
439
|
+
}
|
|
440
|
+
return staticHtml
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const parts = chunks.map((c) => {
|
|
444
|
+
if (c.static)
|
|
445
|
+
return JSON.stringify(c.text)
|
|
446
|
+
return `((_v) => _v == null || typeof _v === "boolean" ? "" : String(_v))(${c.expr})`
|
|
447
|
+
})
|
|
448
|
+
const combined = parts.length === 1 ? parts[0]! : parts.join(' + ')
|
|
449
|
+
const updateBody = `{ const _s = ${combined}; if (${elVar}.textContent !== _s) ${elVar}.textContent = _s; }`
|
|
450
|
+
ops.push(allStaticExprs ? updateBody : `${use('effect')}(() => ${updateBody});`)
|
|
451
|
+
return ''
|
|
452
|
+
}
|