Skip to content

add element.getAttributeNames() #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,22 @@ pub const Element = struct {
return true;
}

pub fn _getAttributeNames(self: *parser.Element, page: *Page) ![]const []const u8 {
const attributes = try parser.nodeGetAttributes(@ptrCast(self)) orelse return &.{};
const ln = try parser.namedNodeMapGetLength(attributes);

const names = try page.call_arena.alloc([]const u8, ln);
var at: usize = 0;

for (0..ln) |i| {
const attribute = try parser.namedNodeMapItem(attributes, @intCast(i)) orelse break;
names[at] = try parser.attributeGetName(attribute);
at += 1;
}

return names[0..at];
}

pub fn _getAttributeNode(self: *parser.Element, name: []const u8) !?*parser.Attribute {
return try parser.elementGetAttributeNode(self, name);
}
Expand Down Expand Up @@ -597,6 +613,7 @@ test "Browser.DOM.Element" {
.{ "let a = document.getElementById('content')", "undefined" },
.{ "a.hasAttributes()", "true" },
.{ "a.attributes.length", "1" },
.{ "a.getAttributeNames()", "id" },
.{ "a.getAttribute('id')", "content" },
.{ "a.attributes['id'].value", "content" },
.{
Expand All @@ -615,6 +632,7 @@ test "Browser.DOM.Element" {
.{ "a.setAttribute('foo', 'bar')", "undefined" },
.{ "a.hasAttribute('foo')", "true" },
.{ "a.getAttribute('foo')", "bar" },
.{ "a.getAttributeNames()", "id,foo" },

.{ "a.setAttribute('foo', 'baz')", "undefined" },
.{ "a.hasAttribute('foo')", "true" },
Expand Down Expand Up @@ -807,6 +825,7 @@ test "Browser.DOM.Element" {

try runner.testCases(&.{
.{ "const rm = document.createElement('div')", null },
.{ "rm.getAttributeNames()", "" },
.{ "rm.id = 'to-remove'", null },
.{ "document.getElementsByTagName('body')[0].appendChild(rm)", null },
.{ "document.getElementById('to-remove') != null", "true" },
Expand Down