From 083359dc131fb41024d75abdba673261207faa79 Mon Sep 17 00:00:00 2001 From: Jayden Carey Date: Thu, 23 Jan 2025 01:03:23 +0800 Subject: [PATCH] add attachments symbol --- packages/svelte/src/attachments/index.js | 1 + packages/svelte/src/attachments/public.d.ts | 4 +++ .../svelte/src/internal/client/constants.js | 1 + .../client/dom/elements/attributes.js | 9 ++++--- packages/svelte/types/index.d.ts | 27 ++++++++++++------- 5 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 packages/svelte/src/attachments/index.js create mode 100644 packages/svelte/src/attachments/public.d.ts diff --git a/packages/svelte/src/attachments/index.js b/packages/svelte/src/attachments/index.js new file mode 100644 index 0000000000..453cf9d90d --- /dev/null +++ b/packages/svelte/src/attachments/index.js @@ -0,0 +1 @@ +export { ATTACHMENTS_SYMBOL as AttachmentsKey } from '../internal/client/constants.js'; diff --git a/packages/svelte/src/attachments/public.d.ts b/packages/svelte/src/attachments/public.d.ts new file mode 100644 index 0000000000..7685151ab4 --- /dev/null +++ b/packages/svelte/src/attachments/public.d.ts @@ -0,0 +1,4 @@ +/** + * A unique symbol used for defining the attachments to be applied to an element or component. + */ +export const AttachmentsKey: unique symbol; diff --git a/packages/svelte/src/internal/client/constants.js b/packages/svelte/src/internal/client/constants.js index a4840ce4eb..42d7db4f17 100644 --- a/packages/svelte/src/internal/client/constants.js +++ b/packages/svelte/src/internal/client/constants.js @@ -25,3 +25,4 @@ export const STATE_SYMBOL = Symbol('$state'); export const STATE_SYMBOL_METADATA = Symbol('$state metadata'); export const LEGACY_PROPS = Symbol('legacy props'); export const LOADING_ATTR_SYMBOL = Symbol(''); +export const ATTACHMENTS_SYMBOL = Symbol.for('svelte.attachments'); diff --git a/packages/svelte/src/internal/client/dom/elements/attributes.js b/packages/svelte/src/internal/client/dom/elements/attributes.js index 17362cedea..4d4539ba57 100644 --- a/packages/svelte/src/internal/client/dom/elements/attributes.js +++ b/packages/svelte/src/internal/client/dom/elements/attributes.js @@ -4,7 +4,7 @@ import { get_descriptors, get_prototype_of } from '../../../shared/utils.js'; import { create_event, delegate } from './events.js'; import { add_form_reset_listener, autofocus } from './misc.js'; import * as w from '../../warnings.js'; -import { LOADING_ATTR_SYMBOL } from '../../constants.js'; +import { ATTACHMENTS_SYMBOL, LOADING_ATTR_SYMBOL } from '../../constants.js'; import { queue_idle_task } from '../task.js'; import { is_capture_event, is_delegated, normalize_attribute } from '../../../../utils.js'; import { @@ -416,8 +416,11 @@ export function set_attributes( } } - for (let symbol of Object.getOwnPropertySymbols(next)) { - attach(element, () => next[symbol]); + const attachments = next[ATTACHMENTS_SYMBOL]; + if (attachments) { + for (let attachment of attachments) { + attach(element, () => attachment); + } } return current; diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index ad328b11ac..7cf170d980 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -593,6 +593,13 @@ declare module 'svelte/action' { export {}; } +declare module 'svelte/attachments' { + /** + * A unique symbol used for defining the attachments to be applied to an element or component. + */ + export const AttachmentsKey: unique symbol; +} + declare module 'svelte/animate' { // todo: same as Transition, should it be shared? export interface AnimationConfig { @@ -1881,10 +1888,10 @@ declare module 'svelte/motion' { * const tween = Tween.of(() => number); * * ``` - * + * */ static of(fn: () => U, options?: TweenedOptions | undefined): Tween; - + constructor(value: T, options?: TweenedOptions); /** * Sets `tween.target` to `value` and returns a `Promise` that resolves if and when `tween.current` catches up to it. @@ -1903,21 +1910,21 @@ declare module 'svelte/motion' { declare module 'svelte/reactivity' { export class SvelteDate extends Date { - + constructor(...params: any[]); #private; } export class SvelteSet extends Set { - + constructor(value?: Iterable | null | undefined); - + add(value: T): this; #private; } export class SvelteMap extends Map { - + constructor(value?: Iterable | null | undefined); - + set(key: K, value: V): this; #private; } @@ -1927,7 +1934,7 @@ declare module 'svelte/reactivity' { } const REPLACE: unique symbol; export class SvelteURLSearchParams extends URLSearchParams { - + [REPLACE](params: URLSearchParams): void; #private; } @@ -1999,7 +2006,7 @@ declare module 'svelte/reactivity' { */ export function createSubscriber(start: (update: () => void) => (() => void) | void): () => void; class ReactiveValue { - + constructor(fn: () => T, onsubscribe: (update: () => void) => void); get current(): T; #private; @@ -2064,7 +2071,7 @@ declare module 'svelte/reactivity/window' { get current(): number | undefined; }; class ReactiveValue { - + constructor(fn: () => T, onsubscribe: (update: () => void) => void); get current(): T; #private;