netsurf: add a parser from string wrapper

This commit is contained in:
Pierre Tachoire
2023-10-03 16:46:35 +02:00
parent c99f92b581
commit cd53d2604c
3 changed files with 61 additions and 4 deletions

View File

@@ -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.?));
}

View File

@@ -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(&params, &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
*

View File

@@ -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_ */