Skip to content

Commit fd6d038

Browse files
Merge pull request #1152 from lightpanda-io/cdp-inserttext
cdp: add input.insertText
2 parents 29f0e71 + 14a2312 commit fd6d038

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/browser/dom/document.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const std = @import("std");
20+
const log = @import("../../log.zig");
2021

2122
const js = @import("../js/js.zig");
2223
const parser = @import("../netsurf.zig");
@@ -313,6 +314,11 @@ pub const Document = struct {
313314
const state = try page.getOrCreateNodeState(@ptrCast(@alignCast(self)));
314315
state.adopted_style_sheets = try sheets.persist();
315316
}
317+
318+
pub fn _hasFocus(_: *parser.Document) bool {
319+
log.debug(.web_api, "not implemented", .{ .feature = "Document hasFocus" });
320+
return true;
321+
}
316322
};
317323

318324
const testing = @import("../../testing.zig");

src/browser/html/elements.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ pub const HTMLInputElement = struct {
732732
pub fn set_value(self: *parser.Input, value: []const u8) !void {
733733
try parser.inputSetValue(self, value);
734734
}
735+
pub fn _select(_: *parser.Input) void {
736+
log.debug(.web_api, "not implemented", .{ .feature = "HTMLInputElement select" });
737+
}
735738
};
736739

737740
pub const HTMLLIElement = struct {

src/browser/page.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,31 @@ pub const Page = struct {
10131013
}
10141014
}
10151015

1016+
// insertText is a shortcut to insert text into the active element.
1017+
pub fn insertText(self: *Page, v: []const u8) !void {
1018+
const Document = @import("dom/document.zig").Document;
1019+
const element = (try Document.getActiveElement(@ptrCast(self.window.document), self)) orelse return;
1020+
const node = parser.elementToNode(element);
1021+
1022+
const tag = (try parser.nodeHTMLGetTagType(node)) orelse return;
1023+
switch (tag) {
1024+
.input => {
1025+
const input_type = try parser.inputGetType(@ptrCast(element));
1026+
if (std.mem.eql(u8, input_type, "text")) {
1027+
const value = try parser.inputGetValue(@ptrCast(element));
1028+
const new_value = try std.mem.concat(self.arena, u8, &.{ value, v });
1029+
try parser.inputSetValue(@ptrCast(element), new_value);
1030+
}
1031+
},
1032+
.textarea => {
1033+
const value = try parser.textareaGetValue(@ptrCast(node));
1034+
const new_value = try std.mem.concat(self.arena, u8, &.{ value, v });
1035+
try parser.textareaSetValue(@ptrCast(node), new_value);
1036+
},
1037+
else => {},
1038+
}
1039+
}
1040+
10161041
// We cannot navigate immediately as navigating will delete the DOM tree,
10171042
// which holds this event's node.
10181043
// As such we schedule the function to be called as soon as possible.

src/cdp/domains/input.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ pub fn processMessage(cmd: anytype) !void {
2323
const action = std.meta.stringToEnum(enum {
2424
dispatchKeyEvent,
2525
dispatchMouseEvent,
26+
insertText,
2627
}, cmd.input.action) orelse return error.UnknownMethod;
2728

2829
switch (action) {
2930
.dispatchKeyEvent => return dispatchKeyEvent(cmd),
3031
.dispatchMouseEvent => return dispatchMouseEvent(cmd),
32+
.insertText => return insertText(cmd),
3133
}
3234
}
3335

@@ -115,6 +117,20 @@ fn dispatchMouseEvent(cmd: anytype) !void {
115117
// result already sent
116118
}
117119

120+
// https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-insertText
121+
fn insertText(cmd: anytype) !void {
122+
const params = (try cmd.params(struct {
123+
text: []const u8, // The text to insert
124+
})) orelse return error.InvalidParams;
125+
126+
const bc = cmd.browser_context orelse return;
127+
const page = bc.session.currentPage() orelse return;
128+
129+
try page.insertText(params.text);
130+
131+
try cmd.sendResult(null, .{});
132+
}
133+
118134
fn clickNavigate(cmd: anytype, uri: std.Uri) !void {
119135
const bc = cmd.browser_context.?;
120136

0 commit comments

Comments
 (0)