Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: input select() must not throw for email/number

Onni Hakala54b61fbc1e8f
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index 493afa689..a56d553ca 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -590,3 +590,24 @@
testing.expectEqual(null, check.selectionEnd);
}
</script>
+
+<script id="select_without_selection_api">
+ {
+ // select() never throws, even where the selection APIs don't apply.
+ for (const type of ['email', 'number', 'date', 'color', 'checkbox', 'radio', 'file', 'hidden']) {
+ const input = document.createElement('input');
+ input.type = type;
+ input.select();
+ testing.expectEqual(null, input.selectionStart);
+ }
+
+ const email = document.createElement('input');
+ email.type = 'email';
+ email.value = '[email protected]';
+ let selects = 0;
+ email.addEventListener('select', () => selects++);
+ email.select();
+ testing.expectEqual(1, selects);
+ testing.expectError('InvalidStateError', () => email.setSelectionRange(0, 1));
+ }
+</script>
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index 5580069e0..cb955f367 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -173,7 +173,7 @@ pub fn setValue(self: *Input, value: []const u8, frame: *Frame) !void {
}
// move the text entry cursor position to the end of the text control
- if (changed and self.selectionAvailable()) {
+ if (changed and self.tracksSelection()) {
self._selection_start = @intCast(sanitized.len);
self._selection_end = @intCast(sanitized.len);
self._selection_direction = .none;
@@ -628,6 +628,7 @@ const entry = text_entry.TextEntry(Input);
pub const select = entry.select;
pub const innerInsert = entry.innerInsert;
pub const acceptsTextEntry = entry.acceptsTextEntry;
+pub const tracksSelection = entry.tracksSelection;
pub const innerDelete = entry.innerDelete;
pub const moveCaret = entry.moveCaret;
pub const CaretMove = entry.CaretMove;
diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zig
index ca68facd6..f1458a391 100644
--- a/src/browser/webapi/element/text_entry.zig
+++ b/src/browser/webapi/element/text_entry.zig
@@ -41,9 +41,30 @@ pub fn TextEntry(comptime T: type) type {
};
}
+ /// Whether the control keeps a text selection/caret internally. Wider
+ /// than selectionAvailable(): email and number have selectable text
+ /// (and a caret) even though the selection APIs don't apply to them.
+ pub fn tracksSelection(self: *const T) bool {
+ if (self.selectionAvailable()) {
+ return true;
+ }
+ if (!@hasField(T, "_input_type")) {
+ return false;
+ }
+ return switch (self._input_type) {
+ .email, .number => true,
+ else => false,
+ };
+ }
+
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
+ // Never throws: without selectable text it just returns.
pub fn select(self: *T, frame: *Frame) !void {
+ if (tracksSelection(self) == false) {
+ return;
+ }
const len: u32 = @intCast(self.getValue().len);
- try setSelectionRange(self, 0, len, null, frame);
+ try applySelectionRange(self, 0, len, .none, frame);
const event = try Event.init("select", .{ .bubbles = true }, frame.page);
try frame._event_manager.dispatch(self.asElement().asEventTarget(), event);
}
@@ -82,12 +103,12 @@ pub fn TextEntry(comptime T: type) type {
},
.none => {
// nothing selected, insert at the caret. Controls without
- // selection support keep no caret; append.
+ // a caret (e.g. date) append.
const current_value = self.getValue();
- const caret = if (self.selectionAvailable()) @min(self._selection_start, current_value.len) else current_value.len;
+ const caret = if (tracksSelection(self)) @min(self._selection_start, current_value.len) else current_value.len;
const new_value = try std.mem.concat(arena, u8, &.{ current_value[0..caret], str, current_value[caret..] });
try self.setUserValue(new_value, frame);
- if (self.selectionAvailable()) {
+ if (tracksSelection(self)) {
// the sanitized value can be shorter than what was inserted
const new_pos: u32 = @intCast(@min(caret + str.len, self.getValue().len));
self._selection_start = new_pos;
@@ -117,8 +138,8 @@ pub fn TextEntry(comptime T: type) type {
start, end = range;
},
.none => {
- // Controls without selection support keep no caret; edit at the end.
- const caret = if (self.selectionAvailable()) @min(self._selection_start, value_len) else value_len;
+ // Controls without a caret (e.g. date) edit at the end.
+ const caret = if (tracksSelection(self)) @min(self._selection_start, value_len) else value_len;
if (forward) {
if (caret >= value_len) {
return;
@@ -158,7 +179,7 @@ pub fn TextEntry(comptime T: type) type {
// ArrowLeft/ArrowRight on a non-collapsed selection collapses it to
// the corresponding edge without moving, like Chrome.
pub fn moveCaret(self: *T, move: CaretMove, extend: bool, frame: *Frame) !void {
- if (self.selectionAvailable() == false) {
+ if (tracksSelection(self) == false) {
return;
}
const value = self.getValue();
@@ -266,6 +287,16 @@ pub fn TextEntry(comptime T: type) type {
} else break :blk .none;
};
+ return applySelectionRange(self, selection_start, selection_end, direction, frame);
+ }
+
+ fn applySelectionRange(
+ self: *T,
+ selection_start: u32,
+ selection_end: u32,
+ direction: Selection.SelectionDirection,
+ frame: *Frame,
+ ) !void {
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;
@@ -285,7 +316,7 @@ pub fn TextEntry(comptime T: type) type {
const HowSelected = union(enum) { partial: struct { u32, u32 }, full, none };
fn howSelected(self: *const T) HowSelected {
- if (self.selectionAvailable() == false) {
+ if (tracksSelection(self) == false) {
return .none;
}
const value_len: u32 = @intCast(self.getValue().len);
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index 5dcdfb190..e2d3e3ccb 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -200,6 +200,37 @@ test "cdp.input: insertText is a user edit for tooLong" {
try testing.expect((try ls.local.compileAndRun("inp.validity.tooLong === false", null)).isTrue());
}
+test "cdp.input: insertText replaces select()ed value of email and number inputs" {
+ 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();
+
+ // What Playwright's fill() does: select() then Input.insertText.
+ _ = try ls.local.compileAndRun(
+ \\const inp = document.getElementById('inp');
+ \\inp.type = 'email';
+ \\inp.value = '[email protected]';
+ \\inp.select();
+ \\inp.focus();
+ , null);
+ try ctx.processMessage(.{ .id = 1, .method = "Input.insertText", .params = .{ .text = "[email protected]" } });
+ try testing.expect((try ls.local.compileAndRun("inp.value === '[email protected]'", null)).isTrue());
+
+ _ = try ls.local.compileAndRun("inp.type = 'number'; inp.value = '12'; inp.select();", null);
+ try ctx.processMessage(.{ .id = 2, .method = "Input.insertText", .params = .{ .text = "345" } });
+ try testing.expect((try ls.local.compileAndRun("inp.value === '345'", null)).isTrue());
+}
+
test "cdp.input: dispatchMouseEvent mouseMoved fires hover events" {
var ctx = try testing.context();
defer ctx.deinit();