From 9dda08b8a9083dfa0b5732da958e5d6f6f03bf70 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 17 Oct 2023 18:18:38 +0200 Subject: [PATCH] dom: document: re-dispatch document function according to spec --- src/dom/document.zig | 68 ++++++++++++++++++++++++++++++++++++------- src/dom/element.zig | 7 +++-- src/html/document.zig | 16 +--------- src/main_wpt.zig | 6 ++-- src/run_tests.zig | 6 ++-- 5 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index 3fa4a9a2..ef7103a2 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -2,11 +2,16 @@ const std = @import("std"); const parser = @import("../netsurf.zig"); -const Node = @import("node.zig").Node; -const NodeUnion = @import("node.zig").Union; -const Element = @import("element.zig").Element; -const HTMLBodyElement = @import("../html/elements.zig").HTMLBodyElement; +const jsruntime = @import("jsruntime"); +const Case = jsruntime.test_utils.Case; +const checkCases = jsruntime.test_utils.checkCases; +const Node = @import("node.zig").Node; + +const Element = @import("element.zig").Element; +const ElementUnion = @import("element.zig").Union; + +// WEB IDL https://dom.spec.whatwg.org/#document pub const Document = struct { pub const Self = parser.Document; pub const prototype = *Node; @@ -20,17 +25,60 @@ pub const Document = struct { // JS funcs // -------- - pub fn get_body(self: *parser.Document) ?*parser.Body { - return parser.documentBody(self); - } - - pub fn _getElementById(self: *parser.Document, id: []const u8) ?NodeUnion { + pub fn _getElementById(self: *parser.Document, id: []const u8) ?ElementUnion { const e = parser.documentGetElementById(self, id) orelse return null; return Element.toInterface(e); } - pub fn _createElement(self: *parser.Document, tag_name: []const u8) NodeUnion { + pub fn _createElement(self: *parser.Document, tag_name: []const u8) ElementUnion { const e = parser.documentCreateElement(self, tag_name); return Element.toInterface(e); } }; + +// Tests +// ----- + +pub fn testExecFn( + _: std.mem.Allocator, + js_env: *jsruntime.Env, + comptime _: []jsruntime.API, +) !void { + var constructor = [_]Case{ + .{ .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" }, + }; + try checkCases(js_env, &constructor); + + var getElementById = [_]Case{ + .{ .src = "let getElementById = document.getElementById('content')", .ex = "undefined" }, + .{ .src = "getElementById.constructor.name", .ex = "HTMLDivElement" }, + .{ .src = "getElementById.localName", .ex = "div" }, + }; + try checkCases(js_env, &getElementById); + + const tags = comptime parser.Tag.all(); + const elements = comptime parser.Tag.allElements(); + 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); + createElements[i * 3] = Case{ + .src = "var " ++ tag_name ++ "Elem = document.createElement('" ++ tag_name ++ "')", + .ex = "undefined", + }; + createElements[(i * 3) + 1] = Case{ + .src = tag_name ++ "Elem.constructor.name", + .ex = "HTML" ++ element_name ++ "Element", + }; + createElements[(i * 3) + 2] = Case{ + .src = tag_name ++ "Elem.localName", + .ex = tag_name, + }; + } + try checkCases(js_env, &createElements); +} diff --git a/src/dom/element.zig b/src/dom/element.zig index 4504868a..f8746648 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -3,16 +3,17 @@ const std = @import("std"); const parser = @import("../netsurf.zig"); const Node = @import("node.zig").Node; -const Union = @import("node.zig").Union; +const HTMLElem = @import("../html/elements.zig"); +pub const Union = @import("../html/elements.zig").Union; +// WEB IDL https://dom.spec.whatwg.org/#element pub const Element = struct { pub const Self = parser.Element; pub const prototype = *Node; pub const mem_guarantied = true; pub fn toInterface(e: *parser.Element) Union { - const n = @as(*parser.Node, @ptrCast(e)); - return Node.toInterface(n); + return HTMLElem.toInterface(Union, e); } // JS funcs diff --git a/src/html/document.zig b/src/html/document.zig index 4f554144..5eb59247 100644 --- a/src/html/document.zig +++ b/src/html/document.zig @@ -9,6 +9,7 @@ const checkCases = jsruntime.test_utils.checkCases; const Document = @import("../dom/document.zig").Document; const HTMLElem = @import("elements.zig"); +// WEB IDL https://html.spec.whatwg.org/#the-document-object pub const HTMLDocument = struct { pub const Self = parser.DocumentHTML; pub const prototype = *Document; @@ -20,21 +21,6 @@ pub const HTMLDocument = struct { pub fn get_body(self: *parser.DocumentHTML) ?*parser.Body { return parser.documentHTMLBody(self); } - - pub fn _getElementById(self: *parser.DocumentHTML, id: []u8) ?HTMLElem.Union { - const doc = parser.documentHTMLToDocument(self); - const elem_dom = parser.documentGetElementById(doc, id); - if (elem_dom) |elem| { - return HTMLElem.toInterface(HTMLElem.Union, elem); - } - return null; - } - - pub fn _createElement(self: *parser.DocumentHTML, tag_name: []const u8) HTMLElem.Union { - const doc_dom = parser.documentHTMLToDocument(self); - const base = parser.documentCreateElement(doc_dom, tag_name); - return HTMLElem.toInterface(HTMLElem.Union, base); - } }; // Tests diff --git a/src/main_wpt.zig b/src/main_wpt.zig index 457f4b0f..a90539cd 100644 --- a/src/main_wpt.zig +++ b/src/main_wpt.zig @@ -154,8 +154,8 @@ fn runWPT(arena: *std.heap.ArenaAllocator, f: []const u8, loader: *FileLoader) ! const alloc = arena.allocator(); // document - const htmldoc = try parser.documentHTMLParseFromFileAlloc(alloc, f); - var doc = parser.documentHTMLToDocument(htmldoc); + const html_doc = try parser.documentHTMLParseFromFileAlloc(alloc, f); + const doc = parser.documentHTMLToDocument(html_doc); // create JS env var loop = try Loop.init(alloc); @@ -172,7 +172,7 @@ fn runWPT(arena: *std.heap.ArenaAllocator, f: []const u8, loader: *FileLoader) ! defer js_env.stop(); // add document object - try js_env.addObject(apis, doc, "document"); + try js_env.addObject(apis, html_doc, "document"); // alias global as self and window try js_env.attachObject(try js_env.getGlobal(), "self", null); diff --git a/src/run_tests.zig b/src/run_tests.zig index a0d34891..5cd637a9 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -6,7 +6,8 @@ const generate = @import("generate.zig"); const parser = @import("netsurf.zig"); const DOM = @import("dom.zig"); -const docTestExecFn = @import("html/document.zig").testExecFn; +const documentTestExecFn = @import("dom/document.zig").testExecFn; +const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn; const nodeTestExecFn = @import("dom/node.zig").testExecFn; const characterDataTestExecFn = @import("dom/character_data.zig").testExecFn; const textTestExecFn = @import("dom/text.zig").testExecFn; @@ -45,7 +46,8 @@ fn testsAllExecFn( comptime apis: []jsruntime.API, ) !void { const testFns = [_]jsruntime.ContextExecFn{ - docTestExecFn, + documentTestExecFn, + HTMLDocumentTestExecFn, nodeTestExecFn, characterDataTestExecFn, textTestExecFn,