browser.interactive: optimize role checks with StaticStringMap

This commit is contained in:
Adrià Arrufat
2026-03-18 20:10:15 +09:00
parent cbab0b712a
commit 694aac5ce8

View File

@@ -254,28 +254,51 @@ pub fn classifyInteractivity(
} }
pub fn isInteractiveRole(role: []const u8) bool { pub fn isInteractiveRole(role: []const u8) bool {
const interactive_roles = [_][]const u8{ const MAX_LEN = "menuitemcheckbox".len;
"button", "link", "tab", "menuitem", if (role.len > MAX_LEN) return false;
"menuitemcheckbox", "menuitemradio", "switch", "checkbox", var buf: [MAX_LEN]u8 = undefined;
"radio", "slider", "spinbutton", "searchbox", const lowered = std.ascii.lowerString(&buf, role);
"combobox", "option", "treeitem", "textbox", const interactive_roles = std.StaticStringMap(void).initComptime(.{
"listbox", "iframe", .{ "button", {} },
}; .{ "checkbox", {} },
return for (interactive_roles) |r| { .{ "combobox", {} },
if (std.ascii.eqlIgnoreCase(role, r)) break true; .{ "iframe", {} },
} else false; .{ "link", {} },
.{ "listbox", {} },
.{ "menuitem", {} },
.{ "menuitemcheckbox", {} },
.{ "menuitemradio", {} },
.{ "option", {} },
.{ "radio", {} },
.{ "searchbox", {} },
.{ "slider", {} },
.{ "spinbutton", {} },
.{ "switch", {} },
.{ "tab", {} },
.{ "textbox", {} },
.{ "treeitem", {} },
});
return interactive_roles.has(lowered);
} }
pub fn isContentRole(role: []const u8) bool { pub fn isContentRole(role: []const u8) bool {
const content_roles = [_][]const u8{ const MAX_LEN = "columnheader".len;
"heading", "cell", "gridcell", if (role.len > MAX_LEN) return false;
"columnheader", "rowheader", "listitem", var buf: [MAX_LEN]u8 = undefined;
"article", "region", "main", const lowered = std.ascii.lowerString(&buf, role);
"navigation", const content_roles = std.StaticStringMap(void).initComptime(.{
}; .{ "article", {} },
return for (content_roles) |r| { .{ "cell", {} },
if (std.ascii.eqlIgnoreCase(role, r)) break true; .{ "columnheader", {} },
} else false; .{ "gridcell", {} },
.{ "heading", {} },
.{ "listitem", {} },
.{ "main", {} },
.{ "navigation", {} },
.{ "region", {} },
.{ "rowheader", {} },
});
return content_roles.has(lowered);
} }
fn getRole(el: *Element) ?[]const u8 { fn getRole(el: *Element) ?[]const u8 {