mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
netsurf: add a parser from string wrapper
This commit is contained in:
@@ -553,11 +553,17 @@ fn documentHTMLVtable(doc_html: *DocumentHTML) c.dom_html_document_vtable {
|
||||
return getVtable(c.dom_html_document_vtable, DocumentHTML, doc_html);
|
||||
}
|
||||
|
||||
pub fn documentHTMLParse(filename: []const u8) *DocumentHTML {
|
||||
var f: []u8 = @constCast(filename);
|
||||
const doc = c.wr_create_doc_dom_from_file(f.ptr);
|
||||
pub fn documentHTMLParse(allocator: std.mem.Allocator, filename: []const u8) !*DocumentHTML {
|
||||
var file = try std.fs.cwd().openFile(filename, .{});
|
||||
defer file.close();
|
||||
|
||||
const file_size = try file.getEndPos();
|
||||
const html = try file.readToEndAlloc(allocator, file_size);
|
||||
defer allocator.free(html);
|
||||
|
||||
const doc = c.wr_create_doc_dom_from_string(html.ptr);
|
||||
if (doc == null) {
|
||||
@panic("error parser");
|
||||
return error.ParserError;
|
||||
}
|
||||
return @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
}
|
||||
|
||||
50
vendor/netsurf/wrapper/wrapper.c
vendored
50
vendor/netsurf/wrapper/wrapper.c
vendored
@@ -1,8 +1,58 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <dom/dom.h>
|
||||
#include <dom/bindings/hubbub/parser.h>
|
||||
|
||||
/**
|
||||
* Generate a LibDOM document DOM from an HTML string
|
||||
*
|
||||
* \param string The HTML string
|
||||
* \return pointer to DOM document, or NULL on error
|
||||
*/
|
||||
dom_document *wr_create_doc_dom_from_string(char *html)
|
||||
{
|
||||
dom_hubbub_parser *parser = NULL;
|
||||
dom_hubbub_error error;
|
||||
dom_hubbub_parser_params params;
|
||||
dom_document *doc;
|
||||
|
||||
params.enc = NULL;
|
||||
params.fix_enc = true;
|
||||
params.enable_script = false;
|
||||
params.msg = NULL;
|
||||
params.script = NULL;
|
||||
params.ctx = NULL;
|
||||
params.daf = NULL;
|
||||
|
||||
/* Create Hubbub parser */
|
||||
error = dom_hubbub_parser_create(¶ms, &parser, &doc);
|
||||
if (error != DOM_HUBBUB_OK) {
|
||||
printf("Can't create Hubbub Parser\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
error = dom_hubbub_parser_parse_chunk(parser, html, strlen(html));
|
||||
if (error != DOM_HUBBUB_OK) {
|
||||
dom_hubbub_parser_destroy(parser);
|
||||
printf("Parsing errors occur\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Done parsing file */
|
||||
error = dom_hubbub_parser_completed(parser);
|
||||
if (error != DOM_HUBBUB_OK) {
|
||||
dom_hubbub_parser_destroy(parser);
|
||||
printf("Parsing error when construct DOM\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Finished with parser */
|
||||
dom_hubbub_parser_destroy(parser);
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a LibDOM document DOM from an HTML file
|
||||
*
|
||||
|
||||
1
vendor/netsurf/wrapper/wrapper.h
vendored
1
vendor/netsurf/wrapper/wrapper.h
vendored
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <dom/dom.h>
|
||||
|
||||
dom_document *wr_create_doc_dom_from_string(char *html);
|
||||
dom_document *wr_create_doc_dom_from_file(char *filename);
|
||||
|
||||
#endif /* wrapper_dom_h_ */
|
||||
|
||||
Reference in New Issue
Block a user