Element.hasAttributes

This commit is contained in:
Karl Seguin
2025-12-02 07:01:14 +08:00
parent e807c9b6be
commit 6a48f6df25
3 changed files with 45 additions and 0 deletions

View File

@@ -129,3 +129,39 @@
testing.expectEqual(false, el1.hasAttribute('toggle-test'));
}
</script>
<script id=hasAttributes>
{
const el1 = $('#attr1');
// Element with attributes should return true
testing.expectEqual(true, el1.hasAttributes());
// Create element with no attributes
const div = document.createElement('div');
testing.expectEqual(false, div.hasAttributes());
// Add an attribute
div.setAttribute('test', 'value');
testing.expectEqual(true, div.hasAttributes());
// Remove the attribute
div.removeAttribute('test');
testing.expectEqual(false, div.hasAttributes());
// Add multiple attributes
div.setAttribute('a', '1');
div.setAttribute('b', '2');
div.setAttribute('c', '3');
testing.expectEqual(true, div.hasAttributes());
// Remove all but one
div.removeAttribute('a');
div.removeAttribute('b');
testing.expectEqual(true, div.hasAttributes());
// Remove the last one
div.removeAttribute('c');
testing.expectEqual(false, div.hasAttributes());
}
</script>

View File

@@ -288,6 +288,11 @@ pub fn hasAttribute(self: *const Element, name: []const u8, page: *Page) !bool {
return value != null;
}
pub fn hasAttributes(self: *const Element) bool {
const attributes = self._attributes orelse return false;
return attributes.isEmpty() == false;
}
pub fn getAttributeNode(self: *Element, name: []const u8, page: *Page) !?*Attribute {
const attributes = self._attributes orelse return null;
return attributes.getAttribute(name, self, page);
@@ -952,6 +957,7 @@ pub const JsApi = struct {
pub const style = bridge.accessor(Element.getStyle, null, .{});
pub const attributes = bridge.accessor(Element.getAttributeNamedNodeMap, null, .{});
pub const hasAttribute = bridge.function(Element.hasAttribute, .{});
pub const hasAttributes = bridge.function(Element.hasAttributes, .{});
pub const getAttribute = bridge.function(Element.getAttribute, .{});
pub const getAttributeNode = bridge.function(Element.getAttributeNode, .{});
pub const setAttribute = bridge.function(Element.setAttribute, .{});

View File

@@ -120,6 +120,9 @@ pub const JsApi = struct {
pub const List = struct {
_list: std.DoublyLinkedList = .{},
pub fn isEmpty(self: *const List) bool {
return self._list.first == null;
}
pub fn get(self: *const List, name: []const u8, page: *Page) !?[]const u8 {
const entry = (try self.getEntry(name, page)) orelse return null;
return entry._value.str();