svelte 5.57.1 → 5.57.3

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 (35) hide show
  1. package/compiler/index.js +1 -1
  2. package/package.json +1 -1
  3. package/src/compiler/errors.js +10 -0
  4. package/src/compiler/migrate/index.js +15 -4
  5. package/src/compiler/phases/1-parse/state/tag.js +2 -0
  6. package/src/compiler/phases/2-analyze/visitors/LetDirective.js +22 -0
  7. package/src/compiler/phases/2-analyze/visitors/shared/a11y/index.js +32 -7
  8. package/src/compiler/phases/3-transform/client/visitors/AwaitExpression.js +2 -1
  9. package/src/compiler/phases/3-transform/client/visitors/shared/events.js +3 -2
  10. package/src/compiler/phases/scope.js +8 -0
  11. package/src/compiler/print/index.js +3 -1
  12. package/src/index-client.js +1 -1
  13. package/src/internal/client/dev/debug.js +14 -0
  14. package/src/internal/client/dev/tracing.js +3 -1
  15. package/src/internal/client/dom/blocks/boundary.js +43 -15
  16. package/src/internal/client/dom/blocks/each.js +8 -6
  17. package/src/internal/client/dom/blocks/svelte-element.js +3 -1
  18. package/src/internal/client/dom/css.js +1 -1
  19. package/src/internal/client/dom/elements/attributes.js +178 -150
  20. package/src/internal/client/dom/elements/bindings/document.js +3 -2
  21. package/src/internal/client/dom/elements/transitions.js +3 -3
  22. package/src/internal/client/error-handling.js +5 -1
  23. package/src/internal/client/proxy.js +2 -1
  24. package/src/internal/client/reactivity/async.js +5 -0
  25. package/src/internal/client/reactivity/batch.js +82 -116
  26. package/src/internal/client/reactivity/deriveds.js +2 -1
  27. package/src/internal/client/reactivity/sources.js +5 -5
  28. package/src/internal/client/reactivity/utils.js +5 -6
  29. package/src/internal/client/runtime.js +13 -16
  30. package/src/internal/server/errors.js +2 -2
  31. package/src/internal/server/index.js +1 -1
  32. package/src/reactivity/date.js +2 -2
  33. package/src/reactivity/url-search-params.js +1 -1
  34. package/src/version.js +1 -1
  35. package/types/index.d.ts.map +1 -1
@@ -178,7 +178,8 @@ export function set_attribute(element, attribute, value, skip_warning) {
178
178
  }
179
179
  }
180
180
 
181
- if (attributes[attribute] === (attributes[attribute] = value)) return;
181
+ var previous = attributes[attribute];
182
+ if (previous === (attributes[attribute] = value)) return;
182
183
 
183
184
  if (attribute === 'loading') {
184
185
  // @ts-expect-error
@@ -190,7 +191,14 @@ export function set_attribute(element, attribute, value, skip_warning) {
190
191
  } else if (typeof value !== 'string' && get_setters(element).has(attribute)) {
191
192
  // @ts-ignore
192
193
  element[attribute] = value;
193
- } else {
194
+ } else if (
195
+ // while hydrating, skip a number or boolean whose string the server already rendered
196
+ !(
197
+ hydrating &&
198
+ (typeof value === 'number' || typeof value === 'boolean') &&
199
+ previous === String(value)
200
+ )
201
+ ) {
194
202
  element.setAttribute(attribute, value);
195
203
  }
196
204
  }
