Lightpanda: the headless browser designed for AI and automation
webapi: prevent click() from triggering click()
Karl Seguine2e8d4c7303f
diff --git a/src/browser/tests/element/html/input_click.html b/src/browser/tests/element/html/input_click.htmlindex de0efff90..6814fbc3c 100644--- a/src/browser/tests/element/html/input_click.html+++ b/src/browser/tests/element/html/input_click.html@@ -281,3 +281,30 @@testing.expectEqual(false, r3.checked);}</script>++<script id="nested_click_is_noop">+ {+ // click() while a click() on the same element is in progress is a no-op+ // (the "click in progress" flag); a re-entrant handler must not recurse.+ const cb = document.createElement('input');+ cb.type = 'checkbox';+ document.body.appendChild(cb);++ let clicks = 0;+ cb.addEventListener('click', () => {+ clicks++;+ cb.click();+ });++ cb.click();+ testing.expectEqual(1, clicks, 'nested click() should not dispatch');+ testing.expectEqual(true, cb.checked);++ // The flag is cleared afterwards, so a fresh click() works again.+ cb.click();+ testing.expectEqual(2, clicks);+ testing.expectEqual(false, cb.checked);++ document.body.removeChild(cb);+ }+</script>diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 36ce60df2..6bb7ee479 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -139,7 +139,14 @@ pub const Namespace = enum(u8) {pub const Flags = packed struct(u8) {shadow_host: bool = false,customized_builtin: bool = false,- _unused: u6 = 0,++ // Prevents nested clicks (which have a specific spec-compliant behavior+ // compared to other events). If this bit can be more useful for something+ // else, a stack in EventManager (for click-specifically) is an alterantive+ // approach+ click_in_progress: bool = false,++ _unused: u5 = 0,};_type: Type,diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zigindex 8940a21a7..0b4333085 100644--- a/src/browser/webapi/element/Html.zig+++ b/src/browser/webapi/element/Html.zig@@ -402,6 +402,13 @@ pub fn click(self: *HtmlElement, frame: *Frame) !void {else => {},}+ const flags = &self.asElement()._flags;+ if (flags.click_in_progress) {+ return;+ }+ flags.click_in_progress = true;+ defer flags.click_in_progress = false;+const event = (try @import("../event/MouseEvent.zig").init("click", .{.bubbles = true,.cancelable = true,