svelte 5.58.0 → 5.59.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 (100) hide show
  1. package/compiler/index.js +1 -1
  2. package/package.json +19 -1
  3. package/renderer.d.ts +1 -0
  4. package/src/compiler/errors.js +19 -0
  5. package/src/compiler/index.js +37 -2
  6. package/src/compiler/migrate/index.js +2 -1
  7. package/src/compiler/phases/1-parse/read/options.js +20 -0
  8. package/src/compiler/phases/2-analyze/index.js +21 -2
  9. package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
  10. package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
  11. package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
  12. package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
  13. package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
  14. package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
  15. package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
  16. package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
  17. package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
  18. package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
  19. package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
  20. package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
  21. package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
  22. package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
  23. package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
  24. package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
  25. package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
  26. package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
  27. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  28. package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
  29. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
  30. package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
  31. package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
  32. package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
  33. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
  34. package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
  35. package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
  36. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  37. package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
  38. package/src/compiler/print/index.js +45 -57
  39. package/src/compiler/state.js +12 -0
  40. package/src/compiler/utils/builders.js +2 -2
  41. package/src/compiler/validate-options.js +47 -1
  42. package/src/index-client.js +3 -3
  43. package/src/internal/client/constants.js +15 -1
  44. package/src/internal/client/custom-renderer/containers.js +213 -0
  45. package/src/internal/client/custom-renderer/cross.js +145 -0
  46. package/src/internal/client/custom-renderer/index.js +28 -0
  47. package/src/internal/client/custom-renderer/state.js +59 -0
  48. package/src/internal/client/dev/css.js +3 -1
  49. package/src/internal/client/dev/debug.js +6 -6
  50. package/src/internal/client/dev/elements.js +9 -7
  51. package/src/internal/client/dev/tracing.js +67 -11
  52. package/src/internal/client/dev/validation.js +2 -1
  53. package/src/internal/client/dom/blocks/async.js +2 -1
  54. package/src/internal/client/dom/blocks/await.js +110 -19
  55. package/src/internal/client/dom/blocks/boundary.js +142 -88
  56. package/src/internal/client/dom/blocks/branches.js +94 -75
  57. package/src/internal/client/dom/blocks/css-props.js +31 -3
  58. package/src/internal/client/dom/blocks/each.js +166 -45
  59. package/src/internal/client/dom/blocks/html.js +19 -13
  60. package/src/internal/client/dom/blocks/slot.js +3 -3
  61. package/src/internal/client/dom/blocks/snippet.js +52 -3
  62. package/src/internal/client/dom/blocks/svelte-element.js +17 -6
  63. package/src/internal/client/dom/blocks/svelte-head.js +13 -4
  64. package/src/internal/client/dom/css.js +4 -4
  65. package/src/internal/client/dom/elements/attributes.js +91 -51
  66. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  67. package/src/internal/client/dom/elements/bindings/select.js +13 -9
  68. package/src/internal/client/dom/elements/class.js +16 -5
  69. package/src/internal/client/dom/elements/events.js +153 -21
  70. package/src/internal/client/dom/elements/misc.js +3 -2
  71. package/src/internal/client/dom/elements/style.js +17 -6
  72. package/src/internal/client/dom/hydration.js +6 -6
  73. package/src/internal/client/dom/operations.js +435 -14
  74. package/src/internal/client/dom/reconciler.js +2 -2
  75. package/src/internal/client/dom/template.js +94 -66
  76. package/src/internal/client/error-handling.js +4 -12
  77. package/src/internal/client/errors.js +85 -2
  78. package/src/internal/client/index.js +9 -2
  79. package/src/internal/client/reactivity/async.js +257 -122
  80. package/src/internal/client/reactivity/batch.js +1124 -531
  81. package/src/internal/client/reactivity/deriveds.js +281 -177
  82. package/src/internal/client/reactivity/effects.js +87 -54
  83. package/src/internal/client/reactivity/sources.js +30 -124
  84. package/src/internal/client/reactivity/status.js +1 -1
  85. package/src/internal/client/render.js +60 -87
  86. package/src/internal/client/runtime.js +931 -153
  87. package/src/internal/client/validate.js +2 -2
  88. package/src/internal/disclose-version.js +1 -1
  89. package/src/internal/flags/custom-renderer-server.js +10 -0
  90. package/src/internal/flags/custom-renderer.js +3 -0
  91. package/src/internal/flags/index.js +6 -0
  92. package/src/internal/init-operations.js +3 -0
  93. package/src/internal/shared/errors.js +20 -0
  94. package/src/legacy/legacy-client.js +6 -2
  95. package/src/renderer/index-server.js +18 -0
  96. package/src/renderer/index.js +2 -0
  97. package/src/version.js +1 -1
  98. package/types/index.d.ts +245 -25
  99. package/types/index.d.ts.map +10 -1
  100. package/src/internal/client/reactivity/utils.js +0 -18
