resuxjs 0.1.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/README.md +442 -0
- package/dist/compiler/index.d.ts +106 -0
- package/dist/compiler/index.js +1599 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/create.d.ts +2 -0
- package/dist/create.js +283 -0
- package/dist/create.js.map +1 -0
- package/dist/globals.d.ts +43 -0
- package/dist/globals.js +2 -0
- package/dist/globals.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1314 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/index.d.ts +304 -0
- package/dist/runtime/index.js +2291 -0
- package/dist/runtime/index.js.map +1 -0
- package/package.json +71 -0
- package/templates/default/.dockerignore +7 -0
- package/templates/default/.resux-nitro/handler.ts +4 -0
- package/templates/default/DEPLOYMENT.md +42 -0
- package/templates/default/Dockerfile +15 -0
- package/templates/default/README.md +14 -0
- package/templates/default/_gitignore +6 -0
- package/templates/default/app.vue +5 -0
- package/templates/default/env.d.ts +1 -0
- package/templates/default/error.vue +18 -0
- package/templates/default/islands/vue/CounterIsland.vue +16 -0
- package/templates/default/layouts/default.vue +12 -0
- package/templates/default/nitro.config.ts +51 -0
- package/templates/default/pages/about.vue +14 -0
- package/templates/default/pages/index.vue +60 -0
- package/templates/default/public/styles.css +220 -0
- package/templates/default/resux.config.ts +23 -0
- package/templates/default/server/api/status.ts +5 -0
- package/templates/default/tsconfig.json +16 -0
|
@@ -0,0 +1,2291 @@
|
|
|
1
|
+
export function defineComponent(definition) {
|
|
2
|
+
return definition;
|
|
3
|
+
}
|
|
4
|
+
export function defineResuxConfig(config) {
|
|
5
|
+
return config;
|
|
6
|
+
}
|
|
7
|
+
export function defineResuxModule(module) {
|
|
8
|
+
return module;
|
|
9
|
+
}
|
|
10
|
+
export function defineResuxPlugin(plugin) {
|
|
11
|
+
return plugin;
|
|
12
|
+
}
|
|
13
|
+
export function defineResuxRouteMiddleware(middleware) {
|
|
14
|
+
return middleware;
|
|
15
|
+
}
|
|
16
|
+
export function navigateTo(to, options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
type: "redirect",
|
|
19
|
+
to,
|
|
20
|
+
statusCode: options.statusCode
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function abortNavigation(message, options = {}) {
|
|
24
|
+
return {
|
|
25
|
+
type: "abort",
|
|
26
|
+
message,
|
|
27
|
+
statusCode: options.statusCode
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function defineEventHandler(handler) {
|
|
31
|
+
return handler;
|
|
32
|
+
}
|
|
33
|
+
export const eventHandler = defineEventHandler;
|
|
34
|
+
export function defineServerMiddleware(middleware) {
|
|
35
|
+
return middleware;
|
|
36
|
+
}
|
|
37
|
+
export async function readBody(event) {
|
|
38
|
+
const req = event.node.req;
|
|
39
|
+
if (!req || typeof req[Symbol.asyncIterator] !== "function") {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
const chunks = [];
|
|
43
|
+
for await (const chunk of req) {
|
|
44
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
45
|
+
}
|
|
46
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
47
|
+
if (!raw) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(raw);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return raw;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function getQuery(event) {
|
|
58
|
+
return event.query;
|
|
59
|
+
}
|
|
60
|
+
export async function renderApp(options) {
|
|
61
|
+
return renderAppAsync(options);
|
|
62
|
+
}
|
|
63
|
+
export function renderDocument(result, title = "Resux App", options = {}) {
|
|
64
|
+
const payload = escapeJsonForHtml(JSON.stringify(result.payload));
|
|
65
|
+
const mergedHead = mergeHead([{ title }, result.head]);
|
|
66
|
+
return [
|
|
67
|
+
"<!doctype html>",
|
|
68
|
+
'<html lang="en">',
|
|
69
|
+
"<head>",
|
|
70
|
+
'<meta charset="utf-8">',
|
|
71
|
+
renderHead(mergedHead),
|
|
72
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">',
|
|
73
|
+
`<style>
|
|
74
|
+
#__resux-loading {
|
|
75
|
+
position: fixed;
|
|
76
|
+
inset: 0 0 auto;
|
|
77
|
+
z-index: 9999;
|
|
78
|
+
color: #18181b;
|
|
79
|
+
font: 600 13px/1.4 ui-sans-serif, system-ui, sans-serif;
|
|
80
|
+
pointer-events: none;
|
|
81
|
+
}
|
|
82
|
+
#__resux-loading[hidden] {
|
|
83
|
+
display: none;
|
|
84
|
+
}
|
|
85
|
+
#__resux-loading .bar {
|
|
86
|
+
height: 3px;
|
|
87
|
+
overflow: hidden;
|
|
88
|
+
background: rgba(24, 24, 27, 0.12);
|
|
89
|
+
}
|
|
90
|
+
#__resux-loading .bar span {
|
|
91
|
+
display: block;
|
|
92
|
+
width: var(--resux-progress, 8%);
|
|
93
|
+
height: 100%;
|
|
94
|
+
background: #2563eb;
|
|
95
|
+
box-shadow: 0 0 18px rgba(37, 99, 235, 0.45);
|
|
96
|
+
transition: width 160ms ease, background 160ms ease;
|
|
97
|
+
}
|
|
98
|
+
#__resux-loading .panel {
|
|
99
|
+
width: fit-content;
|
|
100
|
+
max-width: min(28rem, calc(100vw - 2rem));
|
|
101
|
+
margin: 0.75rem auto 0;
|
|
102
|
+
padding: 0.55rem 0.8rem;
|
|
103
|
+
border: 1px solid rgba(24, 24, 27, 0.12);
|
|
104
|
+
border-radius: 8px;
|
|
105
|
+
background: rgba(255, 255, 255, 0.92);
|
|
106
|
+
box-shadow: 0 12px 32px rgba(24, 24, 27, 0.12);
|
|
107
|
+
backdrop-filter: blur(12px);
|
|
108
|
+
}
|
|
109
|
+
#__resux-loading[data-state="error"] .bar span {
|
|
110
|
+
background: #dc2626;
|
|
111
|
+
}
|
|
112
|
+
#__resux-loading[data-state="complete"] .bar span {
|
|
113
|
+
background: #16a34a;
|
|
114
|
+
}
|
|
115
|
+
#__resux[data-route-transition="loading"] {
|
|
116
|
+
opacity: 0.72;
|
|
117
|
+
transition: opacity 120ms ease;
|
|
118
|
+
}
|
|
119
|
+
@media (prefers-reduced-motion: reduce) {
|
|
120
|
+
#__resux-loading .bar span,
|
|
121
|
+
#__resux[data-route-transition="loading"] {
|
|
122
|
+
transition: none;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
</style>`,
|
|
126
|
+
"</head>",
|
|
127
|
+
"<body>",
|
|
128
|
+
'<div id="__resux">',
|
|
129
|
+
result.html,
|
|
130
|
+
"</div>",
|
|
131
|
+
'<div id="__resux-loading" hidden data-state="idle" aria-live="polite" aria-busy="false"><div class="bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0" aria-valuetext="Idle"><span></span></div><div class="panel" data-rx-transition-message>Ready</div></div>',
|
|
132
|
+
`<script>window.__RESUX__=${payload}</script>`,
|
|
133
|
+
options.devReload ? getDevReloadScript() : "",
|
|
134
|
+
'<script type="module" src="/__resux/runtime-client.mjs"></script>',
|
|
135
|
+
"</body>",
|
|
136
|
+
"</html>"
|
|
137
|
+
].join("");
|
|
138
|
+
}
|
|
139
|
+
function getDevReloadScript() {
|
|
140
|
+
return `<script type="module">
|
|
141
|
+
if (typeof EventSource !== "undefined" && !window.__RESUX_DEV_RELOAD__) {
|
|
142
|
+
window.__RESUX_DEV_RELOAD__ = true;
|
|
143
|
+
const events = new EventSource("/__resux/dev-events");
|
|
144
|
+
events.addEventListener("hmr", (event) => {
|
|
145
|
+
const payload = event.data ? JSON.parse(event.data) : {};
|
|
146
|
+
if (typeof window.__RESUX_APPLY_DEV_UPDATE__ === "function") {
|
|
147
|
+
void window.__RESUX_APPLY_DEV_UPDATE__(payload).catch(() => window.location.reload());
|
|
148
|
+
} else {
|
|
149
|
+
window.location.reload();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
events.addEventListener("reload", () => window.location.reload());
|
|
153
|
+
}
|
|
154
|
+
</script>`;
|
|
155
|
+
}
|
|
156
|
+
class ResuxRenderer {
|
|
157
|
+
route;
|
|
158
|
+
components;
|
|
159
|
+
modules;
|
|
160
|
+
runtimeConfig;
|
|
161
|
+
nextScopeId = 0;
|
|
162
|
+
scopes = {};
|
|
163
|
+
headEntries = [];
|
|
164
|
+
constructor(route, components, modules, runtimeConfig = {}) {
|
|
165
|
+
this.route = route;
|
|
166
|
+
this.components = components;
|
|
167
|
+
this.modules = modules;
|
|
168
|
+
this.runtimeConfig = runtimeConfig;
|
|
169
|
+
}
|
|
170
|
+
async renderComponent(definition, renderPage, renderSlot, props = {}) {
|
|
171
|
+
const scopeId = `s${this.nextScopeId++}`;
|
|
172
|
+
const stateRefs = {};
|
|
173
|
+
const asyncDataRefs = {};
|
|
174
|
+
const resuxApp = createResuxApp(this.route, this.modules, this.runtimeConfig);
|
|
175
|
+
const setupContext = createServerSetupContext(this.route, props, stateRefs, asyncDataRefs, this.headEntries, resuxApp, this.runtimeConfig);
|
|
176
|
+
const scope = await definition.script(setupContext);
|
|
177
|
+
this.scopes[scopeId] = {
|
|
178
|
+
id: scopeId,
|
|
179
|
+
moduleId: definition.id,
|
|
180
|
+
props,
|
|
181
|
+
stateRefs,
|
|
182
|
+
asyncDataRefs
|
|
183
|
+
};
|
|
184
|
+
return renderTemplateNodes(definition.template, {
|
|
185
|
+
scope,
|
|
186
|
+
scopeId,
|
|
187
|
+
moduleId: definition.id,
|
|
188
|
+
route: this.route,
|
|
189
|
+
components: this.components,
|
|
190
|
+
layouts: {},
|
|
191
|
+
pageMeta: definition.meta ?? {},
|
|
192
|
+
renderPage,
|
|
193
|
+
renderSlot
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
createPayload() {
|
|
197
|
+
const scopes = {};
|
|
198
|
+
for (const [id, scope] of Object.entries(this.scopes)) {
|
|
199
|
+
scopes[id] = {
|
|
200
|
+
id,
|
|
201
|
+
moduleId: scope.moduleId,
|
|
202
|
+
props: serializeProps(scope.props),
|
|
203
|
+
state: serializeRefs(scope.stateRefs),
|
|
204
|
+
asyncData: serializeAsyncData(scope.asyncDataRefs)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
route: this.route,
|
|
209
|
+
scopes,
|
|
210
|
+
modules: this.modules,
|
|
211
|
+
config: publicRuntimeConfig(this.runtimeConfig)
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function createServerSetupContext(route, props, stateRefs, asyncDataRefs, headEntries, resuxApp, runtimeConfig) {
|
|
216
|
+
const fetchJson = async (url) => {
|
|
217
|
+
const response = await fetch(url);
|
|
218
|
+
if (!response.ok) {
|
|
219
|
+
throw new Error(`Fetch failed for ${url}: ${response.status}`);
|
|
220
|
+
}
|
|
221
|
+
return response.json();
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
props,
|
|
225
|
+
useState(key, factory) {
|
|
226
|
+
if (stateRefs[key]) {
|
|
227
|
+
return stateRefs[key];
|
|
228
|
+
}
|
|
229
|
+
const value = factory ? factory() : undefined;
|
|
230
|
+
assertJsonSerializable(value, `useState("${key}")`);
|
|
231
|
+
const ref = { value: value };
|
|
232
|
+
stateRefs[key] = ref;
|
|
233
|
+
return ref;
|
|
234
|
+
},
|
|
235
|
+
useAsyncData(key, handler) {
|
|
236
|
+
if (asyncDataRefs[key]) {
|
|
237
|
+
return asyncDataRefs[key];
|
|
238
|
+
}
|
|
239
|
+
const pending = createPendingAsyncDataResource();
|
|
240
|
+
asyncDataRefs[key] = pending.resource;
|
|
241
|
+
pending.setCompletion(settleAsyncDataResource(pending.resource, handler, key));
|
|
242
|
+
return pending.resource;
|
|
243
|
+
},
|
|
244
|
+
defineProps() {
|
|
245
|
+
return props;
|
|
246
|
+
},
|
|
247
|
+
useRoute() {
|
|
248
|
+
return route;
|
|
249
|
+
},
|
|
250
|
+
useHead(input) {
|
|
251
|
+
headEntries.push(input);
|
|
252
|
+
},
|
|
253
|
+
useSeoMeta(input) {
|
|
254
|
+
headEntries.push(seoMetaToHead(input));
|
|
255
|
+
},
|
|
256
|
+
useRuntimeConfig() {
|
|
257
|
+
return runtimeConfig;
|
|
258
|
+
},
|
|
259
|
+
useResuxApp() {
|
|
260
|
+
return resuxApp;
|
|
261
|
+
},
|
|
262
|
+
async useFetch(url) {
|
|
263
|
+
return {
|
|
264
|
+
value: await fetchJson(url)
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
$fetch(url) {
|
|
268
|
+
return fetchJson(url);
|
|
269
|
+
},
|
|
270
|
+
onMounted() {
|
|
271
|
+
// Client lifecycle hooks run when a resumable scope is first resumed.
|
|
272
|
+
},
|
|
273
|
+
definePageMeta() {
|
|
274
|
+
// Page meta is compiled statically in Resux.
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function createPendingAsyncDataResource() {
|
|
279
|
+
const value = { value: undefined };
|
|
280
|
+
let completion = Promise.resolve();
|
|
281
|
+
const resource = {
|
|
282
|
+
value,
|
|
283
|
+
pending: true,
|
|
284
|
+
error: null,
|
|
285
|
+
then(onfulfilled, onrejected) {
|
|
286
|
+
return completion.then(() => {
|
|
287
|
+
const snapshot = {
|
|
288
|
+
value: resource.value.value,
|
|
289
|
+
pending: resource.pending,
|
|
290
|
+
error: resource.error
|
|
291
|
+
};
|
|
292
|
+
return onfulfilled ? onfulfilled(snapshot) : snapshot;
|
|
293
|
+
}, onrejected);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
return {
|
|
297
|
+
resource,
|
|
298
|
+
setCompletion(nextCompletion) {
|
|
299
|
+
completion = nextCompletion;
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
async function settleAsyncDataResource(resource, handler, key) {
|
|
304
|
+
try {
|
|
305
|
+
const value = handler ? await handler() : undefined;
|
|
306
|
+
assertJsonSerializable(value, `useAsyncData("${key}")`);
|
|
307
|
+
resource.value.value = value;
|
|
308
|
+
resource.error = null;
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
resource.value.value = undefined;
|
|
312
|
+
resource.error = normalizeAsyncDataError(error);
|
|
313
|
+
}
|
|
314
|
+
finally {
|
|
315
|
+
resource.pending = false;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function renderTemplateNodes(nodes, context, locals = {}) {
|
|
319
|
+
let html = "";
|
|
320
|
+
for (const node of nodes) {
|
|
321
|
+
html += renderTemplateNode(node, context, locals);
|
|
322
|
+
}
|
|
323
|
+
return html;
|
|
324
|
+
}
|
|
325
|
+
function renderTemplateNode(node, context, locals) {
|
|
326
|
+
if (node.type === "text") {
|
|
327
|
+
return escapeHtml(node.value);
|
|
328
|
+
}
|
|
329
|
+
if (node.type === "interpolation") {
|
|
330
|
+
const value = stringifyValue(evaluateExpression(node.expression, context.scope, locals));
|
|
331
|
+
return `<span data-rx-text="${context.scopeId}:${node.bindingId}">${escapeHtml(value)}</span>`;
|
|
332
|
+
}
|
|
333
|
+
if (node.for) {
|
|
334
|
+
const items = evaluateExpression(node.for.source, context.scope, locals);
|
|
335
|
+
const entries = Array.isArray(items) ? items : [];
|
|
336
|
+
const rendered = entries
|
|
337
|
+
.map((item, index) => {
|
|
338
|
+
const nextLocals = { ...locals, [node.for.value]: item };
|
|
339
|
+
if (node.for.index) {
|
|
340
|
+
nextLocals[node.for.index] = index;
|
|
341
|
+
}
|
|
342
|
+
return renderElement({ ...node, for: undefined, if: undefined }, context, nextLocals);
|
|
343
|
+
})
|
|
344
|
+
.join("");
|
|
345
|
+
return `<span data-rx-block="${context.scopeId}:${node.for.blockId}">${rendered}</span>`;
|
|
346
|
+
}
|
|
347
|
+
if (node.if) {
|
|
348
|
+
const rendered = evaluateExpression(node.if.expression, context.scope, locals)
|
|
349
|
+
? renderElement({ ...node, if: undefined }, context, locals)
|
|
350
|
+
: "";
|
|
351
|
+
return `<span data-rx-block="${context.scopeId}:${node.if.blockId}">${rendered}</span>`;
|
|
352
|
+
}
|
|
353
|
+
return renderElement(node, context, locals);
|
|
354
|
+
}
|
|
355
|
+
function renderElement(node, context, locals) {
|
|
356
|
+
if (node.tag === "ResuxPage") {
|
|
357
|
+
if (!context.renderPage) {
|
|
358
|
+
return "";
|
|
359
|
+
}
|
|
360
|
+
throw new Error("ResuxPage must be rendered by renderTemplateNodesAsync.");
|
|
361
|
+
}
|
|
362
|
+
if (node.tag === "ResuxLayout") {
|
|
363
|
+
throw new Error("ResuxLayout must be rendered by renderTemplateNodesAsync.");
|
|
364
|
+
}
|
|
365
|
+
if (node.tag === "slot") {
|
|
366
|
+
throw new Error("<slot> must be rendered by renderTemplateNodesAsync.");
|
|
367
|
+
}
|
|
368
|
+
if (node.tag === "ResuxLink") {
|
|
369
|
+
return renderNativeElement(node, context, locals);
|
|
370
|
+
}
|
|
371
|
+
if (node.tag === "VueIsland") {
|
|
372
|
+
return renderVueIsland(node, context, locals);
|
|
373
|
+
}
|
|
374
|
+
if (isComponentTag(node.tag)) {
|
|
375
|
+
const component = context.components[node.tag];
|
|
376
|
+
if (!component) {
|
|
377
|
+
throw new Error(`Unknown component <${node.tag}>.`);
|
|
378
|
+
}
|
|
379
|
+
throw new Error(`Component <${node.tag}> must be rendered by renderTemplateNodesAsync.`);
|
|
380
|
+
}
|
|
381
|
+
return renderNativeElement(node, context, locals);
|
|
382
|
+
}
|
|
383
|
+
function renderNativeElement(node, context, locals) {
|
|
384
|
+
const attrs = [];
|
|
385
|
+
const tag = nativeElementTag(node);
|
|
386
|
+
for (const attr of node.attrs) {
|
|
387
|
+
const attrName = nativeAttributeName(node, attr.name);
|
|
388
|
+
if (attr.kind === "static") {
|
|
389
|
+
attrs.push(`${attrName}="${escapeAttribute(attr.value)}"`);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const value = evaluateExpression(attr.value, context.scope, locals);
|
|
393
|
+
if (value === false || value === null || value === undefined) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const marker = attr.bindingId ? ` data-rx-attr-${attr.bindingId}="${context.scopeId}:${attr.bindingId}"` : "";
|
|
397
|
+
attrs.push(`${attrName}="${escapeAttribute(stringifyAttributeValue(attrName, value))}"${marker}`);
|
|
398
|
+
}
|
|
399
|
+
for (const event of node.events) {
|
|
400
|
+
attrs.push(`data-rx-on-${event.name}="${context.scopeId}:${context.moduleId}:${event.handler}"`);
|
|
401
|
+
if (event.modifiers?.length) {
|
|
402
|
+
attrs.push(`data-rx-mod-${event.name}="${escapeAttribute(event.modifiers.join(","))}"`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (node.html) {
|
|
406
|
+
attrs.push(`data-rx-html-${node.html.bindingId}="${context.scopeId}:${node.html.bindingId}"`);
|
|
407
|
+
}
|
|
408
|
+
const attrText = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
|
|
409
|
+
const children = node.html
|
|
410
|
+
? sanitizeHtml(evaluateExpression(node.html.expression, context.scope, locals))
|
|
411
|
+
: renderTemplateNodes(node.children, context, locals);
|
|
412
|
+
return `<${tag}${attrText}>${children}</${tag}>`;
|
|
413
|
+
}
|
|
414
|
+
export async function renderTemplateNodesAsync(nodes, context, renderComponent, locals = {}) {
|
|
415
|
+
let html = "";
|
|
416
|
+
for (const node of nodes) {
|
|
417
|
+
html += await renderTemplateNodeAsync(node, context, renderComponent, locals);
|
|
418
|
+
}
|
|
419
|
+
return html;
|
|
420
|
+
}
|
|
421
|
+
async function renderTemplateNodeAsync(node, context, renderComponent, locals) {
|
|
422
|
+
if (node.type !== "element") {
|
|
423
|
+
return renderTemplateNode(node, context, locals);
|
|
424
|
+
}
|
|
425
|
+
if (node.for) {
|
|
426
|
+
const items = evaluateExpression(node.for.source, context.scope, locals);
|
|
427
|
+
const entries = Array.isArray(items) ? items : [];
|
|
428
|
+
const rendered = [];
|
|
429
|
+
for (let index = 0; index < entries.length; index++) {
|
|
430
|
+
const nextLocals = { ...locals, [node.for.value]: entries[index] };
|
|
431
|
+
if (node.for.index) {
|
|
432
|
+
nextLocals[node.for.index] = index;
|
|
433
|
+
}
|
|
434
|
+
rendered.push(await renderElementAsync({ ...node, for: undefined, if: undefined }, context, renderComponent, nextLocals));
|
|
435
|
+
}
|
|
436
|
+
return `<span data-rx-block="${context.scopeId}:${node.for.blockId}">${rendered.join("")}</span>`;
|
|
437
|
+
}
|
|
438
|
+
if (node.if) {
|
|
439
|
+
const rendered = evaluateExpression(node.if.expression, context.scope, locals)
|
|
440
|
+
? await renderElementAsync({ ...node, if: undefined }, context, renderComponent, locals)
|
|
441
|
+
: "";
|
|
442
|
+
return `<span data-rx-block="${context.scopeId}:${node.if.blockId}">${rendered}</span>`;
|
|
443
|
+
}
|
|
444
|
+
return renderElementAsync(node, context, renderComponent, locals);
|
|
445
|
+
}
|
|
446
|
+
async function renderElementAsync(node, context, renderComponent, locals) {
|
|
447
|
+
if (node.tag === "ResuxPage") {
|
|
448
|
+
return context.renderPage ? `<span data-rx-page="">${await context.renderPage()}</span>` : "";
|
|
449
|
+
}
|
|
450
|
+
if (node.tag === "ResuxLayout") {
|
|
451
|
+
const layoutName = resolveLayoutName(node, context, locals);
|
|
452
|
+
const renderChildren = () => renderTemplateNodesAsync(node.children, context, renderComponent, locals);
|
|
453
|
+
if (layoutName === false) {
|
|
454
|
+
return renderChildren();
|
|
455
|
+
}
|
|
456
|
+
if (!context.renderLayout) {
|
|
457
|
+
return renderChildren();
|
|
458
|
+
}
|
|
459
|
+
const rendered = await context.renderLayout(layoutName, renderChildren);
|
|
460
|
+
const selectedLayoutSource = layoutName ?? (context.pageMeta.layout === false ? "default" : context.pageMeta.layout) ?? "default";
|
|
461
|
+
const selectedLayout = normalizeLayoutName(selectedLayoutSource);
|
|
462
|
+
return `<span data-rx-layout="${escapeAttribute(selectedLayout)}">${rendered}</span>`;
|
|
463
|
+
}
|
|
464
|
+
if (node.tag === "slot") {
|
|
465
|
+
return context.renderSlot ? context.renderSlot() : "";
|
|
466
|
+
}
|
|
467
|
+
if (node.tag === "ResuxLink") {
|
|
468
|
+
return renderNativeElementAsync(node, context, renderComponent, locals);
|
|
469
|
+
}
|
|
470
|
+
if (node.tag === "VueIsland") {
|
|
471
|
+
return renderVueIsland(node, context, locals);
|
|
472
|
+
}
|
|
473
|
+
if (isComponentTag(node.tag)) {
|
|
474
|
+
const component = context.components[node.tag];
|
|
475
|
+
if (!component) {
|
|
476
|
+
throw new Error(`Unknown component <${node.tag}>.`);
|
|
477
|
+
}
|
|
478
|
+
const props = collectComponentProps(node, context.scope, locals);
|
|
479
|
+
const renderChildren = () => renderTemplateNodesAsync(node.children, context, renderComponent, locals);
|
|
480
|
+
return renderComponent(component, props, renderChildren);
|
|
481
|
+
}
|
|
482
|
+
return renderNativeElementAsync(node, context, renderComponent, locals);
|
|
483
|
+
}
|
|
484
|
+
async function renderNativeElementAsync(node, context, renderComponent, locals) {
|
|
485
|
+
const attrs = [];
|
|
486
|
+
const tag = nativeElementTag(node);
|
|
487
|
+
for (const attr of node.attrs) {
|
|
488
|
+
const attrName = nativeAttributeName(node, attr.name);
|
|
489
|
+
if (attr.kind === "static") {
|
|
490
|
+
attrs.push(`${attrName}="${escapeAttribute(attr.value)}"`);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const value = evaluateExpression(attr.value, context.scope, locals);
|
|
494
|
+
if (value === false || value === null || value === undefined) {
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const marker = attr.bindingId ? ` data-rx-attr-${attr.bindingId}="${context.scopeId}:${attr.bindingId}"` : "";
|
|
498
|
+
attrs.push(`${attrName}="${escapeAttribute(stringifyAttributeValue(attrName, value))}"${marker}`);
|
|
499
|
+
}
|
|
500
|
+
for (const event of node.events) {
|
|
501
|
+
attrs.push(`data-rx-on-${event.name}="${context.scopeId}:${context.moduleId}:${event.handler}"`);
|
|
502
|
+
if (event.modifiers?.length) {
|
|
503
|
+
attrs.push(`data-rx-mod-${event.name}="${escapeAttribute(event.modifiers.join(","))}"`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (node.html) {
|
|
507
|
+
attrs.push(`data-rx-html-${node.html.bindingId}="${context.scopeId}:${node.html.bindingId}"`);
|
|
508
|
+
}
|
|
509
|
+
const attrText = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
|
|
510
|
+
const children = node.html
|
|
511
|
+
? sanitizeHtml(evaluateExpression(node.html.expression, context.scope, locals))
|
|
512
|
+
: await renderTemplateNodesAsync(node.children, context, renderComponent, locals);
|
|
513
|
+
return `<${tag}${attrText}>${children}</${tag}>`;
|
|
514
|
+
}
|
|
515
|
+
function renderVueIsland(node, context, locals) {
|
|
516
|
+
const name = resolveVueIslandName(node, context, locals);
|
|
517
|
+
const props = resolveVueIslandProps(node, context, locals);
|
|
518
|
+
return `<div data-rx-vue-island="${escapeAttribute(name)}" data-rx-vue-props="${escapeAttribute(JSON.stringify(props))}"></div>`;
|
|
519
|
+
}
|
|
520
|
+
function resolveVueIslandName(node, context, locals) {
|
|
521
|
+
const dynamic = node.attrs.find((attr) => attr.kind === "dynamic" && attr.name === "name");
|
|
522
|
+
if (dynamic) {
|
|
523
|
+
return stringifyValue(evaluateExpression(dynamic.value, context.scope, locals));
|
|
524
|
+
}
|
|
525
|
+
const fixed = node.attrs.find((attr) => attr.kind === "static" && attr.name === "name");
|
|
526
|
+
if (!fixed?.value) {
|
|
527
|
+
throw new Error("<VueIsland> needs a name attribute.");
|
|
528
|
+
}
|
|
529
|
+
return fixed.value;
|
|
530
|
+
}
|
|
531
|
+
function resolveVueIslandProps(node, context, locals) {
|
|
532
|
+
const propsAttr = node.attrs.find((attr) => attr.name === "props");
|
|
533
|
+
if (!propsAttr) {
|
|
534
|
+
return {};
|
|
535
|
+
}
|
|
536
|
+
const props = propsAttr.kind === "static"
|
|
537
|
+
? {}
|
|
538
|
+
: evaluateExpression(propsAttr.value, context.scope, locals);
|
|
539
|
+
if (!props || typeof props !== "object" || Array.isArray(props)) {
|
|
540
|
+
throw new Error("<VueIsland> props must evaluate to an object.");
|
|
541
|
+
}
|
|
542
|
+
assertJsonSerializable(props, "<VueIsland> props");
|
|
543
|
+
return props;
|
|
544
|
+
}
|
|
545
|
+
export class AsyncResuxRenderer {
|
|
546
|
+
route;
|
|
547
|
+
components;
|
|
548
|
+
modules;
|
|
549
|
+
vueIslands;
|
|
550
|
+
layouts;
|
|
551
|
+
pageMeta;
|
|
552
|
+
runtimeConfig;
|
|
553
|
+
nextScopeId = 0;
|
|
554
|
+
scopes = {};
|
|
555
|
+
headEntries = [];
|
|
556
|
+
resuxApp;
|
|
557
|
+
constructor(route, components, modules, vueIslands = {}, layouts = {}, pageMeta = {}, runtimeConfig = {}) {
|
|
558
|
+
this.route = route;
|
|
559
|
+
this.components = components;
|
|
560
|
+
this.modules = modules;
|
|
561
|
+
this.vueIslands = vueIslands;
|
|
562
|
+
this.layouts = layouts;
|
|
563
|
+
this.pageMeta = pageMeta;
|
|
564
|
+
this.runtimeConfig = runtimeConfig;
|
|
565
|
+
this.resuxApp = createResuxApp(route, modules, runtimeConfig);
|
|
566
|
+
}
|
|
567
|
+
async renderComponent(definition, renderPage, renderSlot, props = {}) {
|
|
568
|
+
const scopeId = `s${this.nextScopeId++}`;
|
|
569
|
+
const stateRefs = {};
|
|
570
|
+
const asyncDataRefs = {};
|
|
571
|
+
const setupContext = createServerSetupContext(this.route, props, stateRefs, asyncDataRefs, this.headEntries, this.resuxApp, this.runtimeConfig);
|
|
572
|
+
const scope = await definition.script(setupContext);
|
|
573
|
+
this.scopes[scopeId] = {
|
|
574
|
+
id: scopeId,
|
|
575
|
+
moduleId: definition.id,
|
|
576
|
+
props,
|
|
577
|
+
stateRefs,
|
|
578
|
+
asyncDataRefs
|
|
579
|
+
};
|
|
580
|
+
return renderTemplateNodesAsync(definition.template, {
|
|
581
|
+
scope,
|
|
582
|
+
scopeId,
|
|
583
|
+
moduleId: definition.id,
|
|
584
|
+
route: this.route,
|
|
585
|
+
components: this.components,
|
|
586
|
+
layouts: this.layouts,
|
|
587
|
+
pageMeta: this.pageMeta,
|
|
588
|
+
renderPage,
|
|
589
|
+
renderSlot,
|
|
590
|
+
renderLayout: (name, slot) => this.renderLayout(name, slot)
|
|
591
|
+
}, (component, props, slot) => this.renderComponent(component, undefined, slot, props));
|
|
592
|
+
}
|
|
593
|
+
async renderLayout(name, renderSlot) {
|
|
594
|
+
if (name === false) {
|
|
595
|
+
return renderSlot();
|
|
596
|
+
}
|
|
597
|
+
const selectedLayout = name ?? this.pageMeta.layout ?? "default";
|
|
598
|
+
if (selectedLayout === false) {
|
|
599
|
+
return renderSlot();
|
|
600
|
+
}
|
|
601
|
+
const layoutName = normalizeLayoutName(selectedLayout);
|
|
602
|
+
const layout = this.layouts[layoutName];
|
|
603
|
+
if (!layout) {
|
|
604
|
+
return renderSlot();
|
|
605
|
+
}
|
|
606
|
+
return this.renderComponent(layout, undefined, renderSlot);
|
|
607
|
+
}
|
|
608
|
+
createPayload() {
|
|
609
|
+
const scopes = {};
|
|
610
|
+
for (const [id, scope] of Object.entries(this.scopes)) {
|
|
611
|
+
scopes[id] = {
|
|
612
|
+
id,
|
|
613
|
+
moduleId: scope.moduleId,
|
|
614
|
+
props: serializeProps(scope.props),
|
|
615
|
+
state: serializeRefs(scope.stateRefs),
|
|
616
|
+
asyncData: serializeAsyncData(scope.asyncDataRefs)
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
route: this.route,
|
|
621
|
+
scopes,
|
|
622
|
+
modules: this.modules,
|
|
623
|
+
vueIslands: this.vueIslands,
|
|
624
|
+
config: publicRuntimeConfig(this.runtimeConfig)
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
export async function renderAppAsync(options) {
|
|
629
|
+
const pageMeta = {
|
|
630
|
+
...(options.page.meta ?? {}),
|
|
631
|
+
...(options.pageMeta ?? {})
|
|
632
|
+
};
|
|
633
|
+
const appHead = mergeHead([
|
|
634
|
+
options.appHead ?? {},
|
|
635
|
+
headFromPageMeta(pageMeta)
|
|
636
|
+
]);
|
|
637
|
+
const renderer = new AsyncResuxRenderer(options.route, options.components ?? {}, options.modules ?? {}, options.vueIslands ?? {}, options.layouts ?? {}, pageMeta, options.runtimeConfig ?? {});
|
|
638
|
+
for (const plugin of options.plugins ?? []) {
|
|
639
|
+
await plugin(renderer.resuxApp);
|
|
640
|
+
}
|
|
641
|
+
renderer.headEntries.push(appHead);
|
|
642
|
+
const pageRenderer = () => renderer.renderComponent(options.page, undefined, undefined, options.pageProps ?? {});
|
|
643
|
+
const html = options.app
|
|
644
|
+
? await renderer.renderComponent(options.app, pageRenderer)
|
|
645
|
+
: await pageRenderer();
|
|
646
|
+
return {
|
|
647
|
+
html,
|
|
648
|
+
payload: renderer.createPayload(),
|
|
649
|
+
head: mergeHead(renderer.headEntries)
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function createResuxApp(route, modules, runtimeConfig) {
|
|
653
|
+
const payload = {
|
|
654
|
+
route,
|
|
655
|
+
scopes: {},
|
|
656
|
+
modules,
|
|
657
|
+
config: publicRuntimeConfig(runtimeConfig)
|
|
658
|
+
};
|
|
659
|
+
const provides = {};
|
|
660
|
+
return {
|
|
661
|
+
route,
|
|
662
|
+
payload,
|
|
663
|
+
$config: runtimeConfig,
|
|
664
|
+
provides,
|
|
665
|
+
provide(key, value) {
|
|
666
|
+
provides[key] = value;
|
|
667
|
+
this[`$${key}`] = value;
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
function publicRuntimeConfig(runtimeConfig) {
|
|
672
|
+
return {
|
|
673
|
+
public: runtimeConfig.public ?? {}
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
function headFromPageMeta(meta) {
|
|
677
|
+
return {
|
|
678
|
+
title: meta.title,
|
|
679
|
+
meta: meta.meta
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
const seoMetaTags = {
|
|
683
|
+
description: { attribute: "name", value: "description" },
|
|
684
|
+
keywords: { attribute: "name", value: "keywords" },
|
|
685
|
+
robots: { attribute: "name", value: "robots" },
|
|
686
|
+
author: { attribute: "name", value: "author" },
|
|
687
|
+
themeColor: { attribute: "name", value: "theme-color" },
|
|
688
|
+
colorScheme: { attribute: "name", value: "color-scheme" },
|
|
689
|
+
applicationName: { attribute: "name", value: "application-name" },
|
|
690
|
+
referrer: { attribute: "name", value: "referrer" },
|
|
691
|
+
generator: { attribute: "name", value: "generator" },
|
|
692
|
+
ogTitle: { attribute: "property", value: "og:title" },
|
|
693
|
+
ogDescription: { attribute: "property", value: "og:description" },
|
|
694
|
+
ogImage: { attribute: "property", value: "og:image" },
|
|
695
|
+
ogImageSecureUrl: { attribute: "property", value: "og:image:secure_url" },
|
|
696
|
+
ogImageAlt: { attribute: "property", value: "og:image:alt" },
|
|
697
|
+
ogImageWidth: { attribute: "property", value: "og:image:width" },
|
|
698
|
+
ogImageHeight: { attribute: "property", value: "og:image:height" },
|
|
699
|
+
ogUrl: { attribute: "property", value: "og:url" },
|
|
700
|
+
ogType: { attribute: "property", value: "og:type" },
|
|
701
|
+
ogSiteName: { attribute: "property", value: "og:site_name" },
|
|
702
|
+
ogLocale: { attribute: "property", value: "og:locale" },
|
|
703
|
+
fbAppId: { attribute: "property", value: "fb:app_id" },
|
|
704
|
+
articlePublishedTime: { attribute: "property", value: "article:published_time" },
|
|
705
|
+
articleModifiedTime: { attribute: "property", value: "article:modified_time" },
|
|
706
|
+
articleExpirationTime: { attribute: "property", value: "article:expiration_time" },
|
|
707
|
+
articleAuthor: { attribute: "property", value: "article:author" },
|
|
708
|
+
articleSection: { attribute: "property", value: "article:section" },
|
|
709
|
+
articleTag: { attribute: "property", value: "article:tag" },
|
|
710
|
+
twitterCard: { attribute: "name", value: "twitter:card" },
|
|
711
|
+
twitterTitle: { attribute: "name", value: "twitter:title" },
|
|
712
|
+
twitterDescription: { attribute: "name", value: "twitter:description" },
|
|
713
|
+
twitterImage: { attribute: "name", value: "twitter:image" },
|
|
714
|
+
twitterImageAlt: { attribute: "name", value: "twitter:image:alt" },
|
|
715
|
+
twitterSite: { attribute: "name", value: "twitter:site" },
|
|
716
|
+
twitterCreator: { attribute: "name", value: "twitter:creator" },
|
|
717
|
+
twitterUrl: { attribute: "name", value: "twitter:url" },
|
|
718
|
+
twitterDomain: { attribute: "name", value: "twitter:domain" },
|
|
719
|
+
twitterLabel1: { attribute: "name", value: "twitter:label1" },
|
|
720
|
+
twitterData1: { attribute: "name", value: "twitter:data1" },
|
|
721
|
+
twitterLabel2: { attribute: "name", value: "twitter:label2" },
|
|
722
|
+
twitterData2: { attribute: "name", value: "twitter:data2" }
|
|
723
|
+
};
|
|
724
|
+
function seoMetaToHead(input) {
|
|
725
|
+
const head = {};
|
|
726
|
+
const meta = [];
|
|
727
|
+
for (const [key, value] of Object.entries(input)) {
|
|
728
|
+
if (key === "title") {
|
|
729
|
+
const title = firstSeoMetaValue(value);
|
|
730
|
+
if (title !== null) {
|
|
731
|
+
head.title = title;
|
|
732
|
+
}
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
const tag = seoMetaTagForKey(key);
|
|
736
|
+
for (const content of normalizeSeoMetaValues(value)) {
|
|
737
|
+
meta.push({ [tag.attribute]: tag.value, content });
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (meta.length > 0) {
|
|
741
|
+
head.meta = meta;
|
|
742
|
+
}
|
|
743
|
+
return head;
|
|
744
|
+
}
|
|
745
|
+
function seoMetaTagForKey(key) {
|
|
746
|
+
const exact = seoMetaTags[key];
|
|
747
|
+
if (exact) {
|
|
748
|
+
return exact;
|
|
749
|
+
}
|
|
750
|
+
if (hasMetaPrefix(key, "og")) {
|
|
751
|
+
return { attribute: "property", value: `og:${camelCaseToMetaName(key.slice(2), "_")}` };
|
|
752
|
+
}
|
|
753
|
+
if (hasMetaPrefix(key, "twitter")) {
|
|
754
|
+
return { attribute: "name", value: `twitter:${camelCaseToMetaName(key.slice(7), "_")}` };
|
|
755
|
+
}
|
|
756
|
+
for (const prefix of ["fb", "article", "book", "profile", "music", "video", "al"]) {
|
|
757
|
+
if (hasMetaPrefix(key, prefix)) {
|
|
758
|
+
return { attribute: "property", value: `${prefix}:${camelCaseToMetaName(key.slice(prefix.length), "_")}` };
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return { attribute: "name", value: camelCaseToMetaName(key, "-") };
|
|
762
|
+
}
|
|
763
|
+
function hasMetaPrefix(key, prefix) {
|
|
764
|
+
return key.startsWith(prefix) && key.length > prefix.length && /[A-Z]/.test(key[prefix.length]);
|
|
765
|
+
}
|
|
766
|
+
function normalizeSeoMetaValues(value) {
|
|
767
|
+
const values = Array.isArray(value) ? value : [value];
|
|
768
|
+
return values
|
|
769
|
+
.map((entry) => normalizeSeoMetaValue(entry))
|
|
770
|
+
.filter((entry) => entry !== null);
|
|
771
|
+
}
|
|
772
|
+
function firstSeoMetaValue(value) {
|
|
773
|
+
return normalizeSeoMetaValues(value)[0] ?? null;
|
|
774
|
+
}
|
|
775
|
+
function normalizeSeoMetaValue(value) {
|
|
776
|
+
if (value === null || value === undefined || value === false) {
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
return String(value);
|
|
780
|
+
}
|
|
781
|
+
function camelCaseToMetaName(value, separator) {
|
|
782
|
+
return value
|
|
783
|
+
.replace(/^[A-Z]/, (match) => match.toLowerCase())
|
|
784
|
+
.replace(/[A-Z]/g, (match) => `${separator}${match.toLowerCase()}`);
|
|
785
|
+
}
|
|
786
|
+
function mergeHead(entries) {
|
|
787
|
+
const merged = {
|
|
788
|
+
meta: [],
|
|
789
|
+
link: []
|
|
790
|
+
};
|
|
791
|
+
for (const entry of entries) {
|
|
792
|
+
if (entry.title) {
|
|
793
|
+
merged.title = entry.title;
|
|
794
|
+
}
|
|
795
|
+
if (entry.meta) {
|
|
796
|
+
merged.meta.push(...entry.meta);
|
|
797
|
+
}
|
|
798
|
+
if (entry.link) {
|
|
799
|
+
merged.link.push(...entry.link);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return merged;
|
|
803
|
+
}
|
|
804
|
+
function renderHead(head) {
|
|
805
|
+
const tags = [];
|
|
806
|
+
tags.push(`<title>${escapeHtml(head.title ?? "Resux App")}</title>`);
|
|
807
|
+
for (const meta of head.meta ?? []) {
|
|
808
|
+
tags.push(`<meta data-rx-head="true" ${renderAttributes(meta)}>`);
|
|
809
|
+
}
|
|
810
|
+
for (const link of head.link ?? []) {
|
|
811
|
+
tags.push(`<link data-rx-head="true" ${renderAttributes(link)}>`);
|
|
812
|
+
}
|
|
813
|
+
return tags.join("");
|
|
814
|
+
}
|
|
815
|
+
function renderAttributes(attributes) {
|
|
816
|
+
return Object.entries(attributes)
|
|
817
|
+
.map(([key, value]) => `${key}="${escapeAttribute(value)}"`)
|
|
818
|
+
.join(" ");
|
|
819
|
+
}
|
|
820
|
+
function resolveLayoutName(node, context, locals) {
|
|
821
|
+
const dynamic = node.attrs.find((attr) => attr.kind === "dynamic" && attr.name === "name");
|
|
822
|
+
if (dynamic) {
|
|
823
|
+
const value = evaluateExpression(dynamic.value, context.scope, locals);
|
|
824
|
+
return value === false ? false : stringifyValue(value);
|
|
825
|
+
}
|
|
826
|
+
const staticName = node.attrs.find((attr) => attr.kind === "static" && attr.name === "name");
|
|
827
|
+
if (staticName) {
|
|
828
|
+
return staticName.value;
|
|
829
|
+
}
|
|
830
|
+
return context.pageMeta.layout;
|
|
831
|
+
}
|
|
832
|
+
function normalizeLayoutName(name) {
|
|
833
|
+
return name
|
|
834
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
835
|
+
.replace(/[^A-Za-z0-9]+/g, "-")
|
|
836
|
+
.replace(/^-|-$/g, "")
|
|
837
|
+
.toLowerCase() || "default";
|
|
838
|
+
}
|
|
839
|
+
export function evaluateExpression(expression, scope, locals = {}) {
|
|
840
|
+
const fn = new Function("scope", "locals", `with (scope) { with (locals) { return (${expression}); } }`);
|
|
841
|
+
return fn(scope, locals);
|
|
842
|
+
}
|
|
843
|
+
export function renderClientPatches(template, scope, scopeId) {
|
|
844
|
+
const patches = [];
|
|
845
|
+
collectPatches(template, scope, scopeId, {}, patches);
|
|
846
|
+
return patches;
|
|
847
|
+
}
|
|
848
|
+
function collectPatches(nodes, scope, scopeId, locals, patches) {
|
|
849
|
+
for (const node of nodes) {
|
|
850
|
+
if (node.type === "interpolation") {
|
|
851
|
+
patches.push({
|
|
852
|
+
type: "text",
|
|
853
|
+
id: node.bindingId,
|
|
854
|
+
value: stringifyValue(evaluateExpression(node.expression, scope, locals))
|
|
855
|
+
});
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (node.type !== "element") {
|
|
859
|
+
continue;
|
|
860
|
+
}
|
|
861
|
+
if (node.for) {
|
|
862
|
+
patches.push({
|
|
863
|
+
type: "block",
|
|
864
|
+
id: node.for.blockId,
|
|
865
|
+
value: renderTemplateNode(node, {
|
|
866
|
+
scope,
|
|
867
|
+
scopeId,
|
|
868
|
+
moduleId: "",
|
|
869
|
+
route: { path: "", params: {}, query: {} },
|
|
870
|
+
components: {},
|
|
871
|
+
layouts: {},
|
|
872
|
+
pageMeta: {}
|
|
873
|
+
}, locals).replace(/^<span[^>]*>|<\/span>$/g, "")
|
|
874
|
+
});
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
if (node.if) {
|
|
878
|
+
patches.push({
|
|
879
|
+
type: "block",
|
|
880
|
+
id: node.if.blockId,
|
|
881
|
+
value: renderTemplateNode(node, {
|
|
882
|
+
scope,
|
|
883
|
+
scopeId,
|
|
884
|
+
moduleId: "",
|
|
885
|
+
route: { path: "", params: {}, query: {} },
|
|
886
|
+
components: {},
|
|
887
|
+
layouts: {},
|
|
888
|
+
pageMeta: {}
|
|
889
|
+
}, locals).replace(/^<span[^>]*>|<\/span>$/g, "")
|
|
890
|
+
});
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
if (node.html) {
|
|
894
|
+
patches.push({
|
|
895
|
+
type: "html",
|
|
896
|
+
id: node.html.bindingId,
|
|
897
|
+
value: sanitizeHtml(evaluateExpression(node.html.expression, scope, locals))
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
for (const attr of node.attrs) {
|
|
901
|
+
if (attr.kind === "dynamic" && attr.bindingId) {
|
|
902
|
+
patches.push({
|
|
903
|
+
type: "attr",
|
|
904
|
+
id: attr.bindingId,
|
|
905
|
+
attr: nativeAttributeName(node, attr.name),
|
|
906
|
+
value: stringifyAttributeValue(nativeAttributeName(node, attr.name), evaluateExpression(attr.value, scope, locals))
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
collectPatches(node.children, scope, scopeId, locals, patches);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function nativeElementTag(node) {
|
|
914
|
+
return node.tag === "ResuxLink" ? "a" : node.tag;
|
|
915
|
+
}
|
|
916
|
+
function nativeAttributeName(node, name) {
|
|
917
|
+
return node.tag === "ResuxLink" && name === "to" ? "href" : name;
|
|
918
|
+
}
|
|
919
|
+
function collectComponentProps(node, scope, locals) {
|
|
920
|
+
const props = {};
|
|
921
|
+
for (const attr of node.attrs) {
|
|
922
|
+
const name = normalizePropName(attr.name);
|
|
923
|
+
props[name] = attr.kind === "static"
|
|
924
|
+
? attr.value
|
|
925
|
+
: evaluateExpression(attr.value, scope, locals);
|
|
926
|
+
}
|
|
927
|
+
return props;
|
|
928
|
+
}
|
|
929
|
+
function normalizePropName(name) {
|
|
930
|
+
return name.replace(/-([a-zA-Z0-9])/g, (_, char) => char.toUpperCase());
|
|
931
|
+
}
|
|
932
|
+
export function getClientRuntimeSource() {
|
|
933
|
+
return String.raw `
|
|
934
|
+
const scopeCache = new Map();
|
|
935
|
+
const routePayloadCache = new Map();
|
|
936
|
+
const mountedVueIslands = new Map();
|
|
937
|
+
let devImportRevision = 0;
|
|
938
|
+
let routeTransitionToken = 0;
|
|
939
|
+
let routeTransitionHideTimer = 0;
|
|
940
|
+
|
|
941
|
+
export function createClientComponent(definition) {
|
|
942
|
+
return {
|
|
943
|
+
async createScope(serializedScope, route) {
|
|
944
|
+
const stateRefs = {};
|
|
945
|
+
const asyncDataRefs = {};
|
|
946
|
+
const pendingCompletions = [];
|
|
947
|
+
const mountedCallbacks = [];
|
|
948
|
+
const props = serializedScope.props ?? {};
|
|
949
|
+
const setupContext = {
|
|
950
|
+
props,
|
|
951
|
+
useState(key, factory) {
|
|
952
|
+
if (!stateRefs[key]) {
|
|
953
|
+
const hasValue = serializedScope.state && Object.prototype.hasOwnProperty.call(serializedScope.state, key);
|
|
954
|
+
stateRefs[key] = { value: hasValue ? serializedScope.state[key] : factory?.() };
|
|
955
|
+
}
|
|
956
|
+
return stateRefs[key];
|
|
957
|
+
},
|
|
958
|
+
useAsyncData(key, handler) {
|
|
959
|
+
if (!asyncDataRefs[key]) {
|
|
960
|
+
const snapshot = serializedScope.asyncData ? serializedScope.asyncData[key] : undefined;
|
|
961
|
+
const resource = createAsyncDataResource(
|
|
962
|
+
snapshot?.value,
|
|
963
|
+
snapshot?.pending ?? false,
|
|
964
|
+
snapshot?.error ?? null
|
|
965
|
+
);
|
|
966
|
+
asyncDataRefs[key] = resource;
|
|
967
|
+
if (resource.pending && typeof handler === "function") {
|
|
968
|
+
const completion = settleAsyncDataResource(resource, handler, key);
|
|
969
|
+
resource.setCompletion(completion);
|
|
970
|
+
pendingCompletions.push(completion);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return asyncDataRefs[key];
|
|
974
|
+
},
|
|
975
|
+
defineProps() {
|
|
976
|
+
return props;
|
|
977
|
+
},
|
|
978
|
+
useRoute() {
|
|
979
|
+
return route;
|
|
980
|
+
},
|
|
981
|
+
useHead() {
|
|
982
|
+
// Head updates are server-rendered in the MVP.
|
|
983
|
+
},
|
|
984
|
+
useSeoMeta() {
|
|
985
|
+
// SEO meta updates are server-rendered in the MVP.
|
|
986
|
+
},
|
|
987
|
+
useRuntimeConfig() {
|
|
988
|
+
return globalThis.__RESUX__?.config ?? { public: {} };
|
|
989
|
+
},
|
|
990
|
+
useResuxApp() {
|
|
991
|
+
return {
|
|
992
|
+
route,
|
|
993
|
+
payload: globalThis.__RESUX__,
|
|
994
|
+
$config: globalThis.__RESUX__?.config ?? { public: {} },
|
|
995
|
+
provides: {},
|
|
996
|
+
provide() {}
|
|
997
|
+
};
|
|
998
|
+
},
|
|
999
|
+
async useFetch(url) {
|
|
1000
|
+
return { value: await setupContext.$fetch(url) };
|
|
1001
|
+
},
|
|
1002
|
+
async $fetch(url) {
|
|
1003
|
+
const response = await fetch(url);
|
|
1004
|
+
if (!response.ok) {
|
|
1005
|
+
throw new Error("Fetch failed for " + url + ": " + response.status);
|
|
1006
|
+
}
|
|
1007
|
+
return response.json();
|
|
1008
|
+
},
|
|
1009
|
+
onMounted(callback) {
|
|
1010
|
+
if (typeof callback === "function") {
|
|
1011
|
+
mountedCallbacks.push(callback);
|
|
1012
|
+
}
|
|
1013
|
+
},
|
|
1014
|
+
definePageMeta() {
|
|
1015
|
+
// Page meta is compiled statically in Resux.
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
const scope = await definition.script(setupContext);
|
|
1019
|
+
await Promise.allSettled(mountedCallbacks.map((callback) => callback()));
|
|
1020
|
+
return { scope, props, stateRefs, asyncDataRefs, pendingCompletions };
|
|
1021
|
+
},
|
|
1022
|
+
async run(scopeRecord, handlerName, event) {
|
|
1023
|
+
const handler = scopeRecord.scope[handlerName];
|
|
1024
|
+
if (typeof handler !== "function") {
|
|
1025
|
+
throw new Error("Missing resumable handler " + handlerName + ".");
|
|
1026
|
+
}
|
|
1027
|
+
await handler(event);
|
|
1028
|
+
return renderClientPatches(definition.template, scopeRecord.scope);
|
|
1029
|
+
},
|
|
1030
|
+
render(scopeRecord) {
|
|
1031
|
+
return renderClientPatches(definition.template, scopeRecord.scope);
|
|
1032
|
+
},
|
|
1033
|
+
serialize(scopeRecord) {
|
|
1034
|
+
return {
|
|
1035
|
+
props: serializeProps(scopeRecord.props ?? {}),
|
|
1036
|
+
state: serializeRefs(scopeRecord.stateRefs),
|
|
1037
|
+
asyncData: serializeAsyncData(scopeRecord.asyncDataRefs)
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
function installResux() {
|
|
1044
|
+
if (globalThis.__RESUX_INSTALLED__) {
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
globalThis.__RESUX_INSTALLED__ = true;
|
|
1048
|
+
document.addEventListener("click", (event) => {
|
|
1049
|
+
if (handleNavigationClick(event)) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
void handleDelegatedEvent("click", event);
|
|
1053
|
+
});
|
|
1054
|
+
document.addEventListener("pointerover", (event) => {
|
|
1055
|
+
void prefetchNavigationTarget(event);
|
|
1056
|
+
});
|
|
1057
|
+
document.addEventListener("focusin", (event) => {
|
|
1058
|
+
void prefetchNavigationTarget(event);
|
|
1059
|
+
});
|
|
1060
|
+
for (const eventName of ["input", "change", "submit", "keydown", "keyup", "keypress", "mousedown", "mouseup", "blur", "focusout"]) {
|
|
1061
|
+
document.addEventListener(eventName, (event) => {
|
|
1062
|
+
void handleDelegatedEvent(eventName, event);
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
if (typeof window !== "undefined" && typeof history !== "undefined" && typeof location !== "undefined") {
|
|
1066
|
+
window.addEventListener("popstate", () => {
|
|
1067
|
+
void navigateTo(location.pathname + location.search, { replace: true });
|
|
1068
|
+
});
|
|
1069
|
+
if (!history.state || !history.state.__resux) {
|
|
1070
|
+
history.replaceState({ __resux: true, path: location.pathname + location.search }, "", location.href);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
if (typeof window !== "undefined") {
|
|
1074
|
+
window.__RESUX_APPLY_DEV_UPDATE__ = applyDevUpdate;
|
|
1075
|
+
}
|
|
1076
|
+
void resumePendingAsyncData();
|
|
1077
|
+
void mountVueIslands();
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
function createAsyncDataResource(value, pending = false, error = null) {
|
|
1081
|
+
let completion = Promise.resolve();
|
|
1082
|
+
const resource = {
|
|
1083
|
+
value: { value },
|
|
1084
|
+
pending,
|
|
1085
|
+
error,
|
|
1086
|
+
then(onfulfilled, onrejected) {
|
|
1087
|
+
return completion.then(() => {
|
|
1088
|
+
const snapshot = {
|
|
1089
|
+
value: resource.value.value,
|
|
1090
|
+
pending: resource.pending,
|
|
1091
|
+
error: resource.error
|
|
1092
|
+
};
|
|
1093
|
+
return onfulfilled ? onfulfilled(snapshot) : snapshot;
|
|
1094
|
+
}, onrejected);
|
|
1095
|
+
},
|
|
1096
|
+
setCompletion(nextCompletion) {
|
|
1097
|
+
completion = nextCompletion;
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
return resource;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
async function settleAsyncDataResource(resource, handler, key) {
|
|
1105
|
+
try {
|
|
1106
|
+
const value = await handler();
|
|
1107
|
+
assertJsonSerializable(value, 'useAsyncData("' + key + '")');
|
|
1108
|
+
resource.value.value = value;
|
|
1109
|
+
resource.error = null;
|
|
1110
|
+
} catch (error) {
|
|
1111
|
+
resource.value.value = undefined;
|
|
1112
|
+
resource.error = normalizeAsyncDataError(error);
|
|
1113
|
+
} finally {
|
|
1114
|
+
resource.pending = false;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
function serializeAsyncData(refs) {
|
|
1119
|
+
const output = {};
|
|
1120
|
+
for (const [key, ref] of Object.entries(refs)) {
|
|
1121
|
+
output[key] = {
|
|
1122
|
+
value: ref.value.value === undefined ? null : ref.value.value,
|
|
1123
|
+
pending: ref.pending,
|
|
1124
|
+
error: ref.error
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
return output;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function setRouteLoading(active) {
|
|
1131
|
+
setRouteTransition(active ? "fetching" : "idle");
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function setRouteTransition(state, options = {}) {
|
|
1135
|
+
const loader = document.getElementById("__resux-loading");
|
|
1136
|
+
const root = document.getElementById("__resux");
|
|
1137
|
+
if (!loader) {
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
if (routeTransitionHideTimer) {
|
|
1142
|
+
clearTimeout(routeTransitionHideTimer);
|
|
1143
|
+
routeTransitionHideTimer = 0;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
const progress = transitionProgress(state, options.progress);
|
|
1147
|
+
const message = options.message ?? transitionMessage(state);
|
|
1148
|
+
loader.hidden = false;
|
|
1149
|
+
loader.dataset.state = state;
|
|
1150
|
+
loader.style.setProperty("--resux-progress", progress + "%");
|
|
1151
|
+
loader.setAttribute("aria-busy", state === "idle" || state === "complete" ? "false" : "true");
|
|
1152
|
+
|
|
1153
|
+
const progressbar = loader.querySelector("[role='progressbar']");
|
|
1154
|
+
if (progressbar) {
|
|
1155
|
+
progressbar.setAttribute("aria-valuenow", String(progress));
|
|
1156
|
+
progressbar.setAttribute("aria-valuetext", message);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
const label = loader.querySelector("[data-rx-transition-message]");
|
|
1160
|
+
if (label) {
|
|
1161
|
+
label.textContent = message;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
if (root) {
|
|
1165
|
+
if (state === "idle" || state === "complete" || state === "error") {
|
|
1166
|
+
root.removeAttribute("aria-busy");
|
|
1167
|
+
root.removeAttribute("data-route-transition");
|
|
1168
|
+
} else {
|
|
1169
|
+
root.setAttribute("aria-busy", "true");
|
|
1170
|
+
root.setAttribute("data-route-transition", "loading");
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
dispatchRouteTransition(state, options);
|
|
1175
|
+
|
|
1176
|
+
if (state === "idle" || state === "complete") {
|
|
1177
|
+
routeTransitionHideTimer = setTimeout(() => {
|
|
1178
|
+
loader.hidden = true;
|
|
1179
|
+
loader.dataset.state = "idle";
|
|
1180
|
+
loader.style.setProperty("--resux-progress", "0%");
|
|
1181
|
+
progressbar?.setAttribute("aria-valuenow", "0");
|
|
1182
|
+
progressbar?.setAttribute("aria-valuetext", "Idle");
|
|
1183
|
+
}, state === "complete" ? 160 : 0);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
function transitionProgress(state, explicitProgress) {
|
|
1188
|
+
if (typeof explicitProgress === "number") {
|
|
1189
|
+
return Math.max(0, Math.min(100, Math.round(explicitProgress)));
|
|
1190
|
+
}
|
|
1191
|
+
if (state === "start") return 8;
|
|
1192
|
+
if (state === "fetching") return 38;
|
|
1193
|
+
if (state === "swapping") return 76;
|
|
1194
|
+
if (state === "complete") return 100;
|
|
1195
|
+
if (state === "error") return 100;
|
|
1196
|
+
return 0;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
function transitionMessage(state) {
|
|
1200
|
+
if (state === "start") return "Starting navigation";
|
|
1201
|
+
if (state === "fetching") return "Loading route";
|
|
1202
|
+
if (state === "swapping") return "Updating page";
|
|
1203
|
+
if (state === "complete") return "Route loaded";
|
|
1204
|
+
if (state === "error") return "Route failed";
|
|
1205
|
+
return "Ready";
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
function dispatchRouteTransition(state, detail = {}) {
|
|
1209
|
+
if (typeof window === "undefined" || typeof window.CustomEvent !== "function") {
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
window.dispatchEvent(new window.CustomEvent("resux:route-transition", {
|
|
1213
|
+
detail: {
|
|
1214
|
+
state,
|
|
1215
|
+
path: detail.path,
|
|
1216
|
+
message: detail.message ?? transitionMessage(state),
|
|
1217
|
+
progress: transitionProgress(state, detail.progress)
|
|
1218
|
+
}
|
|
1219
|
+
}));
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
function handleNavigationClick(event) {
|
|
1223
|
+
if (
|
|
1224
|
+
event.defaultPrevented ||
|
|
1225
|
+
event.button !== 0 ||
|
|
1226
|
+
event.metaKey ||
|
|
1227
|
+
event.ctrlKey ||
|
|
1228
|
+
event.shiftKey ||
|
|
1229
|
+
event.altKey
|
|
1230
|
+
) {
|
|
1231
|
+
return false;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
const anchor = event.target && event.target.closest
|
|
1235
|
+
? event.target.closest("a[href]")
|
|
1236
|
+
: null;
|
|
1237
|
+
if (!anchor || anchor.target || anchor.hasAttribute("download")) {
|
|
1238
|
+
return false;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
const actionTarget = event.target && event.target.closest
|
|
1242
|
+
? event.target.closest("[data-rx-on-click]")
|
|
1243
|
+
: null;
|
|
1244
|
+
if (actionTarget && readEventModifiers(actionTarget, "click").includes("prevent")) {
|
|
1245
|
+
return false;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
const target = new URL(anchor.getAttribute("href"), location.href);
|
|
1249
|
+
if (target.origin !== location.origin) {
|
|
1250
|
+
return false;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
const nextPath = target.pathname + target.search;
|
|
1254
|
+
const currentPath = location.pathname + location.search;
|
|
1255
|
+
if (nextPath === currentPath && target.hash) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
event.preventDefault();
|
|
1260
|
+
void navigateTo(nextPath + target.hash);
|
|
1261
|
+
return true;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
async function navigateTo(target, options = {}) {
|
|
1265
|
+
const nextUrl = new URL(target, location.href);
|
|
1266
|
+
const routePath = nextUrl.pathname + nextUrl.search;
|
|
1267
|
+
const transitionToken = ++routeTransitionToken;
|
|
1268
|
+
let completed = false;
|
|
1269
|
+
setRouteTransition("start", { path: routePath });
|
|
1270
|
+
|
|
1271
|
+
try {
|
|
1272
|
+
setRouteTransition("fetching", { path: routePath });
|
|
1273
|
+
const result = await fetchRoutePayload(routePath);
|
|
1274
|
+
if (result.redirect) {
|
|
1275
|
+
await navigateTo(result.redirect, { replace: true });
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
const root = document.getElementById("__resux");
|
|
1280
|
+
if (!root) {
|
|
1281
|
+
location.href = nextUrl.href;
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
setRouteTransition("swapping", { path: routePath });
|
|
1286
|
+
const previousPayload = globalThis.__RESUX__;
|
|
1287
|
+
const preserved = replaceRouteHtml(root, result.html);
|
|
1288
|
+
globalThis.__RESUX__ = mergePersistentLayoutPayload(previousPayload, result.payload, preserved.scopeIds);
|
|
1289
|
+
applyHead(result.head);
|
|
1290
|
+
clearScopeCacheExcept(preserved.scopeIds);
|
|
1291
|
+
void resumePendingAsyncData();
|
|
1292
|
+
void mountVueIslands(preserved.root);
|
|
1293
|
+
|
|
1294
|
+
if (options.replace) {
|
|
1295
|
+
history.replaceState({ __resux: true, path: routePath }, "", nextUrl.href);
|
|
1296
|
+
} else {
|
|
1297
|
+
history.pushState({ __resux: true, path: routePath }, "", nextUrl.href);
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
if (nextUrl.hash) {
|
|
1301
|
+
document.getElementById(nextUrl.hash.slice(1))?.scrollIntoView();
|
|
1302
|
+
} else if (typeof scrollTo === "function") {
|
|
1303
|
+
scrollTo(0, 0);
|
|
1304
|
+
}
|
|
1305
|
+
if (transitionToken === routeTransitionToken) {
|
|
1306
|
+
setRouteTransition("complete", { path: routePath });
|
|
1307
|
+
completed = true;
|
|
1308
|
+
}
|
|
1309
|
+
} catch {
|
|
1310
|
+
setRouteTransition("error", { path: routePath });
|
|
1311
|
+
location.href = nextUrl.href;
|
|
1312
|
+
} finally {
|
|
1313
|
+
if (!completed && transitionToken === routeTransitionToken) {
|
|
1314
|
+
setRouteTransition("idle", { path: routePath });
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
function replaceRouteHtml(root, html) {
|
|
1320
|
+
const currentLayout = root.querySelector("[data-rx-layout]");
|
|
1321
|
+
const template = document.createElement("template");
|
|
1322
|
+
template.innerHTML = html;
|
|
1323
|
+
const nextLayout = template.content.querySelector("[data-rx-layout]");
|
|
1324
|
+
|
|
1325
|
+
if (!currentLayout || !nextLayout || currentLayout.getAttribute("data-rx-layout") !== nextLayout.getAttribute("data-rx-layout")) {
|
|
1326
|
+
unmountVueIslands(root);
|
|
1327
|
+
root.innerHTML = html;
|
|
1328
|
+
return { root, scopeIds: new Set() };
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
const currentPage = currentLayout.querySelector("[data-rx-page]");
|
|
1332
|
+
const nextPage = nextLayout.querySelector("[data-rx-page]");
|
|
1333
|
+
if (!currentPage || !nextPage) {
|
|
1334
|
+
unmountVueIslands(root);
|
|
1335
|
+
root.innerHTML = html;
|
|
1336
|
+
return { root, scopeIds: new Set() };
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
const preservedScopeIds = collectScopeIds(currentLayout, currentPage);
|
|
1340
|
+
unmountVueIslands(currentPage);
|
|
1341
|
+
currentPage.innerHTML = nextPage.innerHTML;
|
|
1342
|
+
return { root: currentPage, scopeIds: preservedScopeIds };
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
function mergePersistentLayoutPayload(previousPayload, nextPayload, preservedScopeIds) {
|
|
1346
|
+
if (!previousPayload || !previousPayload.scopes || !nextPayload || !nextPayload.scopes) {
|
|
1347
|
+
return nextPayload;
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
for (const scopeId of preservedScopeIds) {
|
|
1351
|
+
if (previousPayload.scopes[scopeId]) {
|
|
1352
|
+
nextPayload.scopes[scopeId] = previousPayload.scopes[scopeId];
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
return nextPayload;
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
function clearScopeCacheExcept(preservedScopeIds) {
|
|
1360
|
+
for (const scopeId of scopeCache.keys()) {
|
|
1361
|
+
if (!preservedScopeIds.has(scopeId)) {
|
|
1362
|
+
scopeCache.delete(scopeId);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
async function applyDevUpdate(payload = {}) {
|
|
1368
|
+
devImportRevision = Number(payload.revision ?? Date.now());
|
|
1369
|
+
routePayloadCache.clear();
|
|
1370
|
+
await hotUpdateActiveScopes();
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
async function hotUpdateActiveScopes() {
|
|
1374
|
+
const payload = globalThis.__RESUX__;
|
|
1375
|
+
if (!payload || !payload.scopes || !payload.modules) {
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
await Promise.all([...scopeCache.entries()].map(async ([scopeId, scopeRecord]) => {
|
|
1380
|
+
const serializedScope = payload.scopes[scopeId];
|
|
1381
|
+
if (!serializedScope) {
|
|
1382
|
+
scopeCache.delete(scopeId);
|
|
1383
|
+
return;
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
const component = await importComponent(serializedScope.moduleId, payload.modules, devImportRevision);
|
|
1387
|
+
const snapshot = {
|
|
1388
|
+
...serializedScope,
|
|
1389
|
+
...component.serialize(scopeRecord)
|
|
1390
|
+
};
|
|
1391
|
+
const nextScopeRecord = await component.createScope(snapshot, payload.route);
|
|
1392
|
+
scopeCache.set(scopeId, nextScopeRecord);
|
|
1393
|
+
const patches = component.render(nextScopeRecord);
|
|
1394
|
+
const serialized = component.serialize(nextScopeRecord);
|
|
1395
|
+
payload.scopes[scopeId].props = serialized.props;
|
|
1396
|
+
payload.scopes[scopeId].state = serialized.state;
|
|
1397
|
+
payload.scopes[scopeId].asyncData = serialized.asyncData;
|
|
1398
|
+
applyPatches(scopeId, patches);
|
|
1399
|
+
}));
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
function collectScopeIds(root, exclude) {
|
|
1403
|
+
const ids = new Set();
|
|
1404
|
+
|
|
1405
|
+
for (const element of [root, ...Array.from(root.querySelectorAll ? root.querySelectorAll("*") : [])]) {
|
|
1406
|
+
if (exclude && (element === exclude || exclude.contains(element))) {
|
|
1407
|
+
continue;
|
|
1408
|
+
}
|
|
1409
|
+
collectScopeIdFromElement(element, ids);
|
|
1410
|
+
}
|
|
1411
|
+
return ids;
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
function collectScopeIdFromElement(element, ids) {
|
|
1415
|
+
for (const attribute of Array.from(element.attributes ?? [])) {
|
|
1416
|
+
const value = attribute.value;
|
|
1417
|
+
if (!value || !value.includes(":")) {
|
|
1418
|
+
continue;
|
|
1419
|
+
}
|
|
1420
|
+
const scopeId = value.split(":")[0];
|
|
1421
|
+
if (scopeId) {
|
|
1422
|
+
ids.add(scopeId);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
async function mountVueIslands(root = document) {
|
|
1428
|
+
const payload = globalThis.__RESUX__;
|
|
1429
|
+
const islands = payload && payload.vueIslands ? payload.vueIslands : {};
|
|
1430
|
+
const elements = root.querySelectorAll ? root.querySelectorAll("[data-rx-vue-island]") : [];
|
|
1431
|
+
|
|
1432
|
+
for (const el of elements) {
|
|
1433
|
+
if (mountedVueIslands.has(el)) {
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
const name = el.getAttribute("data-rx-vue-island");
|
|
1438
|
+
const modulePath = name ? islands[name] : null;
|
|
1439
|
+
if (!name || !modulePath) {
|
|
1440
|
+
el.setAttribute("data-rx-vue-error", "missing");
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
try {
|
|
1445
|
+
const props = JSON.parse(el.getAttribute("data-rx-vue-props") || "{}");
|
|
1446
|
+
const island = await import(/* @vite-ignore */ modulePath);
|
|
1447
|
+
const app = island.mount ? island.mount(el, props) : null;
|
|
1448
|
+
mountedVueIslands.set(el, app);
|
|
1449
|
+
} catch {
|
|
1450
|
+
el.setAttribute("data-rx-vue-error", "mount");
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
function unmountVueIslands(root) {
|
|
1456
|
+
for (const [el, app] of mountedVueIslands.entries()) {
|
|
1457
|
+
if (root && root !== el && !(root.contains && root.contains(el))) {
|
|
1458
|
+
continue;
|
|
1459
|
+
}
|
|
1460
|
+
if (app && typeof app.unmount === "function") {
|
|
1461
|
+
app.unmount();
|
|
1462
|
+
}
|
|
1463
|
+
mountedVueIslands.delete(el);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
async function prefetchNavigationTarget(event) {
|
|
1468
|
+
const anchor = event.target && event.target.closest
|
|
1469
|
+
? event.target.closest("a[href]")
|
|
1470
|
+
: null;
|
|
1471
|
+
const routePath = getPrefetchPath(anchor);
|
|
1472
|
+
if (!routePath || routePayloadCache.has(routePath)) {
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
try {
|
|
1477
|
+
routePayloadCache.set(routePath, await fetchRoutePayload(routePath));
|
|
1478
|
+
} catch {
|
|
1479
|
+
routePayloadCache.delete(routePath);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
function getPrefetchPath(anchor) {
|
|
1484
|
+
if (!anchor || anchor.target || anchor.hasAttribute("download")) {
|
|
1485
|
+
return null;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
const target = new URL(anchor.getAttribute("href"), location.href);
|
|
1489
|
+
if (target.origin !== location.origin) {
|
|
1490
|
+
return null;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
const routePath = target.pathname + target.search;
|
|
1494
|
+
if (routePath === location.pathname + location.search) {
|
|
1495
|
+
return null;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
return routePath;
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
async function fetchRoutePayload(routePath) {
|
|
1502
|
+
if (routePayloadCache.has(routePath)) {
|
|
1503
|
+
return routePayloadCache.get(routePath);
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
const response = await fetch("/__resux/route?path=" + encodeURIComponent(routePath), {
|
|
1507
|
+
headers: {
|
|
1508
|
+
accept: "application/json"
|
|
1509
|
+
}
|
|
1510
|
+
});
|
|
1511
|
+
|
|
1512
|
+
if (!response.ok) {
|
|
1513
|
+
throw new Error("Route payload request failed: " + response.status);
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
const result = await response.json();
|
|
1517
|
+
routePayloadCache.set(routePath, result);
|
|
1518
|
+
return result;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
function applyHead(head) {
|
|
1522
|
+
if (!head) {
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
if (head.title) {
|
|
1527
|
+
document.title = head.title;
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
document.querySelectorAll("[data-rx-head]").forEach((element) => element.remove());
|
|
1531
|
+
|
|
1532
|
+
for (const meta of head.meta ?? []) {
|
|
1533
|
+
const element = document.createElement("meta");
|
|
1534
|
+
element.setAttribute("data-rx-head", "true");
|
|
1535
|
+
for (const [key, value] of Object.entries(meta)) {
|
|
1536
|
+
element.setAttribute(key, value);
|
|
1537
|
+
}
|
|
1538
|
+
document.head.appendChild(element);
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
for (const link of head.link ?? []) {
|
|
1542
|
+
const element = document.createElement("link");
|
|
1543
|
+
element.setAttribute("data-rx-head", "true");
|
|
1544
|
+
for (const [key, value] of Object.entries(link)) {
|
|
1545
|
+
element.setAttribute(key, value);
|
|
1546
|
+
}
|
|
1547
|
+
document.head.appendChild(element);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
async function handleDelegatedEvent(eventName, event) {
|
|
1552
|
+
const target = event.target && event.target.closest
|
|
1553
|
+
? event.target.closest("[data-rx-on-" + eventName + "]")
|
|
1554
|
+
: null;
|
|
1555
|
+
if (!target) {
|
|
1556
|
+
return;
|
|
1557
|
+
}
|
|
1558
|
+
const modifiers = readEventModifiers(target, eventName);
|
|
1559
|
+
if (modifiers.includes("self") && event.target !== target) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
if (!eventMatchesModifiers(event, modifiers, eventName)) {
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
const onceMarker = "data-rx-once-" + eventName;
|
|
1566
|
+
if (modifiers.includes("once")) {
|
|
1567
|
+
if (target.hasAttribute(onceMarker)) {
|
|
1568
|
+
return;
|
|
1569
|
+
}
|
|
1570
|
+
target.setAttribute(onceMarker, "true");
|
|
1571
|
+
}
|
|
1572
|
+
if ((eventName === "submit" || modifiers.includes("prevent")) && !modifiers.includes("passive")) {
|
|
1573
|
+
event.preventDefault();
|
|
1574
|
+
}
|
|
1575
|
+
if (modifiers.includes("stop")) {
|
|
1576
|
+
event.stopPropagation();
|
|
1577
|
+
}
|
|
1578
|
+
const marker = target.getAttribute("data-rx-on-" + eventName);
|
|
1579
|
+
const [scopeId, moduleId, handlerName] = marker.split(":");
|
|
1580
|
+
const payload = globalThis.__RESUX__;
|
|
1581
|
+
const component = await importComponent(moduleId, payload.modules, devImportRevision);
|
|
1582
|
+
let scopeRecord = scopeCache.get(scopeId);
|
|
1583
|
+
if (!scopeRecord) {
|
|
1584
|
+
scopeRecord = await component.createScope(payload.scopes[scopeId], payload.route);
|
|
1585
|
+
scopeCache.set(scopeId, scopeRecord);
|
|
1586
|
+
}
|
|
1587
|
+
const patches = await component.run(scopeRecord, handlerName, event);
|
|
1588
|
+
const serialized = component.serialize(scopeRecord);
|
|
1589
|
+
payload.scopes[scopeId].props = serialized.props;
|
|
1590
|
+
payload.scopes[scopeId].state = serialized.state;
|
|
1591
|
+
payload.scopes[scopeId].asyncData = serialized.asyncData;
|
|
1592
|
+
applyPatches(scopeId, patches);
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
function readEventModifiers(target, eventName) {
|
|
1596
|
+
return (target.getAttribute("data-rx-mod-" + eventName) || "")
|
|
1597
|
+
.split(",")
|
|
1598
|
+
.map((modifier) => modifier.trim())
|
|
1599
|
+
.filter(Boolean);
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
function eventMatchesModifiers(event, modifiers, eventName) {
|
|
1603
|
+
if (modifiers.includes("ctrl") && !event.ctrlKey) return false;
|
|
1604
|
+
if (modifiers.includes("shift") && !event.shiftKey) return false;
|
|
1605
|
+
if (modifiers.includes("alt") && !event.altKey) return false;
|
|
1606
|
+
if (modifiers.includes("meta") && !event.metaKey) return false;
|
|
1607
|
+
if (modifiers.includes("exact")) {
|
|
1608
|
+
const expected = new Set(modifiers.filter((modifier) => ["ctrl", "shift", "alt", "meta"].includes(modifier)));
|
|
1609
|
+
if (event.ctrlKey !== expected.has("ctrl")) return false;
|
|
1610
|
+
if (event.shiftKey !== expected.has("shift")) return false;
|
|
1611
|
+
if (event.altKey !== expected.has("alt")) return false;
|
|
1612
|
+
if (event.metaKey !== expected.has("meta")) return false;
|
|
1613
|
+
}
|
|
1614
|
+
if ((eventName === "click" || eventName === "mousedown" || eventName === "mouseup") && !mouseButtonMatches(event, modifiers)) {
|
|
1615
|
+
return false;
|
|
1616
|
+
}
|
|
1617
|
+
if (eventName.startsWith("key") && !keyMatches(event, modifiers)) {
|
|
1618
|
+
return false;
|
|
1619
|
+
}
|
|
1620
|
+
return true;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function mouseButtonMatches(event, modifiers) {
|
|
1624
|
+
const buttonModifiers = modifiers.filter((modifier) => ["left", "middle", "right"].includes(modifier));
|
|
1625
|
+
if (!buttonModifiers.length) {
|
|
1626
|
+
return true;
|
|
1627
|
+
}
|
|
1628
|
+
return buttonModifiers.some((modifier) => {
|
|
1629
|
+
if (modifier === "left") return event.button === 0;
|
|
1630
|
+
if (modifier === "middle") return event.button === 1;
|
|
1631
|
+
return event.button === 2;
|
|
1632
|
+
});
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function keyMatches(event, modifiers) {
|
|
1636
|
+
const aliases = {
|
|
1637
|
+
enter: "Enter",
|
|
1638
|
+
tab: "Tab",
|
|
1639
|
+
delete: ["Delete", "Backspace"],
|
|
1640
|
+
esc: "Escape",
|
|
1641
|
+
escape: "Escape",
|
|
1642
|
+
space: " ",
|
|
1643
|
+
up: "ArrowUp",
|
|
1644
|
+
down: "ArrowDown",
|
|
1645
|
+
left: "ArrowLeft",
|
|
1646
|
+
right: "ArrowRight"
|
|
1647
|
+
};
|
|
1648
|
+
const keyModifiers = modifiers.filter((modifier) => Object.prototype.hasOwnProperty.call(aliases, modifier));
|
|
1649
|
+
if (!keyModifiers.length) {
|
|
1650
|
+
return true;
|
|
1651
|
+
}
|
|
1652
|
+
return keyModifiers.some((modifier) => {
|
|
1653
|
+
const expected = aliases[modifier];
|
|
1654
|
+
return Array.isArray(expected) ? expected.includes(event.key) : event.key === expected;
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
async function resumePendingAsyncData() {
|
|
1659
|
+
const payload = globalThis.__RESUX__;
|
|
1660
|
+
if (!payload || !payload.scopes || !payload.modules) {
|
|
1661
|
+
return;
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
await Promise.all(Object.entries(payload.scopes).map(async ([scopeId, serializedScope]) => {
|
|
1665
|
+
if (!hasPendingAsyncData(serializedScope)) {
|
|
1666
|
+
return;
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
const component = await importComponent(serializedScope.moduleId, payload.modules, devImportRevision).catch(() => null);
|
|
1670
|
+
if (!component) {
|
|
1671
|
+
return;
|
|
1672
|
+
}
|
|
1673
|
+
let scopeRecord = scopeCache.get(scopeId);
|
|
1674
|
+
if (!scopeRecord) {
|
|
1675
|
+
scopeRecord = await component.createScope(serializedScope, payload.route);
|
|
1676
|
+
scopeCache.set(scopeId, scopeRecord);
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
const completions = scopeRecord.pendingCompletions ?? [];
|
|
1680
|
+
if (completions.length === 0) {
|
|
1681
|
+
return;
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
await Promise.allSettled(completions);
|
|
1685
|
+
scopeRecord.pendingCompletions = [];
|
|
1686
|
+
const patches = component.render(scopeRecord);
|
|
1687
|
+
const serialized = component.serialize(scopeRecord);
|
|
1688
|
+
payload.scopes[scopeId].props = serialized.props;
|
|
1689
|
+
payload.scopes[scopeId].state = serialized.state;
|
|
1690
|
+
payload.scopes[scopeId].asyncData = serialized.asyncData;
|
|
1691
|
+
applyPatches(scopeId, patches);
|
|
1692
|
+
}));
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
function hasPendingAsyncData(serializedScope) {
|
|
1696
|
+
return Object.values(serializedScope.asyncData ?? {}).some((entry) => entry && entry.pending);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
async function importComponent(moduleId, modules, revision = 0) {
|
|
1700
|
+
const modulePath = modules[moduleId];
|
|
1701
|
+
if (!modulePath) {
|
|
1702
|
+
throw new Error("Missing resumable module " + moduleId + ".");
|
|
1703
|
+
}
|
|
1704
|
+
const cacheBustedPath = revision ? appendImportRevision(modulePath, revision) : modulePath;
|
|
1705
|
+
return (await import(/* @vite-ignore */ cacheBustedPath)).default;
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
function appendImportRevision(modulePath, revision) {
|
|
1709
|
+
const separator = modulePath.includes("?") ? "&" : "?";
|
|
1710
|
+
return modulePath + separator + "t=" + encodeURIComponent(String(revision));
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
function renderClientPatches(template, scope) {
|
|
1714
|
+
const patches = [];
|
|
1715
|
+
collectPatches(template, scope, {}, patches);
|
|
1716
|
+
return patches;
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
function collectPatches(nodes, scope, locals, patches) {
|
|
1720
|
+
for (const node of nodes) {
|
|
1721
|
+
if (node.type === "interpolation") {
|
|
1722
|
+
patches.push({ type: "text", id: node.bindingId, value: stringifyValue(evaluateExpression(node.expression, scope, locals)) });
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
if (node.type !== "element") {
|
|
1726
|
+
continue;
|
|
1727
|
+
}
|
|
1728
|
+
if (node.for) {
|
|
1729
|
+
patches.push({ type: "block", id: node.for.blockId, value: renderNode(node, scope, locals).replace(/^<span[^>]*>|<\/span>$/g, "") });
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
if (node.if) {
|
|
1733
|
+
patches.push({ type: "block", id: node.if.blockId, value: renderNode(node, scope, locals).replace(/^<span[^>]*>|<\/span>$/g, "") });
|
|
1734
|
+
continue;
|
|
1735
|
+
}
|
|
1736
|
+
if (node.html) {
|
|
1737
|
+
patches.push({ type: "html", id: node.html.bindingId, value: sanitizeHtml(evaluateExpression(node.html.expression, scope, locals)) });
|
|
1738
|
+
}
|
|
1739
|
+
for (const attr of node.attrs) {
|
|
1740
|
+
if (attr.kind === "dynamic" && attr.bindingId) {
|
|
1741
|
+
const attrName = nativeAttributeName(node, attr.name);
|
|
1742
|
+
patches.push({ type: "attr", id: attr.bindingId, attr: attrName, value: stringifyAttributeValue(attrName, evaluateExpression(attr.value, scope, locals)) });
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
collectPatches(node.children, scope, locals, patches);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
function renderNode(node, scope, locals) {
|
|
1750
|
+
if (node.type === "text") {
|
|
1751
|
+
return escapeHtml(node.value);
|
|
1752
|
+
}
|
|
1753
|
+
if (node.type === "interpolation") {
|
|
1754
|
+
return '<span data-rx-text=":' + node.bindingId + '">' + escapeHtml(stringifyValue(evaluateExpression(node.expression, scope, locals))) + '</span>';
|
|
1755
|
+
}
|
|
1756
|
+
if (node.for) {
|
|
1757
|
+
const items = evaluateExpression(node.for.source, scope, locals);
|
|
1758
|
+
const rendered = Array.isArray(items)
|
|
1759
|
+
? items.map((item, index) => {
|
|
1760
|
+
const nextLocals = { ...locals, [node.for.value]: item };
|
|
1761
|
+
if (node.for.index) nextLocals[node.for.index] = index;
|
|
1762
|
+
return renderElement({ ...node, for: undefined, if: undefined }, scope, nextLocals);
|
|
1763
|
+
}).join("")
|
|
1764
|
+
: "";
|
|
1765
|
+
return '<span data-rx-block=":' + node.for.blockId + '">' + rendered + '</span>';
|
|
1766
|
+
}
|
|
1767
|
+
if (node.if) {
|
|
1768
|
+
const rendered = evaluateExpression(node.if.expression, scope, locals)
|
|
1769
|
+
? renderElement({ ...node, if: undefined }, scope, locals)
|
|
1770
|
+
: "";
|
|
1771
|
+
return '<span data-rx-block=":' + node.if.blockId + '">' + rendered + '</span>';
|
|
1772
|
+
}
|
|
1773
|
+
return renderElement(node, scope, locals);
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
function renderElement(node, scope, locals) {
|
|
1777
|
+
const tag = nativeElementTag(node);
|
|
1778
|
+
const attrs = [];
|
|
1779
|
+
for (const attr of node.attrs) {
|
|
1780
|
+
const attrName = nativeAttributeName(node, attr.name);
|
|
1781
|
+
if (attr.kind === "static") {
|
|
1782
|
+
attrs.push(attrName + '="' + escapeAttribute(attr.value) + '"');
|
|
1783
|
+
} else {
|
|
1784
|
+
const value = evaluateExpression(attr.value, scope, locals);
|
|
1785
|
+
if (value !== false && value !== null && value !== undefined) {
|
|
1786
|
+
const marker = attr.bindingId ? ' data-rx-attr-' + attr.bindingId + '=":' + attr.bindingId + '"' : "";
|
|
1787
|
+
attrs.push(attrName + '="' + escapeAttribute(stringifyAttributeValue(attrName, value)) + '"' + marker);
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
for (const event of node.events) {
|
|
1792
|
+
attrs.push('data-rx-on-' + event.name + '=":' + event.handler + '"');
|
|
1793
|
+
if (event.modifiers && event.modifiers.length) {
|
|
1794
|
+
attrs.push('data-rx-mod-' + event.name + '="' + escapeAttribute(event.modifiers.join(",")) + '"');
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
if (node.html) {
|
|
1798
|
+
attrs.push('data-rx-html-' + node.html.bindingId + '=":' + node.html.bindingId + '"');
|
|
1799
|
+
}
|
|
1800
|
+
const attrText = attrs.length ? " " + attrs.join(" ") : "";
|
|
1801
|
+
const children = node.html
|
|
1802
|
+
? sanitizeHtml(evaluateExpression(node.html.expression, scope, locals))
|
|
1803
|
+
: node.children.map((child) => renderNode(child, scope, locals)).join("");
|
|
1804
|
+
return "<" + tag + attrText + ">" + children + "</" + tag + ">";
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
function nativeElementTag(node) {
|
|
1808
|
+
return node.tag === "ResuxLink" ? "a" : node.tag;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
function nativeAttributeName(node, name) {
|
|
1812
|
+
return node.tag === "ResuxLink" && name === "to" ? "href" : name;
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
function applyPatches(scopeId, patches) {
|
|
1816
|
+
for (const patch of patches) {
|
|
1817
|
+
if (patch.type === "text") {
|
|
1818
|
+
document.querySelectorAll('[data-rx-text="' + scopeId + ':' + patch.id + '"]').forEach((element) => {
|
|
1819
|
+
element.textContent = patch.value;
|
|
1820
|
+
});
|
|
1821
|
+
continue;
|
|
1822
|
+
}
|
|
1823
|
+
if (patch.type === "attr") {
|
|
1824
|
+
document.querySelectorAll('[data-rx-attr-' + patch.id + '="' + scopeId + ':' + patch.id + '"]').forEach((element) => {
|
|
1825
|
+
if (patch.value === "" || patch.value === "false" || patch.value == null) {
|
|
1826
|
+
element.removeAttribute(patch.attr);
|
|
1827
|
+
if (patch.attr === "checked" && "checked" in element) {
|
|
1828
|
+
element.checked = false;
|
|
1829
|
+
}
|
|
1830
|
+
} else {
|
|
1831
|
+
element.setAttribute(patch.attr, patch.value);
|
|
1832
|
+
if (patch.attr === "value" && "value" in element) {
|
|
1833
|
+
element.value = patch.value;
|
|
1834
|
+
}
|
|
1835
|
+
if (patch.attr === "checked" && "checked" in element) {
|
|
1836
|
+
element.checked = true;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
});
|
|
1840
|
+
continue;
|
|
1841
|
+
}
|
|
1842
|
+
if (patch.type === "html") {
|
|
1843
|
+
document.querySelectorAll('[data-rx-html-' + patch.id + '="' + scopeId + ':' + patch.id + '"]').forEach((element) => {
|
|
1844
|
+
unmountVueIslands(element);
|
|
1845
|
+
element.innerHTML = patch.value;
|
|
1846
|
+
void mountVueIslands(element);
|
|
1847
|
+
});
|
|
1848
|
+
continue;
|
|
1849
|
+
}
|
|
1850
|
+
document.querySelectorAll('[data-rx-block="' + scopeId + ':' + patch.id + '"]').forEach((element) => {
|
|
1851
|
+
unmountVueIslands(element);
|
|
1852
|
+
element.innerHTML = patch.value;
|
|
1853
|
+
void mountVueIslands(element);
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
function evaluateExpression(expression, scope, locals) {
|
|
1859
|
+
return Function("scope", "locals", "with (scope) { with (locals) { return (" + expression + "); } }")(scope, locals);
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
function serializeRefs(refs) {
|
|
1863
|
+
const output = {};
|
|
1864
|
+
for (const [key, ref] of Object.entries(refs)) {
|
|
1865
|
+
output[key] = ref.value;
|
|
1866
|
+
}
|
|
1867
|
+
return output;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
function serializeProps(props) {
|
|
1871
|
+
const output = {};
|
|
1872
|
+
for (const [key, value] of Object.entries(props)) {
|
|
1873
|
+
output[key] = value;
|
|
1874
|
+
}
|
|
1875
|
+
return output;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
function stringifyValue(value) {
|
|
1879
|
+
if (value === null || value === undefined) {
|
|
1880
|
+
return "";
|
|
1881
|
+
}
|
|
1882
|
+
return String(value);
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function stringifyAttributeValue(name, value) {
|
|
1886
|
+
if (name === "class") {
|
|
1887
|
+
return stringifyClassValue(value);
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
if (name === "style") {
|
|
1891
|
+
return stringifyStyleValue(value);
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
return stringifyValue(value);
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
function stringifyClassValue(value) {
|
|
1898
|
+
if (!value) {
|
|
1899
|
+
return "";
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
if (Array.isArray(value)) {
|
|
1903
|
+
return value.map(stringifyClassValue).filter(Boolean).join(" ");
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
if (typeof value === "object") {
|
|
1907
|
+
return Object.entries(value)
|
|
1908
|
+
.filter(([, active]) => Boolean(active))
|
|
1909
|
+
.map(([className]) => className)
|
|
1910
|
+
.join(" ");
|
|
1911
|
+
}
|
|
1912
|
+
|
|
1913
|
+
return String(value);
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
function stringifyStyleValue(value) {
|
|
1917
|
+
if (!value) {
|
|
1918
|
+
return "";
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
1922
|
+
return Object.entries(value)
|
|
1923
|
+
.filter(([, styleValue]) => styleValue !== null && styleValue !== undefined && styleValue !== false)
|
|
1924
|
+
.map(([name, styleValue]) => kebabCaseStyleName(name) + ":" + String(styleValue))
|
|
1925
|
+
.join(";");
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
return String(value);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
function kebabCaseStyleName(value) {
|
|
1932
|
+
return value.replace(/[A-Z]/g, (match) => "-" + match.toLowerCase());
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
const allowedHtmlTags = new Set([
|
|
1936
|
+
"a", "abbr", "b", "blockquote", "br", "code", "dd", "del", "div", "dl", "dt", "em",
|
|
1937
|
+
"h1", "h2", "h3", "h4", "h5", "h6", "hr", "i", "img", "ins", "li", "ol", "p",
|
|
1938
|
+
"pre", "s", "span", "strong", "sub", "sup", "table", "tbody", "td", "th", "thead",
|
|
1939
|
+
"tr", "u", "ul"
|
|
1940
|
+
]);
|
|
1941
|
+
const htmlTagsWithRemovedContent = new Set(["script", "style", "iframe", "object", "embed", "svg", "math", "template"]);
|
|
1942
|
+
const allowedGlobalHtmlAttrs = new Set(["class", "id", "title", "role"]);
|
|
1943
|
+
const allowedHtmlAttrsByTag = {
|
|
1944
|
+
a: new Set(["href", "target", "rel"]),
|
|
1945
|
+
img: new Set(["src", "alt", "width", "height", "loading"]),
|
|
1946
|
+
td: new Set(["colspan", "rowspan"]),
|
|
1947
|
+
th: new Set(["colspan", "rowspan", "scope"])
|
|
1948
|
+
};
|
|
1949
|
+
|
|
1950
|
+
function sanitizeHtml(value) {
|
|
1951
|
+
let html = stringifyValue(value);
|
|
1952
|
+
html = html.replace(/<!--[\s\S]*?-->/g, "");
|
|
1953
|
+
for (const tag of htmlTagsWithRemovedContent) {
|
|
1954
|
+
html = html.replace(new RegExp("<" + tag + "\\b[^>]*>[\\s\\S]*?<\\/" + tag + ">", "gi"), "");
|
|
1955
|
+
html = html.replace(new RegExp("<\\/?" + tag + "\\b[^>]*>", "gi"), "");
|
|
1956
|
+
}
|
|
1957
|
+
return html.replace(/<\/?([A-Za-z][A-Za-z0-9:-]*)\b([^>]*)>/g, (full, rawTag, rawAttrs) => {
|
|
1958
|
+
const tag = rawTag.toLowerCase();
|
|
1959
|
+
if (!allowedHtmlTags.has(tag)) {
|
|
1960
|
+
return "";
|
|
1961
|
+
}
|
|
1962
|
+
if (full.startsWith("</")) {
|
|
1963
|
+
return "</" + tag + ">";
|
|
1964
|
+
}
|
|
1965
|
+
const attrs = sanitizeHtmlAttributes(tag, rawAttrs);
|
|
1966
|
+
const attrText = attrs.length ? " " + attrs.join(" ") : "";
|
|
1967
|
+
return "<" + tag + attrText + ">";
|
|
1968
|
+
});
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
function sanitizeHtmlAttributes(tag, rawAttrs) {
|
|
1972
|
+
const attrs = [];
|
|
1973
|
+
const allowedForTag = allowedHtmlAttrsByTag[tag] ?? new Set();
|
|
1974
|
+
const attrPattern = /([^\s"'=<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>]+)))?/g;
|
|
1975
|
+
let match;
|
|
1976
|
+
let hasRel = false;
|
|
1977
|
+
let opensBlankTarget = false;
|
|
1978
|
+
while ((match = attrPattern.exec(rawAttrs))) {
|
|
1979
|
+
const name = match[1].toLowerCase();
|
|
1980
|
+
if (name.startsWith("on") || name === "style" || name === "srcdoc") {
|
|
1981
|
+
continue;
|
|
1982
|
+
}
|
|
1983
|
+
if (!allowedGlobalHtmlAttrs.has(name) && !allowedForTag.has(name) && !name.startsWith("aria-")) {
|
|
1984
|
+
continue;
|
|
1985
|
+
}
|
|
1986
|
+
const value = match[2] ?? match[3] ?? match[4] ?? "";
|
|
1987
|
+
if ((name === "href" || name === "src") && !isSafeHtmlUrl(value)) {
|
|
1988
|
+
continue;
|
|
1989
|
+
}
|
|
1990
|
+
if (name === "target" && value === "_blank") {
|
|
1991
|
+
opensBlankTarget = true;
|
|
1992
|
+
}
|
|
1993
|
+
if (name === "rel") {
|
|
1994
|
+
hasRel = true;
|
|
1995
|
+
}
|
|
1996
|
+
attrs.push(name + '="' + escapeAttribute(value) + '"');
|
|
1997
|
+
}
|
|
1998
|
+
if (tag === "a" && opensBlankTarget && !hasRel) {
|
|
1999
|
+
attrs.push('rel="noopener noreferrer"');
|
|
2000
|
+
}
|
|
2001
|
+
return attrs;
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
function isSafeHtmlUrl(value) {
|
|
2005
|
+
const trimmed = value.trim();
|
|
2006
|
+
if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith("/") || trimmed.startsWith("./") || trimmed.startsWith("../")) {
|
|
2007
|
+
return true;
|
|
2008
|
+
}
|
|
2009
|
+
try {
|
|
2010
|
+
const url = new URL(trimmed, "https://resux.local");
|
|
2011
|
+
return ["http:", "https:", "mailto:", "tel:"].includes(url.protocol);
|
|
2012
|
+
} catch {
|
|
2013
|
+
return false;
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
function escapeHtml(value) {
|
|
2018
|
+
return String(value)
|
|
2019
|
+
.replaceAll("&", "&")
|
|
2020
|
+
.replaceAll("<", "<")
|
|
2021
|
+
.replaceAll(">", ">");
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
function escapeAttribute(value) {
|
|
2025
|
+
return escapeHtml(value).replaceAll('"', """);
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
function normalizeAsyncDataError(error) {
|
|
2029
|
+
if (error instanceof Error) {
|
|
2030
|
+
return {
|
|
2031
|
+
name: error.name,
|
|
2032
|
+
message: error.message
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
return {
|
|
2037
|
+
name: "Error",
|
|
2038
|
+
message: String(error)
|
|
2039
|
+
};
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
function assertJsonSerializable(value, label) {
|
|
2043
|
+
if (value === undefined || typeof value === "function" || typeof value === "symbol" || typeof value === "bigint") {
|
|
2044
|
+
throw new Error(label + " must be JSON serializable.");
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
try {
|
|
2048
|
+
JSON.stringify(value);
|
|
2049
|
+
} catch {
|
|
2050
|
+
throw new Error(label + " must be JSON serializable.");
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
installResux();
|
|
2055
|
+
`;
|
|
2056
|
+
}
|
|
2057
|
+
function serializeRefs(refs) {
|
|
2058
|
+
const output = {};
|
|
2059
|
+
for (const [key, ref] of Object.entries(refs)) {
|
|
2060
|
+
assertJsonSerializable(ref.value, key);
|
|
2061
|
+
output[key] = ref.value;
|
|
2062
|
+
}
|
|
2063
|
+
return output;
|
|
2064
|
+
}
|
|
2065
|
+
function serializeProps(props) {
|
|
2066
|
+
const output = {};
|
|
2067
|
+
for (const [key, value] of Object.entries(props)) {
|
|
2068
|
+
assertJsonSerializable(value, `prop "${key}"`);
|
|
2069
|
+
output[key] = value;
|
|
2070
|
+
}
|
|
2071
|
+
return output;
|
|
2072
|
+
}
|
|
2073
|
+
function serializeAsyncData(refs) {
|
|
2074
|
+
const output = {};
|
|
2075
|
+
for (const [key, resource] of Object.entries(refs)) {
|
|
2076
|
+
const value = resource.value.value;
|
|
2077
|
+
if (!resource.pending && !resource.error) {
|
|
2078
|
+
assertJsonSerializable(value, `useAsyncData("${key}")`);
|
|
2079
|
+
}
|
|
2080
|
+
output[key] = {
|
|
2081
|
+
value: value === undefined ? null : value,
|
|
2082
|
+
pending: resource.pending,
|
|
2083
|
+
error: resource.error
|
|
2084
|
+
};
|
|
2085
|
+
}
|
|
2086
|
+
return output;
|
|
2087
|
+
}
|
|
2088
|
+
function normalizeAsyncDataError(error) {
|
|
2089
|
+
if (error instanceof Error) {
|
|
2090
|
+
return {
|
|
2091
|
+
name: error.name,
|
|
2092
|
+
message: error.message
|
|
2093
|
+
};
|
|
2094
|
+
}
|
|
2095
|
+
return {
|
|
2096
|
+
name: "Error",
|
|
2097
|
+
message: String(error)
|
|
2098
|
+
};
|
|
2099
|
+
}
|
|
2100
|
+
function assertJsonSerializable(value, label) {
|
|
2101
|
+
if (value === undefined || typeof value === "function" || typeof value === "symbol" || typeof value === "bigint") {
|
|
2102
|
+
throw new Error(`${label} must be JSON serializable.`);
|
|
2103
|
+
}
|
|
2104
|
+
try {
|
|
2105
|
+
JSON.stringify(value);
|
|
2106
|
+
}
|
|
2107
|
+
catch {
|
|
2108
|
+
throw new Error(`${label} must be JSON serializable.`);
|
|
2109
|
+
}
|
|
2110
|
+
}
|
|
2111
|
+
function stringifyValue(value) {
|
|
2112
|
+
if (value === null || value === undefined) {
|
|
2113
|
+
return "";
|
|
2114
|
+
}
|
|
2115
|
+
return String(value);
|
|
2116
|
+
}
|
|
2117
|
+
function stringifyAttributeValue(name, value) {
|
|
2118
|
+
if (name === "class") {
|
|
2119
|
+
return stringifyClassValue(value);
|
|
2120
|
+
}
|
|
2121
|
+
if (name === "style") {
|
|
2122
|
+
return stringifyStyleValue(value);
|
|
2123
|
+
}
|
|
2124
|
+
return stringifyValue(value);
|
|
2125
|
+
}
|
|
2126
|
+
function stringifyClassValue(value) {
|
|
2127
|
+
if (!value) {
|
|
2128
|
+
return "";
|
|
2129
|
+
}
|
|
2130
|
+
if (Array.isArray(value)) {
|
|
2131
|
+
return value.map(stringifyClassValue).filter(Boolean).join(" ");
|
|
2132
|
+
}
|
|
2133
|
+
if (typeof value === "object") {
|
|
2134
|
+
return Object.entries(value)
|
|
2135
|
+
.filter(([, active]) => Boolean(active))
|
|
2136
|
+
.map(([className]) => className)
|
|
2137
|
+
.join(" ");
|
|
2138
|
+
}
|
|
2139
|
+
return String(value);
|
|
2140
|
+
}
|
|
2141
|
+
function stringifyStyleValue(value) {
|
|
2142
|
+
if (!value) {
|
|
2143
|
+
return "";
|
|
2144
|
+
}
|
|
2145
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
2146
|
+
return Object.entries(value)
|
|
2147
|
+
.filter(([, styleValue]) => styleValue !== null && styleValue !== undefined && styleValue !== false)
|
|
2148
|
+
.map(([name, styleValue]) => `${kebabCaseStyleName(name)}:${String(styleValue)}`)
|
|
2149
|
+
.join(";");
|
|
2150
|
+
}
|
|
2151
|
+
return String(value);
|
|
2152
|
+
}
|
|
2153
|
+
function kebabCaseStyleName(value) {
|
|
2154
|
+
return value.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
|
|
2155
|
+
}
|
|
2156
|
+
const allowedHtmlTags = new Set([
|
|
2157
|
+
"a",
|
|
2158
|
+
"abbr",
|
|
2159
|
+
"b",
|
|
2160
|
+
"blockquote",
|
|
2161
|
+
"br",
|
|
2162
|
+
"code",
|
|
2163
|
+
"dd",
|
|
2164
|
+
"del",
|
|
2165
|
+
"div",
|
|
2166
|
+
"dl",
|
|
2167
|
+
"dt",
|
|
2168
|
+
"em",
|
|
2169
|
+
"h1",
|
|
2170
|
+
"h2",
|
|
2171
|
+
"h3",
|
|
2172
|
+
"h4",
|
|
2173
|
+
"h5",
|
|
2174
|
+
"h6",
|
|
2175
|
+
"hr",
|
|
2176
|
+
"i",
|
|
2177
|
+
"img",
|
|
2178
|
+
"ins",
|
|
2179
|
+
"li",
|
|
2180
|
+
"ol",
|
|
2181
|
+
"p",
|
|
2182
|
+
"pre",
|
|
2183
|
+
"s",
|
|
2184
|
+
"span",
|
|
2185
|
+
"strong",
|
|
2186
|
+
"sub",
|
|
2187
|
+
"sup",
|
|
2188
|
+
"table",
|
|
2189
|
+
"tbody",
|
|
2190
|
+
"td",
|
|
2191
|
+
"th",
|
|
2192
|
+
"thead",
|
|
2193
|
+
"tr",
|
|
2194
|
+
"u",
|
|
2195
|
+
"ul"
|
|
2196
|
+
]);
|
|
2197
|
+
const htmlTagsWithRemovedContent = new Set([
|
|
2198
|
+
"script",
|
|
2199
|
+
"style",
|
|
2200
|
+
"iframe",
|
|
2201
|
+
"object",
|
|
2202
|
+
"embed",
|
|
2203
|
+
"svg",
|
|
2204
|
+
"math",
|
|
2205
|
+
"template"
|
|
2206
|
+
]);
|
|
2207
|
+
const allowedGlobalHtmlAttrs = new Set(["class", "id", "title", "role"]);
|
|
2208
|
+
const allowedHtmlAttrsByTag = {
|
|
2209
|
+
a: new Set(["href", "target", "rel"]),
|
|
2210
|
+
img: new Set(["src", "alt", "width", "height", "loading"]),
|
|
2211
|
+
td: new Set(["colspan", "rowspan"]),
|
|
2212
|
+
th: new Set(["colspan", "rowspan", "scope"])
|
|
2213
|
+
};
|
|
2214
|
+
function sanitizeHtml(value) {
|
|
2215
|
+
let html = stringifyValue(value);
|
|
2216
|
+
html = html.replace(/<!--[\s\S]*?-->/g, "");
|
|
2217
|
+
for (const tag of htmlTagsWithRemovedContent) {
|
|
2218
|
+
html = html.replace(new RegExp(`<${tag}\\b[^>]*>[\\s\\S]*?<\\/${tag}>`, "gi"), "");
|
|
2219
|
+
html = html.replace(new RegExp(`<\\/?${tag}\\b[^>]*>`, "gi"), "");
|
|
2220
|
+
}
|
|
2221
|
+
return html.replace(/<\/?([A-Za-z][A-Za-z0-9:-]*)\b([^>]*)>/g, (full, rawTag, rawAttrs) => {
|
|
2222
|
+
const tag = rawTag.toLowerCase();
|
|
2223
|
+
if (!allowedHtmlTags.has(tag)) {
|
|
2224
|
+
return "";
|
|
2225
|
+
}
|
|
2226
|
+
if (full.startsWith("</")) {
|
|
2227
|
+
return `</${tag}>`;
|
|
2228
|
+
}
|
|
2229
|
+
const attrs = sanitizeHtmlAttributes(tag, rawAttrs);
|
|
2230
|
+
const attrText = attrs.length > 0 ? ` ${attrs.join(" ")}` : "";
|
|
2231
|
+
return `<${tag}${attrText}>`;
|
|
2232
|
+
});
|
|
2233
|
+
}
|
|
2234
|
+
function sanitizeHtmlAttributes(tag, rawAttrs) {
|
|
2235
|
+
const attrs = [];
|
|
2236
|
+
const allowedForTag = allowedHtmlAttrsByTag[tag] ?? new Set();
|
|
2237
|
+
const attrPattern = /([^\s"'=<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>]+)))?/g;
|
|
2238
|
+
let match;
|
|
2239
|
+
let hasRel = false;
|
|
2240
|
+
let opensBlankTarget = false;
|
|
2241
|
+
while ((match = attrPattern.exec(rawAttrs))) {
|
|
2242
|
+
const name = match[1].toLowerCase();
|
|
2243
|
+
if (name.startsWith("on") || name === "style" || name === "srcdoc") {
|
|
2244
|
+
continue;
|
|
2245
|
+
}
|
|
2246
|
+
if (!allowedGlobalHtmlAttrs.has(name) && !allowedForTag.has(name) && !name.startsWith("aria-")) {
|
|
2247
|
+
continue;
|
|
2248
|
+
}
|
|
2249
|
+
const value = match[2] ?? match[3] ?? match[4] ?? "";
|
|
2250
|
+
if ((name === "href" || name === "src") && !isSafeHtmlUrl(value)) {
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
if (name === "target" && value === "_blank") {
|
|
2254
|
+
opensBlankTarget = true;
|
|
2255
|
+
}
|
|
2256
|
+
if (name === "rel") {
|
|
2257
|
+
hasRel = true;
|
|
2258
|
+
}
|
|
2259
|
+
attrs.push(`${name}="${escapeAttribute(value)}"`);
|
|
2260
|
+
}
|
|
2261
|
+
if (tag === "a" && opensBlankTarget && !hasRel) {
|
|
2262
|
+
attrs.push('rel="noopener noreferrer"');
|
|
2263
|
+
}
|
|
2264
|
+
return attrs;
|
|
2265
|
+
}
|
|
2266
|
+
function isSafeHtmlUrl(value) {
|
|
2267
|
+
const trimmed = value.trim();
|
|
2268
|
+
if (!trimmed || trimmed.startsWith("#") || trimmed.startsWith("/") || trimmed.startsWith("./") || trimmed.startsWith("../")) {
|
|
2269
|
+
return true;
|
|
2270
|
+
}
|
|
2271
|
+
try {
|
|
2272
|
+
const url = new URL(trimmed, "https://resux.local");
|
|
2273
|
+
return ["http:", "https:", "mailto:", "tel:"].includes(url.protocol);
|
|
2274
|
+
}
|
|
2275
|
+
catch {
|
|
2276
|
+
return false;
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
function isComponentTag(tag) {
|
|
2280
|
+
return /^[A-Z]/.test(tag);
|
|
2281
|
+
}
|
|
2282
|
+
function escapeJsonForHtml(value) {
|
|
2283
|
+
return value.replaceAll("<", "\\u003c").replaceAll(">", "\\u003e").replaceAll("&", "\\u0026");
|
|
2284
|
+
}
|
|
2285
|
+
function escapeHtml(value) {
|
|
2286
|
+
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
2287
|
+
}
|
|
2288
|
+
function escapeAttribute(value) {
|
|
2289
|
+
return escapeHtml(value).replaceAll('"', """);
|
|
2290
|
+
}
|
|
2291
|
+
//# sourceMappingURL=index.js.map
|