@ivlev-dev/silent-core 0.5.0 → 0.7.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.
Files changed (103) hide show
  1. package/dist/component-lite.d.ts +14 -0
  2. package/dist/component-lite.d.ts.map +1 -0
  3. package/dist/component-lite.js +138 -0
  4. package/dist/component-lite.js.map +1 -0
  5. package/dist/component-state.d.ts +42 -0
  6. package/dist/component-state.d.ts.map +1 -0
  7. package/dist/component-state.js +98 -0
  8. package/dist/component-state.js.map +1 -0
  9. package/dist/component-view.d.ts +5 -0
  10. package/dist/component-view.d.ts.map +1 -0
  11. package/dist/component-view.js +445 -0
  12. package/dist/component-view.js.map +1 -0
  13. package/dist/component.d.ts +4 -35
  14. package/dist/component.d.ts.map +1 -1
  15. package/dist/component.js +5 -588
  16. package/dist/component.js.map +1 -1
  17. package/dist/control-flow.d.ts.map +1 -1
  18. package/dist/control-flow.js +447 -161
  19. package/dist/control-flow.js.map +1 -1
  20. package/dist/decorators.d.ts.map +1 -1
  21. package/dist/decorators.js +4 -1
  22. package/dist/decorators.js.map +1 -1
  23. package/dist/dom-hydration-state.d.ts +21 -0
  24. package/dist/dom-hydration-state.d.ts.map +1 -0
  25. package/dist/dom-hydration-state.js +30 -0
  26. package/dist/dom-hydration-state.js.map +1 -0
  27. package/dist/dom-hydration.d.ts +10 -0
  28. package/dist/dom-hydration.d.ts.map +1 -0
  29. package/dist/dom-hydration.js +537 -0
  30. package/dist/dom-hydration.js.map +1 -0
  31. package/dist/dom.d.ts +35 -5
  32. package/dist/dom.d.ts.map +1 -1
  33. package/dist/dom.js +809 -285
  34. package/dist/dom.js.map +1 -1
  35. package/dist/error-boundary.d.ts.map +1 -1
  36. package/dist/error-boundary.js +68 -20
  37. package/dist/error-boundary.js.map +1 -1
  38. package/dist/hydrate.d.ts +4 -0
  39. package/dist/hydrate.d.ts.map +1 -0
  40. package/dist/hydrate.js +131 -0
  41. package/dist/hydrate.js.map +1 -0
  42. package/dist/hydration-proxy.d.ts +14 -0
  43. package/dist/hydration-proxy.d.ts.map +1 -0
  44. package/dist/hydration-proxy.js +40 -0
  45. package/dist/hydration-proxy.js.map +1 -0
  46. package/dist/hydration-state.d.ts +47 -0
  47. package/dist/hydration-state.d.ts.map +1 -0
  48. package/dist/hydration-state.js +83 -0
  49. package/dist/hydration-state.js.map +1 -0
  50. package/dist/hydration.d.ts +7 -32
  51. package/dist/hydration.d.ts.map +1 -1
  52. package/dist/hydration.js +299 -123
  53. package/dist/hydration.js.map +1 -1
  54. package/dist/index-list.d.ts.map +1 -1
  55. package/dist/index-list.js +249 -63
  56. package/dist/index-list.js.map +1 -1
  57. package/dist/index.d.ts +3 -1
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +3 -1
  60. package/dist/index.js.map +1 -1
  61. package/dist/jsx-runtime.d.ts +1 -1
  62. package/dist/jsx-runtime.d.ts.map +1 -1
  63. package/dist/jsx-runtime.js +4 -3
  64. package/dist/jsx-runtime.js.map +1 -1
  65. package/dist/metadata.d.ts +1 -0
  66. package/dist/metadata.d.ts.map +1 -1
  67. package/dist/metadata.js +29 -11
  68. package/dist/metadata.js.map +1 -1
  69. package/dist/mount-core.d.ts +14 -0
  70. package/dist/mount-core.d.ts.map +1 -0
  71. package/dist/mount-core.js +64 -0
  72. package/dist/mount-core.js.map +1 -0
  73. package/dist/mount.d.ts +1 -2
  74. package/dist/mount.d.ts.map +1 -1
  75. package/dist/mount.js +17 -132
  76. package/dist/mount.js.map +1 -1
  77. package/dist/server-backend.d.ts +23 -0
  78. package/dist/server-backend.d.ts.map +1 -0
  79. package/dist/server-backend.js +25 -0
  80. package/dist/server-backend.js.map +1 -0
  81. package/dist/server-hole.d.ts +3 -0
  82. package/dist/server-hole.d.ts.map +1 -0
  83. package/dist/server-hole.js +12 -0
  84. package/dist/server-hole.js.map +1 -0
  85. package/dist/server-proxy.d.ts +6 -0
  86. package/dist/server-proxy.d.ts.map +1 -0
  87. package/dist/server-proxy.js +14 -0
  88. package/dist/server-proxy.js.map +1 -0
  89. package/dist/server-state.d.ts +10 -0
  90. package/dist/server-state.d.ts.map +1 -0
  91. package/dist/server-state.js +14 -0
  92. package/dist/server-state.js.map +1 -0
  93. package/dist/static-effect.d.ts +4 -0
  94. package/dist/static-effect.d.ts.map +1 -0
  95. package/dist/static-effect.js +11 -0
  96. package/dist/static-effect.js.map +1 -0
  97. package/dist/suspense.d.ts.map +1 -1
  98. package/dist/suspense.js +4 -5
  99. package/dist/suspense.js.map +1 -1
  100. package/dist/switch.d.ts.map +1 -1
  101. package/dist/switch.js +37 -33
  102. package/dist/switch.js.map +1 -1
  103. package/package.json +4 -4
