mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
cdp: add input.insertText
This commit is contained in:
@@ -1013,6 +1013,31 @@ pub const Page = struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// insertText is a shortcut to insert text into the active element.
|
||||||
|
pub fn insertText(self: *Page, v: []const u8) !void {
|
||||||
|
const Document = @import("dom/document.zig").Document;
|
||||||
|
const element = (try Document.getActiveElement(@ptrCast(self.window.document), self)) orelse return;
|
||||||
|
const node = parser.elementToNode(element);
|
||||||
|
|
||||||
|
const tag = (try parser.nodeHTMLGetTagType(node)) orelse return;
|
||||||
|
switch (tag) {
|
||||||
|
.input => {
|
||||||
|
const input_type = try parser.inputGetType(@ptrCast(element));
|
||||||
|
if (std.mem.eql(u8, input_type, "text")) {
|
||||||
|
const value = try parser.inputGetValue(@ptrCast(element));
|
||||||
|
const new_value = try std.mem.concat(self.arena, u8, &.{ value, v });
|
||||||
|
try parser.inputSetValue(@ptrCast(element), new_value);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.textarea => {
|
||||||
|
const value = try parser.textareaGetValue(@ptrCast(node));
|
||||||
|
const new_value = try std.mem.concat(self.arena, u8, &.{ value, v });
|
||||||
|
try parser.textareaSetValue(@ptrCast(node), new_value);
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// We cannot navigate immediately as navigating will delete the DOM tree,
|
// We cannot navigate immediately as navigating will delete the DOM tree,
|
||||||
// which holds this event's node.
|
// which holds this event's node.
|
||||||
// As such we schedule the function to be called as soon as possible.
|
// As such we schedule the function to be called as soon as possible.
|
||||||
|
|||||||
@@ -23,11 +23,13 @@ pub fn processMessage(cmd: anytype) !void {
|
|||||||
const action = std.meta.stringToEnum(enum {
|
const action = std.meta.stringToEnum(enum {
|
||||||
dispatchKeyEvent,
|
dispatchKeyEvent,
|
||||||
dispatchMouseEvent,
|
dispatchMouseEvent,
|
||||||
|
insertText,
|
||||||
}, cmd.input.action) orelse return error.UnknownMethod;
|
}, cmd.input.action) orelse return error.UnknownMethod;
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
.dispatchKeyEvent => return dispatchKeyEvent(cmd),
|
.dispatchKeyEvent => return dispatchKeyEvent(cmd),
|
||||||
.dispatchMouseEvent => return dispatchMouseEvent(cmd),
|
.dispatchMouseEvent => return dispatchMouseEvent(cmd),
|
||||||
|
.insertText => return insertText(cmd),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,6 +117,20 @@ fn dispatchMouseEvent(cmd: anytype) !void {
|
|||||||
// result already sent
|
// result already sent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
|
||||||
|
fn insertText(cmd: anytype) !void {
|
||||||
|
const params = (try cmd.params(struct {
|
||||||
|
text: []const u8, // The text to insert
|
||||||
|
})) orelse return error.InvalidParams;
|
||||||
|
|
||||||
|
const bc = cmd.browser_context orelse return;
|
||||||
|
const page = bc.session.currentPage() orelse return;
|
||||||
|
|
||||||
|
try page.insertText(params.text);
|
||||||
|
|
||||||
|
try cmd.sendResult(null, .{});
|
||||||
|
}
|
||||||
|
|
||||||
fn clickNavigate(cmd: anytype, uri: std.Uri) !void {
|
fn clickNavigate(cmd: anytype, uri: std.Uri) !void {
|
||||||
const bc = cmd.browser_context.?;
|
const bc = cmd.browser_context.?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user