accept js.Value for element.setAttribute

This commit is contained in:
Pierre Tachoire
2026-01-29 17:10:43 +01:00
parent f02fc95958
commit 100764d79e
2 changed files with 22 additions and 2 deletions

View File

@@ -248,7 +248,7 @@
}
</script>
<div id=legacy></a>
<a id=legacy></a>
<script id=legacy>
{
let a = document.getElementById('legacy').attributes;
@@ -266,3 +266,19 @@
testing.expectEqual('abc123', a[0].value);
}
</script>
<div id="nsa"></div>
<script id=non-string-attr>
{
let nsa = document.getElementById('nsa');
nsa.setAttribute('int', 1);
testing.expectEqual('1', nsa.getAttribute('int'));
nsa.setAttribute('obj', {});
testing.expectEqual('[object Object]', nsa.getAttribute('obj'));
nsa.setAttribute('arr', []);
testing.expectEqual('', nsa.getAttribute('arr'));
}
</script>

View File

@@ -1528,6 +1528,11 @@ pub const JsApi = struct {
return null;
}
pub const setAttribute = bridge.function(_setAttribute, .{ .dom_exception = true });
fn _setAttribute(self: *Element, name: String, value: js.Value, page: *Page) !void {
return self.setAttribute(name, .wrap(try value.toString(.{ .allocator = page.call_arena })), page);
}
pub const localName = bridge.accessor(Element.getLocalName, null, .{});
pub const id = bridge.accessor(Element.getId, Element.setId, .{});
pub const slot = bridge.accessor(Element.getSlot, Element.setSlot, .{});
@@ -1542,7 +1547,6 @@ pub const JsApi = struct {
pub const getAttribute = bridge.function(Element.getAttribute, .{});
pub const getAttributeNS = bridge.function(Element.getAttributeNS, .{});
pub const getAttributeNode = bridge.function(Element.getAttributeNode, .{});
pub const setAttribute = bridge.function(Element.setAttribute, .{ .dom_exception = true });
pub const setAttributeNS = bridge.function(Element.setAttributeNS, .{ .dom_exception = true });
pub const setAttributeNode = bridge.function(Element.setAttributeNode, .{});
pub const removeAttribute = bridge.function(Element.removeAttribute, .{});