|
| 1 | +# zig-xml |
| 2 | + |
| 3 | +A small, lenient, allocation-light **XML/HTML tokenizer** for Zig, with HTML5 |
| 4 | +character-reference decoding. |
| 5 | + |
| 6 | +It is a *pull* tokenizer: you call `next()` and get one structural event at a |
| 7 | +time — element open/close, text, CDATA, comments, processing instructions, and |
| 8 | +declarations — with every name and raw value **borrowed from the input**. There |
| 9 | +is no allocation per event (attribute storage is a single reused list), and no |
| 10 | +implicit entity decoding; decoding is a separate, explicit step. |
| 11 | + |
| 12 | +Leniency is deliberate. zig-xml is built to inspect untrusted, possibly |
| 13 | +malformed markup (WAF request bodies, scrapers, editor tooling) the way |
| 14 | +browsers and Go's non-strict `encoding/xml` do — not to validate |
| 15 | +well-formedness. Unterminated constructs run to end of input instead of |
| 16 | +erroring, mismatched close tags are reported verbatim, and unquoted or |
| 17 | +valueless attributes are accepted. It performs no DTD processing and no external |
| 18 | +entity resolution, so it is not exposed to XXE or entity-expansion attacks. |
| 19 | + |
| 20 | +## Install |
| 21 | + |
| 22 | +With [Pantry](https://github.com/home-lang/pantry): |
| 23 | + |
| 24 | +```sh |
| 25 | +pantry add zig-xml |
| 26 | +``` |
| 27 | + |
| 28 | +Then add the module in `build.zig`: |
| 29 | + |
| 30 | +```zig |
| 31 | +const xml = b.dependency("xml", .{ .target = target, .optimize = optimize }); |
| 32 | +your_module.addImport("xml", xml.module("xml")); |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +```zig |
| 38 | +const std = @import("std"); |
| 39 | +const xml = @import("xml"); |
| 40 | +
|
| 41 | +pub fn main() !void { |
| 42 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 43 | + const allocator = gpa.allocator(); |
| 44 | +
|
| 45 | + var reader = xml.Reader.init(allocator, "<a href=\"?x=1&y=2\">hello</a>"); |
| 46 | + defer reader.deinit(); |
| 47 | +
|
| 48 | + while (try reader.next()) |event| switch (event) { |
| 49 | + .open, .self_closing => |element| { |
| 50 | + std.debug.print("<{s}>\n", .{element.name}); |
| 51 | + for (element.attributes) |attr| { |
| 52 | + // Attribute values are raw; decode when you need the text. |
| 53 | + const value = try xml.decode(allocator, attr.value); |
| 54 | + defer allocator.free(value); |
| 55 | + std.debug.print(" {s} = {s}\n", .{ attr.name, value }); |
| 56 | + } |
| 57 | + }, |
| 58 | + .text => |raw| { |
| 59 | + const text = try xml.decode(allocator, raw); |
| 60 | + defer allocator.free(text); |
| 61 | + std.debug.print("text: {s}\n", .{text}); |
| 62 | + }, |
| 63 | + .close => |name| std.debug.print("</{s}>\n", .{name}), |
| 64 | + else => {}, |
| 65 | + }; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Attribute slices (and the `Element.attributes` list) are valid only until the |
| 70 | +next call to `next()` — copy anything you need to retain. |
| 71 | + |
| 72 | +## API |
| 73 | + |
| 74 | +- `Reader.init(allocator, input) Reader` / `reader.deinit()` |
| 75 | +- `reader.next() !?Event` |
| 76 | +- `Event` — `open` / `self_closing` (`Element`), `close` (name), `text`, |
| 77 | + `cdata`, `comment`, `processing_instruction`, `declaration` |
| 78 | +- `Element{ name, attributes }`, with `element.attribute(name) ?[]const u8` |
| 79 | +- `decode(allocator, raw) ![]u8` / `decodeInto(&list, allocator, raw) !void` |
| 80 | +- `entity.lookup(name) ?[2]u21` — the underlying HTML5 named-entity table |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +MIT. The bundled HTML5 named-entity table is vendored from Bun (MIT) — see |
| 85 | +[THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). |
0 commit comments