Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion src/browser/netsurf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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));
}