aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-17 17:12:56 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-17 17:12:56 +0200
commit1e9efc4ed7ee8a493d461d28bd282f3deaaa8bd0 (patch)
tree0c9fd46356d4682dcc504bece01934e239dda644
parentfe95233027f0c01ce8edb6468c967bbd8764a8b8 (diff)
feat(dom): helper to manage class list
-rw-r--r--src/dom/Element.zig17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/dom/Element.zig b/src/dom/Element.zig
index 3ab7789..ed7611c 100644
--- a/src/dom/Element.zig
+++ b/src/dom/Element.zig
@@ -105,10 +105,11 @@ fn renderClass(self: *const Self, alloc: Allocator) !std.ArrayList(u8) {
if (iter.len == 0) return .empty;
var acc = try std.ArrayList(u8).initCapacity(alloc, 2);
errdefer acc.deinit(alloc);
+ const n = self.class_list.count();
var i: usize = 0;
while (iter.next()) |it| : (i += 1) {
try acc.appendSlice(alloc, it.*);
- if (i < self.class_list.count() - 1) try acc.append(alloc, ' ');
+ if (i < n - 1) try acc.append(alloc, ' ');
}
return acc;
}
@@ -125,6 +126,18 @@ pub fn hasAttribute(self: *Self, k: []const u8) bool {
return self.attributes.contains(k);
}
+pub fn appendClass(self: *Self, v: []const u8) !void {
+ try self.class_list.insert(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);
+}
+
pub fn appendContent(self: *Self, content: Self) !void {
return self.content.append(self.alloc, content);
}
@@ -200,7 +213,7 @@ test "content element" {
var div = init(alloc, .content, "div");
defer div.deinit();
- try div.class_list.insert("foo-bar");
+ try div.appendClass("foo-bar");
try div.appendContent(try initParagraph(alloc, "hello world"));
try div.appendContent(try initImg(alloc, "example.org", "example"));