From b3fe3d02c9ffe335396e06ec90222d024c8f9ded Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Mon, 7 Jul 2025 19:47:17 +0800 Subject: [PATCH] Attribute.set_value uses element, if possible Only when setAttribute is called directly on the element, does libdom raise a `DOMAttrModified` event (which MutationObserver uses). From what I can tell, libdom's element set attribute _does_ rely on the underlying attribute set value, so the behavior should be pretty close, it just does extra things on top of that. --- src/browser/dom/attribute.zig | 10 ++++++++-- src/browser/dom/namednodemap.zig | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/browser/dom/attribute.zig b/src/browser/dom/attribute.zig index ece5a2e2..604c663f 100644 --- a/src/browser/dom/attribute.zig +++ b/src/browser/dom/attribute.zig @@ -15,7 +15,6 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . - const parser = @import("../netsurf.zig"); const Node = @import("node.zig").Node; @@ -47,7 +46,14 @@ pub const Attr = struct { } pub fn set_value(self: *parser.Attribute, v: []const u8) !?[]const u8 { - try parser.attributeSetValue(self, v); + if (try parser.attributeGetOwnerElement(self)) |el| { + // if possible, go through the element, as that triggers a + // DOMAttrModified event (which MutationObserver cares about) + const name = try parser.attributeGetName(self); + try parser.elementSetAttribute(el, name, v); + } else { + try parser.attributeSetValue(self, v); + } return v; } diff --git a/src/browser/dom/namednodemap.zig b/src/browser/dom/namednodemap.zig index ec68782d..534ba1c3 100644 --- a/src/browser/dom/namednodemap.zig +++ b/src/browser/dom/namednodemap.zig @@ -134,5 +134,7 @@ test "Browser.DOM.NamedNodeMap" { .{ "a['id'].name", "id" }, .{ "a['id'].value", "content" }, .{ "a['other']", "undefined" }, + .{ "a[0].value = 'abc123'", null }, + .{ "a[0].value", "abc123" }, }, .{}); }