webapi: move tag category logic to Tag enum

This commit is contained in:
Adrià Arrufat
2026-03-06 16:31:28 +09:00
parent e0f0b9f210
commit 45705a3e29
3 changed files with 52 additions and 21 deletions

View File

@@ -79,10 +79,8 @@ fn getXPathSegment(self: @This(), node: *Node) ![]const u8 {
fn dump(self: Self, node: *Node, jw: *std.json.Stringify, parent_xpath: []const u8) !void {
// 1. Skip non-content nodes
if (node.is(Element)) |el| {
switch (el.getTag()) {
.script, .style, .meta, .link, .noscript, .svg, .head, .title => return,
else => {},
}
const tag = el.getTag();
if (tag.isMetadata() or tag == .svg) return;
// CSS display: none visibility check (inline style only for now)
if (el.getAttributeSafe(comptime lp.String.wrap("style"))) |style| {
@@ -118,9 +116,7 @@ fn dump(self: Self, node: *Node, jw: *std.json.Stringify, parent_xpath: []const
node_name = el.getTagNameLower();
const ax_role = std.meta.stringToEnum(AXNode.AXRole, role) orelse .none;
if (ax_role.isInteractive()) {
is_interactive = true;
}
is_interactive = ax_role.isInteractive();
const event_target = node.asEventTarget();
if (self.page._event_manager.hasListener(event_target, "click") or

View File

@@ -47,13 +47,6 @@ const State = struct {
last_char_was_newline: bool = true,
};
fn isBlock(tag: Element.Tag) bool {
return switch (tag) {
.p, .div, .section, .article, .main, .header, .footer, .nav, .aside, .h1, .h2, .h3, .h4, .h5, .h6, .ul, .ol, .blockquote, .pre, .table, .hr => true,
else => false,
};
}
fn shouldAddSpacing(tag: Element.Tag) bool {
return switch (tag) {
.p, .h1, .h2, .h3, .h4, .h5, .h6, .blockquote, .pre, .table => true,
@@ -100,10 +93,8 @@ fn isSignificantText(node: *Node) bool {
}
fn isVisibleElement(el: *Element) bool {
return switch (el.getTag()) {
.script, .style, .noscript, .template, .head, .meta, .link, .title, .svg => false,
else => true,
};
const tag = el.getTag();
return !tag.isMetadata() and tag != .svg;
}
fn getAnchorLabel(el: *Element) ?[]const u8 {
@@ -113,7 +104,7 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {
fn hasBlockDescendant(root: *Node) bool {
var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{});
while (tw.next()) |el| {
if (isBlock(el.getTag())) return true;
if (el.getTag().isBlock()) return true;
}
return false;
}
@@ -187,7 +178,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
// --- Opening Tag Logic ---
// Ensure block elements start on a new line (double newline for paragraphs etc)
if (isBlock(tag) and !state.in_table) {
if (tag.isBlock() and !state.in_table) {
try ensureNewline(state, writer);
if (shouldAddSpacing(tag)) {
try writer.writeByte('\n');
@@ -426,7 +417,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
}
// Post-block newlines
if (isBlock(tag) and !state.in_table) {
if (tag.isBlock() and !state.in_table) {
try ensureNewline(state, writer);
}
}

View File

@@ -1588,6 +1588,50 @@ pub const Tag = enum {
else => tag,
};
}
pub fn isBlock(self: Tag) bool {
return switch (self) {
.p,
.div,
.section,
.article,
.main,
.header,
.footer,
.nav,
.aside,
.h1,
.h2,
.h3,
.h4,
.h5,
.h6,
.ul,
.ol,
.blockquote,
.pre,
.table,
.hr,
=> true,
else => false,
};
}
pub fn isMetadata(self: Tag) bool {
return switch (self) {
.script,
.style,
.meta,
.link,
.noscript,
.head,
.title,
.base,
.template,
=> true,
else => false,
};
}
};
pub const JsApi = struct {