diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 17:44:46 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 17:44:46 +0200 |
| commit | 0260f392c19db7911496172c7d43d1178c6215cb (patch) | |
| tree | c5a1f8ff56d3ea727da1d8d29059fc046b102890 /src/z/html/parser.zig | |
| parent | ec30b659dfd337f5edf512ea78032fe18251b704 (diff) | |
add html parser
Diffstat (limited to 'src/z/html/parser.zig')
| -rw-r--r-- | src/z/html/parser.zig | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/z/html/parser.zig b/src/z/html/parser.zig new file mode 100644 index 0000000..ee926f1 --- /dev/null +++ b/src/z/html/parser.zig @@ -0,0 +1,173 @@ +const std = @import("std"); +const mod = @import("root.zig"); +const Element = mod.Element; +const Attribute = mod.Attribute; +const Tag = mod.Tag; +const Fragment = mod.Fragment; + +const alphanumeric: []const u8 = std.ascii.letters ++ .{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; +const void_elements: std.StaticStringMap(void) = .initComptime(.{ + .{"area"}, + .{"base"}, + .{"br"}, + .{"col"}, + .{"embed"}, + .{"hr"}, + .{"img"}, + .{"input"}, + .{"link"}, + .{"meta"}, + .{"param"}, + .{"source"}, + .{"track"}, + .{"wbr"}, +}); + +pub fn fragment(buffer: []const u8) Fragment { + var elements: []const Element = &.{}; + var buf = buffer; + + while (buf.len > 0) { + const e, buf = element(buf); + elements = elements ++ .{e}; + } + + return .{ .elements = elements }; +} + +pub fn element(buffer: []const u8) struct { Element, []const u8 } { + if (buffer.len == 0) @panic("EOB, expected element"); + + switch (buffer[0]) { + '<' => { + const t, const b = tag(buffer); + return .{ .{ .tag = t }, b }; + }, + else => { + const t, const b = text(buffer); + return .{ .{ .string = t }, b }; + }, + } +} + +pub fn tag(buffer: []const u8) struct { Tag, []const u8 } { + if (buffer.len == 0) @panic("EOB, expected tag"); + + if (buffer[0] != '<') @panic("expected '<'"); + + var buf: []const u8 = buffer[1..]; + + const name = name: { + const index = std.mem.findNone(u8, buf, alphanumeric) orelse @panic("uncosed tag"); + defer buf = buf[index..]; + break :name buf[0..index]; + }; + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + var attributes: []const Attribute = &.{}; + + while (buf.len > 0 and buf[0] != '>') { + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + const attr, buf = attribute(buf); + attributes = attributes ++ .{attr}; + } + + if (buf.len == 0 or buf[0] != '>') @panic("expected tag-close"); + + buf = buf[1..]; + + if (void_elements.has(name)) { + return .{.{ + .name = name, + .attributes = attributes, + .content = .void, + }}; + } + + var children: []const Element = &.{}; + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + const closing = std.fmt.comptimePrint("</{s}>", .{name}); + + while (buf.len > 0 and !std.mem.startsWith(u8, buf, closing)) { + const child, buf = element(buf); + children = children ++ .{child}; + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + } + + if (!std.mem.startsWith(u8, buf, closing)) @panic("expected closing-tag"); + + buf = buf[closing.len..]; + + return .{ + .{ + .name = name, + .attributes = attributes, + .content = .{ .children = children }, + }, + buf, + }; +} + +pub fn text(buffer: []const u8) struct { []const u8, []const u8 } { + const index = std.mem.findScalar(u8, buffer, '<') orelse buffer.len; + return .{ + std.mem.trim(u8, buffer[0..index], &std.ascii.whitespace), + buffer[index..], + }; +} + +pub fn attribute(buffer: []const u8) struct { Attribute, []const u8 } { + var buf = buffer; + + const name = name: { + const index = std.mem.findAny( + u8, + buf, + ">=\"'" ++ std.ascii.whitespace, + ) orelse @panic("EOB, expected attr-value"); + defer buf = buf[index..]; + break :name buf[0..index]; + }; + + if (name.len == 0) @panic("expected attr-name"); + + buf = std.mem.trimStart(u8, buf, &std.ascii.whitespace); + + if (buf.len == 0 or buf[0] == '>') { + return .{ .name = name, .value = "" }; + } + + if (buf[0] != '=') @panic("expected attr-eql"); + + buf = std.mem.trimStart(u8, buf[1..], &std.ascii.whitespace); + + const delim = switch (buf[0]) { + '"' => "\"", + '\'' => "'", + else => std.ascii.whitespace ++ "=\"'<>`", + }; + + if (delim.len == 1) { + buf = buf[1..]; + } + + const value = value: { + const index = std.mem.findAny(u8, buf, delim) orelse if (delim.len == 1) + @panic("unclosed attr-value") + else + buf.len; + defer buf = buf[index + @intFromBool(delim.len == 1) ..]; + break :value buf[0..index]; + }; + + return .{ + .{ + .name = name, + .value = value, + }, + buf, + }; +} |