SemanticTree: use payload captures for CData.Text checks

Improves conciseness and idiomatic Zig style by replacing .is(CData.Text) != null and .as() with direct payload captures in if statements.
This commit is contained in:
Adrià Arrufat
2026-03-11 16:39:59 +09:00
parent 37735b1caa
commit 65d7a39554

View File

@@ -99,8 +99,7 @@ fn walk(self: @This(), node: *Node, xpath_buffer: *std.ArrayList(u8), parent_nam
if (el.is(Element.Html)) |html_el| { if (el.is(Element.Html)) |html_el| {
if (html_el.getHidden()) return; if (html_el.getHidden()) return;
} }
} else if (node.is(CData.Text) != null) { } else if (node.is(CData.Text)) |text_node| {
const text_node = node.as(CData.Text);
const text = text_node.getWholeText(); const text = text_node.getWholeText();
if (isAllWhitespace(text)) { if (isAllWhitespace(text)) {
return; return;
@@ -266,7 +265,7 @@ fn appendXPathSegment(node: *Node, writer: anytype, index: usize) !void {
if (node.is(Element)) |el| { if (node.is(Element)) |el| {
const tag = el.getTagNameLower(); const tag = el.getTagNameLower();
try std.fmt.format(writer, "/{s}[{d}]", .{ tag, index }); try std.fmt.format(writer, "/{s}[{d}]", .{ tag, index });
} else if (node.is(CData.Text) != null) { } else if (node.is(CData.Text)) |_| {
try std.fmt.format(writer, "/text()[{d}]", .{index}); try std.fmt.format(writer, "/text()[{d}]", .{index});
} }
} }
@@ -338,8 +337,7 @@ const JsonVisitor = struct {
} }
try self.jw.endArray(); try self.jw.endArray();
} }
} else if (node.is(CData.Text) != null) { } else if (node.is(CData.Text)) |text_node| {
const text_node = node.is(CData.Text).?;
try self.jw.objectField("nodeType"); try self.jw.objectField("nodeType");
try self.jw.write(3); try self.jw.write(3);
try self.jw.objectField("nodeValue"); try self.jw.objectField("nodeValue");
@@ -401,8 +399,7 @@ const TextVisitor = struct {
if (n.len > 0) { if (n.len > 0) {
try self.writer.writeAll(n); try self.writer.writeAll(n);
} }
} else if (node.is(CData.Text) != null) { } else if (node.is(CData.Text)) |text_node| {
const text_node = node.is(CData.Text).?;
const trimmed = std.mem.trim(u8, text_node.getWholeText(), " \t\r\n"); const trimmed = std.mem.trim(u8, text_node.getWholeText(), " \t\r\n");
if (trimmed.len > 0) { if (trimmed.len > 0) {
try self.writer.writeAll(trimmed); try self.writer.writeAll(trimmed);