Merge pull request #1984 from navidemad/fix-submit-event-submitter
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / wba-demo-scripts (push) Has been cancelled
e2e-test / wba-test (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig fmt (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

Fix Form.requestSubmit(submitter) not setting SubmitEvent.submitter
This commit is contained in:
Halil Durak
2026-03-25 15:39:57 +03:00
committed by GitHub
5 changed files with 142 additions and 1 deletions

View File

@@ -61,6 +61,7 @@ const IntersectionObserver = @import("webapi/IntersectionObserver.zig");
const CustomElementDefinition = @import("webapi/CustomElementDefinition.zig");
const storage = @import("webapi/storage/storage.zig");
const PageTransitionEvent = @import("webapi/event/PageTransitionEvent.zig");
const SubmitEvent = @import("webapi/event/SubmitEvent.zig");
const NavigationKind = @import("webapi/navigation/root.zig").NavigationKind;
const KeyboardEvent = @import("webapi/event/KeyboardEvent.zig");
const MouseEvent = @import("webapi/event/MouseEvent.zig");
@@ -3487,7 +3488,8 @@ pub fn submitForm(self: *Page, submitter_: ?*Element, form_: ?*Element.Html.Form
};
if (submit_opts.fire_event) {
const submit_event = try Event.initTrusted(comptime .wrap("submit"), .{ .bubbles = true, .cancelable = true }, self);
const submitter_html: ?*HtmlElement = if (submitter_) |s| s.is(HtmlElement) else null;
const submit_event = (try SubmitEvent.initTrusted(comptime .wrap("submit"), .{ .bubbles = true, .cancelable = true, .submitter = submitter_html }, self)).asEvent();
// so submit_event is still valid when we check _prevent_default
submit_event.acquireRef();

View File

@@ -850,6 +850,7 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/event/TextEvent.zig"),
@import("../webapi/event/InputEvent.zig"),
@import("../webapi/event/PromiseRejectionEvent.zig"),
@import("../webapi/event/SubmitEvent.zig"),
@import("../webapi/MessageChannel.zig"),
@import("../webapi/MessagePort.zig"),
@import("../webapi/media/MediaError.zig"),

View File

@@ -463,3 +463,44 @@
});
}
</script>
<!-- Test: requestSubmit(submitter) sets SubmitEvent.submitter -->
<form id="test_form_submitter" action="/should-not-navigate6" method="get">
<button id="submitter_btn" type="submit">Save</button>
</form>
<script id="requestSubmit_sets_submitter">
{
const form = $('#test_form_submitter');
const btn = $('#submitter_btn');
let capturedSubmitter = undefined;
form.addEventListener('submit', (e) => {
e.preventDefault();
capturedSubmitter = e.submitter;
});
form.requestSubmit(btn);
testing.expectEqual(btn, capturedSubmitter);
}
</script>
<!-- Test: requestSubmit() without submitter sets submitter to the form element -->
<form id="test_form_submitter2" action="/should-not-navigate7" method="get">
<input type="text" name="q" value="test">
</form>
<script id="requestSubmit_default_submitter_is_form">
{
const form = $('#test_form_submitter2');
let capturedSubmitter = undefined;
form.addEventListener('submit', (e) => {
e.preventDefault();
capturedSubmitter = e.submitter;
});
form.requestSubmit();
testing.expectEqual(form, capturedSubmitter);
}
</script>

View File

@@ -76,6 +76,7 @@ pub const Type = union(enum) {
pop_state_event: *@import("event/PopStateEvent.zig"),
ui_event: *@import("event/UIEvent.zig"),
promise_rejection_event: *@import("event/PromiseRejectionEvent.zig"),
submit_event: *@import("event/SubmitEvent.zig"),
};
pub const Options = struct {
@@ -174,6 +175,7 @@ pub fn is(self: *Event, comptime T: type) ?*T {
.page_transition_event => |e| return if (T == @import("event/PageTransitionEvent.zig")) e else null,
.pop_state_event => |e| return if (T == @import("event/PopStateEvent.zig")) e else null,
.promise_rejection_event => |e| return if (T == @import("event/PromiseRejectionEvent.zig")) e else null,
.submit_event => |e| return if (T == @import("event/SubmitEvent.zig")) e else null,
.ui_event => |e| {
if (T == @import("event/UIEvent.zig")) {
return e;

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const String = @import("../../../string.zig").String;
const js = @import("../../js/js.zig");
const Page = @import("../../Page.zig");
const Session = @import("../../Session.zig");
const Event = @import("../Event.zig");
const HtmlElement = @import("../element/Html.zig");
const Allocator = std.mem.Allocator;
// https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent
const SubmitEvent = @This();
_proto: *Event,
_submitter: ?*HtmlElement,
const SubmitEventOptions = struct {
submitter: ?*HtmlElement = null,
};
const Options = Event.inheritOptions(SubmitEvent, SubmitEventOptions);
pub fn init(typ: []const u8, opts_: ?Options, page: *Page) !*SubmitEvent {
const arena = try page.getArena(.{ .debug = "SubmitEvent" });
errdefer page.releaseArena(arena);
const type_string = try String.init(arena, typ, .{});
return initWithTrusted(arena, type_string, opts_, false, page);
}
pub fn initTrusted(typ: String, _opts: ?Options, page: *Page) !*SubmitEvent {
const arena = try page.getArena(.{ .debug = "SubmitEvent.trusted" });
errdefer page.releaseArena(arena);
return initWithTrusted(arena, typ, _opts, true, page);
}
fn initWithTrusted(arena: Allocator, typ: String, _opts: ?Options, trusted: bool, page: *Page) !*SubmitEvent {
const opts = _opts orelse Options{};
const event = try page._factory.event(
arena,
typ,
SubmitEvent{
._proto = undefined,
._submitter = opts.submitter,
},
);
Event.populatePrototypes(event, opts, trusted);
return event;
}
pub fn deinit(self: *SubmitEvent, shutdown: bool, session: *Session) void {
self._proto.deinit(shutdown, session);
}
pub fn asEvent(self: *SubmitEvent) *Event {
return self._proto;
}
pub fn getSubmitter(self: *const SubmitEvent) ?*HtmlElement {
return self._submitter;
}
pub const JsApi = struct {
pub const bridge = js.Bridge(SubmitEvent);
pub const Meta = struct {
pub const name = "SubmitEvent";
pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined;
pub const weak = true;
pub const finalizer = bridge.finalizer(SubmitEvent.deinit);
};
pub const constructor = bridge.constructor(SubmitEvent.init, .{});
pub const submitter = bridge.accessor(SubmitEvent.getSubmitter, null, .{});
};