Merge pull request #727 from lightpanda-io/named_node_map_named_index_and_iteartor

Implement named_get and iterator on NamedNodeMap
This commit is contained in:
Karl Seguin
2025-05-30 22:22:06 +08:00
committed by GitHub
3 changed files with 53 additions and 2 deletions

View File

@@ -34,6 +34,7 @@ pub const Interfaces = .{
EventTarget, EventTarget,
DOMImplementation, DOMImplementation,
NamedNodeMap, NamedNodeMap,
NamedNodeMap.Iterator,
DOMTokenList.Interfaces, DOMTokenList.Interfaces,
NodeList.Interfaces, NodeList.Interfaces,
Node.Node, Node.Node,

View File

@@ -470,8 +470,17 @@ test "Browser.DOM.Element" {
.{ "let a = document.getElementById('content')", "undefined" }, .{ "let a = document.getElementById('content')", "undefined" },
.{ "a.hasAttributes()", "true" }, .{ "a.hasAttributes()", "true" },
.{ "a.attributes.length", "1" }, .{ "a.attributes.length", "1" },
.{ "a.getAttribute('id')", "content" }, .{ "a.getAttribute('id')", "content" },
.{ "a.attributes['id'].value", "content" },
.{
\\ let x = '';
\\ for (const attr of a.attributes) {
\\ x += attr.name + '=' + attr.value;
\\ }
\\ x;
,
"id=content",
},
.{ "a.hasAttribute('foo')", "false" }, .{ "a.hasAttribute('foo')", "false" },
.{ "a.getAttribute('foo')", "null" }, .{ "a.getAttribute('foo')", "null" },

View File

@@ -25,6 +25,7 @@ pub const NamedNodeMap = struct {
pub const Self = parser.NamedNodeMap; pub const Self = parser.NamedNodeMap;
pub const Exception = DOMException; pub const Exception = DOMException;
pub const Iterator = NamedNodeMapIterator;
// TODO implement LegacyUnenumerableNamedProperties. // TODO implement LegacyUnenumerableNamedProperties.
// https://webidl.spec.whatwg.org/#LegacyUnenumerableNamedProperties // https://webidl.spec.whatwg.org/#LegacyUnenumerableNamedProperties
@@ -70,11 +71,48 @@ pub const NamedNodeMap = struct {
} }
pub fn indexed_get(self: *parser.NamedNodeMap, index: u32, has_value: *bool) !*parser.Attribute { pub fn indexed_get(self: *parser.NamedNodeMap, index: u32, has_value: *bool) !*parser.Attribute {
return (try NamedNodeMap._item(self, index)) orelse { return (try _item(self, index)) orelse {
has_value.* = false; has_value.* = false;
return undefined; return undefined;
}; };
} }
pub fn named_get(self: *parser.NamedNodeMap, name: []const u8, has_value: *bool) !*parser.Attribute {
return (try _getNamedItem(self, name)) orelse {
has_value.* = false;
return undefined;
};
}
pub fn _symbol_iterator(self: *parser.NamedNodeMap) NamedNodeMapIterator {
return .{ .map = self };
}
};
pub const NamedNodeMapIterator = struct {
index: u32 = 0,
map: *parser.NamedNodeMap,
pub const Return = struct {
done: bool,
value: ?*parser.Attribute,
};
pub fn _next(self: *NamedNodeMapIterator) !Return {
const e = try NamedNodeMap._item(self.map, self.index);
if (e == null) {
return .{
.value = null,
.done = true,
};
}
self.index += 1;
return .{
.value = e,
.done = false,
};
}
}; };
// Tests // Tests
@@ -93,5 +131,8 @@ test "Browser.DOM.NamedNodeMap" {
.{ "a.getNamedItem('id')", "[object Attr]" }, .{ "a.getNamedItem('id')", "[object Attr]" },
.{ "a.getNamedItem('foo')", "null" }, .{ "a.getNamedItem('foo')", "null" },
.{ "a.setNamedItem(a.getNamedItem('id'))", "[object Attr]" }, .{ "a.setNamedItem(a.getNamedItem('id'))", "[object Attr]" },
.{ "a['id'].name", "id" },
.{ "a['id'].value", "content" },
.{ "a['other']", "undefined" },
}, .{}); }, .{});
} }