Updates libdom

libdom's parsing is now less strict with respect to attribute names. See:
https://github.com/lightpanda-io/libdom/pull/33

However, the attribute name in setAttribute has stricter validation applied to
it, which we now handle directly.
This commit is contained in:
Karl Seguin
2025-08-08 16:22:25 +08:00
parent fbd40a6514
commit 7f2c360f33
3 changed files with 21 additions and 3 deletions

View File

@@ -633,6 +633,7 @@ test "Browser.DOM.Element" {
.{ "a.hasAttribute('foo')", "true" },
.{ "a.getAttribute('foo')", "bar" },
.{ "a.getAttributeNames()", "id,foo" },
.{ " try { a.setAttribute('.foo', 'invalid') } catch (e) { e }", "Error: InvalidCharacterError" },
.{ "a.setAttribute('foo', 'baz')", "undefined" },
.{ "a.hasAttribute('foo')", "true" },
@@ -832,4 +833,11 @@ test "Browser.DOM.Element" {
.{ "rm.remove()", "undefined" },
.{ "document.getElementById('to-remove') != null", "false" },
}, .{});
try runner.testCases(&.{
.{ "const div2 = document.createElement('div');", null },
.{ "div2.innerHTML = '<p id=1 .lit$id=9>a</p>';", null },
.{ "div2.innerHTML", "<p id=\"1\" .lit$id=\"9\">a</p>" },
.{ "div2.childNodes[0].getAttributeNames()", "id,.lit$id" },
}, .{});
}

View File

@@ -1633,9 +1633,14 @@ pub fn elementGetAttributeNS(elem: *Element, ns: []const u8, name: []const u8) !
}
pub fn elementSetAttribute(elem: *Element, qname: []const u8, value: []const u8) !void {
const dom_str = try strFromData(qname);
if (!c._dom_validate_name(dom_str)) {
return error.InvalidCharacterError;
}
const err = elementVtable(elem).dom_element_set_attribute.?(
elem,
try strFromData(qname),
dom_str,
try strFromData(value),
);
try DOMErr(err);
@@ -1647,10 +1652,15 @@ pub fn elementSetAttributeNS(
qname: []const u8,
value: []const u8,
) !void {
const dom_str = try strFromData(qname);
if (!c._dom_validate_name(dom_str)) {
return error.InvalidCharacterError;
}
const err = elementVtable(elem).dom_element_set_attribute_ns.?(
elem,
try strFromData(ns),
try strFromData(qname),
dom_str,
try strFromData(value),
);
try DOMErr(err);