From 57dd85e4ac065040cc8beba0e40a951ccc245cba Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Wed, 9 Jul 2025 14:36:08 +0100 Subject: [PATCH] LibWeb/DOM: Throw errors from correct realm in `Node::move_node()` --- Libraries/LibWeb/DOM/Node.cpp | 14 ++--- .../dom/nodes/moveBefore/Node-moveBefore.txt | 5 +- .../css-transition-cross-document.txt | 6 +++ .../dom/nodes/moveBefore/throws-exception.txt | 6 +++ .../css-transition-cross-document.html | 51 +++++++++++++++++++ .../nodes/moveBefore/throws-exception.html | 20 ++++++++ 6 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.txt create mode 100644 Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/throws-exception.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.html create mode 100644 Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/throws-exception.html diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index e951a1a285c..32fbb7ab8ee 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -1215,33 +1215,35 @@ WebIDL::ExceptionOr> Node::clone_node(Document* document, bool sub // https://dom.spec.whatwg.org/#move WebIDL::ExceptionOr Node::move_node(Node& new_parent, Node* child) { + auto& realm = new_parent.realm(); + // 1. If newParent’s shadow-including root is not the same as node’s shadow-including root, then throw a "HierarchyRequestError" DOMException. if (&new_parent.shadow_including_root() != &shadow_including_root()) - return WebIDL::HierarchyRequestError::create(realm(), "New parent is not in the same shadow tree"_string); + return WebIDL::HierarchyRequestError::create(realm, "New parent is not in the same shadow tree"_string); // NOTE: This has the side effect of ensuring that a move is only performed if newParent’s connected is node’s connected. // 2. If node is a host-including inclusive ancestor of newParent, then throw a "HierarchyRequestError" DOMException. if (is_host_including_inclusive_ancestor_of(new_parent)) - return WebIDL::HierarchyRequestError::create(realm(), "New parent is an ancestor of this node"_string); + return WebIDL::HierarchyRequestError::create(realm, "New parent is an ancestor of this node"_string); // 3. If child is non-null and its parent is not newParent, then throw a "NotFoundError" DOMException. if (child && child->parent() != &new_parent) - return WebIDL::NotFoundError::create(realm(), "Child does not belong to the new parent"_string); + return WebIDL::NotFoundError::create(realm, "Child does not belong to the new parent"_string); // 4. If node is not an Element or a CharacterData node, then throw a "HierarchyRequestError" DOMException. if (!is(*this) && !is(*this)) - return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion"_string); + return WebIDL::HierarchyRequestError::create(realm, "Invalid node type for insertion"_string); // 5. If node is a Text node and newParent is a document, then throw a "HierarchyRequestError" DOMException. if (is(*this) && is(new_parent)) - return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion"_string); + return WebIDL::HierarchyRequestError::create(realm, "Invalid node type for insertion"_string); // 6. If newParent is a document, node is an Element node, and either newParent has an element child, child is a doctype, // or child is non-null and a doctype is following child then throw a "HierarchyRequestError" DOMException. if (is(new_parent) && is(*this)) { if (new_parent.has_child_of_type() || is(child) || (child && child->has_following_node_of_type_in_tree_order())) - return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion"_string); + return WebIDL::HierarchyRequestError::create(realm, "Invalid node type for insertion"_string); } // 7. Let oldParent be node’s parent. diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/Node-moveBefore.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/Node-moveBefore.txt index a02099d3252..430bd6e856c 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/Node-moveBefore.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/Node-moveBefore.txt @@ -2,8 +2,7 @@ Harness status: OK Found 32 tests -31 Pass -1 Fail +32 Pass Pass If node is a host-including inclusive ancestor of parent, then throw a HierarchyRequestError DOMException. Pass If node is not a DocumentFragment, DocumentType, Element, Text, ProcessingInstruction, or Comment node, then throw a HierarchyRequestError DOMException. Pass If node is a Text node and parent is a document, then throw a HierarchyRequestError DOMException. @@ -18,7 +17,7 @@ Pass Calling moveBefore with second argument missing, or other than Node, null, Pass moveBefore() method does not exist on non-ParentNode Nodes Pass moveBefore() on disconnected parent throws a HierarchyRequestError Pass moveBefore() with disconnected target node throws a HierarchyRequestError -Fail moveBefore() on a cross-document target node throws a HierarchyRequestError +Pass moveBefore() on a cross-document target node throws a HierarchyRequestError Pass moveBefore() into a Document throws a HierarchyRequestError Pass moveBefore() CharacterData into a Document Pass moveBefore() with node being an inclusive ancestor of parent throws a HierarchyRequestError diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.txt new file mode 100644 index 00000000000..9de1a52ea18 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass Moving a transition across documents should reset its state \ No newline at end of file diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/throws-exception.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/throws-exception.txt new file mode 100644 index 00000000000..6d221a966ad --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/throws-exception.txt @@ -0,0 +1,6 @@ +Harness status: OK + +Found 1 tests + +1 Pass +Pass moveBefore() on a cross-document target node \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.html new file mode 100644 index 00000000000..f90bc5046c1 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/css-transition-cross-document.html @@ -0,0 +1,51 @@ + +Node.moveBefore should not preserve CSS transition state when crossing document boundaries + + + + + +
+
+ + + diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/throws-exception.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/throws-exception.html new file mode 100644 index 00000000000..f8befbfbe7f --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/throws-exception.html @@ -0,0 +1,20 @@ + +moveBefore exception conditions + + + + +
+ +