markdown: use node.is() for type checking and casting

This commit is contained in:
Adrià Arrufat
2026-02-20 08:14:15 +09:00
parent 423034d5c4
commit 68d5edca60

View File

@@ -19,6 +19,7 @@
const std = @import("std"); const std = @import("std");
const Page = @import("Page.zig"); const Page = @import("Page.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");
@@ -68,24 +69,23 @@ fn isLayoutBlock(tag: Element.Tag) bool {
fn isStandaloneAnchor(el: *Element) bool { fn isStandaloneAnchor(el: *Element) bool {
const node = el.asNode(); const node = el.asNode();
const parent = node.parentNode() orelse return false; const parent = node.parentNode() orelse return false;
const parent_el = parent.is(Element) orelse return false;
if (parent._type != .element) return false;
const parent_el = parent.as(Element);
if (!isLayoutBlock(parent_el.getTag())) return false; if (!isLayoutBlock(parent_el.getTag())) return false;
var prev = node.previousSibling(); var prev = node.previousSibling();
while (prev) |p| : (prev = p.previousSibling()) { while (prev) |p| : (prev = p.previousSibling()) {
if (isSignificantText(p)) return false; if (isSignificantText(p)) return false;
if (p._type == .element) { if (p.is(Element)) |pe| {
if (isVisibleElement(p.as(Element))) break; if (isVisibleElement(pe)) break;
} }
} }
var next = node.nextSibling(); var next = node.nextSibling();
while (next) |n| : (next = n.nextSibling()) { while (next) |n| : (next = n.nextSibling()) {
if (isSignificantText(n)) return false; if (isSignificantText(n)) return false;
if (n._type == .element) { if (n.is(Element)) |ne| {
if (isVisibleElement(n.as(Element))) break; if (isVisibleElement(ne)) break;
} }
} }
@@ -93,12 +93,8 @@ fn isStandaloneAnchor(el: *Element) bool {
} }
fn isSignificantText(node: *Node) bool { fn isSignificantText(node: *Node) bool {
if (node._type != .cdata) return false; const text = node.is(Node.CData.Text) orelse return false;
const cd = node.as(Node.CData); return !isAllWhitespace(text.getWholeText());
if (node.is(Node.CData.Text)) |_| {
return !isAllWhitespace(cd.getData());
}
return false;
} }
fn isVisibleElement(el: *Element) bool { fn isVisibleElement(el: *Element) bool {
@@ -117,8 +113,7 @@ fn isAllWhitespace(text: []const u8) bool {
fn hasBlockDescendant(node: *Node) bool { fn hasBlockDescendant(node: *Node) bool {
var it = node.childrenIterator(); var it = node.childrenIterator();
while (it.next()) |child| { while (it.next()) |child| {
if (child._type == .element) { if (child.is(Element)) |el| {
const el = child.as(Element);
if (isBlock(el.getTag())) return true; if (isBlock(el.getTag())) return true;
if (hasBlockDescendant(child)) return true; if (hasBlockDescendant(child)) return true;
} }