1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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,
};
}
|