mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 14:43:28 +00:00
Compare commits
3 Commits
fb6fbffe3f
...
pandasurf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ec0d0b84c | ||
|
|
3544e98871 | ||
|
|
446e5b2ddd |
@@ -43,6 +43,7 @@ const Matcher = struct {
|
||||
}
|
||||
};
|
||||
|
||||
const Elements = @import("../html/elements.zig");
|
||||
test "matchFirst" {
|
||||
const alloc = std.testing.allocator;
|
||||
|
||||
@@ -161,7 +162,7 @@ test "matchFirst" {
|
||||
for (testcases) |tc| {
|
||||
matcher.reset();
|
||||
|
||||
const doc = try parser.documentHTMLParseFromStr(tc.html);
|
||||
const doc = try parser.documentHTMLParseFromStr(tc.html, &Elements.createElement);
|
||||
defer parser.documentHTMLClose(doc) catch {};
|
||||
|
||||
const s = css.parse(alloc, tc.q, .{}) catch |e| {
|
||||
@@ -302,7 +303,7 @@ test "matchAll" {
|
||||
for (testcases) |tc| {
|
||||
matcher.reset();
|
||||
|
||||
const doc = try parser.documentHTMLParseFromStr(tc.html);
|
||||
const doc = try parser.documentHTMLParseFromStr(tc.html, &Elements.createElement);
|
||||
defer parser.documentHTMLClose(doc) catch {};
|
||||
|
||||
const s = css.parse(alloc, tc.q, .{}) catch |e| {
|
||||
|
||||
@@ -30,6 +30,7 @@ const css = @import("css.zig");
|
||||
|
||||
const Element = @import("element.zig").Element;
|
||||
const ElementUnion = @import("element.zig").Union;
|
||||
const Elements = @import("../html/elements.zig");
|
||||
const TreeWalker = @import("tree_walker.zig").TreeWalker;
|
||||
|
||||
const Env = @import("../env.zig").Env;
|
||||
@@ -45,6 +46,7 @@ pub const Document = struct {
|
||||
pub fn constructor(page: *const Page) !*parser.DocumentHTML {
|
||||
const doc = try parser.documentCreateDocument(
|
||||
try parser.documentHTMLGetTitle(page.window.document),
|
||||
&Elements.createElement,
|
||||
);
|
||||
|
||||
// we have to work w/ document instead of html document.
|
||||
|
||||
@@ -30,8 +30,8 @@ pub const DOMParser = struct {
|
||||
// TODO: Support XML
|
||||
return error.TypeError;
|
||||
}
|
||||
|
||||
return try parser.documentHTMLParseFromStr(string);
|
||||
const Elements = @import("../html/elements.zig");
|
||||
return try parser.documentHTMLParseFromStr(string, &Elements.createElement);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -42,7 +42,8 @@ pub const DOMImplementation = struct {
|
||||
}
|
||||
|
||||
pub fn _createHTMLDocument(_: *DOMImplementation, title: ?[]const u8) !*parser.DocumentHTML {
|
||||
return try parser.domImplementationCreateHTMLDocument(title);
|
||||
const Elements = @import("../html/elements.zig");
|
||||
return try parser.domImplementationCreateHTMLDocument(title, &Elements.createElement);
|
||||
}
|
||||
|
||||
pub fn _hasFeature(_: *DOMImplementation) bool {
|
||||
|
||||
@@ -182,7 +182,7 @@ fn writeEscapedAttributeValue(writer: anytype, value: []const u8) !void {
|
||||
|
||||
const testing = std.testing;
|
||||
test "dump.writeHTML" {
|
||||
try parser.init();
|
||||
try parser.init(testing.allocator);
|
||||
defer parser.deinit();
|
||||
|
||||
try testWriteHTML(
|
||||
@@ -225,7 +225,8 @@ fn testWriteFullHTML(comptime expected: []const u8, src: []const u8) !void {
|
||||
var buf = std.ArrayListUnmanaged(u8){};
|
||||
defer buf.deinit(testing.allocator);
|
||||
|
||||
const doc_html = try parser.documentHTMLParseFromStr(src);
|
||||
const Elements = @import("html/elements.zig");
|
||||
const doc_html = try parser.documentHTMLParseFromStr(src, &Elements.createElement);
|
||||
defer parser.documentHTMLClose(doc_html) catch {};
|
||||
|
||||
const doc = parser.documentHTMLToDocument(doc_html);
|
||||
|
||||
@@ -26,6 +26,7 @@ const urlStitch = @import("../../url.zig").URL.stitch;
|
||||
const URL = @import("../url/url.zig").URL;
|
||||
const Node = @import("../dom/node.zig").Node;
|
||||
const Element = @import("../dom/element.zig").Element;
|
||||
const State = @import("../State.zig");
|
||||
|
||||
const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration;
|
||||
|
||||
@@ -626,11 +627,85 @@ pub const HTMLImageElement = struct {
|
||||
};
|
||||
};
|
||||
|
||||
pub fn createElement(params: [*c]parser.c.dom_html_element_create_params, elem: [*c][*c]parser.ElementHTML) callconv(.c) parser.c.dom_exception {
|
||||
const p: *parser.c.dom_html_element_create_params = @ptrCast(params);
|
||||
switch (p.type) {
|
||||
parser.c.DOM_HTML_ELEMENT_TYPE_INPUT => {
|
||||
return HTMLInputElement.dom_create(params, elem);
|
||||
},
|
||||
else => return parser.c.DOM_NO_ERR,
|
||||
}
|
||||
}
|
||||
|
||||
var input_protected_vtable: parser.c.dom_element_protected_vtable = .{
|
||||
.base = .{
|
||||
.destroy = HTMLInputElement.node_destroy,
|
||||
.copy = HTMLInputElement.node_copy,
|
||||
},
|
||||
.dom_element_parse_attribute = HTMLInputElement.element_parse_attribute,
|
||||
};
|
||||
|
||||
pub const HTMLInputElement = struct {
|
||||
pub const Self = parser.Input;
|
||||
pub const prototype = *HTMLElement;
|
||||
pub const subtype = .node;
|
||||
|
||||
base: parser.ElementHTML,
|
||||
|
||||
type: []const u8 = "text",
|
||||
|
||||
pub fn dom_create(params: *parser.c.dom_html_element_create_params, output: *?*parser.ElementHTML) parser.c.dom_exception {
|
||||
var self = parser.ARENA.?.create(HTMLInputElement) catch return parser.c.DOM_NO_MEM_ERR;
|
||||
output.* = &self.base; // Self can be recovered using @fieldParentPtr
|
||||
|
||||
self.base.base.base.base.vtable = &parser.c._dom_html_element_vtable; // TODO replace get/setAttribute
|
||||
self.base.base.base.vtable = &input_protected_vtable;
|
||||
|
||||
return self.dom_initialise(params);
|
||||
}
|
||||
// Initialise is separated from create such that the leaf type sets the vtable, then calls all the way up the protochain to init
|
||||
pub fn dom_initialise(self: *HTMLInputElement, params: *parser.c.dom_html_element_create_params) parser.c.dom_exception {
|
||||
return parser.c._dom_html_element_initialise(params, &self.base);
|
||||
}
|
||||
|
||||
// This should always be the same and we should not have cleanup for new zig implementation, hopefully
|
||||
pub fn node_destroy(node: [*c]parser.Node) callconv(.c) void {
|
||||
const elem = parser.nodeToHtmlElement(node);
|
||||
parser.c._dom_html_element_finalise(elem);
|
||||
}
|
||||
|
||||
pub fn node_copy(old: [*c]parser.Node, new: [*c][*c]parser.Node) callconv(.c) parser.c.dom_exception {
|
||||
const old_elem = parser.nodeToHtmlElement(old);
|
||||
const self = @as(*HTMLInputElement, @fieldParentPtr("base", old_elem));
|
||||
|
||||
var copy = parser.ARENA.?.create(HTMLInputElement) catch return parser.c.DOM_NO_MEM_ERR;
|
||||
copy.type = self.type;
|
||||
|
||||
const err = parser.c._dom_html_element_copy_internal(old_elem, ©.base);
|
||||
if (err != parser.c.DOM_NO_ERR) {
|
||||
return err;
|
||||
}
|
||||
|
||||
new.* = @ptrCast(copy);
|
||||
return parser.c.DOM_NO_ERR;
|
||||
}
|
||||
|
||||
// fn ([*c]cimport.struct_dom_element, [*c]cimport.struct_dom_string, [*c]cimport.struct_dom_string, [*c][*c]cimport.struct_dom_string) callconv(.c) c_uint
|
||||
pub fn element_parse_attribute(self: [*c]parser.Element, name: [*c]parser.c.dom_string, value: [*c]parser.c.dom_string, parsed: [*c][*c]parser.c.dom_string) callconv(.c) parser.c.dom_exception {
|
||||
_ = name;
|
||||
_ = self;
|
||||
parsed.* = value;
|
||||
_ = parser.c.dom_string_ref(value);
|
||||
|
||||
// TODO actual implementation
|
||||
// Probably should not use this and instead override the getAttribute setAttribute Element methods directly, perhaps other related functions.
|
||||
|
||||
// handle defaultValue likes
|
||||
// Call setter or store in general attribute store
|
||||
// increment domstring ref?
|
||||
return parser.c.DOM_NO_ERR;
|
||||
}
|
||||
|
||||
pub fn get_defaultValue(self: *parser.Input) ![]const u8 {
|
||||
return try parser.inputGetDefaultValue(self);
|
||||
}
|
||||
@@ -702,10 +777,26 @@ pub const HTMLInputElement = struct {
|
||||
try parser.inputSetSrc(self, new_src);
|
||||
}
|
||||
pub fn get_type(self: *parser.Input) ![]const u8 {
|
||||
return try parser.inputGetType(self);
|
||||
const elem = parser.nodeToHtmlElement(@alignCast(@ptrCast(self)));
|
||||
const input = @as(*HTMLInputElement, @fieldParentPtr("base", elem));
|
||||
|
||||
return input.type;
|
||||
}
|
||||
pub fn set_type(self: *parser.Input, type_: []const u8) !void {
|
||||
try parser.inputSetType(self, type_);
|
||||
const elem = parser.nodeToHtmlElement(@alignCast(@ptrCast(self)));
|
||||
const input = @as(*HTMLInputElement, @fieldParentPtr("base", elem));
|
||||
|
||||
const possible_values = [_][]const u8{ "text", "search", "tel", "url", "email", "password", "date", "month", "week", "time", "datetime-local", "number", "range", "color", "checkbox", "radio", "file", "hidden", "image", "button", "submit", "reset" };
|
||||
var found = false;
|
||||
for (possible_values) |item| {
|
||||
if (std.mem.eql(u8, type_, item)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
input.type = if (found) type_ else "text";
|
||||
|
||||
// TODO DOM events
|
||||
}
|
||||
pub fn get_value(self: *parser.Input) ![]const u8 {
|
||||
return try parser.inputGetValue(self);
|
||||
|
||||
@@ -61,7 +61,8 @@ pub const Window = struct {
|
||||
|
||||
pub fn create(target: ?[]const u8, navigator: ?Navigator) !Window {
|
||||
var fbs = std.io.fixedBufferStream("");
|
||||
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8");
|
||||
const Elements = @import("../html/elements.zig");
|
||||
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8", &Elements.createElement);
|
||||
const doc = parser.documentHTMLToDocument(html_doc);
|
||||
try parser.documentSetDocumentURI(doc, "about:blank");
|
||||
|
||||
|
||||
@@ -17,8 +17,9 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const c = @cImport({
|
||||
pub const c = @cImport({
|
||||
@cInclude("dom/dom.h");
|
||||
@cInclude("core/pi.h");
|
||||
@cInclude("dom/bindings/hubbub/parser.h");
|
||||
@@ -32,11 +33,13 @@ const c = @cImport({
|
||||
});
|
||||
|
||||
const mimalloc = @import("mimalloc.zig");
|
||||
pub var ARENA: ?Allocator = null;
|
||||
|
||||
// init initializes netsurf lib.
|
||||
// init starts a mimalloc heap arena for the netsurf session. The caller must
|
||||
// call deinit() to free the arena memory.
|
||||
pub fn init() !void {
|
||||
pub fn init(allocator: Allocator) !void {
|
||||
ARENA = allocator;
|
||||
try mimalloc.create();
|
||||
}
|
||||
|
||||
@@ -49,6 +52,7 @@ pub fn deinit() void {
|
||||
c.lwc_deinit_strings();
|
||||
|
||||
mimalloc.destroy();
|
||||
ARENA = null;
|
||||
}
|
||||
|
||||
// Vtable
|
||||
@@ -1357,6 +1361,10 @@ pub inline fn nodeToElement(node: *Node) *Element {
|
||||
return @as(*Element, @ptrCast(node));
|
||||
}
|
||||
|
||||
pub inline fn nodeToHtmlElement(node: *Node) *ElementHTML {
|
||||
return @as(*ElementHTML, @alignCast(@ptrCast(node)));
|
||||
}
|
||||
|
||||
// nodeToDocument is an helper to convert a node to an document.
|
||||
pub inline fn nodeToDocument(node: *Node) *Document {
|
||||
return @as(*Document, @ptrCast(node));
|
||||
@@ -1989,8 +1997,10 @@ pub inline fn domImplementationCreateDocumentType(
|
||||
return dt.?;
|
||||
}
|
||||
|
||||
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8) !*DocumentHTML {
|
||||
const doc_html = try documentCreateDocument(title);
|
||||
pub const CreateElementFn = ?*const fn ([*c]c.dom_html_element_create_params, [*c][*c]ElementHTML) callconv(.c) c.dom_exception;
|
||||
|
||||
pub inline fn domImplementationCreateHTMLDocument(title: ?[]const u8, create_element: CreateElementFn) !*DocumentHTML {
|
||||
const doc_html = try documentCreateDocument(title, create_element);
|
||||
const doc = documentHTMLToDocument(doc_html);
|
||||
|
||||
// add hierarchy: html, head, body.
|
||||
@@ -2071,7 +2081,7 @@ pub inline fn documentSetInputEncoding(doc: *Document, enc: []const u8) !void {
|
||||
try DOMErr(err);
|
||||
}
|
||||
|
||||
pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
|
||||
pub inline fn documentCreateDocument(title: ?[]const u8, create_element: CreateElementFn) !*DocumentHTML {
|
||||
var doc: ?*Document = undefined;
|
||||
const err = c.dom_implementation_create_document(
|
||||
c.DOM_IMPLEMENTATION_HTML,
|
||||
@@ -2085,6 +2095,9 @@ pub inline fn documentCreateDocument(title: ?[]const u8) !*DocumentHTML {
|
||||
try DOMErr(err);
|
||||
const doc_html = @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
if (title) |t| try documentHTMLSetTitle(doc_html, t);
|
||||
|
||||
doc_html.create_element_external = create_element;
|
||||
|
||||
return doc_html;
|
||||
}
|
||||
|
||||
@@ -2248,24 +2261,26 @@ fn parserErr(err: HubbubErr) ParserError!void {
|
||||
|
||||
// documentHTMLParseFromStr parses the given HTML string.
|
||||
// The caller is responsible for closing the document.
|
||||
pub fn documentHTMLParseFromStr(str: []const u8) !*DocumentHTML {
|
||||
pub fn documentHTMLParseFromStr(str: []const u8, create_element: CreateElementFn) !*DocumentHTML {
|
||||
var fbs = std.io.fixedBufferStream(str);
|
||||
return try documentHTMLParse(fbs.reader(), "UTF-8");
|
||||
return try documentHTMLParse(fbs.reader(), "UTF-8", create_element);
|
||||
}
|
||||
|
||||
pub fn documentHTMLParse(reader: anytype, enc: ?[:0]const u8) !*DocumentHTML {
|
||||
pub fn documentHTMLParse(reader: anytype, enc: ?[:0]const u8, create_element: CreateElementFn) !*DocumentHTML {
|
||||
var parser: ?*c.dom_hubbub_parser = undefined;
|
||||
var doc: ?*c.dom_document = undefined;
|
||||
var err: c.hubbub_error = undefined;
|
||||
var params = parseParams(enc);
|
||||
|
||||
err = c.dom_hubbub_parser_create(¶ms, &parser, &doc);
|
||||
const result = @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
result.create_element_external = create_element;
|
||||
try parserErr(err);
|
||||
defer c.dom_hubbub_parser_destroy(parser);
|
||||
|
||||
try parseData(parser.?, reader);
|
||||
|
||||
return @as(*DocumentHTML, @ptrCast(doc.?));
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn documentParseFragmentFromStr(self: *Document, str: []const u8) !*DocumentFragment {
|
||||
|
||||
@@ -286,7 +286,8 @@ pub const Page = struct {
|
||||
pub fn loadHTMLDoc(self: *Page, reader: anytype, charset: []const u8) !void {
|
||||
const ccharset = try self.arena.dupeZ(u8, charset);
|
||||
|
||||
const html_doc = try parser.documentHTMLParse(reader, ccharset);
|
||||
const Elements = @import("html/elements.zig");
|
||||
const html_doc = try parser.documentHTMLParse(reader, ccharset, &Elements.createElement);
|
||||
const doc = parser.documentHTMLToDocument(html_doc);
|
||||
|
||||
// inject the URL to the document including the fragment.
|
||||
|
||||
@@ -85,14 +85,14 @@ pub const Session = struct {
|
||||
pub fn createPage(self: *Session) !*Page {
|
||||
std.debug.assert(self.page == null);
|
||||
|
||||
// Start netsurf memory arena.
|
||||
// We need to init this early as JS event handlers may be registered through Runtime.evaluate before the first html doc is loaded
|
||||
try parser.init();
|
||||
|
||||
const page_arena = &self.browser.page_arena;
|
||||
_ = page_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
|
||||
_ = self.browser.state_pool.reset(.{ .retain_with_limit = 4 * 1024 });
|
||||
|
||||
// Start netsurf memory arena.
|
||||
// We need to init this early as JS event handlers may be registered through Runtime.evaluate before the first html doc is loaded
|
||||
try parser.init(page_arena.allocator());
|
||||
|
||||
self.page = @as(Page, undefined);
|
||||
const page = &self.page.?;
|
||||
try Page.init(page, page_arena.allocator(), self);
|
||||
|
||||
@@ -756,7 +756,8 @@ pub const XMLHttpRequest = struct {
|
||||
}
|
||||
|
||||
var fbs = std.io.fixedBufferStream(self.response_bytes.items);
|
||||
const doc = parser.documentHTMLParse(fbs.reader(), ccharset) catch {
|
||||
const Elements = @import("../html/elements.zig");
|
||||
const doc = parser.documentHTMLParse(fbs.reader(), ccharset, &Elements.createElement) catch {
|
||||
self.response_obj = .{ .Failure = {} };
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -522,7 +522,7 @@ test {
|
||||
|
||||
var test_wg: std.Thread.WaitGroup = .{};
|
||||
test "tests:beforeAll" {
|
||||
try parser.init();
|
||||
try parser.init(std.testing.allocator);
|
||||
log.opts.level = .err;
|
||||
log.opts.format = .logfmt;
|
||||
|
||||
|
||||
@@ -211,14 +211,16 @@ pub const Document = struct {
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
pub fn init(html: []const u8) !Document {
|
||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||
parser.deinit();
|
||||
try parser.init();
|
||||
try parser.init(arena.allocator());
|
||||
|
||||
var fbs = std.io.fixedBufferStream(html);
|
||||
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8");
|
||||
const Elements = @import("browser/html/elements.zig");
|
||||
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8", &Elements.createElement);
|
||||
|
||||
return .{
|
||||
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||
.arena = arena,
|
||||
.doc = html_doc,
|
||||
};
|
||||
}
|
||||
|
||||
2
vendor/netsurf/libdom
vendored
2
vendor/netsurf/libdom
vendored
Submodule vendor/netsurf/libdom updated: 614187b0aa...f22449c52e
Reference in New Issue
Block a user