diff --git a/src/browser/webapi/element/html/Button.zig b/src/browser/webapi/element/html/Button.zig index b7edcb66..a6c4fbe6 100644 --- a/src/browser/webapi/element/html/Button.zig +++ b/src/browser/webapi/element/html/Button.zig @@ -144,19 +144,7 @@ pub fn setFormAction(self: *Button, value: []const u8, frame: *Frame) !void { } pub fn getFormEnctype(self: *const Button) []const u8 { - const enctype = self.asConstElement().getAttributeSafe(comptime .wrap("formenctype")) orelse return ""; - - if (std.ascii.eqlIgnoreCase(enctype, "multipart/form-data")) { - return "multipart/form-data"; - } - if (std.ascii.eqlIgnoreCase(enctype, "text/plain")) { - return "text/plain"; - } - if (std.ascii.eqlIgnoreCase(enctype, "application/x-www-form-urlencoded")) { - return "application/x-www-form-urlencoded"; - } - // invalid -> invalid-value default state (application/x-www-form-urlencoded) - return "application/x-www-form-urlencoded"; + return Form.normalizeEnctype(self.asConstElement().getAttributeSafe(comptime .wrap("formenctype")), ""); } pub fn setFormEnctype(self: *Button, value: []const u8, frame: *Frame) !void { @@ -164,16 +152,7 @@ pub fn setFormEnctype(self: *Button, value: []const u8, frame: *Frame) !void { } pub fn getFormMethod(self: *const Button) []const u8 { - const method = self.asConstElement().getAttributeSafe(comptime .wrap("formmethod")) orelse return ""; - - if (std.ascii.eqlIgnoreCase(method, "post")) { - return "post"; - } - if (std.ascii.eqlIgnoreCase(method, "dialog")) { - return "dialog"; - } - // "get" or invalid -> invalid-value default state (get) - return "get"; + return Form.normalizeMethod(self.asConstElement().getAttributeSafe(comptime .wrap("formmethod")), ""); } pub fn setFormMethod(self: *Button, value: []const u8, frame: *Frame) !void { diff --git a/src/browser/webapi/element/html/Form.zig b/src/browser/webapi/element/html/Form.zig index 8473677f..3e3d3585 100644 --- a/src/browser/webapi/element/html/Form.zig +++ b/src/browser/webapi/element/html/Form.zig @@ -54,19 +54,34 @@ pub fn setName(self: *Form, name: []const u8, frame: *Frame) !void { try self.asElement().setAttributeSafe(comptime .wrap("name"), .wrap(name), frame); } -pub fn getMethod(self: *const Form) []const u8 { - const method = self.asConstElement().getAttributeSafe(comptime .wrap("method")) orelse return "get"; - - if (std.ascii.eqlIgnoreCase(method, "post")) { - return "post"; - } - if (std.ascii.eqlIgnoreCase(method, "dialog")) { - return "dialog"; - } - // invalid, or it was get all along +/// Canonicalize the `method` content attribute (or its `formmethod` submitter +/// override) per WHATWG HTML "limited to only known values": +/// - missing → returns `missing_default` +/// - "post" / "dialog" → returns the lowercased keyword +/// - empty / invalid / "get" → returns "get" (invalid-value default) +pub fn normalizeMethod(attr: ?[]const u8, missing_default: []const u8) []const u8 { + const method = attr orelse return missing_default; + if (std.ascii.eqlIgnoreCase(method, "post")) return "post"; + if (std.ascii.eqlIgnoreCase(method, "dialog")) return "dialog"; return "get"; } +/// Canonicalize the `enctype` content attribute (or its `formenctype` submitter +/// override) per WHATWG HTML "limited to only known values": +/// - missing → returns `missing_default` +/// - "multipart/form-data" / "text/plain" → returns the lowercased keyword +/// - empty / invalid / urlencoded → returns "application/x-www-form-urlencoded" +pub fn normalizeEnctype(attr: ?[]const u8, missing_default: []const u8) []const u8 { + const enctype = attr orelse return missing_default; + if (std.ascii.eqlIgnoreCase(enctype, "multipart/form-data")) return "multipart/form-data"; + if (std.ascii.eqlIgnoreCase(enctype, "text/plain")) return "text/plain"; + return "application/x-www-form-urlencoded"; +} + +pub fn getMethod(self: *const Form) []const u8 { + return normalizeMethod(self.asConstElement().getAttributeSafe(comptime .wrap("method")), "get"); +} + pub fn setMethod(self: *Form, method: []const u8, frame: *Frame) !void { try self.asElement().setAttributeSafe(comptime .wrap("method"), .wrap(method), frame); } @@ -120,16 +135,7 @@ pub fn setAcceptCharset(self: *Form, value: []const u8, frame: *Frame) !void { } pub fn getEnctype(self: *const Form) []const u8 { - const enctype = self.asConstElement().getAttributeSafe(comptime .wrap("enctype")) orelse return "application/x-www-form-urlencoded"; - - if (std.ascii.eqlIgnoreCase(enctype, "multipart/form-data")) { - return "multipart/form-data"; - } - if (std.ascii.eqlIgnoreCase(enctype, "text/plain")) { - return "text/plain"; - } - // invalid, or it was application/x-www-form-urlencoded all along - return "application/x-www-form-urlencoded"; + return normalizeEnctype(self.asConstElement().getAttributeSafe(comptime .wrap("enctype")), "application/x-www-form-urlencoded"); } pub fn setEnctype(self: *Form, value: []const u8, frame: *Frame) !void { diff --git a/src/browser/webapi/element/html/Input.zig b/src/browser/webapi/element/html/Input.zig index cfa93ab5..f712e259 100644 --- a/src/browser/webapi/element/html/Input.zig +++ b/src/browser/webapi/element/html/Input.zig @@ -852,19 +852,7 @@ pub fn setFormAction(self: *Input, value: []const u8, frame: *Frame) !void { } pub fn getFormEnctype(self: *const Input) []const u8 { - const enctype = self.asConstElement().getAttributeSafe(comptime .wrap("formenctype")) orelse return ""; - - if (std.ascii.eqlIgnoreCase(enctype, "multipart/form-data")) { - return "multipart/form-data"; - } - if (std.ascii.eqlIgnoreCase(enctype, "text/plain")) { - return "text/plain"; - } - if (std.ascii.eqlIgnoreCase(enctype, "application/x-www-form-urlencoded")) { - return "application/x-www-form-urlencoded"; - } - // invalid -> invalid-value default state (application/x-www-form-urlencoded) - return "application/x-www-form-urlencoded"; + return Form.normalizeEnctype(self.asConstElement().getAttributeSafe(comptime .wrap("formenctype")), ""); } pub fn setFormEnctype(self: *Input, value: []const u8, frame: *Frame) !void { @@ -872,16 +860,7 @@ pub fn setFormEnctype(self: *Input, value: []const u8, frame: *Frame) !void { } pub fn getFormMethod(self: *const Input) []const u8 { - const method = self.asConstElement().getAttributeSafe(comptime .wrap("formmethod")) orelse return ""; - - if (std.ascii.eqlIgnoreCase(method, "post")) { - return "post"; - } - if (std.ascii.eqlIgnoreCase(method, "dialog")) { - return "dialog"; - } - // "get" or invalid -> invalid-value default state (get) - return "get"; + return Form.normalizeMethod(self.asConstElement().getAttributeSafe(comptime .wrap("formmethod")), ""); } pub fn setFormMethod(self: *Input, value: []const u8, frame: *Frame) !void {