mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-30 07:31:47 +00:00
netsurf: fix DocumentHTMLParseFromStr and add test
This commit is contained in:
@@ -1396,6 +1396,17 @@ fn parserErr(err: HubbubErr) ParserError!void {
|
|||||||
// documentHTMLParseFromFile parses the given HTML file.
|
// documentHTMLParseFromFile parses the given HTML file.
|
||||||
// The caller is responsible for closing the document.
|
// The caller is responsible for closing the document.
|
||||||
pub fn documentHTMLParseFromFile(file: std.fs.File) !*DocumentHTML {
|
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 parser: ?*c.dom_hubbub_parser = undefined;
|
||||||
var doc: ?*c.dom_document = undefined;
|
var doc: ?*c.dom_document = undefined;
|
||||||
var err: c.hubbub_error = 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 buffer: [1024]u8 = undefined;
|
||||||
var ln = buffer.len;
|
var ln = buffer.len;
|
||||||
while (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);
|
err = c.dom_hubbub_parser_parse_chunk(parser, &buffer, ln);
|
||||||
try parserErr(err);
|
try parserErr(err);
|
||||||
}
|
}
|
||||||
@@ -1428,36 +1439,6 @@ pub fn documentHTMLParseFromFile(file: std.fs.File) !*DocumentHTML {
|
|||||||
return @as(*DocumentHTML, @ptrCast(doc.?));
|
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(¶ms, &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.
|
// documentHTMLClose closes the document.
|
||||||
pub fn documentHTMLClose(doc: *DocumentHTML) !void {
|
pub fn documentHTMLClose(doc: *DocumentHTML) !void {
|
||||||
const err = documentHTMLVtable(doc).close.?(doc);
|
const err = documentHTMLVtable(doc).close.?(doc);
|
||||||
|
|||||||
@@ -98,3 +98,14 @@ test {
|
|||||||
|
|
||||||
try jsruntime.loadEnv(&arena_alloc, testsAllExecFn, apis);
|
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 {};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user