diff --git a/src/dom/document.zig b/src/dom/document.zig index d3622e78..af9dd4f4 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -3,7 +3,9 @@ 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; pub const Document = struct { pub const Self = parser.Document; @@ -15,20 +17,21 @@ pub const Document = struct { // return .{}; // } - pub fn getElementById(self: *parser.Document, id: []const u8) ?*parser.Element { - return parser.documentGetElementById(self, id); - } - // JS funcs // -------- - pub fn get_body(_: *parser.Document) ?*parser.Body { - // TODO - return null; + pub fn get_body(self: *parser.Document) ?*HTMLBodyElement { + const b = parser.documentBody(self) orelse null; + return @as(*HTMLBodyElement, @ptrCast(b)); } - pub fn _getElementById(_: *parser.Document, _: []u8) ?*parser.Element { - // TODO - return null; + pub fn _getElementById(self: *parser.Document, id: []const u8) ?NodeUnion { + const e = parser.documentGetElementById(self, id) orelse return null; + return Element.toInterface(e); + } + + pub fn _createElement(self: *parser.Document, tag_name: []const u8) NodeUnion { + const e = parser.documentCreateElement(self, tag_name); + return Element.toInterface(e); } }; diff --git a/src/dom/element.zig b/src/dom/element.zig index 717da26a..4504868a 100644 --- a/src/dom/element.zig +++ b/src/dom/element.zig @@ -3,12 +3,18 @@ const std = @import("std"); const parser = @import("../netsurf.zig"); const Node = @import("node.zig").Node; +const Union = @import("node.zig").Union; 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); + } + // JS funcs // -------- diff --git a/src/netsurf.zig b/src/netsurf.zig index bb21cbda..b2ed11c4 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -671,6 +671,16 @@ pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) *Eleme return elem.?; } +pub inline fn documentBody(doc: *Document) ?*Body { + const dochtml = @as(*DocumentHTML, @ptrCast(doc)); + var body: ?*ElementHTML = undefined; + _ = documentHTMLVtable(dochtml).get_body.?(dochtml, &body); + if (body == null) { + return null; + } + return @as(*Body, @ptrCast(body.?)); +} + // DocumentHTML pub const DocumentHTML = c.dom_html_document;