element: add prefix and localName accessors

This commit is contained in:
Pierre Tachoire
2025-11-25 11:46:54 +01:00
parent 6d6f1340af
commit 4a4602137b
2 changed files with 28 additions and 0 deletions

View File

@@ -28,5 +28,13 @@
const regularDiv = document.createElement('div');
testing.expectEqual('DIV', regularDiv.tagName);
testing.expectEqual('div', regularDiv.localName);
testing.expectEqual(null, regularDiv.prefix);
testing.expectEqual('http://www.w3.org/1999/xhtml', regularDiv.namespaceURI);
const custom = document.createElementNS('test', 'te:ST');
testing.expectEqual('TE:ST', custom.tagName); // Should be te:ST
testing.expectEqual('te', custom.prefix);
testing.expectEqual('ST', custom.localName);
testing.expectEqual('http://www.w3.org/1999/xhtml', custom.namespaceURI); // Should be test
</script>

View File

@@ -913,6 +913,26 @@ pub const JsApi = struct {
return buf.written();
}
pub const prefix = bridge.accessor(_prefix, null, .{});
fn _prefix(self: *Element) ?[]const u8 {
const name = self.getTagNameLower();
if (std.mem.indexOfPos(u8, name, 0, ":")) |pos| {
return name[0..pos];
}
return null;
}
pub const localName = bridge.accessor(_localName, null, .{});
fn _localName(self: *Element) []const u8 {
const name = self.getTagNameLower();
if (std.mem.indexOfPos(u8, name, 0, ":")) |pos| {
return name[pos + 1 ..];
}
return name;
}
pub const id = bridge.accessor(Element.getId, Element.setId, .{});
pub const className = bridge.accessor(Element.getClassName, Element.setClassName, .{});
pub const classList = bridge.accessor(Element.getClassList, null, .{});