mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-22 04:34:44 +00:00
webapi: move tag category logic to Tag enum
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user