netsurf: fix DocumentHTMLParseFromStr and add test

This commit is contained in:
Pierre Tachoire
2024-01-09 11:33:02 +01:00
parent 57e948ded5
commit cc3a7ec4a1
2 changed files with 23 additions and 31 deletions

View File

@@ -1396,6 +1396,17 @@ fn parserErr(err: HubbubErr) ParserError!void {
// documentHTMLParseFromFile parses the given HTML file.
// The caller is responsible for closing the document.
pub fn documentHTMLParseFromFile(file: std.fs.File) !*DocumentHTML {
return try documentHTMLParse(file.reader());
}
// documentHTMLParseFromStr parses the given HTML string.
// The caller is responsible for closing the document.
pub fn documentHTMLParseFromStr(str: []const u8) !*DocumentHTML {
var fbs = std.io.fixedBufferStream(str);
return try documentHTMLParse(fbs.reader());
}
pub fn documentHTMLParse(reader: anytype) !*DocumentHTML {
var parser: ?*c.dom_hubbub_parser = undefined;
var doc: ?*c.dom_document = undefined;
var err: c.hubbub_error = undefined;
@@ -1417,7 +1428,7 @@ pub fn documentHTMLParseFromFile(file: std.fs.File) !*DocumentHTML {
var buffer: [1024]u8 = undefined;
var ln = buffer.len;
while (ln == buffer.len) {
ln = try file.readAll(&buffer);
ln = try reader.read(&buffer);
err = c.dom_hubbub_parser_parse_chunk(parser, &buffer, ln);
try parserErr(err);
}
@@ -1428,36 +1439,6 @@ pub fn documentHTMLParseFromFile(file: std.fs.File) !*DocumentHTML {
return @as(*DocumentHTML, @ptrCast(doc.?));
}
// documentHTMLParseFromStr parses the given HTML string.
// The caller is responsible for closing the document.
pub fn documentHTMLParseFromStr(str: []const u8) ParserError!*DocumentHTML {
var parser: ?*c.dom_hubbub_parser = undefined;
var doc: ?*c.dom_document = undefined;
var err: c.hubbub_error = undefined;
var params = c.dom_hubbub_parser_params{
.enc = null,
.fix_enc = true,
.msg = null,
.script = null,
.enable_script = false,
.ctx = null,
.daf = null,
};
err = c.dom_hubbub_parser_create(&params, &parser, &doc);
try parserErr(err);
defer c.dom_hubbub_parser_destroy(parser);
err = c.dom_hubbub_parser_parse_chunk(parser, str, str.len);
try parserErr(err);
err = c.dom_hubbub_parser_completed(parser);
try parserErr(err);
return @as(*DocumentHTML, @ptrCast(doc.?));
}
// documentHTMLClose closes the document.
pub fn documentHTMLClose(doc: *DocumentHTML) !void {
const err = documentHTMLVtable(doc).close.?(doc);

View File

@@ -98,3 +98,14 @@ test {
try jsruntime.loadEnv(&arena_alloc, testsAllExecFn, apis);
}
test "DocumentHTMLParseFromStr" {
const file = try std.fs.cwd().openFile("test.html", .{});
defer file.close();
const str = try file.readToEndAlloc(std.testing.allocator, std.math.maxInt(u32));
defer std.testing.allocator.free(str);
doc = try parser.documentHTMLParseFromStr(str);
parser.documentHTMLClose(doc) catch {};
}