diff --git a/src/browser/dom/dom.zig b/src/browser/dom/dom.zig index 0b998063..f9b7514f 100644 --- a/src/browser/dom/dom.zig +++ b/src/browser/dom/dom.zig @@ -34,6 +34,7 @@ pub const Interfaces = .{ EventTarget, DOMImplementation, NamedNodeMap, + NamedNodeMap.Iterator, DOMTokenList.Interfaces, NodeList.Interfaces, Node.Node, diff --git a/src/browser/dom/element.zig b/src/browser/dom/element.zig index 12e5f39d..698307cb 100644 --- a/src/browser/dom/element.zig +++ b/src/browser/dom/element.zig @@ -470,8 +470,17 @@ test "Browser.DOM.Element" { .{ "let a = document.getElementById('content')", "undefined" }, .{ "a.hasAttributes()", "true" }, .{ "a.attributes.length", "1" }, - .{ "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.getAttribute('foo')", "null" }, diff --git a/src/browser/dom/namednodemap.zig b/src/browser/dom/namednodemap.zig index 1923b12c..ec68782d 100644 --- a/src/browser/dom/namednodemap.zig +++ b/src/browser/dom/namednodemap.zig @@ -25,6 +25,7 @@ pub const NamedNodeMap = struct { pub const Self = parser.NamedNodeMap; pub const Exception = DOMException; + pub const Iterator = NamedNodeMapIterator; // TODO implement 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 { - return (try NamedNodeMap._item(self, index)) orelse { + return (try _item(self, index)) orelse { has_value.* = false; 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 @@ -93,5 +131,8 @@ test "Browser.DOM.NamedNodeMap" { .{ "a.getNamedItem('id')", "[object Attr]" }, .{ "a.getNamedItem('foo')", "null" }, .{ "a.setNamedItem(a.getNamedItem('id'))", "[object Attr]" }, + .{ "a['id'].name", "id" }, + .{ "a['id'].value", "content" }, + .{ "a['other']", "undefined" }, }, .{}); }