Skip to content

Commit fdc0e25

Browse files
Merge pull request #621 from lightpanda-io/attributes
A few attribute fixes
2 parents 5cc338d + beb960b commit fdc0e25

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/browser/dom/element.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ pub const Element = struct {
167167
return try parser.elementHasAttribute(self, qname);
168168
}
169169

170+
pub fn _hasAttributeNS(self: *parser.Element, ns: []const u8, qname: []const u8) !bool {
171+
return try parser.elementHasAttributeNS(self, ns, qname);
172+
}
173+
170174
// https://dom.spec.whatwg.org/#dom-element-toggleattribute
171-
pub fn _toggleAttribute(self: *parser.Element, qname: []const u8, force: ?bool) !bool {
175+
pub fn _toggleAttribute(self: *parser.Element, qname: []u8, force: ?bool) !bool {
176+
_ = std.ascii.lowerString(qname, qname);
172177
const exists = try parser.elementHasAttribute(self, qname);
173178

174179
// If attribute is null, then:
@@ -181,6 +186,9 @@ pub const Element = struct {
181186
try parser.elementSetAttribute(self, qname, "");
182187
return true;
183188
}
189+
if (try parser.validateName(qname) == false) {
190+
return parser.DOMError.InvalidCharacter;
191+
}
184192

185193
// Return false.
186194
return false;

src/browser/netsurf.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const c = @cImport({
2525
@cInclude("events/event_target.h");
2626
@cInclude("events/event.h");
2727
@cInclude("events/mouse_event.h");
28+
@cInclude("utils/validate.h");
2829
});
2930

3031
const mimalloc = @import("mimalloc.zig");
@@ -1509,6 +1510,13 @@ pub fn elementHasAttribute(elem: *Element, qname: []const u8) !bool {
15091510
return res;
15101511
}
15111512

1513+
pub fn elementHasAttributeNS(elem: *Element, ns: []const u8, qname: []const u8) !bool {
1514+
var res: bool = undefined;
1515+
const err = elementVtable(elem).dom_element_has_attribute_ns.?(elem, if (ns.len == 0) null else try strFromData(ns), try strFromData(qname), &res);
1516+
try DOMErr(err);
1517+
return res;
1518+
}
1519+
15121520
pub fn elementGetAttributeNode(elem: *Element, name: []const u8) !?*Attribute {
15131521
var a: ?*Attribute = undefined;
15141522
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
15201528
var a: ?*Attribute = undefined;
15211529
const err = elementVtable(elem).dom_element_get_attribute_node_ns.?(
15221530
elem,
1523-
try strFromData(ns),
1531+
if (ns.len == 0) null else try strFromData(ns),
15241532
try strFromData(name),
15251533
&a,
15261534
);
@@ -2307,3 +2315,7 @@ pub fn documentHTMLGetLocation(T: type, doc: *DocumentHTML) !?*T {
23072315
const ptr: *align(@alignOf(*T)) anyopaque = @alignCast(l.?);
23082316
return @as(*T, @ptrCast(ptr));
23092317
}
2318+
2319+
pub fn validateName(name: []const u8) !bool {
2320+
return c._dom_validate_name(try strFromData(name));
2321+
}

0 commit comments

Comments
 (0)