A few attribute fixes

Driven by dom/nodes/attributes.html. However, many issues remain and seem
complicated to fix. Some of the remaining issues are documented in
https://github.com/lightpanda-io/project/discussions/124
This commit is contained in:
Karl Seguin
2025-05-10 14:16:03 +08:00
parent 5cc338dedc
commit beb960b753
2 changed files with 22 additions and 2 deletions

View File

@@ -167,8 +167,13 @@ pub const Element = struct {
return try parser.elementHasAttribute(self, qname);
}
pub fn _hasAttributeNS(self: *parser.Element, ns: []const u8, qname: []const u8) !bool {
return try parser.elementHasAttributeNS(self, ns, qname);
}
// https://dom.spec.whatwg.org/#dom-element-toggleattribute
pub fn _toggleAttribute(self: *parser.Element, qname: []const u8, force: ?bool) !bool {
pub fn _toggleAttribute(self: *parser.Element, qname: []u8, force: ?bool) !bool {
_ = std.ascii.lowerString(qname, qname);
const exists = try parser.elementHasAttribute(self, qname);
// If attribute is null, then:
@@ -181,6 +186,9 @@ pub const Element = struct {
try parser.elementSetAttribute(self, qname, "");
return true;
}
if (try parser.validateName(qname) == false) {
return parser.DOMError.InvalidCharacter;
}
// Return false.
return false;

View File

@@ -25,6 +25,7 @@ const c = @cImport({
@cInclude("events/event_target.h");
@cInclude("events/event.h");
@cInclude("events/mouse_event.h");
@cInclude("utils/validate.h");
});
const mimalloc = @import("mimalloc.zig");
@@ -1509,6 +1510,13 @@ pub fn elementHasAttribute(elem: *Element, qname: []const u8) !bool {
return res;
}
pub fn elementHasAttributeNS(elem: *Element, ns: []const u8, qname: []const u8) !bool {
var res: bool = undefined;
const err = elementVtable(elem).dom_element_has_attribute_ns.?(elem, if (ns.len == 0) null else try strFromData(ns), try strFromData(qname), &res);
try DOMErr(err);
return res;
}
pub fn elementGetAttributeNode(elem: *Element, name: []const u8) !?*Attribute {
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_get_attribute_node.?(elem, try strFromData(name), &a);
@@ -1520,7 +1528,7 @@ pub fn elementGetAttributeNodeNS(elem: *Element, ns: []const u8, name: []const u
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_get_attribute_node_ns.?(
elem,
try strFromData(ns),
if (ns.len == 0) null else try strFromData(ns),
try strFromData(name),
&a,
);
@@ -2307,3 +2315,7 @@ pub fn documentHTMLGetLocation(T: type, doc: *DocumentHTML) !?*T {
const ptr: *align(@alignOf(*T)) anyopaque = @alignCast(l.?);
return @as(*T, @ptrCast(ptr));
}
pub fn validateName(name: []const u8) !bool {
return c._dom_validate_name(try strFromData(name));
}