Skip to content

Commit c3e91a3

Browse files
committed
fix: better input cursor restoration for bind:value
If cursor was at end and new input is longer, move cursor to new end No test because not possible to reproduce using our test setup. Follow-up to #14649, helps with #16577
1 parent 93012e1 commit c3e91a3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

.changeset/fair-aliens-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: better input cursor restoration for `bind:value`

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,22 @@ export function bind_value(input, get, set = get) {
4343
if (value !== (value = get())) {
4444
var start = input.selectionStart;
4545
var end = input.selectionEnd;
46+
var length = input.value.length;
4647

4748
// the value is coerced on assignment
4849
input.value = value ?? '';
4950

5051
// Restore selection
5152
if (end !== null) {
52-
input.selectionStart = start;
53-
input.selectionEnd = Math.min(end, input.value.length);
53+
var new_length = input.value.length;
54+
// If cursor was at end and new input is longer, move cursor to new end
55+
if (start === end && end === length && new_length > length) {
56+
input.selectionStart = new_length;
57+
input.selectionEnd = new_length;
58+
} else {
59+
input.selectionStart = start;
60+
input.selectionEnd = Math.min(end, new_length);
61+
}
5462
}
5563
}
5664
});

0 commit comments

Comments
 (0)