mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
netsurf: refacot again to reuse wr_create_doc_dom_from_file
This commit is contained in:
@@ -4,11 +4,6 @@ const c = @cImport({
|
|||||||
@cInclude("wrapper.h");
|
@cInclude("wrapper.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
// max html file size read and loaded in memory for parsing.
|
|
||||||
// use the same as zig for reference.
|
|
||||||
// see https://github.com/ziglang/zig/blob/9f0d2f94175a131d965e86a6396f5ac508b27bf8/src/main.zig#L71C8-L71C8
|
|
||||||
pub const max_html_size = std.math.maxInt(u32); // 4GB
|
|
||||||
|
|
||||||
// Vtable
|
// Vtable
|
||||||
// ------
|
// ------
|
||||||
|
|
||||||
@@ -559,20 +554,19 @@ fn documentHTMLVtable(doc_html: *DocumentHTML) c.dom_html_document_vtable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// documentHTMLParseFromFile reads the full document, loads the content in a
|
// documentHTMLParseFromFile reads the full document, loads the content in a
|
||||||
// buffer and parse the buffer content.
|
// The allocator is required to create a null terminated string from filename.
|
||||||
// The buffer is freed by the function.
|
// The buffer is freed by the function.
|
||||||
// The caller is responsible for closing the document.
|
// The caller is responsible for closing the document.
|
||||||
pub fn documentHTMLParseFromFile(allocator: std.mem.Allocator, filename: []const u8) !*DocumentHTML {
|
pub fn documentHTMLParseFromFile(allocator: std.mem.Allocator, filename: []const u8) !*DocumentHTML {
|
||||||
var file = try std.fs.cwd().openFile(filename, .{});
|
// create a null terminated c string.
|
||||||
defer file.close();
|
const cstr = try allocator.dupeZ(u8, filename);
|
||||||
|
|
||||||
const file_size = try file.getEndPos();
|
|
||||||
|
|
||||||
// read the file and return the result in a null terminted c string.
|
|
||||||
const cstr = try file.readToEndAllocOptions(allocator, max_html_size, file_size + 1, @alignOf(u8), 0);
|
|
||||||
defer allocator.free(cstr);
|
defer allocator.free(cstr);
|
||||||
|
|
||||||
return documentHTMLParseFromCStr(cstr);
|
const doc = c.wr_create_doc_dom_from_file(cstr.ptr);
|
||||||
|
if (doc == null) {
|
||||||
|
return error.ParserError;
|
||||||
|
}
|
||||||
|
return @as(*DocumentHTML, @ptrCast(doc.?));
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentHTMLParseFromCStrparses the given string.
|
// documentHTMLParseFromCStrparses the given string.
|
||||||
@@ -581,7 +575,7 @@ pub fn documentHTMLParseFromFile(allocator: std.mem.Allocator, filename: []const
|
|||||||
// The caller is responsible for closing the document.
|
// The caller is responsible for closing the document.
|
||||||
pub fn documentHTMLParseFromStr(allocator: std.mem.Allocator, str: [:0]const u8) !*DocumentHTML {
|
pub fn documentHTMLParseFromStr(allocator: std.mem.Allocator, str: [:0]const u8) !*DocumentHTML {
|
||||||
// create a null terminated c string.
|
// create a null terminated c string.
|
||||||
const cstr = std.cstr.addNullByte(allocator, str);
|
const cstr = try allocator.dupeZ(u8, str);
|
||||||
defer allocator.free(cstr);
|
defer allocator.free(cstr);
|
||||||
|
|
||||||
return documentHTMLParseFromCStr(cstr);
|
return documentHTMLParseFromCStr(cstr);
|
||||||
|
|||||||
2
vendor/netsurf/wrapper/wrapper.c
vendored
2
vendor/netsurf/wrapper/wrapper.c
vendored
@@ -59,7 +59,7 @@ dom_document *wr_create_doc_dom_from_string(const char *html)
|
|||||||
* \param file The file path
|
* \param file The file path
|
||||||
* \return pointer to DOM document, or NULL on error
|
* \return pointer to DOM document, or NULL on error
|
||||||
*/
|
*/
|
||||||
dom_document *wr_create_doc_dom_from_file(char *filename)
|
dom_document *wr_create_doc_dom_from_file(const char *filename)
|
||||||
{
|
{
|
||||||
size_t buffer_size = 1024;
|
size_t buffer_size = 1024;
|
||||||
dom_hubbub_parser *parser = NULL;
|
dom_hubbub_parser *parser = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user