package/dist/dom.js CHANGED
@@ -1,12 +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 { FRAGMENT, INSERT_END, INSERT_START, claimNext, collectBetween, currentWindow, findInsertWindow, isHydrating, isMarker, mergeTopLevelMetadata, mismatch, shapeMatches, withWindow, } from "./hydration.js";
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";
4
7
  export const NODE_DISPOSER = Symbol("silent.disposer");
5
- const templateCache = new Map();
8
+ const ITEM_DISPOSER = Symbol("silent.itemDisposer");
9
+ const parseCache = new Map();
6
10
  const METADATA_PATTERN = /<(?:title|meta|link)[\s/>]/i;
7
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)$/;
8
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;
9
22
  let serverMode = false;
23
+ const SERVER = typeof __SILENT_SERVER__ === "undefined" || __SILENT_SERVER__;
10
24
  export function setServerMode(enabled) {
11
25
  serverMode = enabled;
12
26
  }
@@ -50,11 +64,26 @@ export const svgTagNames = new Set([
50
64
  function isRenderableChild(value) {
51
65
  return value !== null && value !== undefined && typeof value !== "boolean";
52
66
  }
53
- function stringOf(value) {
54
- if (value === null || value === undefined || typeof value === "boolean") {
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") {
55
77
  return "";
56
78
  }
57
- return String(value);
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";
58
87
  }
59
88
  function valuesEqual(a, b) {
60
89
  if (a === b) {
@@ -71,48 +100,69 @@ function valuesEqual(a, b) {
71
100
  export function attachDisposer(node, dispose) {
72
101
  node[NODE_DISPOSER] = dispose;
73
102
  }
103
+ export function attachItemDisposer(node, dispose) {
104
+ node[ITEM_DISPOSER] = dispose;
105
+ }
106
+ export function clearItemDisposer(node) {
107
+ delete node[ITEM_DISPOSER];
108
+ }
74
109
  function readDisposer(node) {
75
110
  const dispose = node[NODE_DISPOSER];
76
111
  return typeof dispose === "function" ? dispose : undefined;
77
112
  }
78
- export function collectNodes(value) {
79
- const nodes = [];
80
- const visit = (input) => {
81
- let current = input;
82
- while (typeof current === "function") {
83
- current = current();
84
- }
85
- if (!isRenderableChild(current)) {
86
- return;
87
- }
88
- if (typeof current === "boolean") {
89
- 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);
90
124
  }
91
- if (Array.isArray(current)) {
92
- for (const item of current) {
93
- visit(item);
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;
94
134
  }
95
135
  return;
96
136
  }
97
- if (current instanceof Node) {
98
- if (current instanceof DocumentFragment) {
99
- let child = current.firstChild;
100
- while (child !== null) {
101
- const next = child.nextSibling;
102
- visit(child);
103
- child = next;
104
- }
105
- return;
106
- }
107
- nodes.push(current);
108
- 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];
109
146
  }
110
- nodes.push(document.createTextNode(stringOf(current)));
111
- };
112
- visit(value);
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);
113
158
  return nodes;
114
159
  }
115
160
  export function disposeNode(node) {
161
+ const itemDispose = node[ITEM_DISPOSER];
162
+ if (typeof itemDispose === "function") {
163
+ itemDispose();
164
+ return;
165
+ }
116
166
  const dispose = readDisposer(node);
117
167
  if (dispose !== undefined) {
118
168
  dispose();
@@ -141,19 +191,12 @@ export function createMountPoint(parent, marker, initial) {
141
191
  let prev = null;
142
192
  let textNode = null;
143
193
  let nodes = initial === undefined ? [] : [...initial];
144
- const holder = () => {
145
- if (marker === null) {
146
- return parent;
147
- }
148
- const live = marker.parentNode;
149
- return live ?? parent;
150
- };
151
- const place = (node) => {
194
+ const place = (target, node) => {
152
195
  if (marker === null) {
153
- holder().appendChild(node);
196
+ target.appendChild(node);
154
197
  return;
155
198
  }
156
- holder().insertBefore(node, marker);
199
+ target.insertBefore(node, marker);
157
200
  };
158
201
  const clear = () => {
159
202
  for (const node of nodes) {
@@ -183,7 +226,7 @@ export function createMountPoint(parent, marker, initial) {
183
226
  clear();
184
227
  textNode = document.createTextNode(next);
185
228
  nodes = [textNode];
186
- place(textNode);
229
+ place(marker === null ? parent : marker.parentNode ?? parent, textNode);
187
230
  return;
188
231
  }
189
232
  if (textNode.data !== next) {
@@ -195,26 +238,44 @@ export function createMountPoint(parent, marker, initial) {
195
238
  if (!isRenderableChild(value)) {
196
239
  return;
197
240
  }
198
- nodes = collectNodes(value);
199
- nodes = extractMetadata(nodes);
241
+ nodes = extractMetadata(collectNodes(value));
242
+ const target = marker === null ? parent : marker.parentNode ?? parent;
200
243
  for (const node of nodes) {
201
- place(node);
244
+ place(target, node);
202
245
  }
203
246
  },
204
247
  };
205
248
  }
206
- export function template(html) {
207
- let cached = templateCache.get(html);
208
- if (cached === undefined) {
209
- const element = document.createElement("template");
210
- element.innerHTML = html;
211
- templateCache.set(html, element);
212
- cached = element;
213
- }
214
- const content = cached.content;
215
- const roots = Array.from(content.childNodes);
216
- 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;
217
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);
218
279
  const mark = (node) => {
219
280
  if (!metadata) {
220
281
  if (node instanceof DocumentFragment) {
@@ -230,19 +291,25 @@ export function template(html) {
230
291
  if (singleRoot !== null) {
231
292
  return mark(singleRoot.cloneNode(true));
232
293
  }
233
- if (roots.length === 0) {
294
+ if (rootCount === 0) {
234
295
  return mark(content.cloneNode(false));
235
296
  }
236
- const fragment = document.createDocumentFragment();
237
- for (const root of roots) {
238
- fragment.appendChild(root.cloneNode(true));
239
- }
240
- return mark(fragment);
297
+ return mark(content.cloneNode(true));
241
298
  };
242
299
  return () => {
243
300
  if (!isHydrating()) {
244
301
  return create();
245
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
+ }
246
313
  const fresh = create();
247
314
  if (fresh instanceof DocumentFragment) {
248
315
  return mismatch("многослойный шаблон не поддерживается при гидрации");
@@ -258,101 +325,292 @@ export function template(html) {
258
325
  return node;
259
326
  };
260
327
  }
261
- function collectPartsMany(nodes) {
262
- const holesByIndex = new Map();
263
- const attrsByIndex = new Map();
264
- const scanAttributes = (element) => {
265
- for (const attribute of Array.from(element.attributes)) {
266
- if (attribute.name === "data-silent-text") {
267
- if (!isServerMode()) {
268
- element.removeAttribute(attribute.name);
269
- }
270
- continue;
271
- }
272
- const match = /^data-silent-a-(\d+)$/.exec(attribute.name);
273
- if (match === null) {
274
- continue;
275
- }
276
- const index = Number(match[1]);
277
- if (Number.isInteger(index)) {
278
- attrsByIndex.set(index, element);
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;
279
358
  }
280
- if (!isServerMode()) {
281
- element.removeAttribute(attribute.name);
359
+ else if (data === INSERT_END) {
360
+ depth = depth > 0 ? depth - 1 : 0;
361
+ marker = true;
282
362
  }
283
363
  }
284
- };
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();
285
385
  const visit = (current) => {
286
- if (current.nodeType === Node.COMMENT_NODE) {
287
- const match = /^silent-hole-(\d+)$/.exec(current.data);
288
- if (match !== null) {
289
- const index = Number(match[1]);
290
- if (Number.isInteger(index)) {
291
- holesByIndex.set(index, current);
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);
292
415
  }
293
416
  }
294
417
  }
295
- else if (current.nodeType === Node.ELEMENT_NODE) {
296
- scanAttributes(current);
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
+ }
297
437
  }
298
- let depth = 0;
299
- let child = current.firstChild;
300
- while (child !== null) {
301
- if (child.nodeType === Node.COMMENT_NODE) {
302
- const data = child.data;
303
- if (data === INSERT_START) {
304
- depth += 1;
305
- 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) {
306
445
  continue;
307
446
  }
308
- if (data === INSERT_END) {
309
- depth = Math.max(0, depth - 1);
310
- child = child.nextSibling;
447
+ const name = attribute.name;
448
+ if (name === TEXT_MARKER) {
449
+ names.push(name);
311
450
  continue;
312
451
  }
452
+ const index = markerIndex(name, ATTR_MARKER);
453
+ if (index !== null) {
454
+ attrsByIndex[index] = path.slice();
455
+ names.push(name);
456
+ }
313
457
  }
314
- if (depth === 0) {
315
- visit(child);
458
+ if (names.length > 0) {
459
+ strips.push({ path: path.slice(), names });
316
460
  }
317
- child = child.nextSibling;
318
461
  }
462
+ forEachPartChild(node, visitChild);
319
463
  };
320
- for (const node of nodes) {
321
- visit(node);
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
+ }
474
+ }
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);
322
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;
503
+ }
504
+ counts.push(countSignificantChildren(node));
505
+ const childIndex = path[index];
506
+ node = childIndex === undefined ? null : significantChildren(node, childIndex);
507
+ }
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
+ }
521
+ }
522
+ }
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);
531
+ }
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;
557
+ }
558
+ function applyPartsPlan(root, plan, strip, significant) {
323
559
  const holes = [];
324
- for (let index = 0; index < holesByIndex.size; index += 1) {
325
- const hole = holesByIndex.get(index);
326
- if (hole === undefined) {
327
- break;
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);
575
+ }
328
576
  }
329
- holes.push(hole);
330
577
  }
331
578
  const attrs = [];
332
- for (let index = 0; index < attrsByIndex.size; index += 1) {
333
- const element = attrsByIndex.get(index);
334
- if (element === undefined) {
335
- break;
579
+ for (const entry of plan.attrs) {
580
+ const node = resolvePath(root, entry, significant);
581
+ if (node !== null) {
582
+ attrs.push(node);
336
583
  }
337
- attrs.push(element);
338
584
  }
339
585
  return { holes, attrs };
340
586
  }
341
- function collectParts(node) {
342
- return collectPartsMany([node]);
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
+ };
343
603
  }
344
- export function templateHoles(html) {
345
- let cached = templateCache.get(html);
346
- if (cached === undefined) {
347
- const element = document.createElement("template");
348
- element.innerHTML = html;
349
- templateCache.set(html, element);
350
- cached = element;
351
- }
352
- const template = cached;
353
- const metadata = METADATA_PATTERN.test(html);
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
+ };
354
612
  const mark = (node) => {
355
- if (!metadata) {
613
+ if (!parse().metadata) {
356
614
  if (node instanceof DocumentFragment) {
357
615
  for (const child of node.childNodes) {
358
616
  markMetadataFree(child);
@@ -363,20 +621,107 @@ export function templateHoles(html) {
363
621
  return node;
364
622
  };
365
623
  const cloneNode = () => {
366
- const content = template.content.cloneNode(true);
367
- const rootNodes = Array.from(content.childNodes);
368
- return rootNodes.length === 1 && rootNodes[0] !== undefined
369
- ? rootNodes[0]
370
- : content;
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;
371
633
  };
372
634
  const create = () => {
373
635
  const node = mark(cloneNode());
374
- return { node, ...collectParts(node) };
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;
375
665
  };
376
666
  return () => {
377
667
  if (!isHydrating()) {
378
668
  return create();
379
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
+ }
380
725
  const fresh = cloneNode();
381
726
  const roots = fresh instanceof DocumentFragment ? Array.from(fresh.childNodes) : [fresh];
382
727
  if (roots.length === 1 && isMetadataElement(roots[0])) {
@@ -390,114 +735,119 @@ export function templateHoles(html) {
390
735
  continue;
391
736
  }
392
737
  const node = claimNext();
393
- if (node === null || !shapeMatches(root, node, true)) {
738
+ if (node === null || !shapeMatches(root, node, true, simple)) {
394
739
  return mismatch("шаблон не совпал с серверной разметкой");
395
740
  }
396
741
  mergeTopLevelMetadata(root, node);
397
742
  claimed.push(node);
398
743
  }
399
- const parts = collectPartsMany(claimed);
400
- if (!metadata) {
401
- for (const node of claimed) {
402
- markMetadataFree(node);
403
- }
404
- }
744
+ const collected = collectPartsMany(claimed);
405
745
  if (claimed.length === 1 && !(fresh instanceof DocumentFragment)) {
406
- return { node: claimed[0], ...parts };
746
+ return { node: claimed[0], ...collected };
407
747
  }
408
748
  const container = document.createDocumentFragment();
409
749
  for (const node of claimed) {
410
750
  container.appendChild(node);
411
751
  }
412
- if (!metadata) {
413
- markMetadataFree(container);
414
- }
415
- return { node: container, ...parts };
752
+ return { node: container, ...collected };
416
753
  };
417
754
  }
418
- export function settleNodes(end, value) {
419
- if (end === null || !(value instanceof Node)) {
420
- return;
421
- }
422
- const parent = end.parentNode;
423
- if (parent === null) {
424
- return;
425
- }
426
- for (const node of collectNodes(value)) {
427
- if (node.parentNode !== parent) {
428
- parent.insertBefore(node, end);
429
- }
430
- }
431
- }
432
- export function writeRegion(start, end, value) {
433
- const parent = end.parentNode;
434
- if (parent === null) {
435
- return;
436
- }
437
- const existing = collectBetween(start, end);
438
- if (typeof value === "string" ||
439
- typeof value === "number" ||
440
- typeof value === "bigint") {
441
- const next = stringOf(value);
442
- if (existing.length === 1 && existing[0] instanceof Text) {
443
- const text = existing[0];
444
- if (text.data !== next) {
445
- 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);
446
772
  }
447
773
  return;
448
774
  }
449
- }
450
- for (const node of existing) {
451
- disposeNode(node);
452
- parent.removeChild(node);
453
- }
454
- for (const node of extractMetadata(collectNodes(value))) {
455
- parent.insertBefore(node, end);
456
- }
457
- }
458
- function insertServer(parent, accessor, marker) {
459
- const start = document.createComment(INSERT_START);
460
- const end = document.createComment(INSERT_END);
461
- if (marker === null) {
462
- parent.appendChild(start);
463
- parent.appendChild(end);
464
- }
465
- else {
466
- parent.insertBefore(start, marker);
467
- parent.insertBefore(end, marker);
468
- }
469
- const mount = createMountPoint(parent, end);
470
- createRenderEffect(() => {
471
- mount.render(accessor());
472
- });
473
- }
474
- function insertHydrated(parent, accessor, marker) {
475
- const window = findInsertWindow(marker, parent);
476
- let initialized = false;
477
- 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();
478
795
  if (!initialized) {
479
796
  initialized = true;
480
- const value = withWindow(window, () => accessor());
481
- settleNodes(window.end, value);
482
- const nodes = collectBetween(window.start, window.end);
483
- if ((typeof value === "string" ||
484
- typeof value === "number" ||
485
- typeof value === "bigint") &&
486
- nodes.length === 1 &&
487
- nodes[0] instanceof Text) {
488
- const text = nodes[0];
489
- const next = stringOf(value);
490
- if (text.data !== next) {
491
- text.data = next;
492
- }
797
+ if (isTextValue(value) && !(previous instanceof Text)) {
798
+ writeText(value);
799
+ return;
493
800
  }
801
+ openRegion().render(value);
802
+ return;
803
+ }
804
+ if (mount !== null) {
805
+ mount.render(value);
494
806
  return;
495
807
  }
496
- writeRegion(window.start, window.end, accessor());
808
+ if (isTextValue(value)) {
809
+ writeText(value);
810
+ return;
811
+ }
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);
497
819
  });
498
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
+ }
499
840
  export function insert(parent, accessor, marker) {
500
- if (isServerMode()) {
841
+ if (isServerHole(marker)) {
842
+ if (SERVER) {
843
+ const value = accessor();
844
+ if (!setServerHoleValue(marker, value)) {
845
+ marker.set(`<!--${INSERT_START}-->${serializeServerValue(value)}<!--${INSERT_END}-->`);
846
+ }
847
+ }
848
+ return;
849
+ }
850
+ if (SERVER && isServerMode()) {
501
851
  insertServer(parent, accessor, marker ?? null);
502
852
  return;
503
853
  }
@@ -511,13 +861,18 @@ export function insert(parent, accessor, marker) {
511
861
  });
512
862
  }
513
863
  export function text(node, accessor) {
514
- createRenderEffect(() => {
864
+ const apply = () => {
515
865
  const value = accessor();
516
866
  const next = stringOf(value);
517
867
  if (node.data !== next) {
518
868
  node.data = next;
519
869
  }
520
- });
870
+ };
871
+ if (isHydrating()) {
872
+ hydratedEffect(apply);
873
+ return;
874
+ }
875
+ createRenderEffect(apply);
521
876
  }
522
877
  function isSvgElement(el) {
523
878
  return el instanceof SVGElement;
@@ -529,18 +884,33 @@ function setClass(el, value) {
529
884
  }
530
885
  el.setAttribute("class", stringOf(value));
531
886
  }
887
+ function serverAttribute(el) {
888
+ return el instanceof Element ? null : el;
889
+ }
532
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
+ }
533
898
  const svg = isSvgElement(el);
534
899
  let prev = accessor();
535
900
  applyAttr(el, name, prev, svg);
536
- createRenderEffect(() => {
901
+ const apply = () => {
537
902
  const value = accessor();
538
903
  if (valuesEqual(value, prev)) {
539
904
  return;
540
905
  }
541
906
  prev = value;
542
907
  applyAttr(el, name, value, svg);
543
- });
908
+ };
909
+ if (isHydrating()) {
910
+ hydratedEffect(apply);
911
+ return;
912
+ }
913
+ createRenderEffect(apply);
544
914
  }
545
915
  export function applyAttr(el, name, value, svg) {
546
916
  if (name === "class" || name === "className") {
@@ -592,8 +962,25 @@ function applyProperty(el, name, value) {
592
962
  el.removeAttribute(name);
593
963
  }
594
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
+ }
595
981
  const svg = isSvgElement(el);
596
982
  let prev = null;
983
+ let prevKeys = null;
597
984
  let currentRef = null;
598
985
  const releaseRef = () => {
599
986
  const previous = currentRef;
@@ -608,8 +995,9 @@ export function spread(el, accessor) {
608
995
  return;
609
996
  }
610
997
  const props = value;
611
- if (prev !== null) {
612
- for (const key of Object.keys(prev)) {
998
+ const keys = Object.keys(props);
999
+ if (prev !== null && prevKeys !== null) {
1000
+ for (const key of prevKeys) {
613
1001
  if (key in props) {
614
1002
  continue;
615
1003
  }
@@ -628,7 +1016,8 @@ export function spread(el, accessor) {
628
1016
  applyAttr(el, key, undefined, svg);
629
1017
  }
630
1018
  }
631
- for (const [key, next] of Object.entries(props)) {
1019
+ for (const key of keys) {
1020
+ const next = props[key];
632
1021
  if (prev !== null && valuesEqual(prev[key], next)) {
633
1022
  continue;
634
1023
  }
@@ -661,10 +1050,18 @@ export function spread(el, accessor) {
661
1050
  applyAttr(el, key, next, svg);
662
1051
  }
663
1052
  prev = props;
1053
+ prevKeys = keys;
664
1054
  });
665
1055
  onCleanup(releaseRef);
666
1056
  }
667
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
+ }
668
1065
  let prev;
669
1066
  let initialized = false;
670
1067
  createRenderEffect(() => {
@@ -678,6 +1075,13 @@ export function prop(el, name, accessor) {
678
1075
  });
679
1076
  }
680
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
+ }
681
1085
  const snapshot = ensureSnapshot(el);
682
1086
  createRenderEffect(() => {
683
1087
  applyStyle(el, accessor(), snapshot);
@@ -691,28 +1095,47 @@ function ensureSnapshot(el) {
691
1095
  }
692
1096
  return snapshot;
693
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
+ }
694
1106
  function applyStyle(el, value, snapshot) {
695
1107
  const style = el.style;
1108
+ const applied = ensureAppliedStyle(el);
696
1109
  if (value === null || value === undefined) {
697
1110
  for (const key of snapshot) {
698
1111
  style.removeProperty(key);
699
1112
  }
700
1113
  snapshot.clear();
1114
+ applied.clear();
701
1115
  return;
702
1116
  }
703
1117
  if (typeof value === "string") {
704
1118
  if (style.cssText !== value) {
705
1119
  style.cssText = value;
706
1120
  }
1121
+ applied.clear();
707
1122
  const keep = new Set();
708
- for (const entry of value.split(";")) {
709
- const separator = entry.indexOf(":");
710
- const key = separator === -1 ? entry.trim() : entry.slice(0, separator).trim();
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();
711
1130
  if (key !== "") {
712
1131
  keep.add(key);
713
1132
  }
1133
+ if (limit === -1) {
1134
+ break;
1135
+ }
1136
+ cursor = limit + 1;
714
1137
  }
715
- for (const key of Array.from(snapshot)) {
1138
+ for (const key of snapshot) {
716
1139
  if (!keep.has(key)) {
717
1140
  style.removeProperty(key);
718
1141
  snapshot.delete(key);
@@ -728,108 +1151,208 @@ function applyStyle(el, value, snapshot) {
728
1151
  }
729
1152
  const props = value;
730
1153
  const keys = new Set();
731
- for (const [raw, next] of Object.entries(props)) {
1154
+ for (const raw of Object.keys(props)) {
1155
+ const next = props[raw];
732
1156
  const key = hyphen(raw);
733
1157
  const target = next === null || next === undefined ? "" : String(next);
734
1158
  keys.add(key);
735
1159
  if (target === "") {
736
- if (style.getPropertyValue(key) !== "") {
1160
+ if (applied.get(key) !== "") {
737
1161
  style.removeProperty(key);
1162
+ applied.delete(key);
738
1163
  }
739
1164
  snapshot.delete(key);
740
1165
  continue;
741
1166
  }
742
- if (style.getPropertyValue(key) !== target) {
1167
+ if (applied.get(key) !== target) {
743
1168
  style.setProperty(key, target);
1169
+ applied.set(key, target);
744
1170
  }
745
1171
  snapshot.add(key);
746
1172
  }
747
- for (const key of Array.from(snapshot)) {
1173
+ for (const key of snapshot) {
748
1174
  if (!keys.has(key)) {
749
1175
  style.removeProperty(key);
750
1176
  snapshot.delete(key);
1177
+ applied.delete(key);
751
1178
  }
752
1179
  }
753
1180
  }
754
1181
  export function hyphen(raw) {
1182
+ const cached = hyphenCache.get(raw);
1183
+ if (cached !== undefined) {
1184
+ return cached;
1185
+ }
1186
+ let result;
755
1187
  if (raw.includes("-")) {
756
- return raw.toLowerCase();
1188
+ result = raw.toLowerCase();
757
1189
  }
758
- let result = "";
759
- for (const char of raw) {
760
- if (char >= "A" && char <= "Z") {
761
- result += "-" + char.toLowerCase();
762
- continue;
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;
763
1198
  }
764
- result += char;
1199
+ }
1200
+ if (hyphenCache.size < AUX_CACHE_LIMIT) {
1201
+ hyphenCache.set(raw, result);
765
1202
  }
766
1203
  return result;
767
1204
  }
768
- function getHolders(el) {
769
- let holders = listenerRegistry.get(el);
770
- if (holders === undefined) {
771
- holders = new Map();
772
- listenerRegistry.set(el, holders);
773
- }
774
- return holders;
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;
775
1219
  }
776
1220
  function isEventName(name) {
777
1221
  return name.startsWith("on:") || (name.startsWith("on") && name.length > 2);
778
1222
  }
779
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) {
780
1239
  if (name.startsWith("on:")) {
781
- return { event: name.slice(3), capture: false };
1240
+ const event = name.slice(3);
1241
+ return { event, capture: false, key: "e:" + event };
782
1242
  }
783
1243
  let event = name.slice(2);
784
- if (event.endsWith("Capture")) {
1244
+ const capture = event.endsWith("Capture");
1245
+ if (capture) {
785
1246
  event = event.slice(0, -7);
786
- const first = event.charAt(0);
787
- event = first === "" ? event : first.toLowerCase() + event.slice(1);
788
- return { event, capture: true };
789
1247
  }
790
1248
  const first = event.charAt(0);
791
1249
  event = first === "" ? event : first.toLowerCase() + event.slice(1);
792
- return { event, capture: false };
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
+ }
793
1277
  }
794
1278
  export function addListener(el, name, handler) {
795
- const { event, capture } = parseEventName(name);
796
- const holders = getHolders(el);
797
- const key = (capture ? "c:" : "e:") + event;
798
- const registered = holders.get(key);
799
- if (registered !== undefined) {
800
- registered.current = typeof handler === "function" ? handler : null;
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
+ }
801
1295
  return;
802
1296
  }
803
- const holder = {
804
- current: typeof handler === "function" ? handler : null,
805
- wrapper: () => { },
806
- };
807
- const wrapper = (event) => {
808
- const current = holder.current;
809
- if (typeof current === "function") {
810
- 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;
811
1302
  }
812
- };
813
- holder.wrapper = wrapper;
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);
814
1320
  holders.set(key, holder);
815
- el.addEventListener(event, wrapper, capture);
1321
+ listenerRegistry.set(el, holders);
1322
+ if (delegated) {
1323
+ ensureDelegated(event, key);
1324
+ }
1325
+ else {
1326
+ el.addEventListener(event, holder.wrapper, capture);
1327
+ }
816
1328
  }
817
1329
  export function removeListener(el, name) {
818
- const { event, capture } = parseEventName(name);
1330
+ const { event, capture, key } = resolveEventName(name);
819
1331
  const holders = listenerRegistry.get(el);
820
1332
  if (holders === undefined) {
821
1333
  return;
822
1334
  }
823
- const key = (capture ? "c:" : "e:") + event;
824
- const registered = holders.get(key);
825
- if (registered === undefined) {
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) {
826
1346
  return;
827
1347
  }
828
- holders.delete(key);
829
- registered.current = null;
830
- el.removeEventListener(event, registered.wrapper, capture);
1348
+ listenerRegistry.delete(el);
1349
+ holders.current = null;
1350
+ el.removeEventListener(event, holders.wrapper, capture);
831
1351
  }
832
1352
  export function on(el, name, accessor) {
1353
+ if (!(el instanceof Element)) {
1354
+ return;
1355
+ }
833
1356
  createRenderEffect(() => {
834
1357
  const value = accessor();
835
1358
  addListener(el, name, typeof value === "function" ? value : null);
@@ -880,7 +1403,7 @@ export function fragment(accessor) {
880
1403
  createRenderEffect(() => {
881
1404
  if (!initialized) {
882
1405
  initialized = true;
883
- const value = withWindow(window, () => accessor());
1406
+ const value = withWindow(window, accessor);
884
1407
  settleNodes(anchor, value);
885
1408
  if (window.cursor !== anchor) {
886
1409
  mismatch("содержимое fragment не совпало");
@@ -915,7 +1438,8 @@ export function applyProps(el, props) {
915
1438
  }
916
1439
  const svg = isSvgElement(el);
917
1440
  const children = props["children"];
918
- for (const [key, value] of Object.entries(props)) {
1441
+ for (const key of Object.keys(props)) {
1442
+ const value = props[key];
919
1443
  if (key === "children" || key === "key") {
920
1444
  continue;
921
1445
  }