aboutsummaryrefslogtreecommitdiff
path: root/src/eval/Image.zig
blob: 7991d692ef215aaec589f9d4bde41ecac2011dbe (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
const std = @import("std");
const Allocator = std.mem.Allocator;
const HTML = @import("html/Element.zig");
const Element = @import("Element.zig");

const Self = @This();

src: []const u8,
alt: ?[]const u8 = null,
source: ?Element = null,

pub fn init(alloc: Allocator, src: []const u8) !*Self {
    const v = try alloc.create(Self);
    v.* = .{
        .src = src,
    };
    return v;
}

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

pub fn deinit(self: *Self, alloc: Allocator) void {
    destroy(self, alloc);
}

fn destroy(context: *anyopaque, alloc: Allocator) void {
    const self: *Self = @ptrCast(@alignCast(context));
    if (self.source) |it| it.deinit(alloc);
    alloc.destroy(self);
}

fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
    const self: *Self = @ptrCast(@alignCast(context));

    var img = try HTML.init(alloc, .void, "img");
    errdefer img.deinit();
    try img.setAttribute("src", self.src);
    if (self.alt) |it| try img.setAttribute("alt", it);
    var el = try HTML.init(alloc, .content, "figure");
    errdefer el.deinit();
    try el.appendContent(img);

    const source = self.source orelse return el;
    var caption = try HTML.init(alloc, .content, "figcaption");
    errdefer caption.deinit();
    try caption.appendContent(try source.html(alloc));
    try el.appendContent(caption);
    return el;
}

test "html" {
    const alloc = std.testing.allocator;
    const expect = std.testing.expect;
    const eql = std.mem.eql;

    var img = try init(alloc, "foo");
    defer img.deinit(alloc);
    const h = try img.element().renderHTML(alloc);
    defer alloc.free(h);
    try expect(eql(u8, h, "<figure><img src=\"foo\"></figure>"));

    img.alt = "bar";
    const h2 = try img.element().renderHTML(alloc);
    defer alloc.free(h2);
    try expect(eql(u8, h2, "<figure><img src=\"foo\" alt=\"bar\"></figure>"));

    const in = try Element.Empty.init(alloc);
    try in.content.append(alloc, (try Element.Literal.init(alloc, "caption")).element());
    img.source = in.element();
    const h3 = try img.element().renderHTML(alloc);
    defer alloc.free(h3);
    try expect(eql(u8, h3, "<figure><img src=\"foo\" alt=\"bar\"><figcaption>caption</figcaption></figure>"));
}