Lightpanda: the headless browser designed for AI and automation
webapi: count text entry selection offsets in UTF-16 code units
Jay Hemnani2ad62b948cc8
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.htmlindex 4d8d76bd8..c96638b86 100644--- a/src/browser/tests/element/html/input.html+++ b/src/browser/tests/element/html/input.html@@ -591,6 +591,26 @@}</script>+<script id="selection_utf16_offsets">+ {+ // Offsets count UTF-16 code units, like value.length.+ const el = document.createElement('input');+ el.value = 'h\u00e9llo';+ testing.expectEqual(5, el.selectionStart);+ testing.expectEqual(5, el.selectionEnd);+ el.setSelectionRange(1, 3);+ testing.expectEqual('\u00e9l', el.value.substring(el.selectionStart, el.selectionEnd));++ el.value = 'a\u{1F600}b';+ testing.expectEqual(4, el.selectionEnd);+ el.selectionStart = 3;+ testing.expectEqual(3, el.selectionStart);+ el.setSelectionRange(10, 20);+ testing.expectEqual(4, el.selectionStart);+ testing.expectEqual(4, el.selectionEnd);+ }+</script>+<script id="select_without_selection_api">{// select() never throws, even where the selection APIs don't apply.diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.htmlindex 4ab35f596..4ab3fcd02 100644--- a/src/browser/tests/element/html/textarea.html+++ b/src/browser/tests/element/html/textarea.html@@ -384,3 +384,23 @@testing.expectEqual('foo', dflt.value);}</script>++<script id="selection_utf16_offsets">+ {+ // Offsets count UTF-16 code units, like value.length.+ const el = document.createElement('textarea');+ el.value = 'h\u00e9llo';+ testing.expectEqual(5, el.selectionStart);+ testing.expectEqual(5, el.selectionEnd);+ el.setSelectionRange(1, 3);+ testing.expectEqual('\u00e9l', el.value.substring(el.selectionStart, el.selectionEnd));++ el.value = 'a\u{1F600}b';+ testing.expectEqual(4, el.selectionEnd);+ el.selectionStart = 3;+ testing.expectEqual(3, el.selectionStart);+ el.setSelectionRange(10, 20);+ testing.expectEqual(4, el.selectionStart);+ testing.expectEqual(4, el.selectionEnd);+ }+</script>diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zigindex d2611912e..c1a79c550 100644--- a/src/browser/webapi/element/html/Input.zig+++ b/src/browser/webapi/element/html/Input.zig@@ -673,12 +673,12 @@ pub fn selectionAvailable(self: *const Input) bool {// Nullable here, unlike <textarea>'s, which is why these two aren't shared.fn getSelectionStart(self: *const Input) !?u32 {if (!self.selectionAvailable()) return null;- return self._selection_start;+ return entry.selectionOffsetToJs(self, self._selection_start);}fn getSelectionEnd(self: *const Input) !?u32 {if (!self.selectionAvailable()) return null;- return self._selection_end;+ return entry.selectionOffsetToJs(self, self._selection_end);}fn getLabels(self: *Input, frame: *Frame) !js.Array {diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zigindex abea8e4c6..4aed44282 100644--- a/src/browser/webapi/element/html/TextArea.zig+++ b/src/browser/webapi/element/html/TextArea.zig@@ -156,11 +156,11 @@ pub fn selectionAvailable(_: *const TextArea) bool {// Non-null unlike inputfn getSelectionStart(self: *const TextArea) u32 {- return self._selection_start;+ return entry.selectionOffsetToJs(self, self._selection_start);}fn getSelectionEnd(self: *const TextArea) u32 {- return self._selection_end;+ return entry.selectionOffsetToJs(self, self._selection_end);}pub fn getForm(self: *TextArea, frame: *Frame) ?*Form {diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zigindex c397c9e80..697d6b14a 100644--- a/src/browser/webapi/element/text_entry.zig+++ b/src/browser/webapi/element/text_entry.zig@@ -23,6 +23,7 @@ const std = @import("std");const Frame = @import("../../Frame.zig");+const CData = @import("../CData.zig");const Event = @import("../Event.zig");const Selection = @import("../Selection.zig");const InputEvent = @import("../event/InputEvent.zig");@@ -274,11 +275,22 @@ pub fn TextEntry(comptime T: type) type {return @tagName(self._selection_direction);}+ // Selection offsets are kept as byte offsets into the UTF-8 value, while+ // the JS API counts UTF-16 code units.+ pub fn selectionOffsetToJs(self: *const T, offset: u32) u32 {+ const value = self.getValue();+ return @intCast(CData.utf16Len(value[0..@min(offset, value.len)]));+ }++ fn selectionOffsetFromJs(self: *const T, offset: u32) u32 {+ return @intCast(CData.utf16OffsetToUtf8Floor(self.getValue(), offset));+ }+pub fn setSelectionStart(self: *T, value: u32, frame: *Frame) !void {if (self.selectionAvailable() == false) {return error.InvalidStateError;}- self._selection_start = value;+ self._selection_start = selectionOffsetFromJs(self, value);try dispatchSelectionChangeEvent(self, frame);}@@ -286,7 +298,7 @@ pub fn TextEntry(comptime T: type) type {if (self.selectionAvailable() == false) {return error.InvalidStateError;}- self._selection_end = value;+ self._selection_end = selectionOffsetFromJs(self, value);try dispatchSelectionChangeEvent(self, frame);}@@ -307,7 +319,7 @@ pub fn TextEntry(comptime T: type) type {} else break :blk .none;};- return applySelectionRange(self, selection_start, selection_end, direction, frame);+ return applySelectionRange(self, selectionOffsetFromJs(self, selection_start), selectionOffsetFromJs(self, selection_end), direction, frame);}fn applySelectionRange(diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex a54988615..418940bde 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -2201,6 +2201,12 @@ test "cdp.input: dispatchKeyEvent caret movement keys move the text entry cursorid += 1;try testing.expect((try ls.local.compileAndRun("t.value === 'aXé'", null)).isTrue());+ // A caret set from JS counts UTF-16 code units, so text goes after the é.+ _ = try ls.local.compileAndRun("t.value = 'éa'; t.setSelectionRange(1, 1)", null);+ try ctx.processMessage(.{ .id = id, .method = "Input.insertText", .params = .{ .text = "X" } });+ id += 1;+ try testing.expect((try ls.local.compileAndRun("t.value === 'éXa' && sel(t) === '2,2,none'", null)).isTrue());+// <textarea>: Home/End stop at the enclosing line break, ArrowLeft crosses it._ = try ls.local.compileAndRun("ta.focus(); ta.value = 'ab\\ncd'; ta.setSelectionRange(5, 5)", null);const textarea_steps = [_]Step{