Document: use document cast to node

This commit is contained in:
Pierre Tachoire
2024-01-09 14:24:55 +01:00
parent be1e55272a
commit ca6bb577c6
2 changed files with 8 additions and 18 deletions

View File

@@ -108,12 +108,7 @@ pub const Document = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
tag_name: []const u8, tag_name: []const u8,
) !collection.HTMLCollection { ) !collection.HTMLCollection {
var elt: ?*parser.Node = null; return try collection.HTMLCollectionByTagName(alloc, parser.documentToNode(self), tag_name, true);
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionByTagName(alloc, elt, tag_name, true);
} }
pub fn _getElementsByClassName( pub fn _getElementsByClassName(
@@ -121,12 +116,7 @@ pub const Document = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
classNames: []const u8, classNames: []const u8,
) !collection.HTMLCollection { ) !collection.HTMLCollection {
var elt: ?*parser.Node = null; return try collection.HTMLCollectionByClassName(alloc, parser.documentToNode(self), classNames, true);
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionByClassName(alloc, elt, classNames, true);
} }
pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment { pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment {
@@ -170,11 +160,7 @@ pub const Document = struct {
// ParentNode // ParentNode
// https://dom.spec.whatwg.org/#parentnode // https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Document) !collection.HTMLCollection { pub fn get_children(self: *parser.Document) !collection.HTMLCollection {
var elt: ?*parser.Node = null; return try collection.HTMLCollectionChildren(parser.documentToNode(self), false);
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionChildren(elt, true);
} }
pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion { pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion {
@@ -219,7 +205,7 @@ pub const Document = struct {
// catch-all, return all elements // catch-all, return all elements
if (selectors[0] == '*') { if (selectors[0] == '*') {
// walk over the node tree fo find the node by id. // 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{}; const walker = Walker{};
var next: ?*parser.Node = null; var next: ?*parser.Node = null;
while (true) { while (true) {

View File

@@ -1216,6 +1216,10 @@ fn documentVtable(doc: *Document) c.dom_document_vtable {
return getVtable(c.dom_document_vtable, Document, doc); 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 { pub inline fn documentGetElementById(doc: *Document, id: []const u8) !?*Element {
var elem: ?*Element = undefined; var elem: ?*Element = undefined;
const err = documentVtable(doc).dom_document_get_element_by_id.?(doc, try strFromData(id), &elem); const err = documentVtable(doc).dom_document_get_element_by_id.?(doc, try strFromData(id), &elem);