Skip to content

Truly independent web browser

LibWeb: Adopt the node before removing the child in replaceChild()

Andreas Kling6024bc2f81ae
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index fa25c83699c..5600439d742 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -1856,7 +1856,10 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::replace_child(GC::Ref<Node> node, GC::R
// 10. Let removedNodes be the empty set.
GC::RootVector<GC::Ref<Node>> removed_nodes;
- // 11. If child’s parent is non-null:
+ // 11. Adopt node into parent's node document.
+ document().adopt_node_steps(node);
+
+ // 12. If child’s parent is non-null:
// NOTE: The above can only be false if child is node.
if (child->parent()) {
// 1. Set removedNodes to « child ».
@@ -1866,7 +1869,7 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::replace_child(GC::Ref<Node> node, GC::R
child->remove(true);
}
- // 12. Let nodes be node’s children if node is a DocumentFragment node; otherwise « node ».
+ // 13. Let nodes be node’s children if node is a DocumentFragment node; otherwise « node ».
GC::RootVector<GC::Ref<Node>> nodes;
if (is<DocumentFragment>(*node))
nodes = node->children_as_vector();
@@ -1877,14 +1880,14 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::replace_child(GC::Ref<Node> node, GC::R
// the sanity of inserting `node` before `reference_child` again, as well as
// `child` not being reinserted elsewhere.
if (!reference_child || (reference_child->parent() == this && !child->parent_node())) {
- // 13. Insert node into parent before referenceChild with suppressObservers set to true.
+ // 14. Insert node into parent before referenceChild with suppressObservers set to true.
insert_before(node, reference_child, true);
}
- // 14. Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild.
+ // 15. Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild.
queue_tree_mutation_record(nodes, removed_nodes, previous_sibling.ptr(), reference_child.ptr());
- // 15. Return child.
+ // 16. Return child.
return child;
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/MutationObserver-childList.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/MutationObserver-childList.txt
index 617a5833ac6..54760be6f71 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/MutationObserver-childList.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/MutationObserver-childList.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 40 tests
-38 Pass
-2 Fail
+40 Pass
Pass childList Node.nodeValue: no mutation
Pass childList Node.textContent: replace content mutation
Pass childList Node.textContent: no previous content mutation
@@ -25,8 +24,8 @@ Pass childList Node.appendChild: fragment removal mutations
Pass childList Node.appendChild: addition outside document tree mutation
Pass childList Node.replaceChild: replacement mutation
Pass childList Node.replaceChild: removal mutation
-Fail childList Node.replaceChild: internal replacement mutation
-Fail childList Node.replaceChild: self internal replacement mutation
+Pass childList Node.replaceChild: internal replacement mutation
+Pass childList Node.replaceChild: self internal replacement mutation
Pass childList ParentNode.replaceChildren: move an existing child
Pass childList ParentNode.replaceChildren: self replacement of only child
Pass childList Node.removeChild: removal mutation