From bd9e4dbc7901aba11b814caad5c45e90c9ba902c Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 5 Aug 2025 14:45:25 +0200 Subject: [PATCH 1/2] node: don't call owner twice in _insertBefore When the ref_node_ is null, call directly _appendChild w/o fixing the node's owner. --- src/browser/dom/node.zig | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/browser/dom/node.zig b/src/browser/dom/node.zig index 3dd6a06f..2660c6ea 100644 --- a/src/browser/dom/node.zig +++ b/src/browser/dom/node.zig @@ -307,27 +307,28 @@ pub const Node = struct { } pub fn _insertBefore(self: *parser.Node, new_node: *parser.Node, ref_node_: ?*parser.Node) !Union { + if (ref_node_ == null) { + return _appendChild(self, new_node); + } + const self_owner = try parser.nodeOwnerDocument(self); const new_node_owner = try parser.nodeOwnerDocument(new_node); - // If the node to be inserted has a different ownerDocument than the parent node, - // modern browsers automatically adopt the node and its descendants into - // the parent's ownerDocument. + // If the node to be inserted has a different ownerDocument than the parent node, + // modern browsers automatically adopt the node and its descendants into + // the parent's ownerDocument. // This process is known as adoption. - // (7.1) https://dom.spec.whatwg.org/#concept-node-insert + // (7.1) https://dom.spec.whatwg.org/#concept-node-insert if (new_node_owner == null or (new_node_owner.? != self_owner.?)) { const w = Walker{}; var current = new_node; - while(true) { + while (true) { current.owner = self_owner; current = try w.get_next(new_node, current) orelse break; } } - if (ref_node_) |ref_node| { - return Node.toInterface(try parser.nodeInsertBefore(self, new_node, ref_node)); - } - return _appendChild(self, new_node); + return Node.toInterface(try parser.nodeInsertBefore(self, new_node, ref_node_.?)); } pub fn _isDefaultNamespace(self: *parser.Node, namespace: ?[]const u8) !bool { From b2b2e97edc60fa00c3b529356e3be5658b9283b7 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 5 Aug 2025 14:47:25 +0200 Subject: [PATCH 2/2] zig fmt --- src/browser/dom/node.zig | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/browser/dom/node.zig b/src/browser/dom/node.zig index 2660c6ea..b9df962f 100644 --- a/src/browser/dom/node.zig +++ b/src/browser/dom/node.zig @@ -201,11 +201,11 @@ pub const Node = struct { const self_owner = try parser.nodeOwnerDocument(self); const child_owner = try parser.nodeOwnerDocument(child); - // If the node to be inserted has a different ownerDocument than the parent node, - // modern browsers automatically adopt the node and its descendants into - // the parent's ownerDocument. + // If the node to be inserted has a different ownerDocument than the parent node, + // modern browsers automatically adopt the node and its descendants into + // the parent's ownerDocument. // This process is known as adoption. - // (7.1) https://dom.spec.whatwg.org/#concept-node-insert + // (7.1) https://dom.spec.whatwg.org/#concept-node-insert if (child_owner == null or (child_owner.? != self_owner.?)) { const w = Walker{}; var current = child; @@ -757,7 +757,7 @@ test "Browser.DOM.node" { } test "Browser.DOM.node.owner" { - var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html= + var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = \\
\\

\\ I am the original reference node. @@ -771,12 +771,9 @@ test "Browser.DOM.node.owner" { .{ \\ const parser = new DOMParser(); \\ const newDoc = parser.parseFromString('

Hey

Marked
', 'text/html'); - \\ const newNode = newDoc.getElementById('new-node'); - \\ const parent = document.getElementById('target-container'); \\ const referenceNode = document.getElementById('reference-node'); - \\ parent.insertBefore(newNode, referenceNode); \\ const k = document.getElementById('new-node'); \\ const ptag = k.querySelector('p'); @@ -785,18 +782,18 @@ test "Browser.DOM.node.owner" { \\ const anotherNewNode = anotherDoc.getElementById('another-new-node'); \\ \\ parent.appendChild(anotherNewNode) - , + , "[object HTMLDivElement]", }, - - .{"parent.ownerDocument === newNode.ownerDocument", "true" }, - .{"parent.ownerDocument === anotherNewNode.ownerDocument", "true"}, - .{"newNode.firstChild.nodeName", "P"}, - .{"ptag.ownerDocument === parent.ownerDocument", "true"}, - .{"spanTag.ownerDocument === parent.ownerDocument", "true"}, - .{"parent.contains(newNode)", "true"}, - .{"parent.contains(anotherNewNode)", "true"}, - .{"anotherDoc.contains(anotherNewNode)", "false"}, - .{"newDoc.contains(newNode)", "false"}, + + .{ "parent.ownerDocument === newNode.ownerDocument", "true" }, + .{ "parent.ownerDocument === anotherNewNode.ownerDocument", "true" }, + .{ "newNode.firstChild.nodeName", "P" }, + .{ "ptag.ownerDocument === parent.ownerDocument", "true" }, + .{ "spanTag.ownerDocument === parent.ownerDocument", "true" }, + .{ "parent.contains(newNode)", "true" }, + .{ "parent.contains(anotherNewNode)", "true" }, + .{ "anotherDoc.contains(anotherNewNode)", "false" }, + .{ "newDoc.contains(newNode)", "false" }, }, .{}); -} \ No newline at end of file +}