1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const Element = @import("Element.zig");
const Node = Element.Node;
const Error = Element.Error;
base: Element.Void,
content: ?Element = null,
node: Node = .{
.ptr = undefined,
.vtable = .{ .element = fromNode },
},
pub const Self = @This();
pub fn init(alloc: Allocator, tag: []const u8) Error!*Self {
const v = try alloc.create(Self);
v.* = .{
.base = .{
.alloc = alloc,
.tag = tag,
.attributes = .init(alloc),
.class_list = .init(alloc),
},
};
v.node.ptr = v;
return v;
}
pub fn element(self: *Self) Element {
return (Element.Wrapper(Self){ .ptr = self }).element();
}
fn fromNode(context: *anyopaque) Element {
const self: *Self = @ptrCast(@alignCast(context));
return self.element();
}
pub fn render(self: *Self, alloc: Allocator) Error![]const u8 {
var base = self.base;
const b = try base.element().render(alloc);
defer alloc.free(b);
var acc = try std.ArrayList(u8).initCapacity(alloc, b.len * 2);
try acc.appendSlice(alloc, b);
if (self.content) |it| {
const sub = try it.render(alloc);
defer alloc.free(sub);
try acc.appendSlice(alloc, sub);
}
try acc.appendSlice(alloc, "</");
try acc.appendSlice(alloc, base.tag);
try acc.append(alloc, '>');
return acc.toOwnedSlice(alloc);
}
|