click on focusable elements calls focus() with events

handleClick() was setting _active_element directly, bypassing
Element.focus() — so no blur/focus events fired on click.
Now calls focus() for input, button, select, textarea, anchor.
This commit is contained in:
egrs
2026-02-18 15:08:40 +01:00
parent 5391854c82
commit b2c030140c
2 changed files with 51 additions and 4 deletions

View File

@@ -3045,21 +3045,25 @@ pub fn handleClick(self: *Page, target: *Node) !void {
return; return;
} }
try element.focus(self);
try self.scheduleNavigation(href, .{ try self.scheduleNavigation(href, .{
.reason = .script, .reason = .script,
.kind = .{ .push = null }, .kind = .{ .push = null },
}, .anchor); }, .anchor);
}, },
.input => |input| switch (input._input_type) { .input => |input| {
.submit => return self.submitForm(element, input.getForm(self), .{}), try element.focus(self);
else => self.window._document._active_element = element, if (input._input_type == .submit) {
return self.submitForm(element, input.getForm(self), .{});
}
}, },
.button => |button| { .button => |button| {
try element.focus(self);
if (std.mem.eql(u8, button.getType(), "submit")) { if (std.mem.eql(u8, button.getType(), "submit")) {
return self.submitForm(element, button.getForm(self), .{}); return self.submitForm(element, button.getForm(self), .{});
} }
}, },
.select, .textarea => self.window._document._active_element = element, .select, .textarea => try element.focus(self),
else => {}, else => {},
} }
} }

View File

@@ -88,3 +88,46 @@
testing.expectEqual(focused, document.activeElement); testing.expectEqual(focused, document.activeElement);
} }
</script> </script>
<script id="click_focuses_element">
{
const input1 = $('#input1');
const input2 = $('#input2');
if (document.activeElement) {
document.activeElement.blur();
}
let focusCount = 0;
let blurCount = 0;
input1.addEventListener('focus', () => focusCount++);
input1.addEventListener('blur', () => blurCount++);
input2.addEventListener('focus', () => focusCount++);
// Click input1 — should focus it and fire focus event
input1.click();
testing.expectEqual(input1, document.activeElement);
testing.expectEqual(1, focusCount);
testing.expectEqual(0, blurCount);
// Click input2 — should move focus, fire blur on input1 and focus on input2
input2.click();
testing.expectEqual(input2, document.activeElement);
testing.expectEqual(2, focusCount);
testing.expectEqual(1, blurCount);
}
</script>
<script id="click_focuses_button">
{
const btn = $('#btn1');
if (document.activeElement) {
document.activeElement.blur();
}
btn.click();
testing.expectEqual(btn, document.activeElement);
}
</script>