Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: async dispatch of select and selectionchange event

Karl Seguin7ad6f72ac6b3
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index ff9a8c7af..53b0ecf36 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -2596,7 +2596,7 @@ pub const QueuedEvent = struct {
element: *Element.Html,
generation: u32 = 0, // Image/Link request generation
- pub const Kind = enum { load, @"error" };
+ pub const Kind = enum { load, @"error", select, selectionchange };
};
pub fn queueLoad(self: *Frame, html: *Element.Html) !void {
@@ -2841,11 +2841,11 @@ fn fireIFrameLoad(self: *Frame, iframe: *IFrame) !void {
defer if (child_doc) |doc| {
doc._iframe_load_in_progress = false;
};
- try self.fireElementEvent(iframe.asElement(), comptime .wrap("load"));
+ try self.fireElementEvent(iframe.asElement(), comptime .wrap("load"), .{});
}
-fn fireElementEvent(self: *Frame, el: *Element, name: String) !void {
- const event = try Event.initTrusted(name, .{}, self.page);
+fn fireElementEvent(self: *Frame, el: *Element, name: String, opts: Event.Options) !void {
+ const event = try Event.initTrusted(name, opts, self.page);
try self._event_manager.dispatch(el.asEventTarget(), event);
}
@@ -2880,13 +2880,22 @@ fn dispatchQueuedEvents(self: *Frame) !void {
if (html_element.is(IFrame)) |iframe| {
try self.fireIFrameLoad(iframe);
} else {
- try self.fireElementEvent(element, comptime .wrap("load"));
+ try self.fireElementEvent(element, comptime .wrap("load"), .{});
}
}
},
.@"error" => {
// errors are rare; not worth a listener-presence check
- try self.fireElementEvent(element, comptime .wrap("error"));
+ try self.fireElementEvent(element, comptime .wrap("error"), .{});
+ },
+ .select => try self.fireElementEvent(element, comptime .wrap("select"), .{ .bubbles = true }),
+ .selectionchange => {
+ if (html_element.is(Element.Html.Input)) |input| {
+ input._selectionchange_pending = false;
+ } else if (html_element.is(Element.Html.TextArea)) |textarea| {
+ textarea._selectionchange_pending = false;
+ }
+ try self.fireElementEvent(element, .wrap("selectionchange"), .{ .bubbles = true });
},
}
}
diff --git a/src/browser/tests/element/html/input.html b/src/browser/tests/element/html/input.html
index f4ecd7a51..70d97361e 100644
--- a/src/browser/tests/element/html/input.html
+++ b/src/browser/tests/element/html/input.html
@@ -211,8 +211,11 @@
});
input.setSelectionRange(1, 4);
+ // Fires asynchronously, and changes before it fires fold into one event.
+ testing.expectEqual(0, eventCount);
+
testing.onload(() => {
- testing.expectEqual(5, eventCount);
+ testing.expectEqual(1, eventCount);
testing.expectEqual('selectionchange', lastEvent.type);
testing.expectEqual(input, lastEvent.target);
testing.expectEqual(true, lastEvent.bubbles);
@@ -551,7 +554,10 @@
input.value = 'hello';
testing.expectEqual(5, input.selectionStart);
testing.expectEqual(5, input.selectionEnd);
- testing.expectEqual('none', input.selectionDirection);
+ // Platforms without a "none" direction report "forward" (Chrome on Linux).
+ const probe = document.createElement('input');
+ probe.setSelectionRange(0, 0, 'none');
+ testing.expectEqual(probe.selectionDirection, input.selectionDirection);
// The caret follows the _sanitized_ value: a text input strips CR/LF.
input.value = 'a\nb';
@@ -628,7 +634,8 @@
let selects = 0;
email.addEventListener('select', () => selects++);
email.select();
- testing.expectEqual(1, selects);
+ // select is queued, not fired synchronously
+ testing.expectEqual(0, selects);
testing.expectError('InvalidStateError', () => email.setSelectionRange(0, 1));
const date = document.createElement('input');
@@ -637,7 +644,11 @@
let date_selects = 0;
date.addEventListener('select', () => date_selects++);
date.select();
- testing.expectEqual(0, date_selects);
+
+ testing.onload(() => {
+ testing.expectEqual(1, selects);
+ testing.expectEqual(0, date_selects);
+ });
}
</script>
diff --git a/src/browser/tests/element/html/textarea.html b/src/browser/tests/element/html/textarea.html
index a785e3a62..36f7d2c81 100644
--- a/src/browser/tests/element/html/textarea.html
+++ b/src/browser/tests/element/html/textarea.html
@@ -367,8 +367,11 @@
});
textarea.setSelectionRange(1, 4);
+ // Fires asynchronously, and changes before it fires fold into one event.
+ testing.expectEqual(0, eventCount);
+
testing.onload(() => {
- testing.expectEqual(5, eventCount);
+ testing.expectEqual(1, eventCount);
testing.expectEqual('selectionchange', lastEvent.type);
testing.expectEqual(textarea, lastEvent.target);
testing.expectEqual(true, lastEvent.bubbles);
@@ -394,7 +397,10 @@
textarea.value = 'hello';
testing.expectEqual(5, textarea.selectionStart);
testing.expectEqual(5, textarea.selectionEnd);
- testing.expectEqual('none', textarea.selectionDirection);
+ // Platforms without a "none" direction report "forward" (Chrome on Linux).
+ const probe = document.createElement('textarea');
+ probe.setSelectionRange(0, 0, 'none');
+ testing.expectEqual(probe.selectionDirection, textarea.selectionDirection);
// Re-assigning the same value leaves the selection alone
// (WPT textfieldselection/selection-after-content-change.html).
diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.js
index 2eefa8411..c29c4d26d 100644
--- a/src/browser/tests/testing.js
+++ b/src/browser/tests/testing.js
@@ -218,7 +218,9 @@
console.warn(`The page is not being executed in the test runner, certain behavior has been adjusted`);
window.testing.HOST = location.hostname;
window.testing.ORIGIN = location.origin;
- window.testing.BASE_URL = location.origin + '/src/browser/tests/';
+ // Relative to this script, so a page served as //src/... (or from another
+ // root) still resolves relative URLs the way the test expects.
+ window.testing.BASE_URL = new URL('./', document.currentScript.src).href;
window.addEventListener('load', testing.assertOk);
}
diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig
index dba08cb95..14da6545f 100644
--- a/src/browser/webapi/element/html/Input.zig
+++ b/src/browser/webapi/element/html/Input.zig
@@ -123,6 +123,8 @@ _files: ?*FileList = null,
_selection_start: u32 = 0,
_selection_end: u32 = 0,
_selection_direction: Selection.SelectionDirection = .none,
+// A queued selectionchange not yet fired; later changes fold into it.
+_selectionchange_pending: bool = false,
pub fn asElement(self: *Input) *Element {
return Factory.protoOf(self).asElement();
diff --git a/src/browser/webapi/element/html/TextArea.zig b/src/browser/webapi/element/html/TextArea.zig
index 93b1ee5e2..0b2cf5615 100644
--- a/src/browser/webapi/element/html/TextArea.zig
+++ b/src/browser/webapi/element/html/TextArea.zig
@@ -48,6 +48,8 @@ _value_before_edit: ?[]const u8 = null,
_selection_start: u32 = 0,
_selection_end: u32 = 0,
_selection_direction: Selection.SelectionDirection = .none,
+// A queued selectionchange not yet fired; later changes fold into it.
+_selectionchange_pending: bool = false,
_custom_validity: ?[]const u8 = null,
_validity: ?*ValidityState = null,
diff --git a/src/browser/webapi/element/text_entry.zig b/src/browser/webapi/element/text_entry.zig
index 6d635a83a..78957475f 100644
--- a/src/browser/webapi/element/text_entry.zig
+++ b/src/browser/webapi/element/text_entry.zig
@@ -22,6 +22,7 @@
const std = @import("std");
const Frame = @import("../../Frame.zig");
+const Factory = @import("../../Factory.zig");
const CData = @import("../CData.zig");
const Event = @import("../Event.zig");
@@ -66,8 +67,7 @@ pub fn TextEntry(comptime T: type) type {
}
const len: u32 = @intCast(self.getValue().len);
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);
+ try frame.queueElementEvent(Factory.protoOf(self), .select);
}
pub fn innerInsert(self: *T, str: []const u8, data: ?[]const u8, input_type: []const u8, frame: *Frame) !void {
@@ -96,7 +96,7 @@ pub fn TextEntry(comptime T: type) type {
self._selection_start = new_pos;
self._selection_end = new_pos;
self._selection_direction = .none;
- try dispatchSelectionChangeEvent(self, frame);
+ try scheduleSelectionChange(self, frame);
}
try dispatchInputEvent(self, data, input_type, frame);
}
@@ -151,7 +151,7 @@ pub fn TextEntry(comptime T: type) type {
self._selection_start = start;
self._selection_end = start;
self._selection_direction = .none;
- try dispatchSelectionChangeEvent(self, frame);
+ try scheduleSelectionChange(self, frame);
try dispatchInputEvent(self, null, input_type, frame);
}
@@ -239,7 +239,7 @@ pub fn TextEntry(comptime T: type) type {
self._selection_start = new_start;
self._selection_end = new_end;
self._selection_direction = new_direction;
- try dispatchSelectionChangeEvent(self, frame);
+ try scheduleSelectionChange(self, frame);
}
// Byte offset the caret lands on when moved from `pos`. Steps over
@@ -290,16 +290,24 @@ pub fn TextEntry(comptime T: type) type {
if (self.selectionAvailable() == false) {
return error.InvalidStateError;
}
- self._selection_start = selectionOffsetFromJs(self, value);
- try dispatchSelectionChangeEvent(self, frame);
+ const start = selectionOffsetFromJs(self, value);
+ if (start == self._selection_start) {
+ return;
+ }
+ self._selection_start = start;
+ try scheduleSelectionChange(self, frame);
}
pub fn setSelectionEnd(self: *T, value: u32, frame: *Frame) !void {
if (self.selectionAvailable() == false) {
return error.InvalidStateError;
}
- self._selection_end = selectionOffsetFromJs(self, value);
- try dispatchSelectionChangeEvent(self, frame);
+ const end = selectionOffsetFromJs(self, value);
+ if (end == self._selection_end) {
+ return;
+ }
+ self._selection_end = end;
+ try scheduleSelectionChange(self, frame);
}
pub fn setSelectionRange(
@@ -427,11 +435,14 @@ pub fn TextEntry(comptime T: type) type {
start = end;
}
+ if (start == self._selection_start and end == self._selection_end and direction == self._selection_direction) {
+ return;
+ }
self._selection_direction = direction;
self._selection_start = start;
self._selection_end = end;
- try dispatchSelectionChangeEvent(self, frame);
+ try scheduleSelectionChange(self, frame);
}
const HowSelected = union(enum) { partial: struct { u32, u32 }, full, none };
@@ -453,9 +464,13 @@ pub fn TextEntry(comptime T: type) type {
return .{ .partial = .{ start, end } };
}
- fn dispatchSelectionChangeEvent(self: *T, frame: *Frame) !void {
- const event = try Event.init("selectionchange", .{ .bubbles = true }, frame.page);
- try frame._event_manager.dispatch(self.asElement().asEventTarget(), event);
+ // https://w3c.github.io/selection-api/#scheduling-selectionchange-event
+ fn scheduleSelectionChange(self: *T, frame: *Frame) !void {
+ if (self._selectionchange_pending) {
+ return;
+ }
+ try frame.queueElementEvent(Factory.protoOf(self), .selectionchange);
+ self._selectionchange_pending = true;
}
fn dispatchInputEvent(self: *T, data: ?[]const u8, input_type: []const u8, frame: *Frame) !void {