mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 23:23:28 +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 parser = @import("../netsurf.zig");
|
||||||
|
|
||||||
const Node = @import("node.zig").Node;
|
const jsruntime = @import("jsruntime");
|
||||||
const NodeUnion = @import("node.zig").Union;
|
const Case = jsruntime.test_utils.Case;
|
||||||
const Element = @import("element.zig").Element;
|
const checkCases = jsruntime.test_utils.checkCases;
|
||||||
const HTMLBodyElement = @import("../html/elements.zig").HTMLBodyElement;
|
|
||||||
|
|
||||||
|
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 Document = struct {
|
||||||
pub const Self = parser.Document;
|
pub const Self = parser.Document;
|
||||||
pub const prototype = *Node;
|
pub const prototype = *Node;
|
||||||
@@ -20,17 +25,60 @@ pub const Document = struct {
|
|||||||
// JS funcs
|
// JS funcs
|
||||||
// --------
|
// --------
|
||||||
|
|
||||||
pub fn get_body(self: *parser.Document) ?*parser.Body {
|
pub fn _getElementById(self: *parser.Document, id: []const u8) ?ElementUnion {
|
||||||
return parser.documentBody(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn _getElementById(self: *parser.Document, id: []const u8) ?NodeUnion {
|
|
||||||
const e = parser.documentGetElementById(self, id) orelse return null;
|
const e = parser.documentGetElementById(self, id) orelse return null;
|
||||||
return Element.toInterface(e);
|
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);
|
const e = parser.documentCreateElement(self, tag_name);
|
||||||
return Element.toInterface(e);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,16 +3,17 @@ const std = @import("std");
|
|||||||
const parser = @import("../netsurf.zig");
|
const parser = @import("../netsurf.zig");
|
||||||
|
|
||||||
const Node = @import("node.zig").Node;
|
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 Element = struct {
|
||||||
pub const Self = parser.Element;
|
pub const Self = parser.Element;
|
||||||
pub const prototype = *Node;
|
pub const prototype = *Node;
|
||||||
pub const mem_guarantied = true;
|
pub const mem_guarantied = true;
|
||||||
|
|
||||||
pub fn toInterface(e: *parser.Element) Union {
|
pub fn toInterface(e: *parser.Element) Union {
|
||||||
const n = @as(*parser.Node, @ptrCast(e));
|
return HTMLElem.toInterface(Union, e);
|
||||||
return Node.toInterface(n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// JS funcs
|
// JS funcs
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const checkCases = jsruntime.test_utils.checkCases;
|
|||||||
const Document = @import("../dom/document.zig").Document;
|
const Document = @import("../dom/document.zig").Document;
|
||||||
const HTMLElem = @import("elements.zig");
|
const HTMLElem = @import("elements.zig");
|
||||||
|
|
||||||
|
// WEB IDL https://html.spec.whatwg.org/#the-document-object
|
||||||
pub const HTMLDocument = struct {
|
pub const HTMLDocument = struct {
|
||||||
pub const Self = parser.DocumentHTML;
|
pub const Self = parser.DocumentHTML;
|
||||||
pub const prototype = *Document;
|
pub const prototype = *Document;
|
||||||
@@ -20,21 +21,6 @@ pub const HTMLDocument = struct {
|
|||||||
pub fn get_body(self: *parser.DocumentHTML) ?*parser.Body {
|
pub fn get_body(self: *parser.DocumentHTML) ?*parser.Body {
|
||||||
return parser.documentHTMLBody(self);
|
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
|
// Tests
|
||||||
|
|||||||
@@ -154,8 +154,8 @@ fn runWPT(arena: *std.heap.ArenaAllocator, f: []const u8, loader: *FileLoader) !
|
|||||||
const alloc = arena.allocator();
|
const alloc = arena.allocator();
|
||||||
|
|
||||||
// document
|
// document
|
||||||
const htmldoc = try parser.documentHTMLParseFromFileAlloc(alloc, f);
|
const html_doc = try parser.documentHTMLParseFromFileAlloc(alloc, f);
|
||||||
var doc = parser.documentHTMLToDocument(htmldoc);
|
const doc = parser.documentHTMLToDocument(html_doc);
|
||||||
|
|
||||||
// create JS env
|
// create JS env
|
||||||
var loop = try Loop.init(alloc);
|
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();
|
defer js_env.stop();
|
||||||
|
|
||||||
// add document object
|
// add document object
|
||||||
try js_env.addObject(apis, doc, "document");
|
try js_env.addObject(apis, html_doc, "document");
|
||||||
|
|
||||||
// alias global as self and window
|
// alias global as self and window
|
||||||
try js_env.attachObject(try js_env.getGlobal(), "self", null);
|
try js_env.attachObject(try js_env.getGlobal(), "self", null);
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ const generate = @import("generate.zig");
|
|||||||
const parser = @import("netsurf.zig");
|
const parser = @import("netsurf.zig");
|
||||||
const DOM = @import("dom.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 nodeTestExecFn = @import("dom/node.zig").testExecFn;
|
||||||
const characterDataTestExecFn = @import("dom/character_data.zig").testExecFn;
|
const characterDataTestExecFn = @import("dom/character_data.zig").testExecFn;
|
||||||
const textTestExecFn = @import("dom/text.zig").testExecFn;
|
const textTestExecFn = @import("dom/text.zig").testExecFn;
|
||||||
@@ -45,7 +46,8 @@ fn testsAllExecFn(
|
|||||||
comptime apis: []jsruntime.API,
|
comptime apis: []jsruntime.API,
|
||||||
) !void {
|
) !void {
|
||||||
const testFns = [_]jsruntime.ContextExecFn{
|
const testFns = [_]jsruntime.ContextExecFn{
|
||||||
docTestExecFn,
|
documentTestExecFn,
|
||||||
|
HTMLDocumentTestExecFn,
|
||||||
nodeTestExecFn,
|
nodeTestExecFn,
|
||||||
characterDataTestExecFn,
|
characterDataTestExecFn,
|
||||||
textTestExecFn,
|
textTestExecFn,
|
||||||
|
|||||||
Reference in New Issue
Block a user