Lightpanda: the headless browser designed for AI and automation
find first editable position when there isn't already a selection
Karl Seguinb301da8936a1
diff --git a/src/browser/dump.zig b/src/browser/dump.zigindex a9b3be26c..b82c06670 100644--- a/src/browser/dump.zig+++ b/src/browser/dump.zig@@ -449,7 +449,7 @@ fn shouldSerializeShadow(shadow: *const Node.ShadowRoot, declarative: Opts.Shadoreturn false;}-fn isVoidElement(el: *Node.Element) bool {+pub fn isVoidElement(el: *Node.Element) bool {if (el._namespace != .html) {// only html has void tagsreturn false;diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 46ce5be78..36a4191d6 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -28,6 +28,7 @@ const lp = @import("lightpanda");const URL = @import("../URL.zig");const Frame = @import("../Frame.zig");const js = @import("../js/js.zig");+const dump = @import("../dump.zig");const Node = @import("../webapi/Node.zig");const Event = @import("../webapi/Event.zig");@@ -1657,8 +1658,8 @@ pub fn applyEdit(frame: *Frame, ctl: anytype, edit: Edit) !bool {/// through beforeinput like a control's edit, then replaces the selection and/// leaves the caret after the text. The events target the editing host of/// `target`, as in Chrome. A selection outside the editable content of the-/// host puts the text at the start of the host, where Chrome places the caret-/// when the host is focused. Only text insertion is supported: line breaks,+/// host puts the text at the host's first editable position, where Chrome+/// places the caret when the host is focused. Only text insertion is supported: line breaks,/// deletion and formatting need an editing pipeline. Returns whether the edit/// happened.fn insertIntoEditingHost(frame: *Frame, target: *Element, text: []const u8) !bool {@@ -1673,8 +1674,7 @@ fn insertIntoEditingHost(frame: *Frame, target: *Element, text: []const u8) !boo}const selection = &frame.document._selection;- var container = host;- var offset: u32 = 0;+ var container: *Node, var offset: u32 = firstCaretPosition(host);if (selection._range) |range| {const abstract = range.asAbstractRange();if (isEditableIn(host, abstract.getStartContainer()) and isEditableIn(host, abstract.getEndContainer())) {@@ -1738,6 +1738,36 @@ fn insertIntoEditingHost(frame: *Frame, target: *Element, text: []const u8) !booreturn true;}+// Where Chrome puts the caret when an editing host is focused: its first+// editable position, so inside a leading <p> rather than before it. Skips+// comments and whitespace-only text, and stops at a void or non-editable+// element.+fn firstCaretPosition(host: *Node) struct { *Node, u32 } {+ var container = host;+ descend: while (true) {+ var index: u32 = 0;+ var child = container.firstChild();+ while (child) |c| : ({+ child = c.nextSibling();+ index += 1;+ }) {+ if (c.is(Node.CData.Text)) |t| {+ if (std.mem.trim(u8, t.asCData().getData().str(), " \t\n\r\x0c").len == 0) {+ continue;+ }+ return .{ c, 0 };+ }+ const el = c.is(Element) orelse continue;+ if (el.isEditable() == false or dump.isVoidElement(el)) {+ return .{ container, index };+ }+ container = c;+ continue :descend;+ }+ return .{ container, 0 };+ }+}+// Whether `node` is inside `host` and editable: the nearest contenteditable// attribute of its element (or of its parent, for a text node) allows editing.fn isEditableIn(host: *Node, node: *Node) bool {diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 00eb1f570..34d8aa1e6 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -3622,6 +3622,41 @@ test "cdp.input: typing into an editing host without a selection inserts at its);}+test "cdp.input: typing into a focused editing host goes into its first paragraph" {+ try expectEditingHostInput(+ \\document.body.insertAdjacentHTML('beforeend', '<div id="host" contenteditable><p id="p">hello</p><p>x</p></div>');+ \\const host = document.getElementById('host');+ \\const p = document.getElementById('p');+ \\host.focus();+ , .{ .key = "c" },+ \\host.innerHTML === '<p id="p">chello</p><p>x</p>' &&+ \\window.types() === 'keypress:host,beforeinput:host,textInput:host,input:host' &&+ \\getSelection().anchorNode === p.firstChild && getSelection().anchorOffset === 1+ );+}++test "cdp.input: the first caret position of an editing host skips whitespace and descends" {+ try expectEditingHostInput(+ \\document.body.insertAdjacentHTML('beforeend', '<div id="host" contenteditable>\n <!--c--><p>\n<b id="b">bold</b> rest</p></div>');+ \\const host = document.getElementById('host');+ \\const b = document.getElementById('b');+ \\host.focus();+ , .{ .insert_text = "X" },+ \\b.textContent === 'Xbold' &&+ \\getSelection().anchorNode === b.firstChild && getSelection().anchorOffset === 1+ );+}++test "cdp.input: the first caret position of an editing host stops at a void element" {+ try expectEditingHostInput(+ \\document.body.insertAdjacentHTML('beforeend', '<div id="host" contenteditable><img>t</div>');+ \\const host = document.getElementById('host');+ \\host.focus();+ , .{ .insert_text = "X" },+ \\host.innerHTML === 'X<img>t'+ );+}+test "cdp.input: text typed into an editing host's descendant targets the host" {try expectEditingHostInput(\\document.body.insertAdjacentHTML('beforeend', '<div id="host" contenteditable><span id="s" tabindex="0">ab</span></div>');