aboutsummaryrefslogtreecommitdiff
path: root/src/eval/html/Literal.zig
blob: d3b697f8f8ddd7020bb919d709abf2ecb16c452e (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const List = std.ArrayList;
const html = @import("html.zig");
const Element = @import("Element.zig");
const Error = Element.Error;

literal: []const u8,

const Self = @This();

pub fn init(alloc: Allocator, literal: []const u8) Error!*Element.Literal {
    const v = try alloc.create(Self);
    v.* = .{ .literal = try html.escape(alloc, literal) };
    return v;
}

pub fn element(self: *Self) Element {
    return .{ .vtable = .{ .render = Self.render }, .ptr = self };
}

fn render(context: *anyopaque, alloc: Allocator) Error![]const u8 {
    const self: *Self = @ptrCast(@alignCast(context));
    return try alloc.dupe(u8, self.literal);
}