Escape HTML
This commit is contained in:
parent
bbbaa3f8b0
commit
25cd5f982b
1 changed files with 12 additions and 2 deletions
|
@ -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("<"),
|
||||
'>' => try writer.writeAll(">"),
|
||||
'"' => try writer.writeAll("""),
|
||||
'&' => try writer.writeAll("&"),
|
||||
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));
|
||||
|
||||
|
|
Loading…
Reference in a new issue