backport some node_lexbor modifications

Signed-off-by: Francis Bouvier <francis.bouvier@gmail.com>
This commit is contained in:
Francis Bouvier
2023-09-21 17:05:38 +02:00
parent 4708dbd679
commit b5b4659c87
5 changed files with 166 additions and 79 deletions

View File

@@ -7,8 +7,7 @@ const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const Document = @import("../dom/document.zig").Document;
const E = @import("elements.zig");
const HTMLElem = @import("elements.zig");
pub const HTMLDocument = struct {
pub const Self = parser.DocumentHTML;
@@ -22,19 +21,19 @@ pub const HTMLDocument = struct {
return parser.documentHTMLBody(self);
}
pub fn _getElementById(self: *parser.DocumentHTML, id: []u8) ?*parser.ElementHTML {
pub fn _getElementById(self: *parser.DocumentHTML, id: []u8) ?HTMLElem.Union {
const doc = parser.documentHTMLToDocument(self);
const elem = parser.documentGetElementById(doc, id);
if (elem) |value| {
return @as(*parser.ElementHTML, @ptrCast(value));
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) E.HTMLElements {
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 E.ElementToHTMLElementInterface(base);
return HTMLElem.toInterface(HTMLElem.Union, base);
}
};
@@ -51,13 +50,14 @@ 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" },
};
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" },
.{ .src = "getElementById.constructor.name", .ex = "HTMLDivElement" },
.{ .src = "getElementById.localName", .ex = "div" },
};
try checkCases(js_env, &getElementById);

View File

@@ -12,7 +12,7 @@ pub const HTMLElement = struct {
pub const mem_guarantied = true;
};
pub const HTMLElementsTypes = .{
pub const Types = .{
HTMLUnknownElement,
HTMLAnchorElement,
HTMLAreaElement,
@@ -74,9 +74,9 @@ pub const HTMLElementsTypes = .{
HTMLUListElement,
HTMLVideoElement,
};
const HTMLElementsGenerated = generate.Union.compile(HTMLElementsTypes);
pub const HTMLElements = HTMLElementsGenerated._union;
pub const HTMLElementsTags = HTMLElementsGenerated._enum;
const Generated = generate.Union.compile(Types);
pub const Union = Generated._union;
pub const Tags = Generated._enum;
// Deprecated HTMLElements in Chrome (2023/03/15)
// HTMLContentelement
@@ -454,7 +454,7 @@ pub const HTMLVideoElement = struct {
pub const mem_guarantied = true;
};
pub fn ElementToHTMLElementInterface(elem: *parser.Element) HTMLElements {
pub fn toInterface(comptime T: type, elem: *parser.Element) T {
const tag = parser.elementHTMLGetTagType(@as(*parser.ElementHTML, @ptrCast(elem)));
return switch (tag) {
.a => .{ .HTMLAnchorElement = @as(*parser.Anchor, @ptrCast(elem)) },