Merge pull request #142 from lightpanda-io/attr

dom: implement Attr interface
This commit is contained in:
Pierre Tachoire
2024-01-08 17:39:27 +01:00
committed by GitHub
5 changed files with 186 additions and 1 deletions

View File

@@ -841,6 +841,46 @@ pub const ProcessingInstruction = c.dom_processing_instruction;
// Attribute
pub const Attribute = c.dom_attr;
fn attributeVtable(a: *Attribute) c.dom_attr_vtable {
return getVtable(c.dom_attr_vtable, Attribute, a);
}
pub fn attributeGetName(a: *Attribute) ![]const u8 {
var s: ?*String = undefined;
const err = attributeVtable(a).dom_attr_get_name.?(a, &s);
try DOMErr(err);
return strToData(s.?);
}
pub fn attributeGetValue(a: *Attribute) !?[]const u8 {
var s: ?*String = undefined;
const err = attributeVtable(a).dom_attr_get_value.?(a, &s);
try DOMErr(err);
if (s == null) return null;
return strToData(s.?);
}
pub fn attributeSetValue(a: *Attribute, v: []const u8) !void {
const err = attributeVtable(a).dom_attr_set_value.?(a, try strFromData(v));
try DOMErr(err);
}
pub fn attributeGetOwnerElement(a: *Attribute) !?*Element {
var elt: ?*Element = undefined;
const err = attributeVtable(a).dom_attr_get_owner_element.?(a, &elt);
try DOMErr(err);
if (elt == null) return null;
return elt.?;
}
// attributeToNode is an helper to convert an attribute to a node.
pub inline fn attributeToNode(a: *Attribute) *Node {
return @as(*Node, @ptrCast(a));
}
// Element
pub const Element = c.dom_element;
@@ -878,6 +918,46 @@ pub fn elementHasAttribute(elem: *Element, qname: []const u8) !bool {
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);
try DOMErr(err);
return a;
}
pub fn elementGetAttributeNodeNS(elem: *Element, ns: []const u8, name: []const u8) !?*Attribute {
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_get_attribute_node_ns.?(
elem,
try strFromData(ns),
try strFromData(name),
&a,
);
try DOMErr(err);
return a;
}
pub fn elementSetAttributeNode(elem: *Element, attr: *Attribute) !?*Attribute {
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_set_attribute_node.?(elem, attr, &a);
try DOMErr(err);
return a;
}
pub fn elementSetAttributeNodeNS(elem: *Element, attr: *Attribute) !?*Attribute {
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_set_attribute_node_ns.?(elem, attr, &a);
try DOMErr(err);
return a;
}
pub fn elementRemoveAttributeNode(elem: *Element, attr: *Attribute) !*Attribute {
var a: ?*Attribute = undefined;
const err = elementVtable(elem).dom_element_remove_attribute_node.?(elem, attr, &a);
try DOMErr(err);
return a.?;
}
pub fn elementHasClass(elem: *Element, class: []const u8) !bool {
var res: bool = undefined;
const err = elementVtable(elem).dom_element_has_class.?(