Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: move the text entry cursor to the end on value set

Navid EMAD31d0de777fa8
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index 6546bfee2..a8c961b3e 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -532,3 +532,36 @@
testing.expectEqual(false, input_checked.checked);
}
</script>
+
+<script id="value_setter_moves_caret">
+ {
+ // Setting `value` moves the text entry cursor to the end of the new value,
+ // unselects any selected text and resets the selection direction.
+ // https://html.spec.whatwg.org/#dom-input-value
+ const input = document.createElement('input');
+ input.type = 'text';
+ document.body.appendChild(input);
+
+ input.value = 'abc';
+ testing.expectEqual(3, input.selectionStart);
+ testing.expectEqual(3, input.selectionEnd);
+
+ input.setSelectionRange(1, 2, 'backward');
+ input.value = 'hello';
+ testing.expectEqual(5, input.selectionStart);
+ testing.expectEqual(5, input.selectionEnd);
+ testing.expectEqual('none', input.selectionDirection);
+
+ // The caret follows the _sanitized_ value: a text input strips CR/LF.
+ input.value = 'a\nb';
+ testing.expectEqual(2, input.selectionStart);
+ testing.expectEqual(2, input.selectionEnd);
+
+ // Types with no text entry cursor keep reporting null rather than a number.
+ const check = document.createElement('input');
+ check.type = 'checkbox';
+ check.value = 'on';
+ testing.expectEqual(null, check.selectionStart);
+ testing.expectEqual(null, check.selectionEnd);
+ }
+</script>
diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.html
index daf571498..bff1c0980 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -305,3 +305,23 @@
});
}
</script>
+
+<script id="value_setter_moves_caret">
+ {
+ // Setting `value` moves the text entry cursor to the end of the new value,
+ // unselects any selected text and resets the selection direction.
+ // https://html.spec.whatwg.org/#dom-textarea-value
+ const textarea = document.createElement('textarea');
+ document.body.appendChild(textarea);
+
+ textarea.value = 'abc';
+ testing.expectEqual(3, textarea.selectionStart);
+ testing.expectEqual(3, textarea.selectionEnd);
+
+ textarea.setSelectionRange(1, 2, 'backward');
+ textarea.value = 'hello';
+ testing.expectEqual(5, textarea.selectionStart);
+ testing.expectEqual(5, textarea.selectionEnd);
+ testing.expectEqual('none', textarea.selectionDirection);
+ }
+</script>
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index c3c49ed72..eb1f5c4e7 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -157,8 +157,18 @@ pub fn setValue(self: *Input, value: []const u8, frame: *Frame) !void {
return error.InvalidStateError;
}
// This should _not_ call setAttribute. It updates the current state only
- self._value = try self.sanitizeValue(true, value, frame);
+ const sanitized = try self.sanitizeValue(true, value, frame);
+ self._value = sanitized;
self._user_edited = false;
+
+ // "move the text entry cursor position to the end of the text control,
+ // unselecting any selected text and resetting the selection direction to
+ // 'none'" -- https://html.spec.whatwg.org/#dom-input-value
+ if (self.selectionAvailable()) {
+ self._selection_start = @intCast(sanitized.len);
+ self._selection_end = @intCast(sanitized.len);
+ self._selection_direction = .none;
+ }
}
pub fn setUserValue(self: *Input, value: []const u8, frame: *Frame) !void {
diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zig
index 3a176b9bd..b2bf0f829 100644
--- a/src/browser/webapi/element/html/TextArea.zig
+++ b/src/browser/webapi/element/html/TextArea.zig
@@ -82,6 +82,13 @@ pub fn setValue(self: *TextArea, value: []const u8, frame: *Frame) !void {
const owned = try frame.arena.dupe(u8, value);
self._value = owned;
self._user_edited = false;
+
+ // "move the text entry cursor position to the end of the text control,
+ // unselecting any selected text and resetting the selection direction to
+ // 'none'" -- https://html.spec.whatwg.org/#dom-textarea-value
+ self._selection_start = @intCast(owned.len);
+ self._selection_end = @intCast(owned.len);
+ self._selection_direction = .none;
}
pub fn setUserValue(self: *TextArea, value: []const u8, frame: *Frame) !void {