diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-25 17:18:00 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-25 17:18:00 +0200 |
| commit | c008d747534f4c8aa59b045efbca754618e14b41 (patch) | |
| tree | 68c094fed420230985724c300195c0389d2339e8 /src/dom/html.zig | |
| parent | a3e7c462dadadc6986d93f6f0203ca7a02863ef8 (diff) | |
style(eval): move in its own package
Diffstat (limited to 'src/dom/html.zig')
| -rw-r--r-- | src/dom/html.zig | 47 |
1 files changed, 0 insertions, 47 deletions
diff --git a/src/dom/html.zig b/src/dom/html.zig deleted file mode 100644 index 064ebb1..0000000 --- a/src/dom/html.zig +++ /dev/null @@ -1,47 +0,0 @@ -const std = @import("std"); -const eql = std.mem.eql; - -pub const Error = error{InvalidUtf8} || std.mem.Allocator.Error; - -pub fn escape(alloc: std.mem.Allocator, v: []const u8) Error![]const u8 { - var acc = try std.ArrayList(u8).initCapacity(alloc, v.len); - errdefer acc.deinit(alloc); - const view = try std.unicode.Utf8View.init(v); - var iter = view.iterator(); - while (iter.nextCodepointSlice()) |rune| { - if (eql(u8, rune, "&")) { - try acc.appendSlice(alloc, "&"); - } else if (eql(u8, rune, "'")) { - try acc.appendSlice(alloc, "'"); - } else if (eql(u8, rune, "<")) { - try acc.appendSlice(alloc, "<"); - } else if (eql(u8, rune, ">")) { - try acc.appendSlice(alloc, ">"); - } else if (eql(u8, rune, "\"")) { - try acc.appendSlice(alloc, """); - } else { - try acc.appendSlice(alloc, rune); - } - } - return acc.toOwnedSlice(alloc); -} - -fn doTest(alloc: std.mem.Allocator, el: []const u8, exp: []const u8) !void { - const got = try escape(alloc, el); - defer alloc.free(got); - std.testing.expect(eql(u8, got, exp)) catch |err| { - std.debug.print("{s}\n", .{got}); - return err; - }; -} - -test "escaping html" { - const alloc = std.testing.allocator; - - try doTest(alloc, "hello world", "hello world"); - try doTest(alloc, "hello&world", "hello&world"); - try doTest(alloc, "hello'world", "hello'world"); - try doTest(alloc, "hello<world", "hello<world"); - try doTest(alloc, "hello>world", "hello>world"); - try doTest(alloc, "hello\"world", "hello"world"); -} |
