Skip to content

Commit 6adb46a

Browse files
authored
Merge pull request #727 from lightpanda-io/named_node_map_named_index_and_iteartor
Implement named_get and iterator on NamedNodeMap
2 parents 3ef1d8b + cfdbd41 commit 6adb46a

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/browser/dom/dom.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub const Interfaces = .{
3434
EventTarget,
3535
DOMImplementation,
3636
NamedNodeMap,
37+
NamedNodeMap.Iterator,
3738
DOMTokenList.Interfaces,
3839
NodeList.Interfaces,
3940
Node.Node,

src/browser/dom/element.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,17 @@ test "Browser.DOM.Element" {
470470
.{ "let a = document.getElementById('content')", "undefined" },
471471
.{ "a.hasAttributes()", "true" },
472472
.{ "a.attributes.length", "1" },
473-
474473
.{ "a.getAttribute('id')", "content" },
474+
.{ "a.attributes['id'].value", "content" },
475+
.{
476+
\\ let x = '';
477+
\\ for (const attr of a.attributes) {
478+
\\ x += attr.name + '=' + attr.value;
479+
\\ }
480+
\\ x;
481+
,
482+
"id=content",
483+
},
475484

476485
.{ "a.hasAttribute('foo')", "false" },
477486
.{ "a.getAttribute('foo')", "null" },

src/browser/dom/namednodemap.zig

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub const NamedNodeMap = struct {
2525
pub const Self = parser.NamedNodeMap;
2626

2727
pub const Exception = DOMException;
28+
pub const Iterator = NamedNodeMapIterator;
2829

2930
// TODO implement LegacyUnenumerableNamedProperties.
3031
// https://webidl.spec.whatwg.org/#LegacyUnenumerableNamedProperties
@@ -70,11 +71,48 @@ pub const NamedNodeMap = struct {
7071
}
7172

7273
pub fn indexed_get(self: *parser.NamedNodeMap, index: u32, has_value: *bool) !*parser.Attribute {
73-
return (try NamedNodeMap._item(self, index)) orelse {
74+
return (try _item(self, index)) orelse {
7475
has_value.* = false;
7576
return undefined;
7677
};
7778
}
79+
80+
pub fn named_get(self: *parser.NamedNodeMap, name: []const u8, has_value: *bool) !*parser.Attribute {
81+
return (try _getNamedItem(self, name)) orelse {
82+
has_value.* = false;
83+
return undefined;
84+
};
85+
}
86+
87+
pub fn _symbol_iterator(self: *parser.NamedNodeMap) NamedNodeMapIterator {
88+
return .{ .map = self };
89+
}
90+
};
91+
92+
pub const NamedNodeMapIterator = struct {
93+
index: u32 = 0,
94+
map: *parser.NamedNodeMap,
95+
96+
pub const Return = struct {
97+
done: bool,
98+
value: ?*parser.Attribute,
99+
};
100+
101+
pub fn _next(self: *NamedNodeMapIterator) !Return {
102+
const e = try NamedNodeMap._item(self.map, self.index);
103+
if (e == null) {
104+
return .{
105+
.value = null,
106+
.done = true,
107+
};
108+
}
109+
110+
self.index += 1;
111+
return .{
112+
.value = e,
113+
.done = false,
114+
};
115+
}
78116
};
79117

80118
// Tests
@@ -93,5 +131,8 @@ test "Browser.DOM.NamedNodeMap" {
93131
.{ "a.getNamedItem('id')", "[object Attr]" },
94132
.{ "a.getNamedItem('foo')", "null" },
95133
.{ "a.setNamedItem(a.getNamedItem('id'))", "[object Attr]" },
134+
.{ "a['id'].name", "id" },
135+
.{ "a['id'].value", "content" },
136+
.{ "a['other']", "undefined" },
96137
}, .{});
97138
}

0 commit comments

Comments
 (0)