Lightpanda: the headless browser designed for AI and automation
click: send pointer events to disabled controls and drop stale input state
Adrià Arrufatd76921da1c36
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 2b7ee4509..486a227b8 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -451,6 +451,20 @@ pub fn deinit(self: *Frame) void {frame.deinit();}+ // The Page outlives an iframe or popup that re-navigates: don't leave its+ // pointer state on this document's elements.+ const page = self.page;+ if (page.input_hover_target) |el| {+ if (el.ownerFrame(self) == self) {+ page.input_hover_target = null;+ }+ }+ if (page.input_pointer.down_target) |el| {+ if (el.ownerFrame(self) == self) {+ page.input_pointer.reset();+ }+ }+if (comptime lp.IS_DEBUG) {log.debug(.frame, "frame.deinit", .{ .url = self.url, .type = self._type });}@@ -478,8 +492,6 @@ pub fn deinit(self: *Frame) void {container.detach();}- const page = self.page;-if (self._queued_navigation) |qn| {qn.arena.release();}diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex 3bba0484f..d4366ece4 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -239,7 +239,7 @@ fn emitMouse(/// MouseEvent/PointerEvent.buttons bitmask for a MouseEvent.button value./// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons-pub fn buttonsBitmask(button: i32) u16 {+fn buttonsBitmask(button: i32) u16 {return switch (button) {mouse_button.main => 1,mouse_button.secondary => 2,@@ -345,10 +345,6 @@ pub fn moveSequence(frame: *Frame, target: *Element, g: Gesture) !void {/// Returns whether the gesture's compat mouse events are suppressed: by the/// opening pointerdown, or for a chorded press, `chord_suppressed` carried over.fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed: bool) !bool {- if (target.isDisabled()) {- return true;- }-const starts_gesture = (g.buttons_down & ~buttonsBitmask(g.button)) == 0;// A chorded press is a buttons-mask change (pointermove), not a second// pointerdown: https://www.w3.org/TR/pointerevents3/#chorded-button-interactions@@ -362,7 +358,9 @@ fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed:return suppressed;}- if (!suppressed and !try emitMouse(frame, target, "mousedown", g, g.click_count)) {+ // A disabled control gets the pointer events, contextmenu and auxclick,+ // but no mouse events, click or focus, as in Chrome.+ if (!suppressed and !target.isDisabled() and !try emitMouse(frame, target, "mousedown", g, g.click_count)) {focusForMouseDown(frame, target) catch |err| log.debug(.app, "mousedown focus", .{ .err = err });}// Chrome on Linux and macOS fires contextmenu on press, even when the@@ -376,22 +374,21 @@ fn pressSequence(frame: *Frame, target: *Element, g: Gesture, chord_suppressed:/// A null `click_target` releases without a click, as for a later release in/// a chord.fn releaseSequence(frame: *Frame, up_target: *Element, g: Gesture, suppressed: bool, click_target: ?*Element) !void {- if (up_target.isDisabled()) {- return;- }-_ = try emitPointer(frame, up_target, if (g.buttons_down == 0) "pointerup" else "pointermove", g, 0);- if (g.emit_mouse_compat and !suppressed) {+ if (g.emit_mouse_compat and !suppressed and !up_target.isDisabled()) {_ = try emitMouse(frame, up_target, "mouseup", g, g.click_count);}// clickCount 0 releases without a click, as in Chrome.const click_el = click_target orelse return;- if (!g.emit_mouse_compat or g.click_count == 0 or (click_el != up_target and click_el.isDisabled())) {+ if (!g.emit_mouse_compat or g.click_count == 0) {return;}if (g.button == mouse_button.main) {+ if (click_el.isDisabled()) {+ return;+ }_ = try emitPointer(frame, click_el, "click", g, g.click_count);if (g.click_count % 2 == 0) {_ = try emitMouse(frame, click_el, "dblclick", g, g.click_count);@@ -405,9 +402,6 @@ fn releaseSequence(frame: *Frame, up_target: *Element, g: Gesture, suppressed: b/// off pointerdown/mousedown, not click alone. A focus failure is logged, not/// returned.pub fn triggerClick(frame: *Frame, target: *Element, modifiers: Modifiers) !void {- if (target.isDisabled()) {- return;- }try moveSequence(frame, target, .{ .modifiers = modifiers });const g: Gesture = .{ .click_count = 1, .modifiers = modifiers };diff --git a/src/browser/tests/mcp_actions.html b/src/browser/tests/mcp_actions.htmlindex 50941d429..f0b69d565 100644--- a/src/browser/tests/mcp_actions.html+++ b/src/browser/tests/mcp_actions.html@@ -61,7 +61,7 @@window.disabledEvents = [];const btnDisabled = document.getElementById('btnDisabled');- for (const t of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click']) {+ for (const t of ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click', 'contextmenu', 'auxclick']) {btnDisabled.addEventListener(t, function(e) { window.disabledEvents.push(e.type); });}diff --git a/src/mcp/tools.zig b/src/mcp/tools.zigindex b6d9b917c..9c109611e 100644--- a/src/mcp/tools.zig+++ b/src/mcp/tools.zig@@ -1416,7 +1416,7 @@ test "MCP - Actions: click, fill, scroll, hover, press, selectOption, setChecked\\ 'pointerup:0:0:0:mouse:true', 'mouseup:0:0:1::true', 'click:0:0:1:mouse:true'\\ ]) &&\\ JSON.stringify(window.seqPrevented) === JSON.stringify(['pointerdown', 'pointerup', 'click']) &&- \\ JSON.stringify(window.disabledEvents) === '[]' &&+ \\ JSON.stringify(window.disabledEvents) === JSON.stringify(['pointerdown', 'pointerup']) &&\\ window.focusTargetFocused === true && window.plainBlurred === true &&\\ window.clicked === true && window.inputVal === 'hello' &&\\ window.changed === true && window.selChanged === 'opt2' &&diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex 25b3b464b..176725651 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -1010,7 +1010,9 @@ test "cdp.input: a cancelled pointerdown suppresses mousedown and mouseup acrosstry testing.expect(result.isTrue());}-test "cdp.input: mousePressed and mouseReleased on a disabled button emit no events" {+// A disabled control gets the pointer events, contextmenu and auxclick, but no+// mouse events or click. Matches Chrome.+test "cdp.input: a disabled button gets only pointer and context events" {var ctx = try testing.context();defer ctx.deinit();@@ -1043,9 +1045,21 @@ test "cdp.input: mousePressed and mouseReleased on a disabled button emit no eve.method = "Input.dispatchMouseEvent",.params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "left", .clickCount = 1 },});+ try ctx.processMessage(.{+ .id = 3,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mousePressed", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 1 },+ });+ try ctx.processMessage(.{+ .id = 4,+ .method = "Input.dispatchMouseEvent",+ .params = .{ .type = "mouseReleased", .x = rect_x, .y = rect_y, .button = "right", .clickCount = 1 },+ });const result = try ls.local.compileAndRun(- \\JSON.stringify(window.disabledEvents) === '[]'+ \\JSON.stringify(window.disabledEvents) === JSON.stringify([+ \\ 'pointerdown', 'pointerup', 'pointerdown', 'contextmenu', 'pointerup', 'auxclick'+ \\]), null);try testing.expect(result.isTrue());}@@ -1695,3 +1709,29 @@ test "cdp.input: dispatchKeyEvent Enter clicks buttons and submits once" {try testing.expectEqualSlices(u8, c.expect, got);}}++test "cdp.input: re-navigating an iframe drops the pointer state on its elements" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .url = "cdp/input_iframe.html" });+ const main = bc.mainFrame().?;+ const page = main.page;+ const child = main.child_frames.items[0];+ const text = child.document.getElementById("text", child) orelse unreachable;++ try lp.actions.click(text.asNode(), child);+ try page.input_pointer.press(child, text, .{});+ try testing.expect(page.input_hover_target == text);+ try testing.expect(page.input_pointer.down_target == text);++ var ls: lp.js.Local.Scope = undefined;+ main.js.localScope(&ls);+ defer ls.deinit();+ _ = try ls.local.compileAndRun("document.querySelector('iframe').src = 'iframe/input_child.html?again'", null);+ _ = try bc.session.processQueuedNavigation();++ try testing.expect(page.input_hover_target == null);+ try testing.expect(page.input_pointer.down_target == null);+ try testing.expectEqual(0, page.input_pointer.held);+}