diff options
Diffstat (limited to 'src/dom')
| -rw-r--r-- | src/dom/Element.zig | 160 | ||||
| -rw-r--r-- | src/dom/html.zig | 20 |
2 files changed, 90 insertions, 90 deletions
diff --git a/src/dom/Element.zig b/src/dom/Element.zig index 60f82e0..7fd2f2d 100644 --- a/src/dom/Element.zig +++ b/src/dom/Element.zig @@ -12,7 +12,7 @@ pub const Kind = enum { const Self = @This(); kind: Kind, -alloc: Allocator, +gpa: Allocator, tag: ?[]const u8 = null, attributes: std.StringArrayHashMap([]const u8), class_list: std.BufSet, @@ -21,33 +21,33 @@ literal: ?[]const u8 = null, /// Init a new Element with the given kind. /// The tag will never be escaped. -pub fn init(alloc: Allocator, knd: Kind, tag: []const u8) Self { +pub fn init(gpa: Allocator, knd: Kind, tag: []const u8) Self { return .{ .kind = knd, - .alloc = alloc, + .gpa = gpa, .tag = tag, - .attributes = .init(alloc), - .class_list = .init(alloc), + .attributes = .init(gpa), + .class_list = .init(gpa), }; } /// Init a new literal element. /// The literal content will never be escaped, see initLitEscaped if you want to escape it. /// The literal content must be free'd by the allocator (use Allocator.dupe if you want to use a const string). -pub fn initLit(alloc: Allocator, literal: []const u8) Self { +pub fn initLit(gpa: Allocator, literal: []const u8) Self { return .{ .kind = .literal, - .alloc = alloc, + .gpa = gpa, .literal = literal, - .attributes = .init(alloc), - .class_list = .init(alloc), + .attributes = .init(gpa), + .class_list = .init(gpa), }; } /// Init a new literal element that is escaped. /// The literal content will be escaped, see initLit if you don't want this behavior. -pub fn initLitEscaped(alloc: Allocator, literal: []const u8) !Self { - return .initLit(alloc, try html.escape(alloc, literal)); +pub fn initLitEscaped(gpa: Allocator, literal: []const u8) !Self { + return .initLit(gpa, try html.escape(gpa, literal)); } pub fn deinit(self: *Self) void { @@ -57,75 +57,75 @@ pub fn deinit(self: *Self) void { var v = it; v.deinit(); } - self.content.deinit(self.alloc); - if (self.literal) |it| self.alloc.free(it); + self.content.deinit(self.gpa); + if (self.literal) |it| self.gpa.free(it); } -pub fn render(self: *Self, alloc: Allocator) ![]const u8 { - const attr = try self.renderAttribute(alloc); - defer if (attr) |it| alloc.free(it); - var acc = try std.ArrayList(u8).initCapacity(alloc, self.content.items.len + if (self.literal) |it| it.len else 0); - errdefer acc.deinit(alloc); +pub fn render(self: *Self, gpa: Allocator) ![]const u8 { + const attr = try self.renderAttribute(gpa); + defer if (attr) |it| gpa.free(it); + var acc = try std.ArrayList(u8).initCapacity(gpa, self.content.items.len + if (self.literal) |it| it.len else 0); + errdefer acc.deinit(gpa); if (self.tag) |tag| { - try acc.append(alloc, '<'); - try acc.appendSlice(alloc, tag); - if (attr) |it| try acc.appendSlice(alloc, it); - try acc.append(alloc, '>'); + try acc.append(gpa, '<'); + try acc.appendSlice(gpa, tag); + if (attr) |it| try acc.appendSlice(gpa, it); + try acc.append(gpa, '>'); } switch (self.kind) { - .void => return acc.toOwnedSlice(alloc), + .void => return acc.toOwnedSlice(gpa), .content => { for (self.content.items) |it| { var v = it; - const sub = try v.render(alloc); - defer alloc.free(sub); - try acc.appendSlice(alloc, sub); + const sub = try v.render(gpa); + defer gpa.free(sub); + try acc.appendSlice(gpa, sub); } }, - .literal => try acc.appendSlice(alloc, self.literal.?), + .literal => try acc.appendSlice(gpa, self.literal.?), } if (self.tag) |tag| { - try acc.appendSlice(alloc, "</"); - try acc.appendSlice(alloc, tag); - try acc.append(alloc, '>'); + try acc.appendSlice(gpa, "</"); + try acc.appendSlice(gpa, tag); + try acc.append(gpa, '>'); } - return acc.toOwnedSlice(alloc); + return acc.toOwnedSlice(gpa); } -fn renderAttribute(self: *Self, alloc: Allocator) !?[]const u8 { - const class = try self.renderClass(alloc); - defer if (class) |it| alloc.free(it); +fn renderAttribute(self: *Self, gpa: Allocator) !?[]const u8 { + const class = try self.renderClass(gpa); + defer if (class) |it| gpa.free(it); if (class) |it| try self.setAttribute("class", it); var iter = self.attributes.iterator(); if (iter.len == 0) return null; - var acc = try std.ArrayList(u8).initCapacity(alloc, iter.len); - errdefer acc.deinit(alloc); - try acc.append(alloc, ' '); + var acc = try std.ArrayList(u8).initCapacity(gpa, iter.len); + errdefer acc.deinit(gpa); + try acc.append(gpa, ' '); 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, ' '); + try acc.appendSlice(gpa, it.key_ptr.*); + try acc.appendSlice(gpa, "=\""); + const escape = try html.escape(gpa, it.value_ptr.*); + defer gpa.free(escape); + try acc.appendSlice(gpa, escape); + try acc.append(gpa, '"'); + if (i < iter.len - 1) try acc.append(gpa, ' '); } - return try acc.toOwnedSlice(alloc); + return try acc.toOwnedSlice(gpa); } -fn renderClass(self: *const Self, alloc: Allocator) !?[]const u8 { +fn renderClass(self: *const Self, gpa: Allocator) !?[]const u8 { var iter = self.class_list.iterator(); if (iter.len == 0) return null; const n = self.class_list.count(); - var acc = try std.ArrayList(u8).initCapacity(alloc, n); - errdefer acc.deinit(alloc); + var acc = try std.ArrayList(u8).initCapacity(gpa, n); + errdefer acc.deinit(gpa); var i: usize = 0; while (iter.next()) |it| : (i += 1) { - try acc.appendSlice(alloc, it.*); - if (i < n - 1) try acc.append(alloc, ' '); + try acc.appendSlice(gpa, it.*); + if (i < n - 1) try acc.append(gpa, ' '); } - return try acc.toOwnedSlice(alloc); + return try acc.toOwnedSlice(gpa); } pub fn setAttribute(self: *Self, k: []const u8, v: []const u8) !void { @@ -153,32 +153,32 @@ pub fn removeClass(self: *Self, v: []const u8) void { } pub fn appendContent(self: *Self, content: Self) !void { - return self.content.append(self.alloc, content); + return self.content.append(self.gpa, content); } -pub fn initImg(alloc: Allocator, src: []const u8, alt: []const u8) !Self { - var el = init(alloc, .void, "img"); +pub fn initImg(gpa: Allocator, src: []const u8, alt: []const u8) !Self { + var el = init(gpa, .void, "img"); try el.setAttribute("src", src); try el.setAttribute("alt", alt); return el; } -pub fn initContent(alloc: Allocator, tag: []const u8, content: []Self) !Self { - var el = init(alloc, .content, tag); +pub fn initContent(gpa: Allocator, tag: []const u8, content: []Self) !Self { + var el = init(gpa, .content, tag); for (content) |it| try el.appendContent(it); return el; } /// Init a paragraph tag with an automatically escaped content. -pub fn initParagraph(alloc: Allocator, content: []const u8) !Self { - var el = init(alloc, .content, "p"); - try el.appendContent(try initLitEscaped(alloc, content)); +pub fn initParagraph(gpa: Allocator, content: []const u8) !Self { + var el = init(gpa, .content, "p"); + try el.appendContent(try initLitEscaped(gpa, content)); return el; } -fn doTest(alloc: Allocator, el: *Self, exp: []const u8) !void { - const got = try el.render(alloc); - defer alloc.free(got); +fn doTest(gpa: Allocator, el: *Self, exp: []const u8) !void { + const got = try el.render(gpa); + defer gpa.free(got); std.testing.expect(eql(u8, got, exp)) catch |err| { std.debug.print("{s}\n", .{got}); return err; @@ -188,49 +188,49 @@ fn doTest(alloc: Allocator, el: *Self, exp: []const u8) !void { test "void element" { var arena = std.heap.DebugAllocator(.{}).init; defer _ = arena.deinit(); - const alloc = arena.allocator(); + const gpa = arena.allocator(); - var br = init(alloc, .void, "br"); + var br = init(gpa, .void, "br"); defer br.deinit(); - try doTest(alloc, &br, "<br>"); + try doTest(gpa, &br, "<br>"); - var img = init(alloc, .void, "img"); + var img = init(gpa, .void, "img"); defer img.deinit(); try img.setAttribute("src", "foo"); try img.setAttribute("alt", "bar"); - try doTest(alloc, &img, "<img src=\"foo\" alt=\"bar\">"); + try doTest(gpa, &img, "<img src=\"foo\" alt=\"bar\">"); - var img2 = try initImg(alloc, "foo", "bar"); + var img2 = try initImg(gpa, "foo", "bar"); defer img2.deinit(); - try doTest(alloc, &img2, "<img src=\"foo\" alt=\"bar\">"); + try doTest(gpa, &img2, "<img src=\"foo\" alt=\"bar\">"); } test "content element" { var arena = std.heap.DebugAllocator(.{}).init; defer _ = arena.deinit(); - const alloc = arena.allocator(); + const gpa = arena.allocator(); - var p = init(alloc, .content, "p"); + var p = init(gpa, .content, "p"); defer p.deinit(); - var content = initLit(alloc, try alloc.dupe(u8, "hello world")); + var content = initLit(gpa, try gpa.dupe(u8, "hello world")); try p.appendContent(content); - try doTest(alloc, &content, "hello world"); - try doTest(alloc, &p, "<p>hello world</p>"); + try doTest(gpa, &content, "hello world"); + try doTest(gpa, &p, "<p>hello world</p>"); - var p_managed = try initParagraph(alloc, "hello world"); + var p_managed = try initParagraph(gpa, "hello world"); defer p_managed.deinit(); - try doTest(alloc, &p_managed, "<p>hello world</p>"); + try doTest(gpa, &p_managed, "<p>hello world</p>"); - var div = init(alloc, .content, "div"); + var div = init(gpa, .content, "div"); defer div.deinit(); try div.appendClass("foo-bar"); - try div.appendContent(try initParagraph(alloc, "hello world")); - try div.appendContent(try initImg(alloc, "example.org", "example")); + try div.appendContent(try initParagraph(gpa, "hello world")); + try div.appendContent(try initImg(gpa, "example.org", "example")); - try doTest(alloc, &div, "<div class=\"foo-bar\"><p>hello world</p><img src=\"example.org\" alt=\"example\"></div>"); + try doTest(gpa, &div, "<div class=\"foo-bar\"><p>hello world</p><img src=\"example.org\" alt=\"example\"></div>"); } diff --git a/src/dom/html.zig b/src/dom/html.zig index bf62fbb..4b39a3f 100644 --- a/src/dom/html.zig +++ b/src/dom/html.zig @@ -24,9 +24,9 @@ pub fn escape(gpa: std.mem.Allocator, v: []const u8) ![]const u8 { return acc.toOwnedSlice(gpa); } -fn doTest(alloc: std.mem.Allocator, el: []const u8, exp: []const u8) !void { - const got = try escape(alloc, el); - defer alloc.free(got); +fn doTest(gpa: std.mem.Allocator, el: []const u8, exp: []const u8) !void { + const got = try escape(gpa, el); + defer gpa.free(got); std.testing.expect(eql(u8, got, exp)) catch |err| { std.debug.print("{s}\n", .{got}); return err; @@ -36,12 +36,12 @@ fn doTest(alloc: std.mem.Allocator, el: []const u8, exp: []const u8) !void { test "escaping html" { var arena = std.heap.DebugAllocator(.{}).init; defer _ = arena.deinit(); - const alloc = arena.allocator(); + const gpa = arena.allocator(); - try doTest(alloc, "hello world", "hello world"); - try doTest(alloc, "hello&world", "hello&world"); - try doTest(alloc, "hello'world", "hello'world"); - try doTest(alloc, "hello<world", "hello<world"); - try doTest(alloc, "hello>world", "hello>world"); - try doTest(alloc, "hello\"world", "hello"world"); + try doTest(gpa, "hello world", "hello world"); + try doTest(gpa, "hello&world", "hello&world"); + try doTest(gpa, "hello'world", "hello'world"); + try doTest(gpa, "hello<world", "hello<world"); + try doTest(gpa, "hello>world", "hello>world"); + try doTest(gpa, "hello\"world", "hello"world"); } |
