diff options
Diffstat (limited to 'src/eval/html')
| -rw-r--r-- | src/eval/html/Element.zig | 12 | ||||
| -rw-r--r-- | src/eval/html/Root.zig | 10 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/eval/html/Element.zig b/src/eval/html/Element.zig index c44f4ef..c6197b8 100644 --- a/src/eval/html/Element.zig +++ b/src/eval/html/Element.zig @@ -45,9 +45,9 @@ pub const Node = struct { vtable: struct { element: *const fn (*anyopaque) Element }, node: std.DoublyLinkedList.Node = .{}, - pub fn from(n: *std.DoublyLinkedList.Node) *Node { - const v: *Node = @fieldParentPtr("node", n); - return v; + pub fn from(n: *std.DoublyLinkedList.Node) Element { + const self: *Node = @fieldParentPtr("node", n); + return self.vtable.element(self.ptr); } pub fn element(self: Node) Element { @@ -106,7 +106,7 @@ test "content" { p.content = root.element(); var content = try Literal.init(alloc, "hello world"); - root.append(content.element()); + try root.append(content.element()); try doTest(alloc, content.element(), "hello world"); try doTest(alloc, p.element(), "<p>hello world</p>"); @@ -115,8 +115,8 @@ test "content" { var rootDiv = try Root.init(alloc); div.content = rootDiv.element(); try div.base.appendClass("foo-bar"); - rootDiv.append(p.element()); - rootDiv.append((try Void.init(alloc, "br")).element()); + try rootDiv.append(p.element()); + try rootDiv.append((try Void.init(alloc, "br")).element()); try doTest(alloc, div.element(), "<div class=\"foo-bar\"><p>hello world</p><br></div>"); } diff --git a/src/eval/html/Root.zig b/src/eval/html/Root.zig index 83859d4..acfe82e 100644 --- a/src/eval/html/Root.zig +++ b/src/eval/html/Root.zig @@ -35,7 +35,13 @@ pub fn allocator(self: *Self) Allocator { return self.arena.allocator(); } -pub fn append(self: *Self, el: Element) void { +pub fn append(self: *Self, raw: anytype) Error!void { + const el: Element = blk: { + const T = @TypeOf(raw); + if (T == Element) break :blk raw; + if (@hasDecl(T, "html")) break :blk try raw.html(self.allocator()); + @compileError("cannot convert " ++ @typeName(T) ++ " into " ++ @typeName(Element)); + }; self.content.append(&el.node().node); } @@ -52,6 +58,6 @@ pub fn render(self: *Self, alloc: Allocator) Error![]const u8 { var arena = Arena.init(alloc); defer arena.deinit(); var v = self.content.first; - while (v) |it| : (v = it.next) try acc.appendSlice(alloc, try Node.from(it).element().render(arena.allocator())); + while (v) |it| : (v = it.next) try acc.appendSlice(alloc, try Node.from(it).render(arena.allocator())); return acc.toOwnedSlice(alloc); } |
