Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Fix inconsistent input range

Karl Seguina15644955dce
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index 70d97361e..c6d66ad40 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -618,6 +618,28 @@
}
</script>
+<script id="selection_setters_keep_order">
+ {
+ // Setting one end past the other drags the other along (start <= end).
+ const el = document.createElement('input');
+ el.value = 'hello';
+ el.setSelectionRange(1, 2, 'backward');
+ el.selectionStart = 4;
+ testing.expectEqual(4, el.selectionStart);
+ testing.expectEqual(4, el.selectionEnd);
+ testing.expectEqual('backward', el.selectionDirection);
+
+ el.setSelectionRange(3, 4);
+ el.selectionEnd = 1;
+ testing.expectEqual(1, el.selectionStart);
+ testing.expectEqual(1, el.selectionEnd);
+
+ el.selectionStart = 10;
+ testing.expectEqual(5, el.selectionStart);
+ testing.expectEqual(5, 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.html
index 36f7d2c81..bd305f905 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -467,3 +467,25 @@
testing.expectEqual(2, el.selectionStart);
}
</script>
+
+<script id="selection_setters_keep_order">
+ {
+ // Setting one end past the other drags the other along (start <= end).
+ const el = document.createElement('textarea');
+ el.value = 'hello';
+ el.setSelectionRange(1, 2, 'backward');
+ el.selectionStart = 4;
+ testing.expectEqual(4, el.selectionStart);
+ testing.expectEqual(4, el.selectionEnd);
+ testing.expectEqual('backward', el.selectionDirection);
+
+ el.setSelectionRange(3, 4);
+ el.selectionEnd = 1;
+ testing.expectEqual(1, el.selectionStart);
+ testing.expectEqual(1, el.selectionEnd);
+
+ el.selectionStart = 10;
+ testing.expectEqual(5, el.selectionStart);
+ testing.expectEqual(5, el.selectionEnd);
+ }
+</script>
diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zig
index 78957475f..ff8e9db1f 100644
--- a/src/browser/webapi/element/text_entry.zig
+++ b/src/browser/webapi/element/text_entry.zig
@@ -290,24 +290,16 @@ pub fn TextEntry(comptime T: type) type {
if (self.selectionAvailable() == false) {
return error.InvalidStateError;
}
+ // Spec: the end is raised to the new start, then set-range runs.
const start = selectionOffsetFromJs(self, value);
- if (start == self._selection_start) {
- return;
- }
- self._selection_start = start;
- try scheduleSelectionChange(self, frame);
+ return applySelectionRange(self, start, @max(start, self._selection_end), self._selection_direction, frame);
}
pub fn setSelectionEnd(self: *T, value: u32, frame: *Frame) !void {
if (self.selectionAvailable() == false) {
return error.InvalidStateError;
}
- const end = selectionOffsetFromJs(self, value);
- if (end == self._selection_end) {
- return;
- }
- self._selection_end = end;
- try scheduleSelectionChange(self, frame);
+ return applySelectionRange(self, self._selection_start, selectionOffsetFromJs(self, value), self._selection_direction, frame);
}
pub fn setSelectionRange(