Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add setRangeText() to <input> and <textarea>

jayhemnani9910b2ab0be3462a
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index 8f09d572e..9f50ed84e 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -652,3 +652,49 @@
testing.expectEqual(false, input.checkValidity());
}
</script>
+
+<script id="setRangeText">
+ {
+ const input = document.createElement('input');
+ const state = () => [input.value, input.selectionStart, input.selectionEnd].join('|');
+ const reset = () => { input.value = 'hello world'; input.setSelectionRange(2, 7); };
+
+ reset();
+ input.setRangeText('XY');
+ testing.expectEqual('heXYorld|2|4', state());
+
+ reset();
+ input.setRangeText('ABC', 0, 5, 'select');
+ testing.expectEqual('ABC world|0|3', state());
+
+ reset();
+ input.setRangeText('ABC', 0, 5, 'start');
+ testing.expectEqual('ABC world|0|0', state());
+
+ reset();
+ input.setRangeText('ABC', 0, 5, 'end');
+ testing.expectEqual('ABC world|3|3', state());
+
+ // 'preserve' keeps the selection on the same text
+ reset();
+ input.setRangeText('ZZZZ', 8, 9);
+ testing.expectEqual('hello woZZZZld|2|7', state());
+
+ reset();
+ input.setRangeText('Q', 4, 9, 'preserve');
+ testing.expectEqual('hellQld|2|5', state());
+
+ reset();
+ input.setRangeText('!', 50, 60);
+ testing.expectEqual('hello world!|2|7', state());
+
+ reset();
+ testing.expectError('IndexSizeError', () => input.setRangeText('!', 5, 2));
+ testing.expectError('TypeError', () => input.setRangeText('!', 1, 2, 'bogus'));
+ testing.expectError('TypeError', () => input.setRangeText('!', 1));
+
+ const checkbox = document.createElement('input');
+ checkbox.type = 'checkbox';
+ testing.expectError('InvalidStateError', () => checkbox.setRangeText('x'));
+ }
+</script>
diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.html
index 223fa074f..0d53f9d2d 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -434,3 +434,21 @@
testing.expectEqual(4, el.selectionEnd);
}
</script>
+
+<script id="setRangeText">
+ {
+ const el = document.createElement('textarea');
+ el.value = 'line1\nline2';
+ el.setSelectionRange(0, 0);
+ el.setRangeText('X\r\nY', 0, 0, 'end');
+ testing.expectEqual('X\nYline1\nline2', el.value);
+ testing.expectEqual(4, el.selectionStart);
+ testing.expectEqual(4, el.selectionEnd);
+
+ el.setSelectionRange(2, 7);
+ el.setRangeText('');
+ testing.expectEqual('X\n1\nline2', el.value);
+ testing.expectEqual(2, el.selectionStart);
+ testing.expectEqual(2, el.selectionEnd);
+ }
+</script>
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index c1a79c550..53c280c2e 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -653,6 +653,7 @@ pub const getSelectionDirection = entry.getSelectionDirection;
pub const setSelectionStart = entry.setSelectionStart;
pub const setSelectionEnd = entry.setSelectionEnd;
pub const setSelectionRange = entry.setSelectionRange;
+pub const setRangeText = entry.setRangeText;
/// https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
pub fn readonlyApplies(self: *const Input) bool {
@@ -1587,6 +1588,7 @@ pub const JsApi = struct {
pub const selectionEnd = bridge.accessor(Input.getSelectionEnd, Input.setSelectionEnd, .{});
pub const selectionDirection = bridge.accessor(Input.getSelectionDirection, null, .{});
pub const setSelectionRange = bridge.function(Input.setSelectionRange, .{});
+ pub const setRangeText = bridge.function(Input.setRangeText, .{});
};
pub const Build = struct {
diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zig
index 45726358a..ae2f16a2b 100644
--- a/src/browser/webapi/element/html/TextArea.zig
+++ b/src/browser/webapi/element/html/TextArea.zig
@@ -172,6 +172,7 @@ pub const getSelectionDirection = entry.getSelectionDirection;
pub const setSelectionStart = entry.setSelectionStart;
pub const setSelectionEnd = entry.setSelectionEnd;
pub const setSelectionRange = entry.setSelectionRange;
+pub const setRangeText = entry.setRangeText;
// <textarea> always supports selection; <input> only does for some types.
pub fn selectionAvailable(_: *const TextArea) bool {
@@ -319,6 +320,7 @@ pub const JsApi = struct {
pub const selectionEnd = bridge.accessor(TextArea.getSelectionEnd, TextArea.setSelectionEnd, .{});
pub const selectionDirection = bridge.accessor(TextArea.getSelectionDirection, null, .{});
pub const setSelectionRange = bridge.function(TextArea.setSelectionRange, .{});
+ pub const setRangeText = bridge.function(TextArea.setRangeText, .{});
};
pub const Build = struct {
diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zig
index 697d6b14a..bc46045d6 100644
--- a/src/browser/webapi/element/text_entry.zig
+++ b/src/browser/webapi/element/text_entry.zig
@@ -322,6 +322,85 @@ pub fn TextEntry(comptime T: type) type {
return applySelectionRange(self, selectionOffsetFromJs(self, selection_start), selectionOffsetFromJs(self, selection_end), direction, frame);
}
+ const SelectionMode = enum { select, start, end, preserve };
+
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setrangetext
+ pub fn setRangeText(
+ self: *T,
+ replacement: []const u8,
+ range_start: ?u32,
+ range_end: ?u32,
+ selection_mode: ?[]const u8,
+ frame: *Frame,
+ ) !void {
+ if (self.selectionAvailable() == false) {
+ return error.InvalidStateError;
+ }
+
+ const value = self.getValue();
+ const value_len: u32 = @intCast(value.len);
+ const sel_start = @min(self._selection_start, value_len);
+ const sel_end = @min(self._selection_end, value_len);
+
+ // setRangeText(replacement) replaces the selection; a start needs an end
+ var start = sel_start;
+ var end = sel_end;
+ if (range_start) |js_start| {
+ const js_end = range_end orelse return error.TypeError;
+ if (js_start > js_end) {
+ return error.IndexSizeError;
+ }
+ start = selectionOffsetFromJs(self, js_start);
+ end = selectionOffsetFromJs(self, js_end);
+ }
+
+ const mode: SelectionMode = if (selection_mode) |m|
+ std.meta.stringToEnum(SelectionMode, m) orelse return error.TypeError
+ else
+ .preserve;
+
+ {
+ const scratch = try frame.getArena(value.len + replacement.len, "TextEntry.setRangeText");
+ defer scratch.release();
+ const new_value = try std.mem.concat(scratch.allocator(), u8, &.{ value[0..start], replacement, value[end..] });
+ try self.setValue(new_value, frame);
+ }
+
+ const new_end: u32 = @intCast(start + replacement.len);
+ var new_start = sel_start;
+ var new_sel_end = sel_end;
+ switch (mode) {
+ .select => {
+ new_start = start;
+ new_sel_end = new_end;
+ },
+ .start => {
+ new_start = start;
+ new_sel_end = start;
+ },
+ .end => {
+ new_start = new_end;
+ new_sel_end = new_end;
+ },
+ .preserve => {
+ // keep the selection on the same text: shift it past the
+ // replaced range, or snap it to the range's edges inside it
+ const delta: i64 = @as(i64, @intCast(replacement.len)) - @as(i64, end - start);
+ if (sel_start > end) {
+ new_start = @intCast(@as(i64, sel_start) + delta);
+ } else if (sel_start > start) {
+ new_start = start;
+ }
+ if (sel_end > end) {
+ new_sel_end = @intCast(@as(i64, sel_end) + delta);
+ } else if (sel_end > start) {
+ new_sel_end = new_end;
+ }
+ },
+ }
+ return applySelectionRange(self, new_start, new_sel_end, .none, frame);
+ }
+
fn applySelectionRange(
self: *T,
selection_start: u32,