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 {
|
fn dump(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| {
|
||||||
switch (el.getTag()) {
|
const tag = el.getTag();
|
||||||
.script, .style, .meta, .link, .noscript, .svg, .head, .title => return,
|
if (tag.isMetadata() or tag == .svg) return;
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
|
|
||||||
// CSS display: none visibility check (inline style only for now)
|
// CSS display: none visibility check (inline style only for now)
|
||||||
if (el.getAttributeSafe(comptime lp.String.wrap("style"))) |style| {
|
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();
|
node_name = el.getTagNameLower();
|
||||||
|
|
||||||
const ax_role = std.meta.stringToEnum(AXNode.AXRole, role) orelse .none;
|
const ax_role = std.meta.stringToEnum(AXNode.AXRole, role) orelse .none;
|
||||||
if (ax_role.isInteractive()) {
|
is_interactive = ax_role.isInteractive();
|
||||||
is_interactive = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const event_target = node.asEventTarget();
|
const event_target = node.asEventTarget();
|
||||||
if (self.page._event_manager.hasListener(event_target, "click") or
|
if (self.page._event_manager.hasListener(event_target, "click") or
|
||||||
|
|||||||
@@ -47,13 +47,6 @@ const State = struct {
|
|||||||
last_char_was_newline: bool = true,
|
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 {
|
fn shouldAddSpacing(tag: Element.Tag) bool {
|
||||||
return switch (tag) {
|
return switch (tag) {
|
||||||
.p, .h1, .h2, .h3, .h4, .h5, .h6, .blockquote, .pre, .table => true,
|
.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 {
|
fn isVisibleElement(el: *Element) bool {
|
||||||
return switch (el.getTag()) {
|
const tag = el.getTag();
|
||||||
.script, .style, .noscript, .template, .head, .meta, .link, .title, .svg => false,
|
return !tag.isMetadata() and tag != .svg;
|
||||||
else => true,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getAnchorLabel(el: *Element) ?[]const u8 {
|
fn getAnchorLabel(el: *Element) ?[]const u8 {
|
||||||
@@ -113,7 +104,7 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {
|
|||||||
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| {
|
||||||
if (isBlock(el.getTag())) return true;
|
if (el.getTag().isBlock()) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -187,7 +178,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
|
|||||||
// --- Opening Tag Logic ---
|
// --- Opening Tag Logic ---
|
||||||
|
|
||||||
// Ensure block elements start on a new line (double newline for paragraphs etc)
|
// 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);
|
try ensureNewline(state, writer);
|
||||||
if (shouldAddSpacing(tag)) {
|
if (shouldAddSpacing(tag)) {
|
||||||
try writer.writeByte('\n');
|
try writer.writeByte('\n');
|
||||||
@@ -426,7 +417,7 @@ fn renderElement(el: *Element, state: *State, writer: *std.Io.Writer, page: *Pag
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Post-block newlines
|
// Post-block newlines
|
||||||
if (isBlock(tag) and !state.in_table) {
|
if (tag.isBlock() and !state.in_table) {
|
||||||
try ensureNewline(state, writer);
|
try ensureNewline(state, writer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1588,6 +1588,50 @@ pub const Tag = enum {
|
|||||||
else => tag,
|
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 {
|
pub const JsApi = struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user