Skip to content

Commit 7335b1d

Browse files
committed
escape incoming plain text
1 parent ec71f8e commit 7335b1d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/browser/page.zig

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,18 @@ pub const Page = struct {
209209
},
210210
.raw_done => |data| return out.writeAll(data),
211211
.text => {
212-
// processed below, along with .html
213-
// return the <pre> element from the HTML
212+
// returns the <pre> element from the HTML
214213
const doc = parser.documentHTMLToDocument(self.window.document);
215214
const list = try parser.documentGetElementsByTagName(doc, "pre");
216215
const pre = try parser.nodeListItem(list, 0) orelse return error.InvalidHTML;
217-
return Dump.writeChildren(pre, .{}, out);
216+
const walker = Walker{};
217+
var next: ?*parser.Node = null;
218+
while (true) {
219+
next = try walker.get_next(pre, next) orelse break;
220+
const v = try parser.nodeTextContent(next.?) orelse return;
221+
try out.writeAll(v);
222+
}
223+
return;
218224
},
219225
.html => {
220226
// maybe page.wait timed-out, print what we have
@@ -656,7 +662,24 @@ pub const Page = struct {
656662
}
657663

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

0 commit comments

Comments
 (0)