@@ -23,7 +23,13 @@ import {
23
23
  create_text,
24
24
  get_first_child,
25
25
  get_next_sibling,
26
- should_defer_append
26
+ should_defer_append,
27
+ get_parent_node,
28
+ append_child,
29
+ create_fragment,
30
+ insert_before,
31
+ node_type,
32
+ get_node_value
27
33
  } from '../operations.js';
28
34
  import {
29
35
  block,
@@ -44,6 +50,9 @@ import { current_batch } from '../../reactivity/batch.js';
44
50
  import * as e from '../../errors.js';
45
51
  import { tag } from '../../dev/tracing.js';
46
52
 
53
+ import { push_renderer, current_renderer, pop_renderer } from '../../custom-renderer/state.js';
54
+ import { custom_renderers_flag } from '../../../flags/index.js';
55
+
47
56
  // When making substantive changes to this file, validate them with the each block stress test:
48
57
  // https://svelte.dev/playground/1972b2cf46564476ad8c8c6405b23b7b
49
58
  // This test also exists in this repo, as `packages/svelte/tests/manual/each-stress-test`
@@ -112,10 +121,10 @@ function pause_effects(state, to_destroy, controlled_anchor) {
112
121
 
113
122
  if (fast_path) {
114
123
  var anchor = /** @type {Element} */ (controlled_anchor);
115
- var parent_node = /** @type {Element} */ (anchor.parentNode);
124
+ var parent_node = /** @type {Element} */ (get_parent_node(anchor));
116
125
 
117
126
  clear_text_content(parent_node);
118
- parent_node.append(anchor);
127
+ append_child(parent_node, anchor);
119
128
 
120
129
  state.items.clear();
121
130
  }
@@ -133,23 +142,26 @@ function pause_effects(state, to_destroy, controlled_anchor) {
133
142
 
134
143
  /**
135
144
  * @param {EachState} state
136
- * @param {Effect[]} to_destroy
137
- * @param {boolean} remove_dom
145
+ * @returns {Set<Effect>}
138
146
  */
139
- function destroy_effects(state, to_destroy, remove_dom = true) {
140
- /** @type {Set<Effect> | undefined} */
141
- var preserved_effects;
142
-
147
+ function selected_effects(state) {
148
+ var selected = new Set();
143
149
  // The loop-in-a-loop isn't ideal, but we should only hit this in relatively rare cases
144
- if (state.pending.size > 0) {
145
- preserved_effects = new Set();
146
-
147
- for (const keys of state.pending.values()) {
148
- for (const key of keys) {
149
- preserved_effects.add(/** @type {EachItem} */ (state.items.get(key)).e);
150
- }
150
+ for (const keys of state.pending.values()) {
151
+ for (const key of keys) {
152
+ selected.add(/** @type {EachItem} */ (state.items.get(key)).e);
151
153
  }
152
154
  }
155
+ return selected;
156
+ }
157
+
158
+ /**
159
+ * @param {EachState} state
160
+ * @param {Effect[]} to_destroy
161
+ * @param {boolean} remove_dom
162
+ */
163
+ function destroy_effects(state, to_destroy, remove_dom = true) {
164
+ var preserved_effects = state.pending.size > 0 ? selected_effects(state) : undefined;
153
165
 
154
166
  for (var i = 0; i < to_destroy.length; i++) {
155
167
  var e = to_destroy[i];
@@ -157,16 +169,64 @@ function destroy_effects(state, to_destroy, remove_dom = true) {
157
169
  if (preserved_effects?.has(e)) {
158
170
  e.f |= EFFECT_OFFSCREEN;
159
171
 
160
- const fragment = document.createDocumentFragment();
172
+ const fragment = create_fragment();
161
173
  move_effect(e, fragment);
174
+
175
+ // `reconcile` expects offscreen items to come after all onscreen items
176
+ if (e !== state.effect.last) {
177
+ link(state, e.prev, e.next);
178
+ link(state, state.effect.last, e);
179
+ link(state, e, null);
180
+ }
162
181
  } else {
163
182
  destroy_effect(to_destroy[i], remove_dom);
164
183
  }
165
184
  }
166
185
  }
167
186
 
168
- /** @type {TemplateNode} */
169
- var offscreen_anchor;
187
+ /**
188
+ * Whether the item is outroing, i.e. `pause_effects` destroys it (or keeps it offscreen) once its outro is done
189
+ * @param {EachState} state
190
+ * @param {Effect} effect
191
+ */
192
+ function is_outroing(state, effect) {
193
+ if (state.outrogroups !== null) {
194
+ for (const group of state.outrogroups) {
195
+ if (group.pending.has(effect) || group.done.has(effect)) return true;
196
+ }
197
+ }
198
+
199
+ return false;
200
+ }
201
+
202
+ // we need to handle the case of multiple renderers so we need to use a WeakMap of offscreen anchors rather than a single variable.
203
+ // we also need the dom_weak_key since `null` is not a viable WeakKey
204
+ /** @type {WeakMap<WeakKey, TemplateNode>} */
205
+ var offscreen_anchors = new WeakMap();
206
+ var dom_weak_key = {};
207
+
208
+ /**
209
+ * Returns an anchor node suitable for offscreen rendering.
210
+ * When a custom renderer is active, the anchor must have a parent
211
+ * (so that `getParent()` works), so we place the text node inside
212
+ * a fragment. The result is cached just like the non-renderer path.
213
+ * @returns {TemplateNode}
214
+ */
215
+ function get_offscreen_anchor() {
216
+ // not doing it inline to please typescript
217
+ var cached = offscreen_anchors.get(current_renderer ?? dom_weak_key);
218
+ if (cached) return cached;
219
+
220
+ var offscreen_anchor = create_text();
221
+
222
+ if (current_renderer) {
223
+ var fragment = create_fragment();
224
+ append_child(fragment, offscreen_anchor);
225
+ }
226
+
227
+ offscreen_anchors.set(current_renderer ?? dom_weak_key, offscreen_anchor);
228
+ return offscreen_anchor;
229
+ }
170
230
 
171
231
  /**
172
232
  * @template V
@@ -186,12 +246,17 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
186
246
 
187
247
  var is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
188
248
 
249
+ // Capture the renderer that was active when this each block was created.
250
+ // Needed so that the commit callback can push the correct renderer when doing
251
+ // DOM operations outside of an effect context (e.g. as a batch commit callback).
252
+ var renderer = current_renderer;
253
+
189
254
  if (is_controlled) {
190
255
  var parent_node = /** @type {Element} */ (node);
191
256
 
192
257
  anchor = hydrating
193
258
  ? set_hydrate_node(get_first_child(parent_node))
194
- : parent_node.appendChild(create_text());
259
+ : /** @type {Text} */ (append_child(parent_node, create_text()));
195
260
  }
196
261
 
197
262
  if (hydrating) {
@@ -229,33 +294,41 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
229
294
  return;
230
295
  }
231
296
 
232
- state.pending.delete(batch);
297
+ var previous_renderer = custom_renderers_flag ? push_renderer(renderer) : null;
233
298
 
234
- // The effect doesn't necessarily re-run in a batch right before that batch commits
235
- // (its view of the collection may not have changed), so we read the collection
236
- // as the committing batch sees it rather than using the most recent block run's result
237
- var array = get(each_array);
299
+ try {
300
+ state.pending.delete(batch);
238
301
 
239
- state.fallback = fallback;
240
- reconcile(state, array, anchor, flags, get_key);
302
+ // The effect doesn't necessarily re-run in a batch right before that batch commits
303
+ // (its view of the collection may not have changed), so we read the collection
304
+ // as the committing batch sees it rather than using the most recent block run's result
305
+ var array = get(each_array);
241
306
 
242
- if (fallback !== null) {
243
- if (array.length === 0) {
244
- if ((fallback.f & EFFECT_OFFSCREEN) === 0) {
245
- resume_effect(fallback);
307
+ state.fallback = fallback;
308
+ reconcile(state, array, anchor, flags, get_key);
309
+
310
+ if (fallback !== null) {
311
+ if (array.length === 0) {
312
+ if ((fallback.f & EFFECT_OFFSCREEN) === 0) {
313
+ resume_effect(fallback);
314
+ } else {
315
+ fallback.f ^= EFFECT_OFFSCREEN;
316
+ move(fallback, null, anchor);
317
+ }
246
318
  } else {
247
- fallback.f ^= EFFECT_OFFSCREEN;
248
- move(fallback, null, anchor);
319
+ pause_effect(fallback, () => {
320
+ // TODO only null out if no pending batch needs it,
321
+ // otherwise re-add `fallback.fragment` and move the
322
+ // effect into it
323
+ fallback = null;
324
+ });
249
325
  }
250
- } else {
251
- pause_effect(fallback, () => {
252
- // TODO only null out if no pending batch needs it,
253
- // otherwise re-add `fallback.fragment` and move the
254
- // effect into it
255
- fallback = null;
256
- });
257
326
  }
327
+ } finally {
328
+ if (custom_renderers_flag) pop_renderer(previous_renderer);
258
329
  }
330
+
331
+ destroy_unselected();
259
332
  }
260
333
 
261
334
  /**
@@ -263,6 +336,42 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
263
336
  */
264
337
  function discard(batch) {
265
338
  state.pending.delete(batch);
339
+
340
+ if ((state.effect.f & DESTROYED) === 0) {
341
+ destroy_unselected();
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Destroys the offscreen items that no pending batch selects anymore. Offscreen items only exist
347
+ * for the batches that select them: they were created in their view, or kept for them when a
348
+ * commit removed them (see `destroy_effects`). A batch stops selecting an item when it's discarded,
349
+ * or when its block runs again without it. `reconcile` doesn't destroy such an item if it was kept,
350
+ * as kept items are paused, and it leaves paused items alone (taking them to be outroing)
351
+ */
352
+ function destroy_unselected() {
353
+ /** @type {Set<Effect> | undefined} */
354
+ var selected;
355
+ var effect = state.effect.last;
356
+
357
+ // offscreen items come after all onscreen items (see `destroy_effects`)
358
+ while (effect !== null) {
359
+ var prev = effect.prev;
360
+
361
+ // (an offscreen fallback isn't paused, so the next commit shows it or destroys it, see above)
362
+ if ((effect.f & BRANCH_EFFECT) !== 0 && effect !== fallback) {
363
+ if ((effect.f & EFFECT_OFFSCREEN) === 0) break;
364
+
365
+ // create lazily to save work in the common case
366
+ selected ??= selected_effects(state);
367
+
368
+ if (!selected.has(effect) && !is_outroing(state, effect)) {
369
+ destroy_effect(effect);
370
+ }
371
+ }
372
+
373
+ effect = prev;
374
+ }
266
375
  }
267
376
 
268
377
  var effect = block(() => {
@@ -292,8 +401,8 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
292
401
  for (var index = 0; index < length; index += 1) {
293
402
  if (
294
403
  hydrating &&
295
- hydrate_node.nodeType === COMMENT_NODE &&
296
- /** @type {Comment} */ (hydrate_node).data === HYDRATION_END
404
+ node_type(hydrate_node) === COMMENT_NODE &&
405
+ get_node_value(hydrate_node) === HYDRATION_END
297
406
  ) {
298
407
  // The server rendered fewer items than expected,
299
408
  // so break out and continue appending non-hydrated items
@@ -326,7 +435,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
326
435
  } else {
327
436
  item = create_item(
328
437
  items,
329
- first_run ? anchor : (offscreen_anchor ??= create_text()),
438
+ first_run ? anchor : get_offscreen_anchor(),
330
439
  value,
331
440
  key,
332
441
  index,
@@ -336,7 +445,9 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
336
445
  );
337
446
 
338
447
  if (!first_run) {
448
+ // until it is committed, the item only exists for the batches that select it
339
449
  item.e.f |= EFFECT_OFFSCREEN;
450
+ if (defer) batch.unskip_new_effect(item.e);
340
451
  }
341
452
 
342
453
  items.set(key, item);
@@ -349,8 +460,18 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
349
460
  if (first_run) {
350
461
  fallback = branch(() => fallback_fn(anchor));
351
462
  } else {
352
- fallback = branch(() => fallback_fn((offscreen_anchor ??= create_text())));
463
+ fallback = branch(() => fallback_fn(get_offscreen_anchor()));
464
+ // until it is committed, the fallback only exists for the batches that select it
353
465
  fallback.f |= EFFECT_OFFSCREEN;
466
+ if (defer) batch.unskip_new_effect(fallback);
467
+ }
468
+ }
469
+
470
+ if (defer && fallback !== null) {
471
+ if (length === 0) {
472
+ batch.unskip_effect(fallback);
473
+ } else {
474
+ batch.skip_effect(fallback);
354
475
  }
355
476
  }
356
477
 
@@ -721,7 +842,7 @@ function move(effect, next, anchor) {
721
842
 
722
843
  while (node !== null) {
723
844
  var next_node = /** @type {TemplateNode} */ (get_next_sibling(node));
724
- dest.before(node);
845
+ insert_before(dest, node);
725
846
 
726
847
  if (node === end) {
727
848
  return;
@@ -14,7 +14,16 @@ import * as w from '../../warnings.js';
14
14
  import { hash, sanitize_location } from '../../../../utils.js';
15
15
  import { DEV } from 'esm-env';
16
16
  import { component_context, dev_current_component_function, dev_stack } from '../../context.js';
17
- import { create_element, get_first_child, get_next_sibling } from '../operations.js';
17
+ import {
18
+ create_element,
19
+ get_first_child,
20
+ get_next_sibling,
21
+ get_last_child,
22
+ set_inner_html,
23
+ insert_before,
24
+ node_type,
25
+ get_node_value
26
+ } from '../operations.js';
18
27
  import { active_effect } from '../../runtime.js';
19
28
  import { COMMENT_NODE, ELEMENT_NODE } from '#client/constants';
20
29
 
@@ -51,7 +60,7 @@ function assign_locations(effect, parent, loc) {
51
60
  var node = effect.nodes.start;
52
61
 
53
62
  while (node !== null) {
54
- if (node.nodeType === ELEMENT_NODE) {
63
+ if (node_type(node) === ELEMENT_NODE) {
55
64
  // @ts-expect-error
56
65
  node.__svelte_meta = {
57
66
  parent,
@@ -110,12 +119,12 @@ export function html(
110
119
  // When @html is the only child, use innerHTML directly.
111
120
  // This also handles contenteditable, where the user may delete the anchor comment.
112
121
  effect.nodes = null;
113
- parent_node.innerHTML = /** @type {string} */ (value);
122
+ set_inner_html(parent_node, /** @type {string} */ (value));
114
123
 
115
124
  if (value !== '') {
116
125
  assign_nodes(
117
126
  /** @type {TemplateNode} */ (get_first_child(parent_node)),
118
- /** @type {TemplateNode} */ (parent_node.lastChild)
127
+ /** @type {TemplateNode} */ (get_last_child(parent_node))
119
128
  );
120
129
 
121
130
  if (DEV && location) assign_locations(effect, parent, location);
@@ -134,16 +143,13 @@ export function html(
134
143
  if (hydrating) {
135
144
  // We're deliberately not trying to repair mismatches between server and client,
136
145
  // as it's costly and error-prone (and it's an edge case to have a mismatch anyway)
137
- var hash = /** @type {Comment} */ (hydrate_node).data;
146
+ var hash = get_node_value(hydrate_node);
138
147
 
139
148
  /** @type {TemplateNode | null} */
140
149
  var next = hydrate_next();
141
150
  var last = next;
142
151
 
143
- while (
144
- next !== null &&
145
- (next.nodeType !== COMMENT_NODE || /** @type {Comment} */ (next).data !== '')
146
- ) {
152
+ while (next !== null && (node_type(next) !== COMMENT_NODE || get_node_value(next) !== '')) {
147
153
  last = next;
148
154
  next = get_next_sibling(next);
149
155
  }
@@ -171,24 +177,24 @@ export function html(
171
177
  var wrapper = /** @type {HTMLTemplateElement | SVGElement | MathMLElement} */ (
172
178
  create_element(svg ? 'svg' : mathml ? 'math' : 'template', ns)
173
179
  );
174
- wrapper.innerHTML = /** @type {any} */ (value);
180
+ set_inner_html(wrapper, /** @type {any} */ (value));
175
181
 
176
182
  /** @type {DocumentFragment | Element} */
177
183
  var node = svg || mathml ? wrapper : /** @type {HTMLTemplateElement} */ (wrapper).content;
178
184
 
179
185
  assign_nodes(
180
186
  /** @type {TemplateNode} */ (get_first_child(node)),
181
- /** @type {TemplateNode} */ (node.lastChild)
187
+ /** @type {TemplateNode} */ (get_last_child(node))
182
188
  );
183
189
 
184
190
  if (DEV && location) assign_locations(effect, parent, location);
185
191
 
186
192
  if (svg || mathml) {
187
193
  while (get_first_child(node)) {
188
- anchor.before(/** @type {TemplateNode} */ (get_first_child(node)));
194
+ insert_before(anchor, /** @type {TemplateNode} */ (get_first_child(node)));
189
195
  }
190
196
  } else {
191
- anchor.before(node);
197
+ insert_before(anchor, node);
192
198
  }
193
199
  });
194
200
  }
@@ -1,7 +1,7 @@
1
1
  import { DEV } from 'esm-env';
2
2
  import { component_context, dev_stack } from '../../context.js';
3
3
  import { hydrate_next, hydrating } from '../hydration.js';
4
- import { create_element, create_text } from '../operations.js';
4
+ import { append_child, create_element, create_text, set_attribute } from '../operations.js';
5
5
  import { append } from '../template.js';
6
6
 
7
7
  /**
@@ -21,7 +21,7 @@ export function slot(anchor, $$props, name, slot_props, fallback_fn, location) {
21
21
  // Use the stored reference because the shadow root may be closed.
22
22
  if ($$props.$$host?.$$shadowRoot) {
23
23
  const element = create_element('slot');
24
- if (name !== 'default') element.name = name;
24
+ if (name !== 'default') set_attribute(element, 'name', name);
25
25
 
26
26
  if (DEV && location) {
27
27
  // Native slots are created outside the template, so mark their ownership
@@ -42,7 +42,7 @@ export function slot(anchor, $$props, name, slot_props, fallback_fn, location) {
42
42
 
43
43
  if (fallback_fn !== null) {
44
44
  const fallback_anchor = create_text();
45
- element.append(fallback_anchor);
45
+ append_child(element, fallback_anchor);
46
46
  fallback_fn(fallback_anchor);
47
47
  }
48
48
 
@@ -1,3 +1,4 @@
1
+ /** @import { Renderer } from '../../custom-renderer/types.js' */
1
2
  /** @import { Snippet } from 'svelte' */
2
3
  /** @import { TemplateNode } from '#client' */
3
4
  /** @import { Getters } from '#shared' */
@@ -15,9 +16,12 @@ import { assign_nodes } from '../template.js';
15
16
  import * as w from '../../warnings.js';
16
17
  import * as e from '../../errors.js';
17
18
  import { DEV } from 'esm-env';
18
- import { get_first_child, get_next_sibling } from '../operations.js';
19
+ import { get_first_child, get_next_sibling, insert_before, node_type } from '../operations.js';
19
20
  import { prevent_snippet_stringification } from '../../../shared/validate.js';
20
21
  import { BranchManager } from './branches.js';
22
+ import { current_renderer } from '../../custom-renderer/state.js';
23
+ import { cross, is_crossing } from '../../custom-renderer/cross.js';
24
+ import { has_own_property } from '../../../shared/utils.js';
21
25
 
22
26
  /**
23
27
  * @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
@@ -63,6 +67,48 @@ export function wrap_snippet(component, fn) {
63
67
  return snippet;
64
68
  }
65
69
 
70
+ /**
71
+ * Wraps a snippet function so that it remembers which renderer it was compiled for
72
+ * (`null` for DOM). If it is rendered by a different renderer, the snippet content
73
+ * is rendered across a renderer boundary (see `cross`).
74
+ * @template {(...args: any[]) => any} T
75
+ * @param {Renderer | null} renderer
76
+ * @param {T} fn
77
+ * @returns {T}
78
+ */
79
+ export function renderer_snippet(renderer, fn) {
80
+ var wrapped = /** @type {T} */ (
81
+ (/** @type {TemplateNode} */ anchor, /** @type {any[]} */ ...args) => {
82
+ if (is_crossing(renderer)) {
83
+ return cross(renderer, anchor, (anchor) => fn(anchor, ...args));
84
+ }
85
+
86
+ return fn(anchor, ...args);
87
+ }
88
+ );
89
+
90
+ /** @type {any} */ (wrapped).__renderer = renderer;
91
+
92
+ return wrapped;
93
+ }
94
+
95
+ /**
96
+ * Validates that a snippet can be rendered by a custom renderer. Snippets compiled with
97
+ * custom renderer support know which renderer they belong to and can cross renderer
98
+ * boundaries; anything else (e.g. snippets from components compiled without the
99
+ * `experimental.customRenderer` option) can only be rendered by the DOM.
100
+ * @template {((...args: any[]) => void) | null | undefined} T
101
+ * @param {T} fn
102
+ * @returns {T}
103
+ */
104
+ export function validate_snippet_renderer(fn) {
105
+ if (fn != null && !has_own_property.call(fn, '__renderer')) {
106
+ e.snippet_renderer_mismatch();
107
+ }
108
+
109
+ return fn;
110
+ }
111
+
66
112
  /**
67
113
  * Create a snippet programmatically
68
114
  * @template {unknown[]} Params
@@ -75,6 +121,9 @@ export function wrap_snippet(component, fn) {
75
121
  export function createRawSnippet(fn) {
76
122
  // @ts-expect-error the types are a lie
77
123
  return (/** @type {TemplateNode} */ anchor, /** @type {Getters<Params>} */ ...params) => {
124
+ if (current_renderer != null) {
125
+ e.invalid_snippet_in_custom_renderer();
126
+ }
78
127
  var snippet = fn(...params);
79
128
 
80
129
  /** @type {Element} */
@@ -88,11 +137,11 @@ export function createRawSnippet(fn) {
88
137
  var fragment = create_fragment_from_html(html);
89
138
  element = /** @type {Element} */ (get_first_child(fragment));
90
139
 
91
- if (DEV && (get_next_sibling(element) !== null || element.nodeType !== ELEMENT_NODE)) {
140
+ if (DEV && (get_next_sibling(element) !== null || node_type(element) !== ELEMENT_NODE)) {
92
141
  w.invalid_raw_snippet_render();
93
142
  }
94
143
 
95
- anchor.before(element);
144
+ insert_before(anchor, element);
96
145
  }
97
146
 
98
147
  if (DEV && dev_stack !== null) {
@@ -7,7 +7,16 @@ import {
7
7
  set_hydrate_node,
8
8
  set_hydrating
9
9
  } from '../hydration.js';
10
- import { create_element, create_text, get_first_child } from '../operations.js';
10
+ import {
11
+ create_element,
12
+ create_text,
13
+ get_first_child,
14
+ append_child,
15
+ create_comment,
16
+ insert_before,
17
+ node_type,
18
+ remove_child
19
+ } from '../operations.js';
11
20
  import { block, teardown } from '../../reactivity/effects.js';
12
21
  import { set_should_intro } from '../../render.js';
13
22
  import { active_effect } from '../../runtime.js';
@@ -40,7 +49,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
40
49
  /** @type {null | Element} */
41
50
  var element = null;
42
51
 
43
- if (hydrating && hydrate_node.nodeType === ELEMENT_NODE) {
52
+ if (hydrating && node_type(hydrate_node) === ELEMENT_NODE) {
44
53
  element = /** @type {Element} */ (hydrate_node);
45
54
  hydrate_next();
46
55
  }
@@ -94,14 +103,14 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
94
103
 
95
104
  if (hydrating && is_raw_text_element(next_tag)) {
96
105
  // prevent hydration glitches (code just below expects an anchor)
97
- element.append((tmp_comment = document.createComment('')));
106
+ append_child(element, (tmp_comment = create_comment('')));
98
107
  }
99
108
 
100
109
  // If hydrating, use the existing ssr comment as the anchor so that the
101
110
  // inner open and close methods can pick up the existing nodes correctly
102
111
  var child_anchor = hydrating
103
112
  ? get_first_child(element)
104
- : element.appendChild(create_text());
113
+ : /** @type {Text} */ (append_child(element, create_text()));
105
114
 
106
115
  if (hydrating) {
107
116
  if (child_anchor === null) {
@@ -118,7 +127,9 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
118
127
  // contains children, it's a user error (which is warned on elsewhere)
119
128
  // and the DOM will be silently discarded
120
129
  render_fn(element, child_anchor);
121
- tmp_comment?.remove();
130
+ if (tmp_comment) {
131
+ remove_child(element, tmp_comment);
132
+ }
122
133
  set_animation_effect_override(null);
123
134
  }
124
135
 
@@ -126,7 +137,7 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
126
137
  /** @type {Effect & { nodes: EffectNodes }} */ (active_effect).nodes.end = element;
127
138
 
128
139
  // we only move the node if we are not hydrating since a claimed element is already in place
129
- if (!is_hydrating) anchor.before(element);
140
+ if (!is_hydrating) insert_before(anchor, element);
130
141
  }
131
142
 
132
143
  if (hydrating) {
@@ -1,6 +1,15 @@
1
1
  /** @import { TemplateNode } from '#client' */
2
2
  import { hydrate_node, hydrating, set_hydrate_node, set_hydrating } from '../hydration.js';
3
- import { create_text, get_first_child, get_next_sibling } from '../operations.js';
3
+
4
+ import {
5
+ create_text,
6
+ get_first_child,
7
+ get_next_sibling,
8
+ remove_node,
9
+ append_child,
10
+ node_type,
11
+ get_node_value
12
+ } from '../operations.js';
4
13
  import { block, branch } from '../../reactivity/effects.js';
5
14
  import { COMMENT_NODE, HEAD_EFFECT } from '#client/constants';
6
15
 
@@ -27,7 +36,7 @@ export function head(hash, render_fn) {
27
36
  // rendered in an arbitrary order — find one corresponding to this component
28
37
  while (
29
38
  head_anchor !== null &&
30
- (head_anchor.nodeType !== COMMENT_NODE || /** @type {Comment} */ (head_anchor).data !== hash)
39
+ (node_type(head_anchor) !== COMMENT_NODE || get_node_value(head_anchor) !== hash)
31
40
  ) {
32
41
  head_anchor = get_next_sibling(head_anchor);
33
42
  }
@@ -38,14 +47,14 @@ export function head(hash, render_fn) {
38
47
  set_hydrating(false);
39
48
  } else {
40
49
  var start = /** @type {TemplateNode} */ (get_next_sibling(head_anchor));
41
- head_anchor.remove(); // in case this component is repeated
50
+ remove_node(/** @type {ChildNode} */ (head_anchor)); // in case this component is repeated
42
51
 
43
52
  set_hydrate_node(start);
44
53
  }
45
54
  }
46
55
 
47
56
  if (!hydrating) {
48
- anchor = document.head.appendChild(create_text());
57
+ anchor = /** @type {Comment | Text} */ (append_child(document.head, create_text()));
49
58
  }
50
59
 
51
60
  try {
@@ -1,7 +1,7 @@
1
1
  import { DEV } from 'esm-env';
2
2
  import { register_style } from '../dev/css.js';
3
3
  import { effect } from '../reactivity/effects.js';
4
- import { create_element } from './operations.js';
4
+ import { create_element, append_child, set_text_content } from './operations.js';
5
5
  import { active_effect } from '../runtime.js';
6
6
 
7
7
  /**
@@ -23,12 +23,12 @@ export function append_styles(anchor, css) {
23
23
 
24
24
  // Always querying the DOM is roughly the same perf as additionally checking for presence in a map first assuming
25
25
  // that you'll get cache hits half of the time, so we just always query the dom for simplicity and code savings.
26
- if (!target.querySelector('#' + css.hash)) {
26
+ if (!(/** @type {Element} */ (target).querySelector('#' + css.hash))) {
27
27
  const style = create_element('style');
28
28
  style.id = css.hash;
29
- style.textContent = css.code;
29
+ set_text_content(style, css.code);
30
30
 
31
- target.appendChild(style);
31
+ append_child(target, style);
32
32
 
33
33
  if (DEV) {
34
34
  register_style(css.hash, style);