Skip to content

Commit 7f2c360

Browse files
committed
Updates libdom
libdom's parsing is now less strict with respect to attribute names. See: lightpanda-io/libdom#33 However, the attribute name in setAttribute has stricter validation applied to it, which we now handle directly.
1 parent fbd40a6 commit 7f2c360

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/browser/dom/element.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ test "Browser.DOM.Element" {
633633
.{ "a.hasAttribute('foo')", "true" },
634634
.{ "a.getAttribute('foo')", "bar" },
635635
.{ "a.getAttributeNames()", "id,foo" },
636+
.{ " try { a.setAttribute('.foo', 'invalid') } catch (e) { e }", "Error: InvalidCharacterError" },
636637

637638
.{ "a.setAttribute('foo', 'baz')", "undefined" },
638639
.{ "a.hasAttribute('foo')", "true" },
@@ -832,4 +833,11 @@ test "Browser.DOM.Element" {
832833
.{ "rm.remove()", "undefined" },
833834
.{ "document.getElementById('to-remove') != null", "false" },
834835
}, .{});
836+
837+
try runner.testCases(&.{
838+
.{ "const div2 = document.createElement('div');", null },
839+
.{ "div2.innerHTML = '<p id=1 .lit$id=9>a</p>';", null },
840+
.{ "div2.innerHTML", "<p id=\"1\" .lit$id=\"9\">a</p>" },
841+
.{ "div2.childNodes[0].getAttributeNames()", "id,.lit$id" },
842+
}, .{});
835843
}

src/browser/netsurf.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,9 +1633,14 @@ pub fn elementGetAttributeNS(elem: *Element, ns: []const u8, name: []const u8) !
16331633
}
16341634

16351635
pub fn elementSetAttribute(elem: *Element, qname: []const u8, value: []const u8) !void {
1636+
const dom_str = try strFromData(qname);
1637+
if (!c._dom_validate_name(dom_str)) {
1638+
return error.InvalidCharacterError;
1639+
}
1640+
16361641
const err = elementVtable(elem).dom_element_set_attribute.?(
16371642
elem,
1638-
try strFromData(qname),
1643+
dom_str,
16391644
try strFromData(value),
16401645
);
16411646
try DOMErr(err);
@@ -1647,10 +1652,15 @@ pub fn elementSetAttributeNS(
16471652
qname: []const u8,
16481653
value: []const u8,
16491654
) !void {
1655+
const dom_str = try strFromData(qname);
1656+
if (!c._dom_validate_name(dom_str)) {
1657+
return error.InvalidCharacterError;
1658+
}
1659+
16501660
const err = elementVtable(elem).dom_element_set_attribute_ns.?(
16511661
elem,
16521662
try strFromData(ns),
1653-
try strFromData(qname),
1663+
dom_str,
16541664
try strFromData(value),
16551665
);
16561666
try DOMErr(err);

0 commit comments

Comments
 (0)