LibWeb: Implement 'State-preserving atomic move integration'

This was recently added to both the HTML and DOM specifications,
introducing the new moveBefore DOM API, as well as the new internal
'removing steps'.

See:

 * 432e8fb
 * eaf2ac7
This commit is contained in:
Shannon Booth 2025-03-08 12:45:26 +13:00 committed by Andrew Kaster
parent a47c4dbc63
commit 31a3bc3681
Notes: github-actions[bot] 2025-04-26 14:46:43 +00:00
39 changed files with 1383 additions and 12 deletions

View file

@ -3,6 +3,7 @@
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -1175,6 +1176,190 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_node(Document* document, bool sub
return GC::Ref { *copy };
}
// https://dom.spec.whatwg.org/#move
WebIDL::ExceptionOr<void> Node::move_node(Node& new_parent, Node* child)
{
// 1. If newParents shadow-including root is not the same as nodes 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);
// NOTE: This has the side effect of ensuring that a move is only performed if newParents connected is nodes 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);
// 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);
// 4. If node is not an Element or a CharacterData node, then throw a "HierarchyRequestError" DOMException.
if (!is<Element>(*this) && !is<CharacterData>(*this))
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<Text>(*this) && is<Document>(new_parent))
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<Document>(new_parent) && is<Element>(*this)) {
if (new_parent.has_child_of_type<Element>() || is<DocumentType>(child) || (child && child->has_following_node_of_type_in_tree_order<DocumentType>()))
return WebIDL::HierarchyRequestError::create(realm(), "Invalid node type for insertion"_string);
}
// 7. Let oldParent be nodes parent.
auto* old_parent = this->parent();
// 8. Assert: oldParent is non-null.
VERIFY(old_parent);
// 9. Run the live range pre-remove steps, given node.
live_range_pre_remove();
// 10. For each NodeIterator object iterator whose roots node document is nodes node document, run the NodeIterator pre-remove steps given node and iterator.
document().for_each_node_iterator([&](NodeIterator& node_iterator) {
node_iterator.run_pre_removing_steps(*this);
});
// 11. Let oldPreviousSibling be nodes previous sibling.
auto* old_previous_sibling = previous_sibling();
// 12. Let oldNextSibling be nodes next sibling.
auto* old_next_sibling = next_sibling();
if (old_parent->is_connected()) {
// Since the tree structure is about to change, we need to invalidate both style and layout.
// In the future, we should find a way to only invalidate the parts that actually need it.
old_parent->invalidate_style(StyleInvalidationReason::NodeRemove);
// NOTE: If we didn't have a layout node before, rebuilding the layout tree isn't gonna give us one
// after we've been removed from the DOM.
if (layout_node())
old_parent->set_needs_layout_tree_update(true, SetNeedsLayoutTreeUpdateReason::NodeRemove);
}
// 13. Remove node from oldParents children.
old_parent->remove_child_impl(*this);
// 14. If node is assigned, then run assign slottables for nodes assigned slot.
if (auto assigned_slot = assigned_slot_for_node(*this))
assign_slottables(*assigned_slot);
// 15. If oldParents root is a shadow root, and oldParent is a slot whose assigned nodes is empty, then run signal a slot change for oldParent.
auto& old_parent_root = old_parent->root();
if (old_parent_root.is_shadow_root() && is<HTML::HTMLSlotElement>(*old_parent)) {
auto& old_parent_slot = static_cast<HTML::HTMLSlotElement&>(*old_parent);
if (old_parent_slot.assigned_nodes_internal().is_empty())
signal_a_slot_change(old_parent_slot);
}
// 16. If node has an inclusive descendant that is a slot:
auto has_descendent_slot = false;
for_each_in_inclusive_subtree_of_type<HTML::HTMLSlotElement>([&](auto const&) {
has_descendent_slot = true;
return TraversalDecision::Break;
});
if (has_descendent_slot) {
// 1. Run assign slottables for a tree with oldParents root.
assign_slottables_for_a_tree(old_parent_root);
// 2. Run assign slottables for a tree with node.
assign_slottables_for_a_tree(*this);
}
// 17. If child is non-null:
if (child) {
// 1. For each live range whose start node is newParent and start offset is greater than childs index, increase its start offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == &new_parent && range->start_offset() > child->index())
range->increase_start_offset({}, 1);
}
// 2. For each live range whose end node is newParent and end offset is greater than childs index, increase its end offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == &new_parent && range->end_offset() > child->index())
range->increase_end_offset({}, 1);
}
}
// 18. Let newPreviousSibling be childs previous sibling if child is non-null, and newParents last child otherwise.
auto* new_previous_sibling = child ? child->previous_sibling() : new_parent.last_child();
// 19. If child is null, then append node to newParents children.
if (!child) {
new_parent.append_child_impl(*this);
}
// 20. Otherwise, insert node into newParents children before childs index.
else {
new_parent.insert_before_impl(*this, child);
}
new_parent.invalidate_style(StyleInvalidationReason::NodeInsertBefore);
if (is_connected()) {
new_parent.set_needs_layout_tree_update(true, SetNeedsLayoutTreeUpdateReason::NodeInsertBefore);
}
// 21. If newParent is a shadow host whose shadow roots slot assignment is "named" and node is a slottable, then assign a slot for node.
if (is<Element>(new_parent) && is<Element>(*this)) {
auto& this_element = static_cast<Element&>(*this);
auto& new_parent_element = static_cast<Element&>(new_parent);
auto is_named_shadow_host = new_parent_element.is_shadow_host()
&& new_parent_element.shadow_root()->slot_assignment() == Bindings::SlotAssignmentMode::Named;
if (is_named_shadow_host && this_element.is_slottable())
assign_a_slot(this_element.as_slottable());
}
// 22. If newParents root is a shadow root, and newParent is a slot whose assigned nodes is empty, then run signal a slot change for newParent.
if (new_parent.root().is_shadow_root() && is<HTML::HTMLSlotElement>(new_parent)) {
auto& new_parent_slot = static_cast<HTML::HTMLSlotElement&>(new_parent);
if (new_parent_slot.assigned_nodes_internal().is_empty())
signal_a_slot_change(new_parent_slot);
}
// 23. Run assign slottables for a tree with nodes root.
assign_slottables_for_a_tree(root());
// 24. For each shadow-including inclusive descendant inclusiveDescendant of node, in shadow-including tree order:
for_each_shadow_including_inclusive_descendant([this, &new_parent, old_parent](Node& inclusive_descendant) {
// 1. If inclusiveDescendant is node, then run the moving steps with inclusiveDescendant and oldParent. Otherwise, run the moving
// steps with inclusiveDescendant and null.
if (&inclusive_descendant == this)
inclusive_descendant.moved_from(*old_parent);
else
inclusive_descendant.moved_from(nullptr);
// NOTE: Because the move algorithm is a separate primitive from insert and remove, it does not invoke the traditional insertion steps or
// removing steps for inclusiveDescendant.
// 2. If inclusiveDescendant is custom and newParent is connected, then enqueue a custom element callback reaction with inclusiveDescendant,
// callback name "connectedMoveCallback", and « ».
if (is<DOM::Element>(inclusive_descendant)) {
auto& element = static_cast<DOM::Element&>(inclusive_descendant);
if (element.is_custom() && new_parent.is_connected()) {
GC::RootVector<JS::Value> empty_arguments { vm().heap() };
element.enqueue_a_custom_element_callback_reaction(HTML::CustomElementReactionNames::connectedMoveCallback, move(empty_arguments));
}
}
return TraversalDecision::Continue;
});
// 25. Queue a tree mutation record for oldParent with « », « node », oldPreviousSibling, and oldNextSibling.
old_parent->queue_tree_mutation_record({}, { *this }, old_previous_sibling, old_next_sibling);
// 26. Queue a tree mutation record for newParent with « node », « », newPreviousSibling, and child.
new_parent.queue_tree_mutation_record({ *this }, {}, new_previous_sibling, child);
document().bump_dom_tree_version();
return {};
}
// https://dom.spec.whatwg.org/#clone-a-single-node
WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_single_node(Document& document) const
{
@ -1488,6 +1673,11 @@ void Node::removed_from(Node*, Node&)
play_or_cancel_animations_after_display_property_change();
}
// https://dom.spec.whatwg.org/#concept-node-move-ext
void Node::moved_from(GC::Ptr<Node>)
{
}
ParentNode* Node::parent_or_shadow_host()
{
if (is<ShadowRoot>(*this))