Skip to content

Commit 24ccfca

Browse files
committed
Fix element.hasAttributes
libdom's hasAttributes is based on the type. Elements, according to libdom, always have attributes, thus hasAttributes always return true, even when the element in question has no attribute. Change our _hasAttributes to only return true if the attribute count > 0.
1 parent bdc49a6 commit 24ccfca

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/browser/dom/element.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ pub const Element = struct {
160160
}
161161
}
162162

163+
// don't use parser.nodeHasAttributes(...) because that returns true/false
164+
// based on the type, e.g. a node never as attributes, an element always has
165+
// attributes. But, Element.hasAttributes is supposed to return true only
166+
// if the element has at least 1 attribute.
163167
pub fn _hasAttributes(self: *parser.Element) !bool {
164-
return try parser.nodeHasAttributes(parser.elementToNode(self));
168+
// an element _must_ have at least an empty attribute
169+
const node_map = try parser.nodeGetAttributes(parser.elementToNode(self)) orelse unreachable;
170+
return try parser.namedNodeMapGetLength(node_map) > 0;
165171
}
166172

167173
pub fn _getAttribute(self: *parser.Element, qname: []const u8) !?[]const u8 {
@@ -679,4 +685,8 @@ test "Browser.DOM.Element" {
679685
.{ "div1.innerHTML = \" <link/><table></table><a href='/a'>a</a><input type='checkbox'/>\"", null },
680686
.{ "div1.getElementsByTagName('a').length", "1" },
681687
}, .{});
688+
689+
try runner.testCases(&.{
690+
.{ "document.createElement('a').hasAttributes()", "false" },
691+
}, .{});
682692
}

0 commit comments

Comments
 (0)