mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
dom: document: re-dispatch document function according to spec
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user