netsurf: move to public only API

And add some Node APIs:
- getters: firstChild, lastChild, nextSibling, previoussibling,
parentNode, parentElement, nodeName, nodeType, ownerDocument,
isConnected
- getters/setters: nodeValue, textContent
- methods: appendChild

And some test comptime optimizations on Document

Signed-off-by: Francis Bouvier <francis.bouvier@gmail.com>
This commit is contained in:
Francis Bouvier
2023-09-26 11:58:05 +02:00
parent 0bce7a5e37
commit fab03586a3
11 changed files with 536 additions and 137 deletions

View File

@@ -41,7 +41,7 @@ pub const HTMLDocument = struct {
// -----
pub fn testExecFn(
alloc: std.mem.Allocator,
_: std.mem.Allocator,
js_env: *jsruntime.Env,
comptime _: []jsruntime.API,
) !void {
@@ -50,7 +50,7 @@ pub fn testExecFn(
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },
.{ .src = "document.__proto__.__proto__.__proto__.__proto__.constructor.name", .ex = "EventTarget" },
.{ .src = "document.body.localName === 'body'", .ex = "true" },
.{ .src = "document.body.localName == 'body'", .ex = "true" },
};
try checkCases(js_env, &constructor);
@@ -63,31 +63,24 @@ pub fn testExecFn(
const tags = comptime parser.Tag.all();
const elements = comptime parser.Tag.allElements();
var createElements: [(tags.len - 1) * 3]Case = undefined;
inline for (tags, 0..) |tag, i| {
if (tag == .undef) {
continue;
}
comptime var createElements: [(tags.len) * 3]Case = undefined;
inline for (tags, elements, 0..) |tag, element_name, i| {
// if (tag == .undef) {
// continue;
// }
const tag_name = @tagName(tag);
const element_name = elements[i];
createElements[i * 3] = Case{
.src = try std.fmt.allocPrint(alloc, "var {s}Elem = document.createElement('{s}')", .{ tag_name, tag_name }),
.src = "var " ++ tag_name ++ "Elem = document.createElement('" ++ tag_name ++ "')",
.ex = "undefined",
};
createElements[(i * 3) + 1] = Case{
.src = try std.fmt.allocPrint(alloc, "{s}Elem.constructor.name", .{tag_name}),
.ex = try std.fmt.allocPrint(alloc, "HTML{s}Element", .{element_name}),
.src = tag_name ++ "Elem.constructor.name",
.ex = "HTML" ++ element_name ++ "Element",
};
createElements[(i * 3) + 2] = Case{
.src = try std.fmt.allocPrint(alloc, "{s}Elem.localName", .{tag_name}),
.src = tag_name ++ "Elem.localName",
.ex = tag_name,
};
}
try checkCases(js_env, &createElements);
var unknown = [_]Case{
.{ .src = "let unknown = document.createElement('unknown')", .ex = "undefined" },
.{ .src = "unknown.constructor.name", .ex = "HTMLUnknownElement" },
};
try checkCases(js_env, &unknown);
}

View File

@@ -454,7 +454,8 @@ pub const HTMLVideoElement = struct {
pub const mem_guarantied = true;
};
pub fn toInterface(comptime T: type, elem: *parser.Element) T {
pub fn toInterface(comptime T: type, e: *parser.Element) T {
const elem: *align(@alignOf(*parser.Element)) parser.Element = @alignCast(e);
const tag = parser.elementHTMLGetTagType(@as(*parser.ElementHTML, @ptrCast(elem)));
return switch (tag) {
.a => .{ .HTMLAnchorElement = @as(*parser.Anchor, @ptrCast(elem)) },