download patch
commit f14d54431aa4cd1fb65aaecb6b10567f4485f758
Author: tri <tri@thac.loan>
Date: Sun Sep 28 12:09:04 2025 +0700
implement html escape util
Not used yet though.
diff --git a/src/html.zig b/src/html.zig
new file mode 100644
index 0000000..50eafb5
--- /dev/null
+++ b/src/html.zig
+const std = @import("std");
+const t = std.testing;
+
+pub fn escape(
+ out: *std.Io.Writer,
+ text: []const u8,
+) !void {
+ for (text) |char| {
+ switch (char) {
+ '&' => try out.writeAll("&"),
+ '"' => try out.writeAll("""),
+ '\'' => try out.writeAll("'"),
+ '<' => try out.writeAll("<"),
+ '>' => try out.writeAll(">"),
+ else => try out.writeByte(char),
+ }
+ }
+}
+
+test "escape" {
+ var buf: [4096]u8 = undefined;
+ inline for (.{
+ .{
+ "<script>alert(\"jello\")</script>",
+ "<script>alert("jello")</script>",
+ },
+ .{
+ "&\"'<>",
+ "&"'<>",
+ },
+ }) |case| {
+ const input = case[0];
+ const expected = case[1];
+ var w = std.Io.Writer.fixed(&buf);
+ try escape(&w, input);
+ try t.expectEqualStrings(expected, w.buffered());
+ }
+}