dom: saner returns for html collection

This commit is contained in:
Pierre Tachoire
2023-11-15 17:06:42 +01:00
parent 6f2e59d663
commit 24d3854138

View File

@@ -8,6 +8,7 @@ const checkCases = jsruntime.test_utils.checkCases;
const utils = @import("utils.z"); const utils = @import("utils.z");
const Element = @import("element.zig").Element; const Element = @import("element.zig").Element;
const Union = @import("element.zig").Union;
// WEB IDL https://dom.spec.whatwg.org/#htmlcollection // WEB IDL https://dom.spec.whatwg.org/#htmlcollection
// HTMLCollection is re implemented in zig here because libdom // HTMLCollection is re implemented in zig here because libdom
@@ -87,7 +88,7 @@ pub const HTMLCollection = struct {
return len; return len;
} }
pub fn _item(self: *HTMLCollection, allocator: std.mem.Allocator, index: u32) !?*parser.Element { pub fn _item(self: *HTMLCollection, allocator: std.mem.Allocator, index: u32) !?Union {
var i: u32 = 0; var i: u32 = 0;
var node: *parser.Node = self.root; var node: *parser.Node = self.root;
var ntype: parser.NodeType = undefined; var ntype: parser.NodeType = undefined;
@@ -113,7 +114,8 @@ pub const HTMLCollection = struct {
self.cur_node = node; self.cur_node = node;
self.cur_idx = i; self.cur_idx = i;
return @as(*parser.Element, @ptrCast(node)); const e = @as(*parser.Element, @ptrCast(node));
return Element.toInterface(e);
} }
i += 1; i += 1;
@@ -126,7 +128,7 @@ pub const HTMLCollection = struct {
return null; return null;
} }
pub fn _namedItem(self: *HTMLCollection, allocator: std.mem.Allocator, name: []const u8) !?*parser.Element { pub fn _namedItem(self: *HTMLCollection, allocator: std.mem.Allocator, name: []const u8) !?Union {
if (name.len == 0) { if (name.len == 0) {
return null; return null;
} }
@@ -148,13 +150,13 @@ pub const HTMLCollection = struct {
var attr = parser.elementGetAttribute(elem, "id"); var attr = parser.elementGetAttribute(elem, "id");
// check if the node id corresponds to the name argument. // check if the node id corresponds to the name argument.
if (attr != null and std.mem.eql(u8, name, attr.?)) { if (attr != null and std.mem.eql(u8, name, attr.?)) {
return elem; return Element.toInterface(elem);
} }
attr = parser.elementGetAttribute(elem, "name"); attr = parser.elementGetAttribute(elem, "name");
// check if the node id corresponds to the name argument. // check if the node id corresponds to the name argument.
if (attr != null and std.mem.eql(u8, name, attr.?)) { if (attr != null and std.mem.eql(u8, name, attr.?)) {
return elem; return Element.toInterface(elem);
} }
} }
} }