Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: clamp text entry selections against the current value

Navid EMAD172614d13861
diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.html
index 9bb61d94f..6400e0b87 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -230,6 +230,42 @@
}
</script>
+<script id="selection_uses_current_value">
+ {
+ // A <textarea>'s API value is its raw value, which comes from its child
+ // text node until something assigns to `value`. The selection APIs clamp
+ // against that value, not against an assigned one.
+ // https://html.spec.whatwg.org/#dom-textarea-value
+ // https://html.spec.whatwg.org/#dom-textarea/input-setselectionrange
+ const host = document.createElement('div');
+ host.innerHTML = '<textarea>abcdef</textarea>';
+ document.body.appendChild(host);
+ const parsed = host.firstChild;
+
+ parsed.select();
+ testing.expectEqual(0, parsed.selectionStart);
+ testing.expectEqual(6, parsed.selectionEnd);
+
+ parsed.setSelectionRange(2, 4, 'backward');
+ testing.expectEqual(2, parsed.selectionStart);
+ testing.expectEqual(4, parsed.selectionEnd);
+ testing.expectEqual('backward', parsed.selectionDirection);
+
+ // Offsets past the end clamp to the length of the value.
+ parsed.setSelectionRange(99, 99);
+ testing.expectEqual(6, parsed.selectionStart);
+ testing.expectEqual(6, parsed.selectionEnd);
+
+ // Same when the default value came from the defaultValue setter.
+ const seeded = document.createElement('textarea');
+ document.body.appendChild(seeded);
+ seeded.defaultValue = 'hello';
+ seeded.select();
+ testing.expectEqual(0, seeded.selectionStart);
+ testing.expectEqual(5, seeded.selectionEnd);
+ }
+</script>
+
<script id="select_event">
{
const textarea = document.createElement('textarea');
diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zig
index 096b0b210..cfe4d8005 100644
--- a/src/browser/webapi/element/text_entry.zig
+++ b/src/browser/webapi/element/text_entry.zig
@@ -30,7 +30,7 @@ const InputEvent = @import("../event/InputEvent.zig");
pub fn TextEntry(comptime T: type) type {
return struct {
pub fn select(self: *T, frame: *Frame) !void {
- const len = if (self._value) |v| @as(u32, @intCast(v.len)) else 0;
+ const len: u32 = @intCast(self.getValue().len);
try setSelectionRange(self, 0, len, null, frame);
const event = try Event.init("select", .{ .bubbles = true }, frame._page);
try frame._event_manager.dispatch(self.asElement().asEventTarget(), event);
@@ -165,14 +165,7 @@ pub fn TextEntry(comptime T: type) type {
} else break :blk .none;
};
- const value = self._value orelse {
- self._selection_start = 0;
- self._selection_end = 0;
- self._selection_direction = .none;
- return;
- };
-
- const len_u32: u32 = @intCast(value.len);
+ const len_u32: u32 = @intCast(self.getValue().len);
var start: u32 = if (selection_start > len_u32) len_u32 else selection_start;
const end: u32 = if (selection_end > len_u32) len_u32 else selection_end;
@@ -194,15 +187,17 @@ pub fn TextEntry(comptime T: type) type {
if (self.selectionAvailable() == false) {
return .none;
}
- const value = self._value orelse return .none;
+ const value_len: u32 = @intCast(self.getValue().len);
+ const start = @min(self._selection_start, value_len);
+ const end = @min(self._selection_end, value_len);
- if (self._selection_start == self._selection_end) {
+ if (start == end) {
return .none;
}
- if (self._selection_start == 0 and self._selection_end == value.len) {
+ if (start == 0 and end == value_len) {
return .full;
}
- return .{ .partial = .{ self._selection_start, self._selection_end } };
+ return .{ .partial = .{ start, end } };
}
fn dispatchSelectionChangeEvent(self: *T, frame: *Frame) !void {
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 85110812f..2c94cf844 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -478,3 +478,47 @@ test "cdp.input: dispatchKeyEvent beforeinput can veto the edit" {
\\clone.value === 'abc' && window.seen.join(',') === 'input:false'
, null)).isTrue());
}
+
+test "cdp.input: editing keys honor the selection of a default-valued textarea" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{});
+ const page = try bc.session.createPage();
+ const frame = page.frame().?;
+
+ try frame.navigate("http://localhost:9582/src/browser/tests/mcp_actions.html", .{ .reason = .address_bar, .kind = .{ .push = null } });
+ try testing.waitForPage(bc);
+
+ var ls: lp.js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+
+ // This <textarea>'s value comes from its child text node — nothing assigns
+ // to `value` — so the selection has to clamp against the parsed text.
+ _ = try ls.local.compileAndRun(
+ \\document.body.innerHTML = '<textarea id="ta">abcdef</textarea>';
+ \\const ta = document.getElementById('ta');
+ \\ta.focus();
+ \\ta.setSelectionRange(2, 4);
+ , null);
+ try testing.expect((try ls.local.compileAndRun("ta.selectionStart === 2 && ta.selectionEnd === 4", null)).isTrue());
+
+ // Backspace over a range deletes the range, leaving the caret at its start.
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Input.dispatchKeyEvent",
+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },
+ });
+ try testing.expect((try ls.local.compileAndRun("ta.value === 'abef'", null)).isTrue());
+ try testing.expect((try ls.local.compileAndRun("ta.selectionStart === 2 && ta.selectionEnd === 2", null)).isTrue());
+
+ // Collapsed caret: Backspace removes the character on its left.
+ _ = try ls.local.compileAndRun("ta.setSelectionRange(3, 3)", null);
+ try ctx.processMessage(.{
+ .id = 2,
+ .method = "Input.dispatchKeyEvent",
+ .params = .{ .type = "keyDown", .key = "Backspace", .code = "Backspace" },
+ });
+ try testing.expect((try ls.local.compileAndRun("ta.value === 'abf'", null)).isTrue());
+}