Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: add missing Table APIs

Karl Seguin37739833b77f
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index 97845265b..97b49d8d9 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -161,7 +161,7 @@ pub fn init(app: *App, opts: InitOpts) !Env {
params.array_buffer_allocator = v8.v8__ArrayBuffer__Allocator__NewDefaultAllocator(js.ArrayBuffer.MAX_LENGTH).?;
errdefer v8.v8__ArrayBuffer__Allocator__DELETE(params.array_buffer_allocator.?);
- params.external_references = &snapshot.external_references;
+ params.external_references = snapshot.external_references;
if (app.config.v8MaxHeapMb()) |mb| {
v8.v8__ResourceConstraints__ConfigureDefaultsFromHeapSize(
diff --git a/src/browser/js/Snapshot.zig b/src/browser/js/Snapshot.zig
index 62728ce72..0c06d78a1 100644
--- a/src/browser/js/Snapshot.zig
+++ b/src/browser/js/Snapshot.zig
@@ -73,7 +73,7 @@ startup_data: v8.StartupData,
// external reference. When we load the snapshot, we need to give it the same
// array with the exact same number of entries in the same order (but, of course
// cross-process, the value (address) might be different).
-external_references: [countExternalReferences()]isize,
+external_references: *const ExternalReferences,
// Track whether this snapshot owns its data (was created in-process)
// If false, the data points into embedded_snapshot_blob and will not be freed
@@ -138,13 +138,13 @@ fn isValid(self: Snapshot) bool {
}
pub fn create() !Snapshot {
- var external_references = collectExternalReferences();
+ const external_references = collectExternalReferences();
var params: v8.CreateParams = undefined;
v8.v8__Isolate__CreateParams__CONSTRUCT(&params);
params.array_buffer_allocator = v8.v8__ArrayBuffer__Allocator__NewDefaultAllocator(4 * 1024 * 1024 * 1024);
defer v8.v8__ArrayBuffer__Allocator__DELETE(params.array_buffer_allocator.?);
- params.external_references = @ptrCast(&external_references);
+ params.external_references = external_references;
const snapshot_creator = v8.v8__SnapshotCreator__CREATE(&params);
defer v8.v8__SnapshotCreator__DESTRUCT(snapshot_creator);
@@ -495,9 +495,18 @@ fn countExternalReferences() comptime_int {
return count + 1; // +1 for null terminator
}
-fn collectExternalReferences() [countExternalReferences()]isize {
+const ExternalReferences = [countExternalReferences()]isize;
+
+// Static rather than carried by value: in debug builds every frame the
+// Snapshot passed through (create, load, App.init) kept its own copy, which
+// was enough to exhaust V8's stack limit before the snapshot's first script.
+var external_references_storage: ExternalReferences = undefined;
+
+fn collectExternalReferences() *const ExternalReferences {
+ @setEvalBranchQuota(100_000);
var idx: usize = 0;
- var references = std.mem.zeroes([countExternalReferences()]isize);
+ const references = &external_references_storage;
+ @memset(references, 0);
references[idx] = @bitCast(@intFromPtr(&illegalConstructorCallback));
idx += 1;
diff --git a/src/browser/tests/element/html/table.html b/src/browser/tests/element/html/table.html
index 10714274d..5685f374d 100644
--- a/src/browser/tests/element/html/table.html
+++ b/src/browser/tests/element/html/table.html
@@ -142,3 +142,234 @@
testing.expectEqual('real', tbody.rows[0].id);
}
</script>
+
+<script id="caption">
+ {
+ const table = document.createElement('table');
+ testing.expectEqual(null, table.caption);
+ table.appendChild(document.createElement('tbody'));
+
+ // createCaption inserts a caption as the first child, then reuses it.
+ const caption = table.createCaption();
+ testing.expectEqual(caption, table.firstChild);
+ testing.expectEqual(caption, table.caption);
+ testing.expectEqual(caption, table.createCaption());
+
+ // The setter replaces the first caption and inserts the new one first.
+ const other = document.createElement('caption');
+ table.appendChild(other);
+ const replacement = document.createElement('caption');
+ table.caption = replacement;
+ testing.expectEqual(null, caption.parentNode);
+ testing.expectEqual(replacement, table.firstChild);
+ testing.expectEqual(table, other.parentNode);
+
+ // Only an HTML caption counts.
+ table.caption = null;
+ testing.expectEqual(other, table.caption);
+ table.deleteCaption();
+ testing.expectEqual(null, other.parentNode);
+ const foreign = table.appendChild(document.createElementNS('foo', 'caption'));
+ testing.expectEqual(null, table.caption);
+ table.deleteCaption();
+ testing.expectEqual(table, foreign.parentNode);
+
+ testing.expectError('TypeError', () => { table.caption = document.createElement('div'); });
+ }
+</script>
+
+<script id="tHead_tFoot">
+ {
+ const table = document.createElement('table');
+ const caption = table.appendChild(document.createElement('caption'));
+ const colgroup = table.appendChild(document.createElement('colgroup'));
+ const tbody = table.appendChild(document.createElement('tbody'));
+ const tfoot = table.appendChild(document.createElement('tfoot'));
+ testing.expectEqual(null, table.tHead);
+ testing.expectEqual(tfoot, table.tFoot);
+
+ // A thead goes after any caption and colgroup.
+ const thead = table.createTHead();
+ testing.expectEqual(thead, colgroup.nextSibling);
+ testing.expectEqual(thead, table.tHead);
+ testing.expectEqual(thead, table.createTHead());
+
+ // A tfoot is appended at the end.
+ table.deleteTFoot();
+ testing.expectEqual(null, tfoot.parentNode);
+ const newFoot = table.createTFoot();
+ testing.expectEqual(newFoot, table.lastChild);
+ table.appendChild(document.createElement('tbody'));
+ table.tFoot = tfoot;
+ testing.expectEqual(null, newFoot.parentNode);
+ testing.expectEqual(tfoot, table.lastChild);
+
+ // Assigning the wrong section throws.
+ testing.expectError('HierarchyRequestError', () => { table.tHead = document.createElement('tbody'); });
+ testing.expectError('HierarchyRequestError', () => { table.tFoot = document.createElement('thead'); });
+ testing.expectEqual(thead, table.tHead);
+
+ table.tHead = null;
+ testing.expectEqual(null, thead.parentNode);
+ testing.expectEqual(null, table.tHead);
+ table.deleteTHead();
+ testing.expectEqual(caption, table.firstChild);
+ }
+</script>
+
+<script id="createTBody">
+ {
+ const table = document.createElement('table');
+ const first = table.createTBody();
+ testing.expectEqual(first, table.firstChild);
+ testing.expectEqual('TBODY', first.tagName);
+
+ // The new tbody goes right after the last tbody, not at the end.
+ const tfoot = table.appendChild(document.createElement('tfoot'));
+ const second = table.createTBody();
+ testing.expectEqual(second, first.nextSibling);
+ testing.expectEqual(tfoot, second.nextSibling);
+ testing.expectEqual(2, table.tBodies.length);
+ }
+</script>
+
+<script id="insertRow">
+ {
+ // An empty table gets a tbody, even with an empty thead.
+ let table = document.createElement('table');
+ const thead = table.appendChild(document.createElement('thead'));
+ let row = table.insertRow();
+ testing.expectEqual('TR', row.tagName);
+ testing.expectEqual('TBODY', row.parentNode.tagName);
+ testing.expectEqual(table, row.parentNode.parentNode);
+ testing.expectEqual(0, thead.childNodes.length);
+
+ // A rowless table with a tbody uses its last tbody.
+ table = document.createElement('table');
+ table.appendChild(document.createElement('tbody'));
+ const lastBody = table.appendChild(document.createElement('tbody'));
+ row = table.insertRow(0);
+ testing.expectEqual(lastBody, row.parentNode);
+
+ // -1 and rows.length append to the last row's parent: the tfoot here.
+ table = document.createElement('table');
+ const tbody = table.appendChild(document.createElement('tbody'));
+ tbody.appendChild(document.createElement('tr'));
+ const tfoot = table.appendChild(document.createElement('tfoot'));
+ tfoot.appendChild(document.createElement('tr'));
+ row = table.insertRow(-1);
+ testing.expectEqual(tfoot, row.parentNode);
+ row = table.insertRow(table.rows.length);
+ testing.expectEqual(tfoot, row.parentNode);
+ testing.expectEqual(row, tfoot.lastChild);
+
+ // Otherwise the row goes before the index-th row.
+ row = table.insertRow(1);
+ testing.expectEqual(tfoot, row.parentNode);
+ testing.expectEqual(row, tfoot.firstChild);
+ testing.expectEqual(1, row.rowIndex);
+
+ testing.expectError('IndexSizeError', () => table.insertRow(-2));
+ testing.expectError('IndexSizeError', () => table.insertRow(table.rows.length + 1));
+ }
+</script>
+
+<script id="section_rows">
+ {
+ const tbody = document.createElement('tbody');
+ tbody.appendChild(document.createElement('div'));
+ const a = tbody.insertRow();
+ const b = tbody.insertRow(0);
+ const c = tbody.insertRow(2);
+ testing.expectEqual([b, a, c], Array.from(tbody.rows));
+
+ testing.expectError('IndexSizeError', () => tbody.insertRow(-2));
+ testing.expectError('IndexSizeError', () => tbody.insertRow(4));
+ testing.expectError('IndexSizeError', () => tbody.deleteRow(3));
+ testing.expectError('IndexSizeError', () => tbody.deleteRow(-2));
+
+ tbody.deleteRow(-1);
+ testing.expectEqual(null, c.parentNode);
+ tbody.deleteRow(0);
+ testing.expectEqual(null, b.parentNode);
+ tbody.deleteRow(0);
+ testing.expectEqual(0, tbody.rows.length);
+
+ // -1 with no rows is a no-op, 0 throws.
+ tbody.deleteRow(-1);
+ testing.expectError('IndexSizeError', () => tbody.deleteRow(0));
+ testing.expectEqual(1, tbody.childNodes.length);
+ }
+</script>
+
+<script id="row_cells">
+ {
+ const tr = document.createElement('tr');
+ const th = tr.appendChild(document.createElement('th'));
+ tr.appendChild(document.createTextNode('x'));
+ const a = tr.insertCell();
+ const b = tr.insertCell(1);
+ testing.expectEqual('TD', a.tagName);
+ testing.expectEqual([th, b, a], Array.from(tr.cells));
+ testing.expectEqual(0, th.cellIndex);
+ testing.expectEqual(1, b.cellIndex);
+ testing.expectEqual(2, a.cellIndex);
+
+ testing.expectError('IndexSizeError', () => tr.insertCell(-2));
+ testing.expectError('IndexSizeError', () => tr.insertCell(4));
+ testing.expectError('IndexSizeError', () => tr.deleteCell(3));
+
+ tr.deleteCell(-1);
+ testing.expectEqual(null, a.parentNode);
+ testing.expectEqual(-1, a.cellIndex);
+ tr.deleteCell(0);
+ testing.expectEqual(0, b.cellIndex);
+
+ // A cell outside an HTML tr has no index.
+ const div = document.createElement('div');
+ div.appendChild(b);
+ testing.expectEqual(-1, b.cellIndex);
+ }
+</script>
+
+<script id="rowIndex">
+ {
+ const table = document.createElement('table');
+ const tfoot = table.appendChild(document.createElement('tfoot'));
+ const foot = tfoot.appendChild(document.createElement('tr'));
+ const top = table.appendChild(document.createElement('tr'));
+ const thead = table.appendChild(document.createElement('thead'));
+ const head1 = thead.appendChild(document.createElement('tr'));
+ const head2 = thead.appendChild(document.createElement('tr'));
+ const tbody = table.appendChild(document.createElement('tbody'));
+ const body = tbody.appendChild(document.createElement('tr'));
+
+ // Rows in spec order: head1, head2, top, body, foot.
+ testing.expectEqual(0, head1.rowIndex);
+ testing.expectEqual(1, head2.rowIndex);
+ testing.expectEqual(2, top.rowIndex);
+ testing.expectEqual(3, body.rowIndex);
+ testing.expectEqual(4, foot.rowIndex);
+
+ // sectionRowIndex is the index in the parent's rows: the table's for a
+ // table-level row.
+ testing.expectEqual(1, head2.sectionRowIndex);
+ testing.expectEqual(2, top.sectionRowIndex);
+ testing.expectEqual(0, body.sectionRowIndex);
+ testing.expectEqual(0, foot.sectionRowIndex);
+
+ // A section outside a table still has a sectionRowIndex, but no rowIndex.
+ const loose = document.createElement('tbody');
+ loose.appendChild(document.createElement('tr'));
+ const row = loose.appendChild(document.createElement('tr'));
+ testing.expectEqual(-1, row.rowIndex);
+ testing.expectEqual(1, row.sectionRowIndex);
+
+ const orphan = document.createElement('tr');
+ testing.expectEqual(-1, orphan.rowIndex);
+ testing.expectEqual(-1, orphan.sectionRowIndex);
+ document.createElement('div').appendChild(orphan);
+ testing.expectEqual(-1, orphan.rowIndex);
+ testing.expectEqual(-1, orphan.sectionRowIndex);
+ }
+</script>
diff --git a/src/browser/webapi/element/html/Table.zig b/src/browser/webapi/element/html/Table.zig
index fd085238b..5d6fb7fbc 100644
--- a/src/browser/webapi/element/html/Table.zig
+++ b/src/browser/webapi/element/html/Table.zig
@@ -27,6 +27,11 @@ const collections = @import("../../collections.zig");
const HtmlElement = @import("../Html.zig");
+const Row = @import("TableRow.zig");
+const Col = @import("TableCol.zig");
+const Section = @import("TableSection.zig");
+const Caption = @import("TableCaption.zig");
+
const Table = @This();
pub const Proto = HtmlElement;
@@ -45,6 +50,177 @@ fn getTBodies(self: *Table, frame: *Frame) collections.NodeLive(.child_tag) {
return collections.NodeLive(.child_tag).init(self.asNode(), .tbody, frame);
}
+fn getCaption(self: *Table) ?*Caption {
+ var child = self.asNode().firstChild();
+ while (child) |node| : (child = node.nextSibling()) {
+ if (node.is(Caption)) |caption| {
+ return caption;
+ }
+ }
+ return null;
+}
+
+fn setCaption(self: *Table, caption_: ?*Caption, frame: *Frame) !void {
+ try self.deleteCaption(frame);
+ const caption = caption_ orelse return;
+ _ = try self.asNode().insertBefore(caption.asNode(), self.asNode().firstChild(), frame);
+}
+
+fn createCaption(self: *Table, frame: *Frame) !*Caption {
+ if (self.getCaption()) |caption| {
+ return caption;
+ }
+ const caption = try createElement(self.asNode(), Caption, "caption", frame);
+ _ = try self.asNode().insertBefore(caption.asNode(), self.asNode().firstChild(), frame);
+ return caption;
+}
+
+fn deleteCaption(self: *Table, frame: *Frame) !void {
+ const caption = self.getCaption() orelse return;
+ _ = try self.asNode().removeChild(caption.asNode(), frame);
+}
+
+fn getTHead(self: *Table) ?*Section {
+ return self.findSection(.thead);
+}
+
+fn setTHead(self: *Table, head_: ?*Section, frame: *Frame) !void {
+ if (head_) |head| {
+ if (head._tag != .thead) {
+ return error.HierarchyError;
+ }
+ }
+ try self.deleteTHead(frame);
+ const head = head_ orelse return;
+ try self.insertTHead(head, frame);
+}
+
+fn createTHead(self: *Table, frame: *Frame) !*Section {
+ if (self.getTHead()) |head| {
+ return head;
+ }
+ const head = try createElement(self.asNode(), Section, "thead", frame);
+ try self.insertTHead(head, frame);
+ return head;
+}
+
+fn deleteTHead(self: *Table, frame: *Frame) !void {
+ const head = self.getTHead() orelse return;
+ _ = try self.asNode().removeChild(head.asNode(), frame);
+}
+
+// A thead goes before the first element child that is neither a caption nor a colgroup
+fn insertTHead(self: *Table, head: *Section, frame: *Frame) !void {
+ var child = self.asNode().firstChild();
+ while (child) |node| : (child = node.nextSibling()) {
+ if (node.is(Element) == null or node.is(Caption) != null) {
+ continue;
+ }
+ if (node.is(Col)) |col| {
+ if (col._tag == .colgroup) {
+ continue;
+ }
+ }
+ break;
+ }
+ _ = try self.asNode().insertBefore(head.asNode(), child, frame);
+}
+
+fn getTFoot(self: *Table) ?*Section {
+ return self.findSection(.tfoot);
+}
+
+fn setTFoot(self: *Table, foot_: ?*Section, frame: *Frame) !void {
+ if (foot_) |foot| {
+ if (foot._tag != .tfoot) {
+ return error.HierarchyError;
+ }
+ }
+ try self.deleteTFoot(frame);
+ const foot = foot_ orelse return;
+ _ = try self.asNode().appendChild(foot.asNode(), frame);
+}
+
+fn createTFoot(self: *Table, frame: *Frame) !*Section {
+ if (self.getTFoot()) |foot| {
+ return foot;
+ }
+ const foot = try createElement(self.asNode(), Section, "tfoot", frame);
+ _ = try self.asNode().appendChild(foot.asNode(), frame);
+ return foot;
+}
+
+fn deleteTFoot(self: *Table, frame: *Frame) !void {
+ const foot = self.getTFoot() orelse return;
+ _ = try self.asNode().removeChild(foot.asNode(), frame);
+}
+
+// The new tbody goes right after the last tbody child, or at the end
+fn createTBody(self: *Table, frame: *Frame) !*Section {
+ const body = try createElement(self.asNode(), Section, "tbody", frame);
+ const ref = if (self.lastTBody()) |last| last.asNode().nextSibling() else null;
+ _ = try self.asNode().insertBefore(body.asNode(), ref, frame);
+ return body;
+}
+
+fn findSection(self: *Table, tag: Element.Tag) ?*Section {
+ var child = self.asNode().firstChild();
+ while (child) |node| : (child = node.nextSibling()) {
+ const section = node.is(Section) orelse continue;
+ if (section._tag == tag) {
+ return section;
+ }
+ }
+ return null;
+}
+
+fn lastTBody(self: *Table) ?*Section {
+ var child = self.asNode().lastChild();
+ while (child) |node| : (child = node.previousSibling()) {
+ const section = node.is(Section) orelse continue;
+ if (section._tag == .tbody) {
+ return section;
+ }
+ }
+ return null;
+}
+
+fn insertRow(self: *Table, index_: ?i32, frame: *Frame) !*Row {
+ const index = index_ orelse -1;
+ if (index < -1) {
+ return error.IndexSizeError;
+ }
+
+ var it = RowWalker.init(self.asNode(), .{});
+ var count: i32 = 0;
+ var target: ?*Node = null;
+ var last: ?*Node = null;
+ while (it.next()) |row| {
+ if (count == index) {
+ target = row;
+ }
+ count += 1;
+ last = row;
+ }
+ if (index > count) {
+ return error.IndexSizeError;
+ }
+
+ const row = try createElement(self.asNode(), Row, "tr", frame);
+ if (target) |ref| {
+ _ = try ref.parentNode().?.insertBefore(row.asNode(), ref, frame);
+ } else if (last) |ref| {
+ _ = try ref.parentNode().?.appendChild(row.asNode(), frame);
+ } else if (self.lastTBody()) |body| {
+ _ = try body.asNode().appendChild(row.asNode(), frame);
+ } else {
+ const body = try createElement(self.asNode(), Section, "tbody", frame);
+ _ = try body.asNode().appendChild(row.asNode(), frame);
+ _ = try self.asNode().appendChild(body.asNode(), frame);
+ }
+ return row;
+}
+
fn deleteRow(self: *Table, index: i32, frame: *Frame) !void {
if (index < -1) {
return error.IndexSizeError;
@@ -78,6 +254,101 @@ fn getRows(self: *Table, frame: *Frame) collections.NodeLive(.table_rows) {
return collections.NodeLive(.table_rows).init(self.asNode(), {}, frame);
}
+pub fn rowIndex(self: *Table, row: *Node) i32 {
+ var it = RowWalker.init(self.asNode(), .{});
+ var index: i32 = 0;
+ while (it.next()) |node| : (index += 1) {
+ if (node == row) {
+ return index;
+ }
+ }
+ return -1;
+}
+
+fn createElement(parent: *Node, comptime T: type, comptime name: []const u8, frame: *Frame) !*T {
+ const node = try Frame.node_factory.createElementNS(parent.getDocument(frame), .html, name, null);
+ return node.as(T);
+}
+
+pub fn childIndex(parent: *Node, comptime T: type, child: *Node) i32 {
+ var index: i32 = 0;
+ var it = parent.firstChild();
+ while (it) |node| : (it = node.nextSibling()) {
+ if (node == child) {
+ return index;
+ }
+ if (node.is(T) != null) {
+ index += 1;
+ }
+ }
+ return -1;
+}
+
+pub fn insertChild(parent: *Node, comptime T: type, comptime name: []const u8, index_: ?i32, frame: *Frame) !*T {
+ const index = index_ orelse -1;
+ if (index < -1) {
+ return error.IndexSizeError;
+ }
+ var count: i32 = 0;
+ var ref: ?*Node = null;
+ var it = parent.firstChild();
+ while (it) |node| : (it = node.nextSibling()) {
+ if (node.is(T) == null) {
+ continue;
+ }
+ if (count == index) {
+ ref = node;
+ }
+ count += 1;
+ }
+ // -1 and an index one past the end both append
+ if (index > count) {
+ return error.IndexSizeError;
+ }
+ const child = try createElement(parent, T, name, frame);
+ _ = try parent.insertBefore(child.asNode(), ref, frame);
+ return child;
+}
+
+pub fn deleteChild(parent: *Node, comptime T: type, index: i32, frame: *Frame) !void {
+ if (index < -1) {
+ return error.IndexSizeError;
+ }
+ if (index == -1) {
+ // -1 with nothing to delete is a no-op
+ const child = lastOfType(parent, T) orelse return;
+ _ = try parent.removeChild(child, frame);
+ return;
+ }
+ const child = nthOfType(parent, T, index) orelse return error.IndexSizeError;
+ _ = try parent.removeChild(child, frame);
+}
+
+fn nthOfType(parent: *Node, comptime T: type, index: i32) ?*Node {
+ var count: i32 = 0;
+ var it = parent.firstChild();
+ while (it) |node| : (it = node.nextSibling()) {
+ if (node.is(T) == null) {
+ continue;
+ }
+ if (count == index) {
+ return node;
+ }
+ count += 1;
+ }
+ return null;
+}
+
+fn lastOfType(parent: *Node, comptime T: type) ?*Node {
+ var it = parent.lastChild();
+ while (it) |node| : (it = node.previousSibling()) {
+ if (node.is(T) != null) {
+ return node;
+ }
+ }
+ return null;
+}
+
// 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 {
@@ -88,9 +359,6 @@ pub const RowWalker = struct {
// the next candidate row inside the current section
_row: ?*Node = null,
- const Row = Element.Html.TableRow;
- const Section = Element.Html.TableSection;
-
const Phase = enum { head, body, foot };
const Opts = struct {};
@@ -182,8 +450,19 @@ pub const JsApi = struct {
pub const bgColor = reflect.stringNullToEmpty("bgcolor");
pub const @"align" = reflect.string("align");
+ pub const caption = bridge.accessor(Table.getCaption, Table.setCaption, .{ .ce_reactions = true });
+ pub const createCaption = bridge.function(Table.createCaption, .{ .ce_reactions = true });
+ pub const deleteCaption = bridge.function(Table.deleteCaption, .{ .ce_reactions = true });
+ pub const tHead = bridge.accessor(Table.getTHead, Table.setTHead, .{ .ce_reactions = true });
+ pub const createTHead = bridge.function(Table.createTHead, .{ .ce_reactions = true });
+ pub const deleteTHead = bridge.function(Table.deleteTHead, .{ .ce_reactions = true });
+ pub const tFoot = bridge.accessor(Table.getTFoot, Table.setTFoot, .{ .ce_reactions = true });
+ pub const createTFoot = bridge.function(Table.createTFoot, .{ .ce_reactions = true });
+ pub const deleteTFoot = bridge.function(Table.deleteTFoot, .{ .ce_reactions = true });
pub const tBodies = bridge.accessor(Table.getTBodies, null, .{});
+ pub const createTBody = bridge.function(Table.createTBody, .{ .ce_reactions = true });
pub const rows = bridge.accessor(Table.getRows, null, .{});
+ pub const insertRow = bridge.function(Table.insertRow, .{ .ce_reactions = true });
pub const deleteRow = bridge.function(Table.deleteRow, .{ .ce_reactions = true });
};
diff --git a/src/browser/webapi/element/html/TableCell.zig b/src/browser/webapi/element/html/TableCell.zig
index 9321622c3..81afed46f 100644
--- a/src/browser/webapi/element/html/TableCell.zig
+++ b/src/browser/webapi/element/html/TableCell.zig
@@ -24,6 +24,8 @@ const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig");
const HtmlElement = @import("../Html.zig");
+const Table = @import("Table.zig");
+const Row = @import("TableRow.zig");
const String = lp.String;
@@ -42,6 +44,16 @@ pub fn asNode(self: *TableCell) *Node {
return self.asElement().asNode();
}
+// The index in the parent row's cells, -1 outside a row
+fn getCellIndex(self: *TableCell) i32 {
+ const node = self.asNode();
+ const parent = node._parent orelse return -1;
+ if (parent.is(Row) == null) {
+ return -1;
+ }
+ return Table.childIndex(parent, TableCell, node);
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(TableCell);
@@ -66,6 +78,7 @@ pub const JsApi = struct {
pub const abbr = reflect.string("abbr");
pub const colSpan = reflect.unsignedLong("colspan", .{ .default = 1, .clamp = .{ .min = 1, .max = 1000 } });
+ pub const cellIndex = bridge.accessor(TableCell.getCellIndex, null, .{});
pub const rowSpan = reflect.unsignedLong("rowspan", .{ .default = 1, .clamp = .{ .min = 0, .max = 65534 } });
};
diff --git a/src/browser/webapi/element/html/TableRow.zig b/src/browser/webapi/element/html/TableRow.zig
index 2408d7042..14f17b177 100644
--- a/src/browser/webapi/element/html/TableRow.zig
+++ b/src/browser/webapi/element/html/TableRow.zig
@@ -26,6 +26,9 @@ const Element = @import("../../Element.zig");
const collections = @import("../../collections.zig");
const HtmlElement = @import("../Html.zig");
+const Table = @import("Table.zig");
+const Cell = @import("TableCell.zig");
+const Section = @import("TableSection.zig");
const TableRow = @This();
@@ -45,6 +48,40 @@ fn getCells(self: *TableRow, frame: *Frame) collections.NodeLive(.cells) {
return collections.NodeLive(.cells).init(self.asNode(), {}, frame);
}
+fn insertCell(self: *TableRow, index: ?i32, frame: *Frame) !*Cell {
+ return Table.insertChild(self.asNode(), Cell, "td", index, frame);
+}
+
+fn deleteCell(self: *TableRow, index: i32, frame: *Frame) !void {
+ return Table.deleteChild(self.asNode(), Cell, index, frame);
+}
+
+fn getRowIndex(self: *TableRow) i32 {
+ const node = self.asNode();
+ const parent = node._parent orelse return -1;
+ if (parent.is(Table)) |table| {
+ return table.rowIndex(node);
+ }
+ if (parent.is(Section) == null) {
+ return -1;
+ }
+ const grandparent = parent._parent orelse return -1;
+ const table = grandparent.is(Table) orelse return -1;
+ return table.rowIndex(node);
+}
+
+fn getSectionRowIndex(self: *TableRow) i32 {
+ const node = self.asNode();
+ const parent = node._parent orelse return -1;
+ if (parent.is(Table)) |table| {
+ return table.rowIndex(node);
+ }
+ if (parent.is(Section) == null) {
+ return -1;
+ }
+ return Table.childIndex(parent, TableRow, node);
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(TableRow);
@@ -62,4 +99,8 @@ pub const JsApi = struct {
pub const @"align" = reflect.string("align");
pub const cells = bridge.accessor(TableRow.getCells, null, .{});
+ pub const insertCell = bridge.function(TableRow.insertCell, .{ .ce_reactions = true });
+ pub const deleteCell = bridge.function(TableRow.deleteCell, .{ .ce_reactions = true });
+ pub const rowIndex = bridge.accessor(TableRow.getRowIndex, null, .{});
+ pub const sectionRowIndex = bridge.accessor(TableRow.getSectionRowIndex, null, .{});
};
diff --git a/src/browser/webapi/element/html/TableSection.zig b/src/browser/webapi/element/html/TableSection.zig
index 55f048d27..94dc12e39 100644
--- a/src/browser/webapi/element/html/TableSection.zig
+++ b/src/browser/webapi/element/html/TableSection.zig
@@ -26,6 +26,8 @@ const Element = @import("../../Element.zig");
const collections = @import("../../collections.zig");
const HtmlElement = @import("../Html.zig");
+const Table = @import("Table.zig");
+const Row = @import("TableRow.zig");
const String = lp.String;
@@ -48,6 +50,14 @@ fn getRows(self: *TableSection, frame: *Frame) collections.NodeLive(.child_tag)
return collections.NodeLive(.child_tag).init(self.asNode(), .tr, frame);
}
+fn insertRow(self: *TableSection, index: ?i32, frame: *Frame) !*Row {
+ return Table.insertChild(self.asNode(), Row, "tr", index, frame);
+}
+
+fn deleteRow(self: *TableSection, index: i32, frame: *Frame) !void {
+ return Table.deleteChild(self.asNode(), Row, index, frame);
+}
+
pub const JsApi = struct {
pub const bridge = js.Bridge(TableSection);
@@ -64,4 +74,6 @@ pub const JsApi = struct {
pub const @"align" = reflect.string("align");
pub const rows = bridge.accessor(TableSection.getRows, null, .{});
+ pub const insertRow = bridge.function(TableSection.insertRow, .{ .ce_reactions = true });
+ pub const deleteRow = bridge.function(TableSection.deleteRow, .{ .ce_reactions = true });
};