Skip to content

Commit f237219

Browse files
committed
escape incoming plain text
1 parent ec71f8e commit f237219

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/browser/page.zig

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,14 @@ pub const Page = struct {
214214
const doc = parser.documentHTMLToDocument(self.window.document);
215215
const list = try parser.documentGetElementsByTagName(doc, "pre");
216216
const pre = try parser.nodeListItem(list, 0) orelse return error.InvalidHTML;
217-
return Dump.writeChildren(pre, .{}, out);
217+
const walker = Walker{};
218+
var next: ?*parser.Node = null;
219+
while (true) {
220+
next = try walker.get_next(pre, next) orelse break;
221+
const v = try parser.nodeTextContent(next.?) orelse return;
222+
try out.writeAll(v);
223+
}
224+
return;
218225
},
219226
.html => {
220227
// maybe page.wait timed-out, print what we have
@@ -656,7 +663,24 @@ pub const Page = struct {
656663
}
657664

658665
switch (self.mode) {
659-
.html, .text => |*p| try p.process(data),
666+
.html => |*p| try p.process(data),
667+
.text => |*p| {
668+
// we have to escape the data...
669+
var v = data;
670+
while (v.len > 0) {
671+
const index = std.mem.indexOfAnyPos(u8, v, 0, &.{ '<', '>' }) orelse {
672+
try p.process(v);
673+
return;
674+
};
675+
try p.process(v[0..index]);
676+
switch (v[index]) {
677+
'<' => try p.process("&lt;"),
678+
'>' => try p.process("&gt;"),
679+
else => unreachable,
680+
}
681+
v = v[index + 1 ..];
682+
}
683+
},
660684
.raw => |*buf| try buf.appendSlice(self.arena, data),
661685
.pre => unreachable,
662686
.parsed => unreachable,

0 commit comments

Comments
 (0)