@@ -298,182 +306,202 @@ function set_attributes(
298
306
  var is_option_element = element.nodeName === OPTION_TAG;
299
307
  var is_select_element = element.nodeName === SELECT_TAG;
300
308
 
301
- for (var key in prev) {
302
- // don't null our internal $$onX listeners
303
- if (!(key in next) && key[0] + key[1] !== '$$') {
304
- next[key] = null;
305
- }
306
- }
307
-
308
- if (next.class) {
309
- next.class = clsx(next.class);
310
- } else if (css_hash || next[CLASS]) {
311
- next.class = null; /* force call to set_class() */
312
- }
313
-
314
- if (next[STYLE]) {
315
- next.style ??= null; /* force call to set_style() */
316
- }
317
-
318
- var setters = get_setters(element);
319
-
320
- if (element.nodeName === INPUT_TAG && 'type' in next && ('value' in next || '__value' in next)) {
321
- var type = next.type;
322
-
323
- if (type !== current.type || (type === undefined && element.hasAttribute('type'))) {
324
- current.type = type;
325
- set_attribute(element, 'type', type, skip_warning);
309
+ try {
310
+ for (var key in prev) {
311
+ // don't null our internal $$onX listeners
312
+ if (!(key in next) && key[0] + key[1] !== '$$') {
313
+ next[key] = null;
314
+ }
326
315
  }
327
- }
328
316
 
329
- // since key is captured we use const
330
- for (const key in next) {
331
- // let instead of var because referenced in a closure
332
- let value = next[key];
333
-
334
- // Up here because we want to do this for the initial value, too, even if it's undefined,
335
- // and this wouldn't be reached in case of undefined because of the equality check below
336
- if (is_option_element && key === 'value' && value == null) {
337
- // The <option> element is a special case because removing the value attribute means
338
- // the value is set to the text content of the option element, and setting the value
339
- // to null or undefined means the value is set to the string "null" or "undefined".
340
- // To align with how we handle this case in non-spread-scenarios, this logic is needed.
341
- // There's a super-edge-case bug here that is left in in favor of smaller code size:
342
- // Because of the "set missing props to null" logic above, we can't differentiate
343
- // between a missing value and an explicitly set value of null or undefined. That means
344
- // that once set, the value attribute of an <option> element can't be removed. This is
345
- // a very rare edge case, and removing the attribute altogether isn't possible either
346
- // for the <option value={undefined}> case, so we're not losing any functionality here.
347
- // @ts-ignore
348
- element.value = element.__value = '';
349
- current[key] = value;
350
- continue;
317
+ if (next.class) {
318
+ next.class = clsx(next.class);
319
+ } else if (css_hash || next[CLASS]) {
320
+ next.class = null; /* force call to set_class() */
351
321
  }
352
322
 
353
- if (key === 'class') {
354
- var is_html = element.namespaceURI === 'http://www.w3.org/1999/xhtml';
355
- set_class(element, is_html, value, css_hash, prev?.[CLASS], next[CLASS]);
356
- current[key] = value;
357
- current[CLASS] = next[CLASS];
358
- continue;
323
+ if (next[STYLE]) {
324
+ next.style ??= null; /* force call to set_style() */
359
325
  }
360
326
 
361
- if (key === 'style') {
362
- set_style(element, value, prev?.[STYLE], next[STYLE]);
363
- current[key] = value;
364
- current[STYLE] = next[STYLE];
365
- continue;
366
- }
327
+ var setters = get_setters(element);
367
328
 
368
- var prev_value = current[key];
329
+ if (
330
+ element.nodeName === INPUT_TAG &&
331
+ 'type' in next &&
332
+ ('value' in next || '__value' in next)
333
+ ) {
334
+ var type = next.type;
369
335
 
370
- // Skip if value is unchanged, unless it's `undefined` and the element still has the attribute
371
- if (value === prev_value && !(value === undefined && element.hasAttribute(key))) {
372
- continue;
336
+ if (type !== current.type || (type === undefined && element.hasAttribute('type'))) {
337
+ current.type = type;
338
+ set_attribute(element, 'type', type, skip_warning);
339
+ }
373
340
  }
374
341
 
375
- current[key] = value;
376
-
377
- var prefix = key[0] + key[1]; // this is faster than key.slice(0, 2)
378
- if (prefix === '$$') continue;
342
+ // since key is captured we use const
343
+ for (const key in next) {
344
+ // let instead of var because referenced in a closure
345
+ let value = next[key];
346
+
347
+ // Up here because we want to do this for the initial value, too, even if it's undefined,
348
+ // and this wouldn't be reached in case of undefined because of the equality check below
349
+ if (is_option_element && key === 'value' && value == null) {
350
+ // The <option> element is a special case because removing the value attribute means
351
+ // the value is set to the text content of the option element, and setting the value
352
+ // to null or undefined means the value is set to the string "null" or "undefined".
353
+ // To align with how we handle this case in non-spread-scenarios, this logic is needed.
354
+ // There's a super-edge-case bug here that is left in in favor of smaller code size:
355
+ // Because of the "set missing props to null" logic above, we can't differentiate
356
+ // between a missing value and an explicitly set value of null or undefined. That means
357
+ // that once set, the value attribute of an <option> element can't be removed. This is
358
+ // a very rare edge case, and removing the attribute altogether isn't possible either
359
+ // for the <option value={undefined}> case, so we're not losing any functionality here.
360
+ // @ts-ignore
361
+ element.value = element.__value = '';
362
+ current[key] = value;
363
+ continue;
364
+ }
379
365
 
380
- if (prefix === 'on') {
381
- /** @type {{ capture?: true }} */
382
- const opts = {};
383
- const event_handle_key = '$$' + key;
384
- let event_name = key.slice(2);
385
- var is_delegated = can_delegate_event(event_name);
366
+ if (key === 'class') {
367
+ var is_html = element.namespaceURI === 'http://www.w3.org/1999/xhtml';
368
+ set_class(element, is_html, value, css_hash, prev?.[CLASS], next[CLASS]);
369
+ current[key] = value;
370
+ current[CLASS] = next[CLASS];
371
+ continue;
372
+ }
386
373
 
387
- if (is_capture_event(event_name)) {
388
- event_name = event_name.slice(0, -7);
389
- opts.capture = true;
374
+ if (key === 'style') {
375
+ set_style(element, value, prev?.[STYLE], next[STYLE]);
376
+ current[key] = value;
377
+ current[STYLE] = next[STYLE];
378
+ continue;
390
379
  }
391
380
 
392
- if (!is_delegated && prev_value) {
393
- // Listening to same event but different handler -> our handle function below takes care of this
394
- // If we were to remove and add listeners in this case, it could happen that the event is "swallowed"
395
- // (the browser seems to not know yet that a new one exists now) and doesn't reach the handler
396
- // https://github.com/sveltejs/svelte/issues/11903
397
- if (value != null) continue;
381
+ var prev_value = current[key];
398
382
 
399
- element.removeEventListener(event_name, current[event_handle_key], opts);
400
- current[event_handle_key] = null;
383
+ // Skip if value is unchanged, unless it's `undefined` and the element still has the attribute
384
+ if (value === prev_value && !(value === undefined && element.hasAttribute(key))) {
385
+ continue;
401
386
  }
402
387
 
403
- if (is_delegated) {
404
- delegated(event_name, element, value);
405
- delegate([event_name]);
406
- } else if (value != null) {
407
- /**
408
- * @this {any}
409
- * @param {Event} evt
410
- */
411
- function handle(evt) {
412
- current[key].call(this, evt);
388
+ current[key] = value;
389
+
390
+ var prefix = key[0] + key[1]; // this is faster than key.slice(0, 2)
391
+ if (prefix === '$$') continue;
392
+
393
+ if (prefix === 'on') {
394
+ /** @type {{ capture?: true }} */
395
+ const opts = {};
396
+ const event_handle_key = '$$' + key;
397
+ let event_name = key.slice(2);
398
+ var is_delegated = can_delegate_event(event_name);
399
+
400
+ if (is_capture_event(event_name)) {
401
+ event_name = event_name.slice(0, -7);
402
+ opts.capture = true;
413
403
  }
414
404
 
415
- current[event_handle_key] = create_event(event_name, element, handle, opts);
416
- }
417
- } else if (key === 'style') {
418
- // avoid using the setter
419
- set_attribute(element, key, value);
420
- } else if (key === 'autofocus') {
421
- autofocus(/** @type {HTMLElement} */ (element), Boolean(value));
422
- } else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) {
423
- // @ts-ignore We're not running this for custom elements because __value is actually
424
- // how Lit stores the current value on the element, and messing with that would break things.
425
- element.value = element.__value = value;
426
- } else if (key === 'selected' && is_option_element) {
427
- set_selected(/** @type {HTMLOptionElement} */ (element), value);
428
- } else {
429
- var name = key;
430
- if (!preserve_attribute_case) {
431
- name = normalize_attribute(name);
432
- }
405
+ if (!is_delegated && prev_value) {
406
+ // Listening to same event but different handler -> our handle function below takes care of this
407
+ // If we were to remove and add listeners in this case, it could happen that the event is "swallowed"
408
+ // (the browser seems to not know yet that a new one exists now) and doesn't reach the handler
409
+ // https://github.com/sveltejs/svelte/issues/11903
410
+ if (value != null) continue;
433
411
 
434
- var is_default = name === 'defaultValue' || name === 'defaultChecked';
412
+ element.removeEventListener(event_name, current[event_handle_key], opts);
413
+ current[event_handle_key] = null;
414
+ }
435
415
 
436
- // A select's default value is represented by selected options, not a property.
437
- if (is_select_element && name === 'defaultValue') continue;
416
+ if (is_delegated) {
417
+ delegated(event_name, element, value);
418
+ delegate([event_name]);
419
+ } else if (value != null) {
420
+ /**
421
+ * @this {any}
422
+ * @param {Event} evt
423
+ */
424
+ function handle(evt) {
425
+ current[key].call(this, evt);
426
+ }
438
427
 
439
- if (value == null && !is_custom_element && !is_default) {
440
- attributes[key] = null;
428
+ current[event_handle_key] = create_event(event_name, element, handle, opts);
429
+ }
430
+ } else if (key === 'style') {
431
+ // avoid using the setter
432
+ set_attribute(element, key, value);
433
+ } else if (key === 'autofocus') {
434
+ autofocus(/** @type {HTMLElement} */ (element), Boolean(value));
435
+ } else if (!is_custom_element && (key === '__value' || (key === 'value' && value != null))) {
436
+ // @ts-ignore We're not running this for custom elements because __value is actually
437
+ // how Lit stores the current value on the element, and messing with that would break things.
438
+ element.__value = value;
439
+ // we don't set the value if it hasn't changed. This supports invalid number inputs like `1e` because
440
+ // 1. user types 1e
441
+ // 2. the state is updated reading e.target.value which is ''
442
+ // 3. the spreaded value is ''
443
+ // 4. updating input.value would thus, clear the user value
444
+ if (
445
+ prev_value == null ||
446
+ // @ts-ignore
447
+ element.value !== value ||
448
+ (value === 0 && element.nodeName === PROGRESS_TAG)
449
+ ) {
450
+ // @ts-ignore
451
+ element.value = value;
452
+ }
453
+ } else if (key === 'selected' && is_option_element) {
454
+ set_selected(/** @type {HTMLOptionElement} */ (element), value);
455
+ } else {
456
+ var name = key;
457
+ if (!preserve_attribute_case) {
458
+ name = normalize_attribute(name);
459
+ }
441
460
 
442
- if (name === 'value' || name === 'checked') {
443
- // removing value/checked also removes defaultValue/defaultChecked — preserve
444
- let input = /** @type {HTMLInputElement} */ (element);
445
- const use_default = prev === undefined;
446
- if (name === 'value') {
447
- let previous = input.defaultValue;
448
- input.removeAttribute(name);
449
- input.defaultValue = previous;
450
- // @ts-ignore
451
- input.value = input.__value = use_default ? previous : null;
461
+ var is_default = name === 'defaultValue' || name === 'defaultChecked';
462
+
463
+ // A select's default value is represented by selected options, not a property.
464
+ if (is_select_element && name === 'defaultValue') continue;
465
+
466
+ if (value == null && !is_custom_element && !is_default) {
467
+ attributes[key] = null;
468
+
469
+ if (name === 'value' || name === 'checked') {
470
+ // removing value/checked also removes defaultValue/defaultChecked — preserve
471
+ let input = /** @type {HTMLInputElement} */ (element);
472
+ const use_default = prev === undefined;
473
+ if (name === 'value') {
474
+ let previous = input.defaultValue;
475
+ input.removeAttribute(name);
476
+ input.defaultValue = previous;
477
+ // @ts-ignore
478
+ input.value = input.__value = use_default ? previous : null;
479
+ } else {
480
+ let previous = input.defaultChecked;
481
+ input.removeAttribute(name);
482
+ input.defaultChecked = previous;
483
+ input.checked = use_default ? previous : false;
484
+ }
452
485
  } else {
453
- let previous = input.defaultChecked;
454
- input.removeAttribute(name);
455
- input.defaultChecked = previous;
456
- input.checked = use_default ? previous : false;
486
+ element.removeAttribute(key);
457
487
  }
458
- } else {
459
- element.removeAttribute(key);
488
+ } else if (
489
+ is_default ||
490
+ ((is_custom_element || typeof value !== 'string') && setters.has(name))
491
+ ) {
492
+ // @ts-ignore
493
+ element[name] = value;
494
+ // remove it from attributes's cache
495
+ if (name in attributes) attributes[name] = UNINITIALIZED;
496
+ } else if (typeof value !== 'function') {
497
+ set_attribute(element, name, value, skip_warning);
460
498
  }
461
- } else if (
462
- is_default ||
463
- ((is_custom_element || typeof value !== 'string') && setters.has(name))
464
- ) {
465
- // @ts-ignore
466
- element[name] = value;
467
- // remove it from attributes's cache
468
- if (name in attributes) attributes[name] = UNINITIALIZED;
469
- } else if (typeof value !== 'function') {
470
- set_attribute(element, name, value, skip_warning);
471
499
  }
472
500
  }
473
- }
474
-
475
- if (is_hydrating_custom_element) {
476
- set_hydrating(true);
501
+ } finally {
502
+ if (is_hydrating_custom_element) {
503
+ set_hydrating(true);
504
+ }
477
505
  }
478
506
 
479
507
  return current;
@@ -1,4 +1,4 @@
1
- import { listen } from './shared.js';
1
+ import { listen, without_reactive_context } from './shared.js';
2
2
 
3
3
  /**
4
4
  * @param {(activeElement: Element | null) => void} update
@@ -12,6 +12,7 @@ export function bind_active_element(update) {
12
12
  return;
13
13
  }
14
14
 
15
- update(document.activeElement);
15
+ // Removing the focused element fires `focusout` while an effect is running
16
+ without_reactive_context(() => update(document.activeElement));
16
17
  });
17
18
  }
@@ -359,9 +359,9 @@ function animate(element, options, counterpart, t2, on_begin, on_finish) {
359
359
  aborted = true;
360
360
  a?.abort();
361
361
  },
362
- deactivate: () => a.deactivate(),
363
- reset: () => a.reset(),
364
- t: () => a.t()
362
+ deactivate: () => a?.deactivate(),
363
+ reset: () => a?.reset(),
364
+ t: () => a?.t() ?? 1 - t2
365
365
  };
366
366
  }
367
367
 
@@ -1,7 +1,7 @@
1
1
  /** @import { Derived, Effect } from '#client' */
2
2
  /** @import { Boundary } from './dom/blocks/boundary.js' */
3
3
  import { DEV } from 'esm-env';
4
- import { FILENAME } from '../../constants.js';
4
+ import { FILENAME, HYDRATION_ERROR } from '../../constants.js';
5
5
  import { is_firefox } from './dom/operations.js';
6
6
  import {
7
7
  ERROR_VALUE,
@@ -52,6 +52,10 @@ export function handle_error(error) {
52
52
  * @param {Effect | null} effect
53
53
  */
54
54
  export function invoke_error_boundary(error, effect) {
55
+ if (error === HYDRATION_ERROR) {
56
+ throw error;
57
+ }
58
+
55
59
  if (effect !== null && (effect.f & DESTROYED) !== 0) {
56
60
  return;
57
61
  }
@@ -241,7 +241,8 @@ export function proxy(value) {
241
241
 
242
242
  if (
243
243
  s !== undefined ||
244
- (active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
244
+ ((active_effect !== null || active_reaction !== null) &&
245
+ (!has || get_descriptor(target, prop)?.writable))
245
246
  ) {
246
247
  if (s === undefined) {
247
248
  s = with_parent(() => {
@@ -103,6 +103,11 @@ export function flatten(blockers, sync, async, fn) {
103
103
 
104
104
  if (blocker_promise) {
105
105
  blocker_promise.then(() => {
106
+ if ((parent.f & DESTROYED) !== 0) {
107
+ decrement_pending();
108
+ return;
109
+ }
110
+
106
111
  restore();
107
112
  run();
108
113
  unset_context();