@@ -201,11 +201,11 @@ pub const Node = struct {
201201 const self_owner = try parser .nodeOwnerDocument (self );
202202 const child_owner = try parser .nodeOwnerDocument (child );
203203
204- // If the node to be inserted has a different ownerDocument than the parent node,
205- // modern browsers automatically adopt the node and its descendants into
206- // the parent's ownerDocument.
204+ // If the node to be inserted has a different ownerDocument than the parent node,
205+ // modern browsers automatically adopt the node and its descendants into
206+ // the parent's ownerDocument.
207207 // This process is known as adoption.
208- // (7.1) https://dom.spec.whatwg.org/#concept-node-insert
208+ // (7.1) https://dom.spec.whatwg.org/#concept-node-insert
209209 if (child_owner == null or (child_owner .? != self_owner .? )) {
210210 const w = Walker {};
211211 var current = child ;
@@ -307,27 +307,28 @@ pub const Node = struct {
307307 }
308308
309309 pub fn _insertBefore (self : * parser.Node , new_node : * parser.Node , ref_node_ : ? * parser.Node ) ! Union {
310+ if (ref_node_ == null ) {
311+ return _appendChild (self , new_node );
312+ }
313+
310314 const self_owner = try parser .nodeOwnerDocument (self );
311315 const new_node_owner = try parser .nodeOwnerDocument (new_node );
312316
313- // If the node to be inserted has a different ownerDocument than the parent node,
314- // modern browsers automatically adopt the node and its descendants into
315- // the parent's ownerDocument.
317+ // If the node to be inserted has a different ownerDocument than the parent node,
318+ // modern browsers automatically adopt the node and its descendants into
319+ // the parent's ownerDocument.
316320 // This process is known as adoption.
317- // (7.1) https://dom.spec.whatwg.org/#concept-node-insert
321+ // (7.1) https://dom.spec.whatwg.org/#concept-node-insert
318322 if (new_node_owner == null or (new_node_owner .? != self_owner .? )) {
319323 const w = Walker {};
320324 var current = new_node ;
321- while (true ) {
325+ while (true ) {
322326 current .owner = self_owner ;
323327 current = try w .get_next (new_node , current ) orelse break ;
324328 }
325329 }
326330
327- if (ref_node_ ) | ref_node | {
328- return Node .toInterface (try parser .nodeInsertBefore (self , new_node , ref_node ));
329- }
330- return _appendChild (self , new_node );
331+ return Node .toInterface (try parser .nodeInsertBefore (self , new_node , ref_node_ .? ));
331332 }
332333
333334 pub fn _isDefaultNamespace (self : * parser.Node , namespace : ? []const u8 ) ! bool {
@@ -756,7 +757,7 @@ test "Browser.DOM.node" {
756757}
757758
758759test "Browser.DOM.node.owner" {
759- var runner = try testing .jsRunner (testing .tracking_allocator , .{ .html =
760+ var runner = try testing .jsRunner (testing .tracking_allocator , .{ .html =
760761 \\ <div id="target-container">
761762 \\ <p id="reference-node">
762763 \\ I am the original reference node.
@@ -770,12 +771,9 @@ test "Browser.DOM.node.owner" {
770771 .{
771772 \\ const parser = new DOMParser();
772773 \\ const newDoc = parser.parseFromString('<div id="new-node"><p>Hey</p><span>Marked</span></div>', 'text/html');
773-
774774 \\ const newNode = newDoc.getElementById('new-node');
775-
776775 \\ const parent = document.getElementById('target-container');
777776 \\ const referenceNode = document.getElementById('reference-node');
778-
779777 \\ parent.insertBefore(newNode, referenceNode);
780778 \\ const k = document.getElementById('new-node');
781779 \\ const ptag = k.querySelector('p');
@@ -784,18 +782,18 @@ test "Browser.DOM.node.owner" {
784782 \\ const anotherNewNode = anotherDoc.getElementById('another-new-node');
785783 \\
786784 \\ parent.appendChild(anotherNewNode)
787- ,
785+ ,
788786 "[object HTMLDivElement]" ,
789787 },
790-
791- .{"parent.ownerDocument === newNode.ownerDocument" , "true" },
792- .{"parent.ownerDocument === anotherNewNode.ownerDocument" , "true" },
793- .{"newNode.firstChild.nodeName" , "P" },
794- .{"ptag.ownerDocument === parent.ownerDocument" , "true" },
795- .{"spanTag.ownerDocument === parent.ownerDocument" , "true" },
796- .{"parent.contains(newNode)" , "true" },
797- .{"parent.contains(anotherNewNode)" , "true" },
798- .{"anotherDoc.contains(anotherNewNode)" , "false" },
799- .{"newDoc.contains(newNode)" , "false" },
788+
789+ .{ "parent.ownerDocument === newNode.ownerDocument" , "true" },
790+ .{ "parent.ownerDocument === anotherNewNode.ownerDocument" , "true" },
791+ .{ "newNode.firstChild.nodeName" , "P" },
792+ .{ "ptag.ownerDocument === parent.ownerDocument" , "true" },
793+ .{ "spanTag.ownerDocument === parent.ownerDocument" , "true" },
794+ .{ "parent.contains(newNode)" , "true" },
795+ .{ "parent.contains(anotherNewNode)" , "true" },
796+ .{ "anotherDoc.contains(anotherNewNode)" , "false" },
797+ .{ "newDoc.contains(newNode)" , "false" },
800798 }, .{});
801- }
799+ }
0 commit comments