String: refactor isAllWhitespace into String

This commit is contained in:
Adrià Arrufat
2026-03-06 15:52:26 +09:00
parent 248851701f
commit 471ba5baf6
3 changed files with 11 additions and 16 deletions

View File

@@ -20,6 +20,7 @@ const std = @import("std");
const lp = @import("lightpanda"); const lp = @import("lightpanda");
const log = @import("log.zig"); const log = @import("log.zig");
const isAllWhitespace = @import("string.zig").isAllWhitespace;
const Page = lp.Page; const Page = lp.Page;
const CData = @import("browser/webapi/CData.zig"); const CData = @import("browser/webapi/CData.zig");
@@ -28,7 +29,7 @@ const Node = @import("browser/webapi/Node.zig");
const AXNode = @import("cdp/AXNode.zig"); const AXNode = @import("cdp/AXNode.zig");
const CDPNode = @import("cdp/Node.zig"); const CDPNode = @import("cdp/Node.zig");
const SemanticTree = @This(); const Self = @This();
dom_node: *Node, dom_node: *Node,
registry: *CDPNode.Registry, registry: *CDPNode.Registry,
@@ -42,13 +43,6 @@ pub fn jsonStringify(self: @This(), jw: *std.json.Stringify) error{WriteFailed}!
}; };
} }
fn isAllWhitespace(_: @This(), text: []const u8) bool {
for (text) |c| {
if (!std.ascii.isWhitespace(c)) return false;
}
return true;
}
fn getXPathSegment(self: @This(), node: *Node) ![]const u8 { fn getXPathSegment(self: @This(), node: *Node) ![]const u8 {
if (node.is(Element)) |el| { if (node.is(Element)) |el| {
const tag = el.getTagNameLower(); const tag = el.getTagNameLower();
@@ -82,7 +76,7 @@ fn getXPathSegment(self: @This(), node: *Node) ![]const u8 {
return ""; return "";
} }
fn dumpNode(self: @This(), node: *Node, jw: *std.json.Stringify, parent_xpath: []const u8) !void { fn dumpNode(self: Self, node: *Node, jw: *std.json.Stringify, parent_xpath: []const u8) !void {
// 1. Skip non-content nodes // 1. Skip non-content nodes
if (node.is(Element)) |el| { if (node.is(Element)) |el| {
const tag = el.getTagNameLower(); const tag = el.getTagNameLower();
@@ -113,7 +107,7 @@ fn dumpNode(self: @This(), node: *Node, jw: *std.json.Stringify, parent_xpath: [
} else if (node.is(CData.Text) != null) { } else if (node.is(CData.Text) != null) {
const text_node = node.is(CData.Text).?; const text_node = node.is(CData.Text).?;
const text = text_node.getWholeText(); const text = text_node.getWholeText();
if (self.isAllWhitespace(text)) { if (isAllWhitespace(text)) {
return; return;
} }
} else if (node._type != .document and node._type != .document_fragment) { } else if (node._type != .document and node._type != .document_fragment) {

View File

@@ -24,6 +24,7 @@ const TreeWalker = @import("webapi/TreeWalker.zig");
const CData = @import("webapi/CData.zig"); const CData = @import("webapi/CData.zig");
const Element = @import("webapi/Element.zig"); const Element = @import("webapi/Element.zig");
const Node = @import("webapi/Node.zig"); const Node = @import("webapi/Node.zig");
const isAllWhitespace = @import("../string.zig").isAllWhitespace;
pub const Opts = struct { pub const Opts = struct {
// Options for future customization (e.g., dialect) // Options for future customization (e.g., dialect)
@@ -109,12 +110,6 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {
return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title")); return el.getAttributeSafe(comptime .wrap("aria-label")) orelse el.getAttributeSafe(comptime .wrap("title"));
} }
fn isAllWhitespace(text: []const u8) bool {
return for (text) |c| {
if (!std.ascii.isWhitespace(c)) break false;
} else true;
}
fn hasBlockDescendant(root: *Node) bool { fn hasBlockDescendant(root: *Node) bool {
var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{}); var tw = TreeWalker.FullExcludeSelf.Elements.init(root, .{});
while (tw.next()) |el| { while (tw.next()) |el| {

View File

@@ -305,6 +305,12 @@ pub const String = packed struct {
} }
}; };
pub fn isAllWhitespace(text: []const u8) bool {
return for (text) |c| {
if (!std.ascii.isWhitespace(c)) break false;
} else true;
}
// Discriminatory type that signals the bridge to use arena instead of call_arena // Discriminatory type that signals the bridge to use arena instead of call_arena
// Use this for strings that need to persist beyond the current call // Use this for strings that need to persist beyond the current call
// The caller can unwrap and store just the underlying .str field // The caller can unwrap and store just the underlying .str field