From eafddf4358ad750173c3573fb92f3d735fdd7bcf Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Mon, 14 Oct 2024 17:05:23 +0200 Subject: [PATCH] fix: avoid assigning input.value if the value is the same to fix `minlength` (#13574) * fix: avoid assigning input.value if the value is the same to fix `minlength` * chore: changeset --- .changeset/slow-badgers-invite.md | 5 +++++ .../svelte/src/internal/client/dom/elements/attributes.js | 4 ++-- .../src/internal/client/dom/elements/bindings/input.js | 8 ++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .changeset/slow-badgers-invite.md diff --git a/.changeset/slow-badgers-invite.md b/.changeset/slow-badgers-invite.md new file mode 100644 index 0000000000..f46fc18f09 --- /dev/null +++ b/.changeset/slow-badgers-invite.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: avoid assigning input.value if the value is the same to fix `minlength` diff --git a/packages/svelte/src/internal/client/dom/elements/attributes.js b/packages/svelte/src/internal/client/dom/elements/attributes.js index db3ac5fa4e..802d45153d 100644 --- a/packages/svelte/src/internal/client/dom/elements/attributes.js +++ b/packages/svelte/src/internal/client/dom/elements/attributes.js @@ -55,8 +55,8 @@ export function remove_input_defaults(input) { export function set_value(element, value) { // @ts-expect-error var attributes = (element.__attributes ??= {}); - - if (attributes.value === (attributes.value = value)) return; + // @ts-expect-error + if (attributes.value === (attributes.value = value) || element.value === value) return; // @ts-expect-error element.value = value; } diff --git a/packages/svelte/src/internal/client/dom/elements/bindings/input.js b/packages/svelte/src/internal/client/dom/elements/bindings/input.js index 91b4346c0a..28a99f7eed 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/input.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/input.js @@ -60,8 +60,12 @@ export function bind_value(input, get, set = get) { return; } - // @ts-expect-error the value is coerced on assignment - input.value = value ?? ''; + // don't set the value of the input if it's the same to allow + // minlength to work properly + if (value !== input.value) { + // @ts-expect-error the value is coerced on assignment + input.value = value ?? ''; + } }); }