Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: implement HTMLTableElement.rows

Jay Hemnania30c717b1d4e
diff --git a/src/browser/tests/element/html/table.html b/src/browser/tests/element/html/table.html
index 6c37dcfcf..feb8d2349 100644
--- a/src/browser/tests/element/html/table.html
+++ b/src/browser/tests/element/html/table.html
@@ -79,3 +79,44 @@
testing.expectEqual('b2', bodies[0].id);
}
</script>
+
+<script id="rows">
+ {
+ // Built by hand, like the deleteRow test, so the tree order differs from
+ // the spec's row order.
+ const table = document.createElement('table');
+ const add = (parent, tag, id) => {
+ const el = document.createElement(tag);
+ if (id) el.id = id;
+ parent.appendChild(el);
+ return el;
+ };
+ add(add(table, 'tfoot'), 'tr', 'foot1');
+ add(table, 'tr', 'top1');
+ add(add(table, 'thead'), 'tr', 'head1');
+ const tbody = add(table, 'tbody');
+ add(tbody, 'tr', 'body1');
+ add(tbody, 'td');
+ // Not rows of this table: a tr inside a caption, a nested table's row.
+ add(add(table, 'caption'), 'tr', 'nested1');
+ add(add(add(tbody.firstChild, 'td'), 'table'), 'tr', 'nested2');
+
+ const ids = () => Array.from(table.rows ?? [], (row) => row.id).join(',');
+
+ // thead rows first, then table-level and tbody rows, then tfoot rows.
+ testing.expectEqual('head1,top1,body1,foot1', ids());
+ testing.expectEqual('HTMLCollection', table.rows?.constructor.name);
+ testing.expectEqual(4, table.rows?.length);
+ testing.expectEqual('body1', table.rows?.[2].id);
+ testing.expectEqual('foot1', table.rows?.namedItem('foot1')?.id);
+ testing.expectEqual(null, table.rows?.namedItem('nested1'));
+
+ // The collection is live.
+ const rows = table.rows;
+ add(tbody, 'tr', 'body2');
+ testing.expectEqual(5, rows?.length);
+ testing.expectEqual('head1,top1,body1,body2,foot1', ids());
+
+ testing.expectEqual(0, document.createElement('table').rows?.length);
+ }
+</script>
diff --git a/src/browser/webapi/collections/HTMLCollection.zig b/src/browser/webapi/collections/HTMLCollection.zig
index 36bcf8f94..bf419da35 100644
--- a/src/browser/webapi/collections/HTMLCollection.zig
+++ b/src/browser/webapi/collections/HTMLCollection.zig
@@ -26,6 +26,7 @@ const Frame = @import("../../Frame.zig");
const Element = @import("../Element.zig");
const TreeWalker = @import("../TreeWalker.zig");
const NodeLive = @import("node_live.zig").NodeLive;
+const Table = @import("../element/html/Table.zig");
const Execution = js.Execution;
const Mode = enum {
@@ -37,6 +38,7 @@ const Mode = enum {
child_elements,
child_tag,
cells,
+ table_rows,
select_options,
selected_options,
links,
@@ -59,6 +61,7 @@ _data: union(Mode) {
child_elements: NodeLive(.child_elements),
child_tag: NodeLive(.child_tag),
cells: NodeLive(.cells),
+ table_rows: NodeLive(.table_rows),
select_options: NodeLive(.select_options),
selected_options: NodeLive(.selected_options),
links: NodeLive(.links),
@@ -131,6 +134,7 @@ pub fn iterator(self: *HTMLCollection, exec: *const Execution) !*Iterator {
.child_elements => |*impl| .{ .child_elements = impl._tw.clone() },
.child_tag => |*impl| .{ .child_tag = impl._tw.clone() },
.cells => |*impl| .{ .cells = impl._tw.clone() },
+ .table_rows => |*impl| .{ .table_rows = impl._tw.clone() },
.select_options => |*impl| .{ .select_options = impl._tw.clone() },
.selected_options => |*impl| .{ .selected_options = impl._tw.clone() },
.links => |*impl| .{ .links = impl._tw.clone() },
@@ -154,6 +158,7 @@ pub const Iterator = GenericIterator(struct {
child_elements: TreeWalker.Children,
child_tag: TreeWalker.Children,
cells: TreeWalker.Children,
+ table_rows: Table.RowWalker,
select_options: TreeWalker.FullExcludeSelf,
selected_options: TreeWalker.FullExcludeSelf,
links: TreeWalker.FullExcludeSelf,
@@ -181,6 +186,7 @@ pub const Iterator = GenericIterator(struct {
.child_elements => |*impl| impl.nextTw(&self.tw.child_elements),
.child_tag => |*impl| impl.nextTw(&self.tw.child_tag),
.cells => |*impl| impl.nextTw(&self.tw.cells),
+ .table_rows => |*impl| impl.nextTw(&self.tw.table_rows),
.select_options => |*impl| impl.nextTw(&self.tw.select_options),
.selected_options => |*impl| impl.nextTw(&self.tw.selected_options),
.links => |*impl| impl.nextTw(&self.tw.links),
diff --git a/src/browser/webapi/collections/node_live.zig b/src/browser/webapi/collections/node_live.zig
index bfaf033ee..c8a3d4642 100644
--- a/src/browser/webapi/collections/node_live.zig
+++ b/src/browser/webapi/collections/node_live.zig
@@ -27,6 +27,7 @@ const Element = @import("../Element.zig");
const TreeWalker = @import("../TreeWalker.zig");
const Selector = @import("../selector/Selector.zig");
const Form = @import("../element/html/Form.zig");
+const Table = @import("../element/html/Table.zig");
const HTMLDocument = @import("../HTMLDocument.zig");
const String = lp.String;
@@ -41,6 +42,7 @@ const Mode = enum {
child_elements,
child_tag,
cells,
+ table_rows,
select_options,
selected_options,
links,
@@ -71,6 +73,7 @@ const Filters = union(Mode) {
child_elements,
child_tag: Element.Tag,
cells,
+ table_rows,
select_options,
selected_options,
links,
@@ -108,6 +111,7 @@ pub fn NodeLive(comptime mode: Mode) type {
const TW = switch (mode) {
.tag, .tag_name, .tag_name_ns, .class_name, .name, .all_elements, .links, .anchors, .form, .document_named => TreeWalker.FullExcludeSelf,
.child_elements, .child_tag, .cells => TreeWalker.Children,
+ .table_rows => Table.RowWalker,
// A select's options can sit one level down, inside an <optgroup>, so
// these two walk the subtree and filter on the parent instead.
.select_options, .selected_options => TreeWalker.FullExcludeSelf,
@@ -325,6 +329,8 @@ pub fn NodeLive(comptime mode: Mode) type {
const el = node.is(Element) orelse return false;
return el.is(Element.Html.TableCell) != null;
},
+ // the RowWalker only yields rows
+ .table_rows => return true,
.select_options, .selected_options => {
const opt = node.is(Element.Html.Option) orelse return false;
@@ -449,6 +455,7 @@ pub fn NodeLive(comptime mode: Mode) type {
.child_elements => .{ ._data = .{ .child_elements = self } },
.child_tag => .{ ._data = .{ .child_tag = self } },
.cells => .{ ._data = .{ .cells = self } },
+ .table_rows => .{ ._data = .{ .table_rows = self } },
.select_options => .{ ._data = .{ .select_options = self } },
.selected_options => .{ ._data = .{ .selected_options = self } },
.links => .{ ._data = .{ .links = self } },
diff --git a/src/browser/webapi/element/html/Table.zig b/src/browser/webapi/element/html/Table.zig
index de601d581..f2bfdac3e 100644
--- a/src/browser/webapi/element/html/Table.zig
+++ b/src/browser/webapi/element/html/Table.zig
@@ -59,79 +59,108 @@ fn deleteRow(self: *Table, index: i32, frame: *Frame) !void {
_ = try row.parentNode().?.removeChild(row, frame);
}
-// Finds the index-th row (or the last row for -1) in spec order: thead, tr,
-// tbody then tfoot
+// Finds the index-th row (or the last row for -1) in spec order
fn findRow(self: *Table, index: i32) ?*Node {
- var scan: RowScan = .{ .index = index };
+ var it = RowWalker.init(self.asNode(), .{});
+ var count: i32 = 0;
+ var last: ?*Node = null;
+ while (it.next()) |row| {
+ if (count == index) {
+ return row;
+ }
+ count += 1;
+ last = row;
+ }
+ return if (index == -1) last else null;
+}
+
+fn getRows(self: *Table, frame: *Frame) collections.NodeLive(.table_rows) {
+ return collections.NodeLive(.table_rows).init(self.asNode(), {}, frame);
+}
- if (self.scanSectionRows(.thead, &scan)) |row| {
- return row;
+// Walks a table's rows in spec order: thead rows, then tr children of the
+// table and tbody rows, then tfoot rows, each group in tree order.
+pub const RowWalker = struct {
+ _root: *Node,
+ _phase: Phase = .head,
+ // the next child of the table to look at
+ _child: ?*Node,
+ // the next candidate row inside the current section
+ _row: ?*Node = null,
+
+ const Phase = enum { head, body, foot };
+ const Opts = struct {};
+
+ pub fn init(root: *Node, _: Opts) RowWalker {
+ return .{
+ ._root = root,
+ ._child = root.firstChild(),
+ };
}
- var it = self.asNode().childrenIterator();
- while (it.next()) |child| {
- const el = child.is(Element) orelse continue;
- switch (el.getTag()) {
- .tr => if (scan.check(child)) |row| {
- return row;
- },
- .tbody => if (scanChildRows(child, &scan)) |row| {
- return row;
- },
- else => {},
+ pub fn next(self: *RowWalker) ?*Node {
+ while (true) {
+ while (self._row) |node| {
+ self._row = node.nextSibling();
+ if (tagOf(node) == .tr) {
+ return node;
+ }
+ }
+
+ const child = self._child orelse {
+ self._phase = switch (self._phase) {
+ .head => .body,
+ .body => .foot,
+ .foot => return null,
+ };
+ self._child = self._root.firstChild();
+ continue;
+ };
+ self._child = child.nextSibling();
+
+ switch (self._phase) {
+ .head => if (tagOf(child) == .thead) {
+ self._row = child.firstChild();
+ },
+ .body => switch (tagOf(child) orelse continue) {
+ .tr => return child,
+ .tbody => self._row = child.firstChild(),
+ else => {},
+ },
+ .foot => if (tagOf(child) == .tfoot) {
+ self._row = child.firstChild();
+ },
+ }
}
}
- if (self.scanSectionRows(.tfoot, &scan)) |row| {
- return row;
+ pub fn reset(self: *RowWalker) void {
+ self.* = init(self._root, .{});
}
- if (index == -1) {
- return scan.last;
- }
- return null;
-}
-const RowScan = struct {
- index: i32,
- count: i32 = 0,
- last: ?*Node = null,
-
- fn check(self: *RowScan, row: *Node) ?*Node {
- if (self.count == self.index) {
- return row;
- }
- self.count += 1;
- self.last = row;
- return null;
+ pub fn clone(self: *const RowWalker) RowWalker {
+ return init(self._root, .{});
}
-};
-fn scanSectionRows(self: *Table, tag: Element.Tag, scan: *RowScan) ?*Node {
- var it = self.asNode().childrenIterator();
- while (it.next()) |child| {
- const el = child.is(Element) orelse continue;
- if (el.getTag() != tag) {
- continue;
+ pub fn contains(self: *const RowWalker, target: *Node) bool {
+ if (tagOf(target) != .tr) {
+ return false;
}
- if (scanChildRows(child, scan)) |row| {
- return row;
+ const parent = target._parent orelse return false;
+ if (parent == self._root) {
+ return true;
}
+ return switch (tagOf(parent) orelse return false) {
+ .thead, .tbody, .tfoot => parent._parent == self._root,
+ else => false,
+ };
}
- return null;
-}
-fn scanChildRows(section: *Node, scan: *RowScan) ?*Node {
- var it = section.childrenIterator();
- while (it.next()) |child| {
- const el = child.is(Element) orelse continue;
- if (el.getTag() == .tr) {
- if (scan.check(child)) |row| {
- return row;
- }
- }
+ fn tagOf(node: *Node) ?Element.Tag {
+ const el = node.is(Element) orelse return null;
+ return el.getTag();
}
- return null;
-}
+};
pub const JsApi = struct {
pub const bridge = js.Bridge(Table);
@@ -154,6 +183,7 @@ pub const JsApi = struct {
pub const @"align" = reflect.string("align");
pub const tBodies = bridge.accessor(Table.getTBodies, null, .{});
+ pub const rows = bridge.accessor(Table.getRows, null, .{});
pub const deleteRow = bridge.function(Table.deleteRow, .{ .ce_reactions = true });
};