Implement HTMLDocument.createElement

Signed-off-by: Francis Bouvier <francis.bouvier@gmail.com>
This commit is contained in:
Francis Bouvier
2023-03-16 18:22:46 +01:00
parent 081daa1245
commit 50ac87ed6a
12 changed files with 1325 additions and 134 deletions

View File

@@ -1,16 +1,9 @@
const std = @import("std");
const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const parser = @import("../parser.zig");
const DOM = @import("../dom.zig");
const Node = DOM.Node;
const Element = DOM.Element;
const HTMLElement = DOM.HTMLElement;
const HTMLBodyElement = DOM.HTMLBodyElement;
const Node = @import("node.zig").Node;
const Element = @import("element.zig").Element;
pub const Document = struct {
proto: Node,
@@ -29,7 +22,7 @@ pub const Document = struct {
return Document.init(null);
}
fn getElementById(self: Document, elem_dom: *parser.Element, id: []const u8) ?Element {
pub fn getElementById(self: Document, elem_dom: *parser.Element, id: []const u8) ?Element {
if (self.base == null) {
return null;
}
@@ -61,62 +54,3 @@ pub const Document = struct {
return null;
}
};
pub const HTMLDocument = struct {
proto: Document,
base: *parser.DocumentHTML,
pub const prototype = *Document;
pub fn init() HTMLDocument {
return .{
.proto = Document.init(null),
.base = parser.documentHTMLInit(),
};
}
pub fn deinit(self: HTMLDocument) void {
parser.documentHTMLDeinit(self.base);
}
pub fn parse(self: *HTMLDocument, html: []const u8) !void {
try parser.documentHTMLParse(self.base, html);
self.proto.base = parser.documentHTMLToDocument(self.base);
}
// JS funcs
// --------
pub fn get_body(self: HTMLDocument) ?HTMLBodyElement {
const body_dom = parser.documentHTMLBody(self.base);
return HTMLBodyElement.init(body_dom);
}
pub fn _getElementById(self: HTMLDocument, id: []u8) ?HTMLElement {
const body_dom = parser.documentHTMLBody(self.base);
if (self.proto.getElementById(body_dom, id)) |elem| {
return HTMLElement.init(elem.base);
}
return null;
}
};
pub fn testExecFn(
js_env: *jsruntime.Env,
comptime _: []jsruntime.API,
) !void {
var constructor = [_]Case{
.{ .src = "document.__proto__.constructor.name", .ex = "HTMLDocument" },
.{ .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" },
};
try checkCases(js_env, &constructor);
var getElementById = [_]Case{
.{ .src = "let getElementById = document.getElementById('content')", .ex = "undefined" },
.{ .src = "getElementById.constructor.name", .ex = "HTMLElement" },
.{ .src = "getElementById.localName", .ex = "main" },
};
try checkCases(js_env, &getElementById);
}