mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-28 15:40:04 +00:00
Fix Form.requestSubmit(submitter) not setting SubmitEvent.submitter
Create SubmitEvent type and use it in submitForm() so that e.submitter is correctly set when requestSubmit(submitter) is called. Fixes #1982 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
95
src/browser/webapi/event/SubmitEvent.zig
Normal file
95
src/browser/webapi/event/SubmitEvent.zig
Normal 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, .{});
|
||||
};
|
||||
Reference in New Issue
Block a user