@ivlev-dev/silent-core 0.6.0 → 0.7.1
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/dist/component-lite.d.ts +14 -0
- package/dist/component-lite.d.ts.map +1 -0
- package/dist/component-lite.js +138 -0
- package/dist/component-lite.js.map +1 -0
- package/dist/component-state.d.ts +42 -0
- package/dist/component-state.d.ts.map +1 -0
- package/dist/component-state.js +98 -0
- package/dist/component-state.js.map +1 -0
- package/dist/component-view.d.ts +5 -0
- package/dist/component-view.d.ts.map +1 -0
- package/dist/component-view.js +445 -0
- package/dist/component-view.js.map +1 -0
- package/dist/component.d.ts +4 -35
- package/dist/component.d.ts.map +1 -1
- package/dist/component.js +5 -588
- package/dist/component.js.map +1 -1
- package/dist/control-flow.d.ts.map +1 -1
- package/dist/control-flow.js +487 -184
- package/dist/control-flow.js.map +1 -1
- package/dist/decorators.d.ts.map +1 -1
- package/dist/decorators.js +4 -1
- package/dist/decorators.js.map +1 -1
- package/dist/dom-hydration-state.d.ts +21 -0
- package/dist/dom-hydration-state.d.ts.map +1 -0
- package/dist/dom-hydration-state.js +30 -0
- package/dist/dom-hydration-state.js.map +1 -0
- package/dist/dom-hydration.d.ts +10 -0
- package/dist/dom-hydration.d.ts.map +1 -0
- package/dist/dom-hydration.js +537 -0
- package/dist/dom-hydration.js.map +1 -0
- package/dist/dom.d.ts +33 -4
- package/dist/dom.d.ts.map +1 -1
- package/dist/dom.js +805 -295
- package/dist/dom.js.map +1 -1
- package/dist/error-boundary.d.ts.map +1 -1
- package/dist/error-boundary.js +68 -20
- package/dist/error-boundary.js.map +1 -1
- package/dist/hydrate.d.ts +4 -0
- package/dist/hydrate.d.ts.map +1 -0
- package/dist/hydrate.js +131 -0
- package/dist/hydrate.js.map +1 -0
- package/dist/hydration-proxy.d.ts +14 -0
- package/dist/hydration-proxy.d.ts.map +1 -0
- package/dist/hydration-proxy.js +40 -0
- package/dist/hydration-proxy.js.map +1 -0
- package/dist/hydration-state.d.ts +47 -0
- package/dist/hydration-state.d.ts.map +1 -0
- package/dist/hydration-state.js +83 -0
- package/dist/hydration-state.js.map +1 -0
- package/dist/hydration.d.ts +7 -32
- package/dist/hydration.d.ts.map +1 -1
- package/dist/hydration.js +299 -123
- package/dist/hydration.js.map +1 -1
- package/dist/index-list.d.ts.map +1 -1
- package/dist/index-list.js +280 -77
- package/dist/index-list.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.d.ts.map +1 -1
- package/dist/jsx-runtime.js +4 -3
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/metadata.d.ts +1 -0
- package/dist/metadata.d.ts.map +1 -1
- package/dist/metadata.js +29 -11
- package/dist/metadata.js.map +1 -1
- package/dist/mount-core.d.ts +14 -0
- package/dist/mount-core.d.ts.map +1 -0
- package/dist/mount-core.js +64 -0
- package/dist/mount-core.js.map +1 -0
- package/dist/mount.d.ts +1 -2
- package/dist/mount.d.ts.map +1 -1
- package/dist/mount.js +17 -132
- package/dist/mount.js.map +1 -1
- package/dist/server-backend.d.ts +5 -0
- package/dist/server-backend.d.ts.map +1 -1
- package/dist/server-backend.js +10 -2
- package/dist/server-backend.js.map +1 -1
- package/dist/server-hole.d.ts +3 -0
- package/dist/server-hole.d.ts.map +1 -0
- package/dist/server-hole.js +12 -0
- package/dist/server-hole.js.map +1 -0
- package/dist/server-proxy.d.ts +6 -0
- package/dist/server-proxy.d.ts.map +1 -0
- package/dist/server-proxy.js +14 -0
- package/dist/server-proxy.js.map +1 -0
- package/dist/server-state.d.ts +10 -0
- package/dist/server-state.d.ts.map +1 -0
- package/dist/server-state.js +14 -0
- package/dist/server-state.js.map +1 -0
- package/dist/static-effect.d.ts +4 -0
- package/dist/static-effect.d.ts.map +1 -0
- package/dist/static-effect.js +11 -0
- package/dist/static-effect.js.map +1 -0
- package/dist/suspense.d.ts.map +1 -1
- package/dist/suspense.js +4 -5
- package/dist/suspense.js.map +1 -1
- package/dist/switch.d.ts.map +1 -1
- package/dist/switch.js +37 -33
- package/dist/switch.js.map +1 -1
- package/package.json +4 -4
package/dist/dom.js
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
import { createRenderEffect, onCleanup } from "@ivlev-dev/silent-reactivity";
|
|
2
|
+
import { createEffectUnlessStatic } from "./static-effect.js";
|
|
2
3
|
import { extractMetadata, isMetadataElement, markMetadataFree } from "./metadata.js";
|
|
3
|
-
import { getServerBackend, isServerHole, serializeServerValue, } from "./server-
|
|
4
|
-
import { FRAGMENT, INSERT_END, INSERT_START, claimNext,
|
|
4
|
+
import { getServerBackend, isServerHole, serializeServerValue, setServerHoleValue, } from "./server-proxy.js";
|
|
5
|
+
import { FRAGMENT, INSERT_END, INSERT_START, claimNext, currentWindow, isHydrating, isMarker, countSignificantChildren, mergeTopLevelMetadata, mismatch, shapeMatches, significantChildren, significantChildrenFromEnd, withWindow, } from "./hydration-proxy.js";
|
|
6
|
+
import { adoptPartsPlan, hydratedEffect, insertHydrated, settleNodes, writeRegion, } from "./dom-hydration-state.js";
|
|
5
7
|
export const NODE_DISPOSER = Symbol("silent.disposer");
|
|
6
|
-
const
|
|
8
|
+
const ITEM_DISPOSER = Symbol("silent.itemDisposer");
|
|
9
|
+
const parseCache = new Map();
|
|
7
10
|
const METADATA_PATTERN = /<(?:title|meta|link)[\s/>]/i;
|
|
8
11
|
const listenerRegistry = new WeakMap();
|
|
12
|
+
const delegatedInstalled = new Set();
|
|
13
|
+
const DELEGATED_EVENTS = /^(?:change|click|contextmenu|dblclick|input|keydown|keyup|mousedown|mouseup|pointerdown|pointerup|submit|touchend|touchstart)$/;
|
|
9
14
|
const styleSnapshots = new WeakMap();
|
|
15
|
+
const styleValues = new WeakMap();
|
|
16
|
+
const hyphenCache = new Map();
|
|
17
|
+
const eventNameCache = new Map();
|
|
18
|
+
export const HOLE_MARKER = "silent-hole-";
|
|
19
|
+
export const ATTR_MARKER = "data-silent-a-";
|
|
20
|
+
export const TEXT_MARKER = "data-silent-text";
|
|
21
|
+
const AUX_CACHE_LIMIT = 1024;
|
|
10
22
|
let serverMode = false;
|
|
23
|
+
const SERVER = typeof __SILENT_SERVER__ === "undefined" || __SILENT_SERVER__;
|
|
11
24
|
export function setServerMode(enabled) {
|
|
12
25
|
serverMode = enabled;
|
|
13
26
|
}
|
|
@@ -51,11 +64,26 @@ export const svgTagNames = new Set([
|
|
|
51
64
|
function isRenderableChild(value) {
|
|
52
65
|
return value !== null && value !== undefined && typeof value !== "boolean";
|
|
53
66
|
}
|
|
54
|
-
function
|
|
55
|
-
|
|
67
|
+
export function resolveValue(value) {
|
|
68
|
+
let current = value;
|
|
69
|
+
while (typeof current === "function") {
|
|
70
|
+
current = current();
|
|
71
|
+
}
|
|
72
|
+
return current;
|
|
73
|
+
}
|
|
74
|
+
export function stringOf(value) {
|
|
75
|
+
const current = resolveValue(value);
|
|
76
|
+
if (current === null || current === undefined || typeof current === "boolean") {
|
|
56
77
|
return "";
|
|
57
78
|
}
|
|
58
|
-
return String(
|
|
79
|
+
return String(current);
|
|
80
|
+
}
|
|
81
|
+
function isTextValue(value) {
|
|
82
|
+
if (value === null || value === undefined) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const type = typeof value;
|
|
86
|
+
return type === "string" || type === "number" || type === "bigint" || type === "boolean";
|
|
59
87
|
}
|
|
60
88
|
function valuesEqual(a, b) {
|
|
61
89
|
if (a === b) {
|
|
@@ -72,48 +100,69 @@ function valuesEqual(a, b) {
|
|
|
72
100
|
export function attachDisposer(node, dispose) {
|
|
73
101
|
node[NODE_DISPOSER] = dispose;
|
|
74
102
|
}
|
|
103
|
+
export function attachItemDisposer(node, dispose) {
|
|
104
|
+
node[ITEM_DISPOSER] = dispose;
|
|
105
|
+
}
|
|
106
|
+
export function clearItemDisposer(node) {
|
|
107
|
+
delete node[ITEM_DISPOSER];
|
|
108
|
+
}
|
|
75
109
|
function readDisposer(node) {
|
|
76
110
|
const dispose = node[NODE_DISPOSER];
|
|
77
111
|
return typeof dispose === "function" ? dispose : undefined;
|
|
78
112
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return;
|
|
113
|
+
function collectInto(nodes, value) {
|
|
114
|
+
let current = value;
|
|
115
|
+
while (typeof current === "function") {
|
|
116
|
+
current = current();
|
|
117
|
+
}
|
|
118
|
+
if (current === null || current === undefined || typeof current === "boolean") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(current)) {
|
|
122
|
+
for (const item of current) {
|
|
123
|
+
collectInto(nodes, item);
|
|
91
124
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (current instanceof Node) {
|
|
128
|
+
if (current instanceof DocumentFragment) {
|
|
129
|
+
let child = current.firstChild;
|
|
130
|
+
while (child !== null) {
|
|
131
|
+
const next = child.nextSibling;
|
|
132
|
+
collectInto(nodes, child);
|
|
133
|
+
child = next;
|
|
95
134
|
}
|
|
96
135
|
return;
|
|
97
136
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
nodes.push(current);
|
|
109
|
-
return;
|
|
137
|
+
nodes.push(current);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
nodes.push(document.createTextNode(stringOf(current)));
|
|
141
|
+
}
|
|
142
|
+
export function collectNodes(value) {
|
|
143
|
+
if (value instanceof Node) {
|
|
144
|
+
if (!(value instanceof DocumentFragment)) {
|
|
145
|
+
return [value];
|
|
110
146
|
}
|
|
111
|
-
nodes
|
|
112
|
-
|
|
113
|
-
|
|
147
|
+
const nodes = [];
|
|
148
|
+
let child = value.firstChild;
|
|
149
|
+
while (child !== null) {
|
|
150
|
+
const next = child.nextSibling;
|
|
151
|
+
collectInto(nodes, child);
|
|
152
|
+
child = next;
|
|
153
|
+
}
|
|
154
|
+
return nodes;
|
|
155
|
+
}
|
|
156
|
+
const nodes = [];
|
|
157
|
+
collectInto(nodes, value);
|
|
114
158
|
return nodes;
|
|
115
159
|
}
|
|
116
160
|
export function disposeNode(node) {
|
|
161
|
+
const itemDispose = node[ITEM_DISPOSER];
|
|
162
|
+
if (typeof itemDispose === "function") {
|
|
163
|
+
itemDispose();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
117
166
|
const dispose = readDisposer(node);
|
|
118
167
|
if (dispose !== undefined) {
|
|
119
168
|
dispose();
|
|
@@ -142,19 +191,12 @@ export function createMountPoint(parent, marker, initial) {
|
|
|
142
191
|
let prev = null;
|
|
143
192
|
let textNode = null;
|
|
144
193
|
let nodes = initial === undefined ? [] : [...initial];
|
|
145
|
-
const
|
|
146
|
-
if (marker === null) {
|
|
147
|
-
return parent;
|
|
148
|
-
}
|
|
149
|
-
const live = marker.parentNode;
|
|
150
|
-
return live ?? parent;
|
|
151
|
-
};
|
|
152
|
-
const place = (node) => {
|
|
194
|
+
const place = (target, node) => {
|
|
153
195
|
if (marker === null) {
|
|
154
|
-
|
|
196
|
+
target.appendChild(node);
|
|
155
197
|
return;
|
|
156
198
|
}
|
|
157
|
-
|
|
199
|
+
target.insertBefore(node, marker);
|
|
158
200
|
};
|
|
159
201
|
const clear = () => {
|
|
160
202
|
for (const node of nodes) {
|
|
@@ -184,7 +226,7 @@ export function createMountPoint(parent, marker, initial) {
|
|
|
184
226
|
clear();
|
|
185
227
|
textNode = document.createTextNode(next);
|
|
186
228
|
nodes = [textNode];
|
|
187
|
-
place(textNode);
|
|
229
|
+
place(marker === null ? parent : marker.parentNode ?? parent, textNode);
|
|
188
230
|
return;
|
|
189
231
|
}
|
|
190
232
|
if (textNode.data !== next) {
|
|
@@ -196,26 +238,44 @@ export function createMountPoint(parent, marker, initial) {
|
|
|
196
238
|
if (!isRenderableChild(value)) {
|
|
197
239
|
return;
|
|
198
240
|
}
|
|
199
|
-
nodes = collectNodes(value);
|
|
200
|
-
|
|
241
|
+
nodes = extractMetadata(collectNodes(value));
|
|
242
|
+
const target = marker === null ? parent : marker.parentNode ?? parent;
|
|
201
243
|
for (const node of nodes) {
|
|
202
|
-
place(node);
|
|
244
|
+
place(target, node);
|
|
203
245
|
}
|
|
204
246
|
},
|
|
205
247
|
};
|
|
206
248
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (cached
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
const roots = Array.from(content.childNodes);
|
|
217
|
-
const singleRoot = roots.length === 1 ? roots[0] ?? null : null;
|
|
249
|
+
function readTemplate(html) {
|
|
250
|
+
const cached = parseCache.get(html);
|
|
251
|
+
if (cached !== undefined) {
|
|
252
|
+
return cached;
|
|
253
|
+
}
|
|
254
|
+
const element = document.createElement("template");
|
|
255
|
+
element.innerHTML = html;
|
|
256
|
+
const content = element.content;
|
|
257
|
+
const rootCount = content.childNodes.length;
|
|
218
258
|
const metadata = METADATA_PATTERN.test(html);
|
|
259
|
+
const parsed = {
|
|
260
|
+
content,
|
|
261
|
+
rootCount,
|
|
262
|
+
singleRoot: rootCount === 1 ? content.firstChild : null,
|
|
263
|
+
metadata,
|
|
264
|
+
simple: !metadata && !html.includes("silent-insert"),
|
|
265
|
+
parts: null,
|
|
266
|
+
};
|
|
267
|
+
parseCache.set(html, parsed);
|
|
268
|
+
return parsed;
|
|
269
|
+
}
|
|
270
|
+
export function template(html) {
|
|
271
|
+
let factory = null;
|
|
272
|
+
return () => {
|
|
273
|
+
factory ??= createTemplateFactory(html);
|
|
274
|
+
return factory();
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function createTemplateFactory(html) {
|
|
278
|
+
const { content, rootCount, singleRoot, metadata, simple } = readTemplate(html);
|
|
219
279
|
const mark = (node) => {
|
|
220
280
|
if (!metadata) {
|
|
221
281
|
if (node instanceof DocumentFragment) {
|
|
@@ -231,19 +291,25 @@ export function template(html) {
|
|
|
231
291
|
if (singleRoot !== null) {
|
|
232
292
|
return mark(singleRoot.cloneNode(true));
|
|
233
293
|
}
|
|
234
|
-
if (
|
|
294
|
+
if (rootCount === 0) {
|
|
235
295
|
return mark(content.cloneNode(false));
|
|
236
296
|
}
|
|
237
|
-
|
|
238
|
-
for (const root of roots) {
|
|
239
|
-
fragment.appendChild(root.cloneNode(true));
|
|
240
|
-
}
|
|
241
|
-
return mark(fragment);
|
|
297
|
+
return mark(content.cloneNode(true));
|
|
242
298
|
};
|
|
243
299
|
return () => {
|
|
244
300
|
if (!isHydrating()) {
|
|
245
301
|
return create();
|
|
246
302
|
}
|
|
303
|
+
if (!metadata) {
|
|
304
|
+
if (rootCount !== 1 || singleRoot === null) {
|
|
305
|
+
return mismatch("многослойный шаблон не поддерживается при гидрации");
|
|
306
|
+
}
|
|
307
|
+
const node = claimNext();
|
|
308
|
+
if (node === null || !shapeMatches(singleRoot, node, true, simple)) {
|
|
309
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
310
|
+
}
|
|
311
|
+
return node;
|
|
312
|
+
}
|
|
247
313
|
const fresh = create();
|
|
248
314
|
if (fresh instanceof DocumentFragment) {
|
|
249
315
|
return mismatch("многослойный шаблон не поддерживается при гидрации");
|
|
@@ -259,110 +325,292 @@ export function template(html) {
|
|
|
259
325
|
return node;
|
|
260
326
|
};
|
|
261
327
|
}
|
|
262
|
-
function
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
328
|
+
export function markerIndex(value, prefix) {
|
|
329
|
+
if (!value.startsWith(prefix)) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
const digits = value.slice(prefix.length);
|
|
333
|
+
if (digits.length === 0) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
let index = 0;
|
|
337
|
+
for (let position = 0; position < digits.length; position += 1) {
|
|
338
|
+
const digit = digits.charCodeAt(position) - 48;
|
|
339
|
+
if (digit < 0 || digit > 9) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
index = index * 10 + digit;
|
|
343
|
+
}
|
|
344
|
+
return index;
|
|
345
|
+
}
|
|
346
|
+
function forEachPartChild(node, visit) {
|
|
347
|
+
let depth = 0;
|
|
348
|
+
let child = node.firstChild;
|
|
349
|
+
let index = 0;
|
|
350
|
+
while (child !== null) {
|
|
351
|
+
const next = child.nextSibling;
|
|
352
|
+
let marker = false;
|
|
353
|
+
if (child.nodeType === Node.COMMENT_NODE) {
|
|
354
|
+
const data = child.data;
|
|
355
|
+
if (data === INSERT_START) {
|
|
356
|
+
depth += 1;
|
|
357
|
+
marker = true;
|
|
280
358
|
}
|
|
281
|
-
if (
|
|
282
|
-
|
|
359
|
+
else if (data === INSERT_END) {
|
|
360
|
+
depth = depth > 0 ? depth - 1 : 0;
|
|
361
|
+
marker = true;
|
|
283
362
|
}
|
|
284
363
|
}
|
|
285
|
-
|
|
364
|
+
if (!marker && depth === 0) {
|
|
365
|
+
visit(child, index);
|
|
366
|
+
index += 1;
|
|
367
|
+
}
|
|
368
|
+
child = next;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function denseParts(sparse) {
|
|
372
|
+
const result = [];
|
|
373
|
+
for (const item of sparse) {
|
|
374
|
+
if (item === undefined) {
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
result.push(item);
|
|
378
|
+
}
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
export function collectPartsMany(nodes) {
|
|
382
|
+
const holesByIndex = [];
|
|
383
|
+
const attrsByIndex = [];
|
|
384
|
+
const strip = !isServerMode();
|
|
286
385
|
const visit = (current) => {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
386
|
+
const nodeType = current.nodeType;
|
|
387
|
+
if (nodeType === Node.COMMENT_NODE) {
|
|
388
|
+
const index = markerIndex(current.data, HOLE_MARKER);
|
|
389
|
+
if (index !== null) {
|
|
390
|
+
holesByIndex[index] = current;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
else if (nodeType === Node.ELEMENT_NODE) {
|
|
394
|
+
const element = current;
|
|
395
|
+
const attributes = element.attributes;
|
|
396
|
+
for (let position = attributes.length - 1; position >= 0; position -= 1) {
|
|
397
|
+
const attribute = attributes.item(position);
|
|
398
|
+
if (attribute === null) {
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
const name = attribute.name;
|
|
402
|
+
if (name === TEXT_MARKER) {
|
|
403
|
+
if (strip) {
|
|
404
|
+
element.removeAttribute(name);
|
|
405
|
+
}
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
const index = markerIndex(name, ATTR_MARKER);
|
|
409
|
+
if (index === null) {
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
attrsByIndex[index] = element;
|
|
413
|
+
if (strip) {
|
|
414
|
+
element.removeAttribute(name);
|
|
293
415
|
}
|
|
294
416
|
}
|
|
295
417
|
}
|
|
296
|
-
|
|
297
|
-
|
|
418
|
+
forEachPartChild(current, visit);
|
|
419
|
+
};
|
|
420
|
+
for (const node of nodes) {
|
|
421
|
+
visit(node);
|
|
422
|
+
}
|
|
423
|
+
return { holes: denseParts(holesByIndex), attrs: denseParts(attrsByIndex) };
|
|
424
|
+
}
|
|
425
|
+
function collectPartsPlan(content) {
|
|
426
|
+
const holesByIndex = [];
|
|
427
|
+
const attrsByIndex = [];
|
|
428
|
+
const strips = [];
|
|
429
|
+
const path = [];
|
|
430
|
+
const visit = (node) => {
|
|
431
|
+
const nodeType = node.nodeType;
|
|
432
|
+
if (nodeType === Node.COMMENT_NODE) {
|
|
433
|
+
const index = markerIndex(node.data, HOLE_MARKER);
|
|
434
|
+
if (index !== null) {
|
|
435
|
+
holesByIndex[index] = path.slice();
|
|
436
|
+
}
|
|
298
437
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
child = child.nextSibling;
|
|
438
|
+
else if (nodeType === Node.ELEMENT_NODE) {
|
|
439
|
+
const element = node;
|
|
440
|
+
const names = [];
|
|
441
|
+
const attributes = element.attributes;
|
|
442
|
+
for (let position = attributes.length - 1; position >= 0; position -= 1) {
|
|
443
|
+
const attribute = attributes.item(position);
|
|
444
|
+
if (attribute === null) {
|
|
307
445
|
continue;
|
|
308
446
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
447
|
+
const name = attribute.name;
|
|
448
|
+
if (name === TEXT_MARKER) {
|
|
449
|
+
names.push(name);
|
|
312
450
|
continue;
|
|
313
451
|
}
|
|
452
|
+
const index = markerIndex(name, ATTR_MARKER);
|
|
453
|
+
if (index !== null) {
|
|
454
|
+
attrsByIndex[index] = path.slice();
|
|
455
|
+
names.push(name);
|
|
456
|
+
}
|
|
314
457
|
}
|
|
315
|
-
if (
|
|
316
|
-
|
|
458
|
+
if (names.length > 0) {
|
|
459
|
+
strips.push({ path: path.slice(), names });
|
|
317
460
|
}
|
|
318
|
-
child = child.nextSibling;
|
|
319
461
|
}
|
|
462
|
+
forEachPartChild(node, visitChild);
|
|
320
463
|
};
|
|
321
|
-
|
|
322
|
-
|
|
464
|
+
const visitChild = (child, index) => {
|
|
465
|
+
path.push(index);
|
|
466
|
+
visit(child);
|
|
467
|
+
path.pop();
|
|
468
|
+
};
|
|
469
|
+
if (content.childNodes.length === 1) {
|
|
470
|
+
const root = content.firstChild;
|
|
471
|
+
if (root !== null) {
|
|
472
|
+
visitChild(root, 0);
|
|
473
|
+
}
|
|
323
474
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
475
|
+
else if (content.childNodes.length > 1) {
|
|
476
|
+
forEachPartChild(content, visitChild);
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
holes: denseParts(holesByIndex).map((entry) => measurePath(content, entry)),
|
|
480
|
+
attrs: denseParts(attrsByIndex).map((entry) => measurePath(content, entry)),
|
|
481
|
+
strips: strips.map((entry) => ({
|
|
482
|
+
...measurePath(content, entry.path),
|
|
483
|
+
names: entry.names,
|
|
484
|
+
})),
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function measurePath(root, path) {
|
|
488
|
+
const counts = [];
|
|
489
|
+
let node;
|
|
490
|
+
if (root instanceof DocumentFragment) {
|
|
491
|
+
counts.push(countSignificantChildren(root));
|
|
492
|
+
const first = path[0];
|
|
493
|
+
node = first === undefined ? null : significantChildren(root, first);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
counts.push(0);
|
|
497
|
+
node = path[0] === 0 ? root : null;
|
|
498
|
+
}
|
|
499
|
+
for (let index = 1; index < path.length; index += 1) {
|
|
500
|
+
if (node === null) {
|
|
501
|
+
counts.push(0);
|
|
502
|
+
continue;
|
|
329
503
|
}
|
|
330
|
-
|
|
504
|
+
counts.push(countSignificantChildren(node));
|
|
505
|
+
const childIndex = path[index];
|
|
506
|
+
node = childIndex === undefined ? null : significantChildren(node, childIndex);
|
|
331
507
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
508
|
+
return { path, counts };
|
|
509
|
+
}
|
|
510
|
+
function pickSignificantChild(parent, index, size) {
|
|
511
|
+
if (size === 1 && index === 0) {
|
|
512
|
+
const first = parent.firstChild;
|
|
513
|
+
if (first !== null) {
|
|
514
|
+
if (first.nodeType !== Node.COMMENT_NODE) {
|
|
515
|
+
return first;
|
|
516
|
+
}
|
|
517
|
+
const data = first.data;
|
|
518
|
+
if (data !== INSERT_START && data !== INSERT_END) {
|
|
519
|
+
return first;
|
|
520
|
+
}
|
|
337
521
|
}
|
|
338
|
-
attrs.push(element);
|
|
339
522
|
}
|
|
340
|
-
|
|
523
|
+
if (size === undefined || size <= 0) {
|
|
524
|
+
return significantChildren(parent, index);
|
|
525
|
+
}
|
|
526
|
+
const last = size - 1;
|
|
527
|
+
if (index >= last - index) {
|
|
528
|
+
return significantChildrenFromEnd(parent, last - index);
|
|
529
|
+
}
|
|
530
|
+
return significantChildren(parent, index);
|
|
341
531
|
}
|
|
342
|
-
function
|
|
343
|
-
|
|
532
|
+
export function resolvePath(root, entry, significant) {
|
|
533
|
+
const path = entry.path;
|
|
534
|
+
const first = path[0];
|
|
535
|
+
if (first === undefined) {
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
let node;
|
|
539
|
+
if (root instanceof DocumentFragment) {
|
|
540
|
+
node = significant
|
|
541
|
+
? pickSignificantChild(root, first, entry.counts[0])
|
|
542
|
+
: root.childNodes[first] ?? null;
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
node = first === 0 ? root : null;
|
|
546
|
+
}
|
|
547
|
+
for (let index = 1; node !== null && index < path.length; index += 1) {
|
|
548
|
+
const childIndex = path[index];
|
|
549
|
+
if (childIndex === undefined) {
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
node = significant
|
|
553
|
+
? pickSignificantChild(node, childIndex, entry.counts[index])
|
|
554
|
+
: node.childNodes[childIndex] ?? null;
|
|
555
|
+
}
|
|
556
|
+
return node;
|
|
344
557
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
558
|
+
function applyPartsPlan(root, plan, strip, significant) {
|
|
559
|
+
const holes = [];
|
|
560
|
+
for (const entry of plan.holes) {
|
|
561
|
+
const node = resolvePath(root, entry, significant);
|
|
562
|
+
if (node !== null) {
|
|
563
|
+
holes.push(node);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (strip) {
|
|
567
|
+
for (const entry of plan.strips) {
|
|
568
|
+
const node = resolvePath(root, entry, significant);
|
|
569
|
+
if (node === null) {
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const element = node;
|
|
573
|
+
for (const name of entry.names) {
|
|
574
|
+
element.removeAttribute(name);
|
|
352
575
|
}
|
|
353
576
|
}
|
|
354
577
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
578
|
+
const attrs = [];
|
|
579
|
+
for (const entry of plan.attrs) {
|
|
580
|
+
const node = resolvePath(root, entry, significant);
|
|
581
|
+
if (node !== null) {
|
|
582
|
+
attrs.push(node);
|
|
583
|
+
}
|
|
361
584
|
}
|
|
362
|
-
|
|
363
|
-
|
|
585
|
+
return { holes, attrs };
|
|
586
|
+
}
|
|
587
|
+
export function templateHoles(html, plan) {
|
|
588
|
+
let clientFactory = null;
|
|
589
|
+
return () => {
|
|
590
|
+
if (SERVER && isServerMode()) {
|
|
591
|
+
const backend = getServerBackend();
|
|
592
|
+
if (backend !== null) {
|
|
593
|
+
const template = backend.createTemplate(html);
|
|
594
|
+
if (template !== null) {
|
|
595
|
+
markMetadataFree(template.node);
|
|
596
|
+
return template;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
clientFactory ??= createClientTemplateHoles(html, plan);
|
|
601
|
+
return clientFactory();
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
const NO_HOLES = [];
|
|
605
|
+
const NO_ATTRIBUTES = [];
|
|
606
|
+
function createClientTemplateHoles(html, providedPlan) {
|
|
607
|
+
let parsed = null;
|
|
608
|
+
const parse = () => {
|
|
609
|
+
parsed ??= readTemplate(html);
|
|
610
|
+
return parsed;
|
|
611
|
+
};
|
|
364
612
|
const mark = (node) => {
|
|
365
|
-
if (!metadata) {
|
|
613
|
+
if (!parse().metadata) {
|
|
366
614
|
if (node instanceof DocumentFragment) {
|
|
367
615
|
for (const child of node.childNodes) {
|
|
368
616
|
markMetadataFree(child);
|
|
@@ -373,20 +621,107 @@ export function templateHoles(html) {
|
|
|
373
621
|
return node;
|
|
374
622
|
};
|
|
375
623
|
const cloneNode = () => {
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
624
|
+
const clone = parse().content.cloneNode(true);
|
|
625
|
+
return clone.childNodes.length === 1 ? clone.firstChild ?? clone : clone;
|
|
626
|
+
};
|
|
627
|
+
const parts = () => {
|
|
628
|
+
const current = parse();
|
|
629
|
+
if (current.parts === null) {
|
|
630
|
+
current.parts = collectPartsPlan(current.content);
|
|
631
|
+
}
|
|
632
|
+
return current.parts;
|
|
381
633
|
};
|
|
382
634
|
const create = () => {
|
|
383
635
|
const node = mark(cloneNode());
|
|
384
|
-
return {
|
|
636
|
+
return {
|
|
637
|
+
node,
|
|
638
|
+
...applyPartsPlan(node, providedPlan ?? parts(), !isServerMode(), true),
|
|
639
|
+
};
|
|
640
|
+
};
|
|
641
|
+
const claimRoots = (plan) => {
|
|
642
|
+
if (plan.roots === 1) {
|
|
643
|
+
const next = claimNext();
|
|
644
|
+
if (next === null) {
|
|
645
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
646
|
+
}
|
|
647
|
+
markMetadataFree(next);
|
|
648
|
+
return next;
|
|
649
|
+
}
|
|
650
|
+
const claimed = [];
|
|
651
|
+
for (let index = 0; index < plan.roots; index += 1) {
|
|
652
|
+
const next = claimNext();
|
|
653
|
+
if (next === null) {
|
|
654
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
655
|
+
}
|
|
656
|
+
markMetadataFree(next);
|
|
657
|
+
claimed.push(next);
|
|
658
|
+
}
|
|
659
|
+
const container = document.createDocumentFragment();
|
|
660
|
+
for (const node of claimed) {
|
|
661
|
+
container.appendChild(node);
|
|
662
|
+
}
|
|
663
|
+
markMetadataFree(container);
|
|
664
|
+
return container;
|
|
385
665
|
};
|
|
386
666
|
return () => {
|
|
387
667
|
if (!isHydrating()) {
|
|
388
668
|
return create();
|
|
389
669
|
}
|
|
670
|
+
if (providedPlan !== undefined) {
|
|
671
|
+
const node = claimRoots(providedPlan);
|
|
672
|
+
const clone = { node, holes: NO_HOLES, attrs: NO_ATTRIBUTES };
|
|
673
|
+
adoptPartsPlan(node, providedPlan, clone);
|
|
674
|
+
return clone;
|
|
675
|
+
}
|
|
676
|
+
const current = parse();
|
|
677
|
+
const content = current.content;
|
|
678
|
+
const metadata = current.metadata;
|
|
679
|
+
const simple = current.simple;
|
|
680
|
+
const rootCount = current.rootCount;
|
|
681
|
+
if (!metadata) {
|
|
682
|
+
if (rootCount === 0) {
|
|
683
|
+
const empty = document.createDocumentFragment();
|
|
684
|
+
markMetadataFree(empty);
|
|
685
|
+
return { node: empty, holes: NO_HOLES, attrs: NO_ATTRIBUTES };
|
|
686
|
+
}
|
|
687
|
+
const expected = parts();
|
|
688
|
+
if (rootCount === 1) {
|
|
689
|
+
const node = claimNext();
|
|
690
|
+
if (node === null) {
|
|
691
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
692
|
+
}
|
|
693
|
+
markMetadataFree(node);
|
|
694
|
+
const collected = collectPartsMany([node]);
|
|
695
|
+
if (collected.holes.length !== expected.holes.length ||
|
|
696
|
+
collected.attrs.length !== expected.attrs.length) {
|
|
697
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
698
|
+
}
|
|
699
|
+
return { node, ...collected };
|
|
700
|
+
}
|
|
701
|
+
const claimed = [];
|
|
702
|
+
let cursor = content.firstChild;
|
|
703
|
+
while (cursor !== null) {
|
|
704
|
+
const next = cursor.nextSibling;
|
|
705
|
+
const node = claimNext();
|
|
706
|
+
if (node === null) {
|
|
707
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
708
|
+
}
|
|
709
|
+
markMetadataFree(node);
|
|
710
|
+
claimed.push(node);
|
|
711
|
+
cursor = next;
|
|
712
|
+
}
|
|
713
|
+
const collected = collectPartsMany(claimed);
|
|
714
|
+
if (collected.holes.length !== expected.holes.length ||
|
|
715
|
+
collected.attrs.length !== expected.attrs.length) {
|
|
716
|
+
return mismatch("шаблон не совпал с серверной разметкой");
|
|
717
|
+
}
|
|
718
|
+
const container = document.createDocumentFragment();
|
|
719
|
+
for (const node of claimed) {
|
|
720
|
+
container.appendChild(node);
|
|
721
|
+
}
|
|
722
|
+
markMetadataFree(container);
|
|
723
|
+
return { node: container, ...collected };
|
|
724
|
+
}
|
|
390
725
|
const fresh = cloneNode();
|
|
391
726
|
const roots = fresh instanceof DocumentFragment ? Array.from(fresh.childNodes) : [fresh];
|
|
392
727
|
if (roots.length === 1 && isMetadataElement(roots[0])) {
|
|
@@ -400,118 +735,119 @@ export function templateHoles(html) {
|
|
|
400
735
|
continue;
|
|
401
736
|
}
|
|
402
737
|
const node = claimNext();
|
|
403
|
-
if (node === null || !shapeMatches(root, node, true)) {
|
|
738
|
+
if (node === null || !shapeMatches(root, node, true, simple)) {
|
|
404
739
|
return mismatch("шаблон не совпал с серверной разметкой");
|
|
405
740
|
}
|
|
406
741
|
mergeTopLevelMetadata(root, node);
|
|
407
742
|
claimed.push(node);
|
|
408
743
|
}
|
|
409
|
-
const
|
|
410
|
-
if (!metadata) {
|
|
411
|
-
for (const node of claimed) {
|
|
412
|
-
markMetadataFree(node);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
744
|
+
const collected = collectPartsMany(claimed);
|
|
415
745
|
if (claimed.length === 1 && !(fresh instanceof DocumentFragment)) {
|
|
416
|
-
return { node: claimed[0], ...
|
|
746
|
+
return { node: claimed[0], ...collected };
|
|
417
747
|
}
|
|
418
748
|
const container = document.createDocumentFragment();
|
|
419
749
|
for (const node of claimed) {
|
|
420
750
|
container.appendChild(node);
|
|
421
751
|
}
|
|
422
|
-
|
|
423
|
-
markMetadataFree(container);
|
|
424
|
-
}
|
|
425
|
-
return { node: container, ...parts };
|
|
752
|
+
return { node: container, ...collected };
|
|
426
753
|
};
|
|
427
754
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const existing = collectBetween(start, end);
|
|
448
|
-
if (typeof value === "string" ||
|
|
449
|
-
typeof value === "number" ||
|
|
450
|
-
typeof value === "bigint") {
|
|
451
|
-
const next = stringOf(value);
|
|
452
|
-
if (existing.length === 1 && existing[0] instanceof Text) {
|
|
453
|
-
const text = existing[0];
|
|
454
|
-
if (text.data !== next) {
|
|
455
|
-
text.data = next;
|
|
755
|
+
function insertServer(parent, accessor, marker) {
|
|
756
|
+
const previous = marker === null ? parent.lastChild : marker.previousSibling;
|
|
757
|
+
let textNode = null;
|
|
758
|
+
let mount = null;
|
|
759
|
+
let initialized = false;
|
|
760
|
+
const writeText = (value) => {
|
|
761
|
+
const text = stringOf(value);
|
|
762
|
+
if (textNode === null) {
|
|
763
|
+
if (text === "") {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
textNode = document.createTextNode(text);
|
|
767
|
+
if (marker === null) {
|
|
768
|
+
parent.appendChild(textNode);
|
|
769
|
+
}
|
|
770
|
+
else {
|
|
771
|
+
parent.insertBefore(textNode, marker);
|
|
456
772
|
}
|
|
457
773
|
return;
|
|
458
774
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
const mount = createMountPoint(parent, end);
|
|
480
|
-
createRenderEffect(() => {
|
|
481
|
-
mount.render(accessor());
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
function insertHydrated(parent, accessor, marker) {
|
|
485
|
-
const window = findInsertWindow(marker, parent);
|
|
486
|
-
let initialized = false;
|
|
487
|
-
createRenderEffect(() => {
|
|
775
|
+
if (textNode.data !== text) {
|
|
776
|
+
textNode.data = text;
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
const openRegion = () => {
|
|
780
|
+
const start = document.createComment(INSERT_START);
|
|
781
|
+
const end = document.createComment(INSERT_END);
|
|
782
|
+
if (marker === null) {
|
|
783
|
+
parent.appendChild(start);
|
|
784
|
+
parent.appendChild(end);
|
|
785
|
+
}
|
|
786
|
+
else {
|
|
787
|
+
parent.insertBefore(start, marker);
|
|
788
|
+
parent.insertBefore(end, marker);
|
|
789
|
+
}
|
|
790
|
+
mount = createMountPoint(parent, end);
|
|
791
|
+
return mount;
|
|
792
|
+
};
|
|
793
|
+
createEffectUnlessStatic(() => {
|
|
794
|
+
const value = accessor();
|
|
488
795
|
if (!initialized) {
|
|
489
796
|
initialized = true;
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
if ((typeof value === "string" ||
|
|
494
|
-
typeof value === "number" ||
|
|
495
|
-
typeof value === "bigint") &&
|
|
496
|
-
nodes.length === 1 &&
|
|
497
|
-
nodes[0] instanceof Text) {
|
|
498
|
-
const text = nodes[0];
|
|
499
|
-
const next = stringOf(value);
|
|
500
|
-
if (text.data !== next) {
|
|
501
|
-
text.data = next;
|
|
502
|
-
}
|
|
797
|
+
if (isTextValue(value) && !(previous instanceof Text)) {
|
|
798
|
+
writeText(value);
|
|
799
|
+
return;
|
|
503
800
|
}
|
|
801
|
+
openRegion().render(value);
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
if (mount !== null) {
|
|
805
|
+
mount.render(value);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
if (isTextValue(value)) {
|
|
809
|
+
writeText(value);
|
|
504
810
|
return;
|
|
505
811
|
}
|
|
506
|
-
|
|
812
|
+
const stale = textNode;
|
|
813
|
+
const created = openRegion();
|
|
814
|
+
if (stale !== null && stale.parentNode !== null) {
|
|
815
|
+
stale.parentNode.removeChild(stale);
|
|
816
|
+
}
|
|
817
|
+
textNode = null;
|
|
818
|
+
created.render(value);
|
|
507
819
|
});
|
|
508
820
|
}
|
|
821
|
+
export function ssrValue(value, bare) {
|
|
822
|
+
if (bare && isTextValue(value)) {
|
|
823
|
+
return serializeServerValue(value);
|
|
824
|
+
}
|
|
825
|
+
return `<!--${INSERT_START}-->${serializeServerValue(value)}<!--${INSERT_END}-->`;
|
|
826
|
+
}
|
|
827
|
+
export function ssrRaw(text) {
|
|
828
|
+
const backend = getServerBackend();
|
|
829
|
+
if (backend === null) {
|
|
830
|
+
throw new Error("ssrRaw() доступен только при серверном рендеринге");
|
|
831
|
+
}
|
|
832
|
+
const created = backend.createRaw?.(text);
|
|
833
|
+
const node = created ?? backend.createTemplate(text)?.node;
|
|
834
|
+
if (node === null || node === undefined) {
|
|
835
|
+
throw new Error("Серверный backend не поддерживает строковые узлы");
|
|
836
|
+
}
|
|
837
|
+
markMetadataFree(node);
|
|
838
|
+
return node;
|
|
839
|
+
}
|
|
509
840
|
export function insert(parent, accessor, marker) {
|
|
510
841
|
if (isServerHole(marker)) {
|
|
511
|
-
|
|
842
|
+
if (SERVER) {
|
|
843
|
+
const value = accessor();
|
|
844
|
+
if (!setServerHoleValue(marker, value)) {
|
|
845
|
+
marker.set(`<!--${INSERT_START}-->${serializeServerValue(value)}<!--${INSERT_END}-->`);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
512
848
|
return;
|
|
513
849
|
}
|
|
514
|
-
if (isServerMode()) {
|
|
850
|
+
if (SERVER && isServerMode()) {
|
|
515
851
|
insertServer(parent, accessor, marker ?? null);
|
|
516
852
|
return;
|
|
517
853
|
}
|
|
@@ -525,13 +861,18 @@ export function insert(parent, accessor, marker) {
|
|
|
525
861
|
});
|
|
526
862
|
}
|
|
527
863
|
export function text(node, accessor) {
|
|
528
|
-
|
|
864
|
+
const apply = () => {
|
|
529
865
|
const value = accessor();
|
|
530
866
|
const next = stringOf(value);
|
|
531
867
|
if (node.data !== next) {
|
|
532
868
|
node.data = next;
|
|
533
869
|
}
|
|
534
|
-
}
|
|
870
|
+
};
|
|
871
|
+
if (isHydrating()) {
|
|
872
|
+
hydratedEffect(apply);
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
createRenderEffect(apply);
|
|
535
876
|
}
|
|
536
877
|
function isSvgElement(el) {
|
|
537
878
|
return el instanceof SVGElement;
|
|
@@ -543,18 +884,33 @@ function setClass(el, value) {
|
|
|
543
884
|
}
|
|
544
885
|
el.setAttribute("class", stringOf(value));
|
|
545
886
|
}
|
|
887
|
+
function serverAttribute(el) {
|
|
888
|
+
return el instanceof Element ? null : el;
|
|
889
|
+
}
|
|
546
890
|
export function attr(el, name, accessor) {
|
|
891
|
+
const server = serverAttribute(el);
|
|
892
|
+
if (server !== null) {
|
|
893
|
+
createEffectUnlessStatic(() => {
|
|
894
|
+
server.setAttribute(name, accessor());
|
|
895
|
+
});
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
547
898
|
const svg = isSvgElement(el);
|
|
548
899
|
let prev = accessor();
|
|
549
900
|
applyAttr(el, name, prev, svg);
|
|
550
|
-
|
|
901
|
+
const apply = () => {
|
|
551
902
|
const value = accessor();
|
|
552
903
|
if (valuesEqual(value, prev)) {
|
|
553
904
|
return;
|
|
554
905
|
}
|
|
555
906
|
prev = value;
|
|
556
907
|
applyAttr(el, name, value, svg);
|
|
557
|
-
}
|
|
908
|
+
};
|
|
909
|
+
if (isHydrating()) {
|
|
910
|
+
hydratedEffect(apply);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
createRenderEffect(apply);
|
|
558
914
|
}
|
|
559
915
|
export function applyAttr(el, name, value, svg) {
|
|
560
916
|
if (name === "class" || name === "className") {
|
|
@@ -606,8 +962,25 @@ function applyProperty(el, name, value) {
|
|
|
606
962
|
el.removeAttribute(name);
|
|
607
963
|
}
|
|
608
964
|
export function spread(el, accessor) {
|
|
965
|
+
const server = serverAttribute(el);
|
|
966
|
+
if (server !== null) {
|
|
967
|
+
createEffectUnlessStatic(() => {
|
|
968
|
+
const value = accessor();
|
|
969
|
+
if (typeof value !== "object" || value === null) {
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
for (const [key, item] of Object.entries(value)) {
|
|
973
|
+
if (key === "ref" || key === "children") {
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
server.setAttribute(key, item);
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
return;
|
|
980
|
+
}
|
|
609
981
|
const svg = isSvgElement(el);
|
|
610
982
|
let prev = null;
|
|
983
|
+
let prevKeys = null;
|
|
611
984
|
let currentRef = null;
|
|
612
985
|
const releaseRef = () => {
|
|
613
986
|
const previous = currentRef;
|
|
@@ -622,8 +995,9 @@ export function spread(el, accessor) {
|
|
|
622
995
|
return;
|
|
623
996
|
}
|
|
624
997
|
const props = value;
|
|
625
|
-
|
|
626
|
-
|
|
998
|
+
const keys = Object.keys(props);
|
|
999
|
+
if (prev !== null && prevKeys !== null) {
|
|
1000
|
+
for (const key of prevKeys) {
|
|
627
1001
|
if (key in props) {
|
|
628
1002
|
continue;
|
|
629
1003
|
}
|
|
@@ -642,7 +1016,8 @@ export function spread(el, accessor) {
|
|
|
642
1016
|
applyAttr(el, key, undefined, svg);
|
|
643
1017
|
}
|
|
644
1018
|
}
|
|
645
|
-
for (const
|
|
1019
|
+
for (const key of keys) {
|
|
1020
|
+
const next = props[key];
|
|
646
1021
|
if (prev !== null && valuesEqual(prev[key], next)) {
|
|
647
1022
|
continue;
|
|
648
1023
|
}
|
|
@@ -675,10 +1050,18 @@ export function spread(el, accessor) {
|
|
|
675
1050
|
applyAttr(el, key, next, svg);
|
|
676
1051
|
}
|
|
677
1052
|
prev = props;
|
|
1053
|
+
prevKeys = keys;
|
|
678
1054
|
});
|
|
679
1055
|
onCleanup(releaseRef);
|
|
680
1056
|
}
|
|
681
1057
|
export function prop(el, name, accessor) {
|
|
1058
|
+
const server = serverAttribute(el);
|
|
1059
|
+
if (server !== null) {
|
|
1060
|
+
createEffectUnlessStatic(() => {
|
|
1061
|
+
server.setAttribute(name, accessor());
|
|
1062
|
+
});
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
682
1065
|
let prev;
|
|
683
1066
|
let initialized = false;
|
|
684
1067
|
createRenderEffect(() => {
|
|
@@ -692,6 +1075,13 @@ export function prop(el, name, accessor) {
|
|
|
692
1075
|
});
|
|
693
1076
|
}
|
|
694
1077
|
export function style(el, accessor) {
|
|
1078
|
+
const server = serverAttribute(el);
|
|
1079
|
+
if (server !== null) {
|
|
1080
|
+
createEffectUnlessStatic(() => {
|
|
1081
|
+
server.setAttribute("style", accessor());
|
|
1082
|
+
});
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
695
1085
|
const snapshot = ensureSnapshot(el);
|
|
696
1086
|
createRenderEffect(() => {
|
|
697
1087
|
applyStyle(el, accessor(), snapshot);
|
|
@@ -705,28 +1095,47 @@ function ensureSnapshot(el) {
|
|
|
705
1095
|
}
|
|
706
1096
|
return snapshot;
|
|
707
1097
|
}
|
|
1098
|
+
function ensureAppliedStyle(el) {
|
|
1099
|
+
let applied = styleValues.get(el);
|
|
1100
|
+
if (applied === undefined) {
|
|
1101
|
+
applied = new Map();
|
|
1102
|
+
styleValues.set(el, applied);
|
|
1103
|
+
}
|
|
1104
|
+
return applied;
|
|
1105
|
+
}
|
|
708
1106
|
function applyStyle(el, value, snapshot) {
|
|
709
1107
|
const style = el.style;
|
|
1108
|
+
const applied = ensureAppliedStyle(el);
|
|
710
1109
|
if (value === null || value === undefined) {
|
|
711
1110
|
for (const key of snapshot) {
|
|
712
1111
|
style.removeProperty(key);
|
|
713
1112
|
}
|
|
714
1113
|
snapshot.clear();
|
|
1114
|
+
applied.clear();
|
|
715
1115
|
return;
|
|
716
1116
|
}
|
|
717
1117
|
if (typeof value === "string") {
|
|
718
1118
|
if (style.cssText !== value) {
|
|
719
1119
|
style.cssText = value;
|
|
720
1120
|
}
|
|
1121
|
+
applied.clear();
|
|
721
1122
|
const keep = new Set();
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
const
|
|
1123
|
+
let cursor = 0;
|
|
1124
|
+
while (cursor <= value.length) {
|
|
1125
|
+
const limit = value.indexOf(";", cursor);
|
|
1126
|
+
const entryEnd = limit === -1 ? value.length : limit;
|
|
1127
|
+
const separator = value.indexOf(":", cursor);
|
|
1128
|
+
const keyEnd = separator !== -1 && separator < entryEnd ? separator : entryEnd;
|
|
1129
|
+
const key = value.slice(cursor, keyEnd).trim();
|
|
725
1130
|
if (key !== "") {
|
|
726
1131
|
keep.add(key);
|
|
727
1132
|
}
|
|
1133
|
+
if (limit === -1) {
|
|
1134
|
+
break;
|
|
1135
|
+
}
|
|
1136
|
+
cursor = limit + 1;
|
|
728
1137
|
}
|
|
729
|
-
for (const key of
|
|
1138
|
+
for (const key of snapshot) {
|
|
730
1139
|
if (!keep.has(key)) {
|
|
731
1140
|
style.removeProperty(key);
|
|
732
1141
|
snapshot.delete(key);
|
|
@@ -742,108 +1151,208 @@ function applyStyle(el, value, snapshot) {
|
|
|
742
1151
|
}
|
|
743
1152
|
const props = value;
|
|
744
1153
|
const keys = new Set();
|
|
745
|
-
for (const
|
|
1154
|
+
for (const raw of Object.keys(props)) {
|
|
1155
|
+
const next = props[raw];
|
|
746
1156
|
const key = hyphen(raw);
|
|
747
1157
|
const target = next === null || next === undefined ? "" : String(next);
|
|
748
1158
|
keys.add(key);
|
|
749
1159
|
if (target === "") {
|
|
750
|
-
if (
|
|
1160
|
+
if (applied.get(key) !== "") {
|
|
751
1161
|
style.removeProperty(key);
|
|
1162
|
+
applied.delete(key);
|
|
752
1163
|
}
|
|
753
1164
|
snapshot.delete(key);
|
|
754
1165
|
continue;
|
|
755
1166
|
}
|
|
756
|
-
if (
|
|
1167
|
+
if (applied.get(key) !== target) {
|
|
757
1168
|
style.setProperty(key, target);
|
|
1169
|
+
applied.set(key, target);
|
|
758
1170
|
}
|
|
759
1171
|
snapshot.add(key);
|
|
760
1172
|
}
|
|
761
|
-
for (const key of
|
|
1173
|
+
for (const key of snapshot) {
|
|
762
1174
|
if (!keys.has(key)) {
|
|
763
1175
|
style.removeProperty(key);
|
|
764
1176
|
snapshot.delete(key);
|
|
1177
|
+
applied.delete(key);
|
|
765
1178
|
}
|
|
766
1179
|
}
|
|
767
1180
|
}
|
|
768
1181
|
export function hyphen(raw) {
|
|
1182
|
+
const cached = hyphenCache.get(raw);
|
|
1183
|
+
if (cached !== undefined) {
|
|
1184
|
+
return cached;
|
|
1185
|
+
}
|
|
1186
|
+
let result;
|
|
769
1187
|
if (raw.includes("-")) {
|
|
770
|
-
|
|
1188
|
+
result = raw.toLowerCase();
|
|
771
1189
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
1190
|
+
else {
|
|
1191
|
+
result = "";
|
|
1192
|
+
for (const char of raw) {
|
|
1193
|
+
if (char >= "A" && char <= "Z") {
|
|
1194
|
+
result += "-" + char.toLowerCase();
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
result += char;
|
|
777
1198
|
}
|
|
778
|
-
|
|
1199
|
+
}
|
|
1200
|
+
if (hyphenCache.size < AUX_CACHE_LIMIT) {
|
|
1201
|
+
hyphenCache.set(raw, result);
|
|
779
1202
|
}
|
|
780
1203
|
return result;
|
|
781
1204
|
}
|
|
782
|
-
function
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
}
|
|
788
|
-
|
|
1205
|
+
function createHolder(key, handler) {
|
|
1206
|
+
const holder = {
|
|
1207
|
+
current: typeof handler === "function" ? handler : null,
|
|
1208
|
+
wrapper: () => { },
|
|
1209
|
+
key,
|
|
1210
|
+
};
|
|
1211
|
+
const wrapper = (event) => {
|
|
1212
|
+
const current = holder.current;
|
|
1213
|
+
if (typeof current === "function") {
|
|
1214
|
+
current(event);
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
holder.wrapper = wrapper;
|
|
1218
|
+
return holder;
|
|
789
1219
|
}
|
|
790
1220
|
function isEventName(name) {
|
|
791
1221
|
return name.startsWith("on:") || (name.startsWith("on") && name.length > 2);
|
|
792
1222
|
}
|
|
793
1223
|
export function parseEventName(name) {
|
|
1224
|
+
const parsed = resolveEventName(name);
|
|
1225
|
+
return { event: parsed.event, capture: parsed.capture };
|
|
1226
|
+
}
|
|
1227
|
+
function resolveEventName(name) {
|
|
1228
|
+
const cached = eventNameCache.get(name);
|
|
1229
|
+
if (cached !== undefined) {
|
|
1230
|
+
return cached;
|
|
1231
|
+
}
|
|
1232
|
+
const parsed = buildEventName(name);
|
|
1233
|
+
if (eventNameCache.size < AUX_CACHE_LIMIT) {
|
|
1234
|
+
eventNameCache.set(name, parsed);
|
|
1235
|
+
}
|
|
1236
|
+
return parsed;
|
|
1237
|
+
}
|
|
1238
|
+
function buildEventName(name) {
|
|
794
1239
|
if (name.startsWith("on:")) {
|
|
795
|
-
|
|
1240
|
+
const event = name.slice(3);
|
|
1241
|
+
return { event, capture: false, key: "e:" + event };
|
|
796
1242
|
}
|
|
797
1243
|
let event = name.slice(2);
|
|
798
|
-
|
|
1244
|
+
const capture = event.endsWith("Capture");
|
|
1245
|
+
if (capture) {
|
|
799
1246
|
event = event.slice(0, -7);
|
|
800
|
-
const first = event.charAt(0);
|
|
801
|
-
event = first === "" ? event : first.toLowerCase() + event.slice(1);
|
|
802
|
-
return { event, capture: true };
|
|
803
1247
|
}
|
|
804
1248
|
const first = event.charAt(0);
|
|
805
1249
|
event = first === "" ? event : first.toLowerCase() + event.slice(1);
|
|
806
|
-
return { event, capture:
|
|
1250
|
+
return { event, capture, key: (capture ? "c:" : "e:") + event };
|
|
1251
|
+
}
|
|
1252
|
+
function ensureDelegated(event, key) {
|
|
1253
|
+
if (delegatedInstalled.has(key)) {
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
delegatedInstalled.add(key);
|
|
1257
|
+
document.addEventListener(event, (nativeEvent) => dispatchDelegated(nativeEvent, key));
|
|
1258
|
+
}
|
|
1259
|
+
function dispatchDelegated(event, key) {
|
|
1260
|
+
let node = event.target;
|
|
1261
|
+
while (node !== null) {
|
|
1262
|
+
if (node instanceof Element) {
|
|
1263
|
+
const entry = listenerRegistry.get(node);
|
|
1264
|
+
const holder = entry instanceof Map ? entry.get(key) : entry !== undefined && entry.key === key ? entry : undefined;
|
|
1265
|
+
if (holder !== undefined) {
|
|
1266
|
+
const handler = holder.current;
|
|
1267
|
+
if (typeof handler === "function") {
|
|
1268
|
+
handler(event);
|
|
1269
|
+
}
|
|
1270
|
+
if (event.cancelBubble) {
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
node = node.parentNode;
|
|
1276
|
+
}
|
|
807
1277
|
}
|
|
808
1278
|
export function addListener(el, name, handler) {
|
|
809
|
-
const { event, capture } =
|
|
810
|
-
const
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
1279
|
+
const { event, capture, key } = resolveEventName(name);
|
|
1280
|
+
const delegated = !capture
|
|
1281
|
+
&& typeof document !== "undefined"
|
|
1282
|
+
&& !isServerMode()
|
|
1283
|
+
&& DELEGATED_EVENTS.test(event)
|
|
1284
|
+
&& (el.ownerDocument === document || el.ownerDocument.defaultView === null);
|
|
1285
|
+
const existing = listenerRegistry.get(el);
|
|
1286
|
+
if (existing === undefined) {
|
|
1287
|
+
const holder = createHolder(key, handler);
|
|
1288
|
+
listenerRegistry.set(el, holder);
|
|
1289
|
+
if (delegated) {
|
|
1290
|
+
ensureDelegated(event, key);
|
|
1291
|
+
}
|
|
1292
|
+
else {
|
|
1293
|
+
el.addEventListener(event, holder.wrapper, capture);
|
|
1294
|
+
}
|
|
815
1295
|
return;
|
|
816
1296
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
const current = holder.current;
|
|
823
|
-
if (typeof current === "function") {
|
|
824
|
-
current(event);
|
|
1297
|
+
if (existing instanceof Map) {
|
|
1298
|
+
const registered = existing.get(key);
|
|
1299
|
+
if (registered !== undefined) {
|
|
1300
|
+
registered.current = typeof handler === "function" ? handler : null;
|
|
1301
|
+
return;
|
|
825
1302
|
}
|
|
826
|
-
|
|
827
|
-
|
|
1303
|
+
const holder = createHolder(key, handler);
|
|
1304
|
+
existing.set(key, holder);
|
|
1305
|
+
if (delegated) {
|
|
1306
|
+
ensureDelegated(event, key);
|
|
1307
|
+
}
|
|
1308
|
+
else {
|
|
1309
|
+
el.addEventListener(event, holder.wrapper, capture);
|
|
1310
|
+
}
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
if (existing.key === key) {
|
|
1314
|
+
existing.current = typeof handler === "function" ? handler : null;
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
const holder = createHolder(key, handler);
|
|
1318
|
+
const holders = new Map();
|
|
1319
|
+
holders.set(existing.key, existing);
|
|
828
1320
|
holders.set(key, holder);
|
|
829
|
-
|
|
1321
|
+
listenerRegistry.set(el, holders);
|
|
1322
|
+
if (delegated) {
|
|
1323
|
+
ensureDelegated(event, key);
|
|
1324
|
+
}
|
|
1325
|
+
else {
|
|
1326
|
+
el.addEventListener(event, holder.wrapper, capture);
|
|
1327
|
+
}
|
|
830
1328
|
}
|
|
831
1329
|
export function removeListener(el, name) {
|
|
832
|
-
const { event, capture } =
|
|
1330
|
+
const { event, capture, key } = resolveEventName(name);
|
|
833
1331
|
const holders = listenerRegistry.get(el);
|
|
834
1332
|
if (holders === undefined) {
|
|
835
1333
|
return;
|
|
836
1334
|
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1335
|
+
if (holders instanceof Map) {
|
|
1336
|
+
const registered = holders.get(key);
|
|
1337
|
+
if (registered === undefined) {
|
|
1338
|
+
return;
|
|
1339
|
+
}
|
|
1340
|
+
holders.delete(key);
|
|
1341
|
+
registered.current = null;
|
|
1342
|
+
el.removeEventListener(event, registered.wrapper, capture);
|
|
1343
|
+
return;
|
|
1344
|
+
}
|
|
1345
|
+
if (holders.key !== key) {
|
|
840
1346
|
return;
|
|
841
1347
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
el.removeEventListener(event,
|
|
1348
|
+
listenerRegistry.delete(el);
|
|
1349
|
+
holders.current = null;
|
|
1350
|
+
el.removeEventListener(event, holders.wrapper, capture);
|
|
845
1351
|
}
|
|
846
1352
|
export function on(el, name, accessor) {
|
|
1353
|
+
if (!(el instanceof Element)) {
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
847
1356
|
createRenderEffect(() => {
|
|
848
1357
|
const value = accessor();
|
|
849
1358
|
addListener(el, name, typeof value === "function" ? value : null);
|
|
@@ -894,7 +1403,7 @@ export function fragment(accessor) {
|
|
|
894
1403
|
createRenderEffect(() => {
|
|
895
1404
|
if (!initialized) {
|
|
896
1405
|
initialized = true;
|
|
897
|
-
const value = withWindow(window,
|
|
1406
|
+
const value = withWindow(window, accessor);
|
|
898
1407
|
settleNodes(anchor, value);
|
|
899
1408
|
if (window.cursor !== anchor) {
|
|
900
1409
|
mismatch("содержимое fragment не совпало");
|
|
@@ -929,7 +1438,8 @@ export function applyProps(el, props) {
|
|
|
929
1438
|
}
|
|
930
1439
|
const svg = isSvgElement(el);
|
|
931
1440
|
const children = props["children"];
|
|
932
|
-
for (const
|
|
1441
|
+
for (const key of Object.keys(props)) {
|
|
1442
|
+
const value = props[key];
|
|
933
1443
|
if (key === "children" || key === "key") {
|
|
934
1444
|
continue;
|
|
935
1445
|
}
|