aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-17 17:54:40 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-17 17:57:45 +0200
commitc745ee1db63f46a393a4f4fc95a56a64ca9898b0 (patch)
tree9cc871067e46bf26605e6746df3791e7e360a0aa /src
parent446e80ef73ab201af898aeb475b304cae426b8fd (diff)
fix(dom): not escaping content of paragraph
Diffstat (limited to 'src')
-rw-r--r--src/dom/Element.zig19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/dom/Element.zig b/src/dom/Element.zig
index eee8c38..3aa4986 100644
--- a/src/dom/Element.zig
+++ b/src/dom/Element.zig
@@ -19,6 +19,8 @@ class_list: std.BufSet,
content: std.ArrayList(Self) = .empty,
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 {
return .{
.kind = knd,
@@ -29,6 +31,9 @@ pub fn init(alloc: Allocator, knd: Kind, tag: []const u8) Self {
};
}
+/// 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 {
return .{
.kind = .literal,
@@ -39,6 +44,12 @@ pub fn initLit(alloc: Allocator, literal: []const u8) Self {
};
}
+/// 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 deinit(self: *Self) void {
self.attributes.deinit();
self.class_list.deinit();
@@ -47,6 +58,7 @@ pub fn deinit(self: *Self) void {
v.deinit();
}
self.content.deinit(self.alloc);
+ if (self.literal) |it| self.alloc.free(it);
}
pub fn render(self: *Self, alloc: Allocator) ![]const u8 {
@@ -105,9 +117,9 @@ fn renderAttribute(self: *Self, alloc: Allocator) !?[]const u8 {
fn renderClass(self: *const Self, alloc: 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, 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.*);
@@ -157,9 +169,10 @@ pub fn initContent(alloc: Allocator, tag: []const u8, content: []Self) !Self {
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(initLit(alloc, content));
+ try el.appendContent(initLitEscaped(alloc, content));
return el;
}
@@ -202,7 +215,7 @@ test "content element" {
var p = init(alloc, .content, "p");
defer p.deinit();
- var content = initLit(alloc, "hello world");
+ var content = initLit(alloc, try alloc.dupe(u8, "hello world"));
try p.appendContent(content);
try doTest(alloc, &content, "hello world");