From ca6bb577c67514ffd6caf26c84abfa9f7176bc07 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 9 Jan 2024 14:24:55 +0100 Subject: [PATCH] Document: use document cast to node --- src/dom/document.zig | 22 ++++------------------ src/netsurf.zig | 4 ++++ 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index 5157e86b..be7581f0 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -108,12 +108,7 @@ pub const Document = struct { alloc: std.mem.Allocator, tag_name: []const u8, ) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - - return try collection.HTMLCollectionByTagName(alloc, elt, tag_name, true); + return try collection.HTMLCollectionByTagName(alloc, parser.documentToNode(self), tag_name, true); } pub fn _getElementsByClassName( @@ -121,12 +116,7 @@ pub const Document = struct { alloc: std.mem.Allocator, classNames: []const u8, ) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - - return try collection.HTMLCollectionByClassName(alloc, elt, classNames, true); + return try collection.HTMLCollectionByClassName(alloc, parser.documentToNode(self), classNames, true); } pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment { @@ -170,11 +160,7 @@ pub const Document = struct { // ParentNode // https://dom.spec.whatwg.org/#parentnode pub fn get_children(self: *parser.Document) !collection.HTMLCollection { - var elt: ?*parser.Node = null; - if (try parser.documentGetDocumentElement(self)) |root| { - elt = parser.elementToNode(root); - } - return try collection.HTMLCollectionChildren(elt, true); + return try collection.HTMLCollectionChildren(parser.documentToNode(self), false); } pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion { @@ -219,7 +205,7 @@ pub const Document = struct { // catch-all, return all elements if (selectors[0] == '*') { // walk over the node tree fo find the node by id. - const root = parser.elementToNode(try parser.documentGetDocumentElement(self) orelse return list); + const root = parser.documentToNode(self); const walker = Walker{}; var next: ?*parser.Node = null; while (true) { diff --git a/src/netsurf.zig b/src/netsurf.zig index a8af4bbb..4dca3fb6 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -1216,6 +1216,10 @@ fn documentVtable(doc: *Document) c.dom_document_vtable { return getVtable(c.dom_document_vtable, Document, doc); } +pub inline fn documentToNode(doc: *Document) *Node { + return @as(*Node, @ptrCast(doc)); +} + pub inline fn documentGetElementById(doc: *Document, id: []const u8) !?*Element { var elem: ?*Element = undefined; const err = documentVtable(doc).dom_document_get_element_by_id.?(doc, try strFromData(id), &elem);