diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js index 4296aa959e..b1c5b54d0a 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js @@ -172,7 +172,16 @@ export function RegularElement(node, context) { bindings.has('group') || (!bindings.has('group') && has_value_attribute)) ) { - context.state.init.push(b.stmt(b.call('$.remove_input_defaults', context.state.node))); + const spreads = has_spread + ? b.object( + attributes + .filter((attr) => attr.type === 'SpreadAttribute') + .map((attr) => b.spread(attr.expression)) + ) + : null; + context.state.init.push( + b.stmt(b.call('$.remove_input_defaults', context.state.node, spreads)) + ); } } diff --git a/packages/svelte/src/internal/client/dom/elements/attributes.js b/packages/svelte/src/internal/client/dom/elements/attributes.js index 22e532f5e4..d01a39dc4f 100644 --- a/packages/svelte/src/internal/client/dom/elements/attributes.js +++ b/packages/svelte/src/internal/client/dom/elements/attributes.js @@ -31,14 +31,20 @@ const IS_CUSTOM_ELEMENT = Symbol('is custom element'); const IS_HTML = Symbol('is html'); /** - * The value/checked attribute in the template actually corresponds to the defaultValue property, so we need - * to remove it upon hydration to avoid a bug when someone resets the form value. + * The value/checked attribute in the template actually corresponds to the defaultValue property, + * so we need to remove it upon hydration to avoid a bug when someone resets the form value, + * unless the property is presented in the spreaded objects and is handled by `set_attributes()` * @param {HTMLInputElement} input + * @param {Record} [spread] * @returns {void} */ -export function remove_input_defaults(input) { +export function remove_input_defaults(input, spread) { if (!hydrating) return; + if (spread && (input.type === 'checkbox' ? 'defaultChecked' : 'defaultValue') in spread) { + return; + } + var already_removed = false; // We try and remove the default attributes later, rather than sync during hydration.