Escape HTML

This commit is contained in:
jaina heartles 2022-12-10 02:44:54 -08:00
parent bbbaa3f8b0
commit 25cd5f982b
1 changed files with 12 additions and 2 deletions

View File

@ -7,7 +7,7 @@ pub fn main() !void {
.{ .test_tmpl = "{.x} {%context_foo}" },
@embedFile("./test.tmp.html"),
.{
.community = .{ .name = "abcd" },
.community = .{ .name = "<abcd>" },
.foo = [_][]const u8{ "5", "4", "3", "2", "1" },
.baz = [_][]const []const u8{
&.{ "5", "4", "3", "2", "1" },
@ -184,9 +184,19 @@ fn executeStatement(
}
}
fn htmlEscape(writer: anytype, str: []const u8) !void {
for (str) |ch| switch (ch) {
'<' => try writer.writeAll("&lt;"),
'>' => try writer.writeAll("&gt;"),
'"' => try writer.writeAll("&quot;"),
'&' => try writer.writeAll("&amp;"),
else => try writer.writeByte(ch),
};
}
fn print(writer: anytype, arg: anytype) !void {
const T = @TypeOf(arg);
if (comptime std.meta.trait.isZigString(T)) return writer.writeAll(arg);
if (comptime std.meta.trait.isZigString(T)) return htmlEscape(writer, arg);
if (comptime std.meta.trait.isNumber(T)) return std.fmt.format(writer, "{}", .{arg});
@compileLog(@TypeOf(arg));