aboutsummaryrefslogtreecommitdiff
path: root/src/eval/html/Void.zig
blob: 431536c9db75f1e0fd1a56bb7a62399cc9bef653 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const std = @import("std");
const Allocator = std.mem.Allocator;
const List = std.ArrayList;
const html = @import("html.zig");
const Element = @import("Element.zig");
const Node = Element.Node;
const Error = Element.Error;

alloc: Allocator,
tag: []const u8,
attributes: std.StringArrayHashMap([]const u8),
class_list: std.BufSet,
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.* = .{
        .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();
}

pub fn setAttribute(self: *Self, k: []const u8, v: []const u8) Error!void {
    try self.attributes.put(try self.alloc.dupe(u8, k), try html.escape(self.alloc, v));
}

pub fn removeAttribute(self: *Self, k: []const u8) void {
    _ = self.attributes.orderedRemove(k);
}

pub fn hasAttribute(self: *Self, k: []const u8) bool {
    return self.attributes.contains(k);
}

pub fn appendClass(self: *Self, v: []const u8) Error!void {
    try self.class_list.insert(try html.escape(self.alloc, v));
}

pub fn hasClass(self: *Self, v: []const u8) bool {
    return self.class_list.contains(v);
}

pub fn removeClass(self: *Self, v: []const u8) void {
    self.class_list.remove(v);
}

fn fromNode(context: *anyopaque) Element {
    const self: *Self = @ptrCast(@alignCast(context));
    return self.element();
}

pub fn render(self: *Self, alloc: Allocator) Error![]const u8 {
    const attr = try renderAttribute(alloc, &self.attributes, &self.class_list);
    defer if (attr) |it| alloc.free(it);
    var acc = try List(u8).initCapacity(alloc, self.tag.len + 2);
    errdefer acc.deinit(alloc);
    try acc.append(alloc, '<');
    try acc.appendSlice(alloc, self.tag);
    if (attr) |it| try acc.appendSlice(alloc, it);
    try acc.append(alloc, '>');
    return acc.toOwnedSlice(alloc);
}

fn renderAttribute(alloc: Allocator, attributes: *std.StringArrayHashMap([]const u8), class_list: *std.BufSet) Error!?[]const u8 {
    const class = try renderClass(alloc, class_list);
    defer if (class) |it| {
        _ = attributes.orderedRemove("class");
        alloc.free(it);
    };
    if (class) |it| try attributes.put("class", it);
    var iter = attributes.iterator();
    if (iter.len == 0) return null;
    var acc = try List(u8).initCapacity(alloc, iter.len);
    errdefer acc.deinit(alloc);
    try acc.append(alloc, ' ');
    var i: usize = 0;
    while (iter.next()) |it| : (i += 1) {
        try acc.appendSlice(alloc, it.key_ptr.*);
        try acc.appendSlice(alloc, "=\"");
        const escape = try html.escape(alloc, it.value_ptr.*);
        defer alloc.free(escape);
        try acc.appendSlice(alloc, escape);
        try acc.append(alloc, '"');
        if (i < iter.len - 1) try acc.append(alloc, ' ');
    }
    return try acc.toOwnedSlice(alloc);
}

fn renderClass(alloc: Allocator, class_list: *std.BufSet) Error!?[]const u8 {
    const n = class_list.count();
    if (n == 0) return null;
    var acc = try List(u8).initCapacity(alloc, n);
    errdefer acc.deinit(alloc);
    var iter = class_list.iterator();
    var i: usize = 0;
    while (iter.next()) |it| : (i += 1) {
        try acc.appendSlice(alloc, it.*);
        if (i < n - 1) try acc.append(alloc, ' ');
    }
    return try acc.toOwnedSlice(alloc);
}