Skip to content

Truly independent web browser

LibWeb: Keep anchor names in tree order across moveBefore()

Andreas Kling2f99567c1822
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 20fcc1b3951..5e05df9da4e 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3339,10 +3339,30 @@ void Element::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, No
});
}
+void Element::unregister_anchor_names_before_leaving_tree_scope(Badge<Node>, Node& tree_root)
+{
+ // Positioned boxes in the scope left behind may hold geometry resolved against these names, which the partial
+ // relayout planner's subtree check can no longer see.
+ if (unregister_current_anchor_names(*this, tree_root))
+ document().record_partial_relayout_escape(PartialRelayoutEscapeReason::AnchorNamesUnregisteredByElementRemoval);
+}
+
void Element::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<Node> old_ancestor)
{
Base::moved_from(is_subtree_root, old_ancestor);
+ // The anchor names an element registers are kept in tree order, which a move changes without changing its style.
+ // A move into another tree scope has already unregistered them from the old one.
+ if (is_connected()) {
+ if (auto const* anchor_values = style_group<CSS::ComputedValues::AnchorValues>(); anchor_values && !anchor_values->anchor_names_span().is_empty()) {
+ auto scope = anchor_name_scope_of(*this, root());
+ for (auto const& name : anchor_values->anchor_names_span()) {
+ scope.names.unregister_name(name, *this, scope.host);
+ scope.names.register_name(name, *this, scope.host);
+ }
+ }
+ }
+
if (is_subtree_root == IsSubtreeRoot::Yes)
HTML::FormAssociatedElement::reposition_associated_elements_after_move({}, *this);
}
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index 795d2ab62f3..6ee879b5dc2 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -937,6 +937,9 @@ public:
void prepare_for_style_computation(Badge<CSS::StyleComputer>) { prepare_for_style_computation(); }
+ // Unregisters the anchor names this element registered in the tree scope `tree_root` is the root of.
+ void unregister_anchor_names_before_leaving_tree_scope(Badge<Node>, Node& tree_root);
+
protected:
Element(Document&, DOM::QualifiedName);
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index a1fc48285ef..54b3102c764 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -2065,6 +2065,16 @@ WebIDL::ExceptionOr<void> Node::move_node(Node& new_parent, Node* child)
auto& old_parent_root = old_parent->root();
auto descendant_slots = descendant_slots_before_removal(old_parent_root);
+ // A move into another tree scope keeps the style of the elements it takes along, so nothing unregisters the anchor
+ // names they registered in the old scope. The elements of shadow trees inside the moved subtree stay in their own
+ // scope. The moving steps register the names in the new scope.
+ if (is_connected() && &old_parent_root != &new_parent.root()) {
+ for_each_in_inclusive_subtree_of_type<Element>([&](Element& element) {
+ element.unregister_anchor_names_before_leaving_tree_scope({}, old_parent_root);
+ return TraversalDecision::Continue;
+ });
+ }
+
// 13. Remove node from oldParent’s children.
old_parent->remove_child_impl(*this);
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/anchor-name-moved-across-tree-scopes.txt b/Tests/LibWeb/Text/expected/css/style-engine/anchor-name-moved-across-tree-scopes.txt
new file mode 100644
index 00000000000..353bde8c779
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/anchor-name-moved-across-tree-scopes.txt
@@ -0,0 +1,7 @@
+follower before the move: 200
+nested follower before the move: 300
+follower after moving the anchor into the shadow tree: 100
+nested follower after moving its host into the shadow tree: 300
+follower after removing the earlier anchor: 7
+follower after moving the anchor back: 200
+follower after removing the anchor: 7
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/install-gated-name-registries.txt b/Tests/LibWeb/Text/expected/css/style-engine/install-gated-name-registries.txt
index 4baacdaf5cb..68daf386bbe 100644
--- a/Tests/LibWeb/Text/expected/css/style-engine/install-gated-name-registries.txt
+++ b/Tests/LibWeb/Text/expected/css/style-engine/install-gated-name-registries.txt
@@ -4,7 +4,7 @@ first after an unrelated change: 100
first follows b: 200
second follows b: 200
second follows a: 100
-shared after moving d before c: 400
+shared after moving d before c: 300
shared after moving c before d: 400
animations: one
animations after a name change: two
diff --git a/Tests/LibWeb/Text/input/css/style-engine/anchor-name-moved-across-tree-scopes.html b/Tests/LibWeb/Text/input/css/style-engine/anchor-name-moved-across-tree-scopes.html
new file mode 100644
index 00000000000..f9259b4e1c7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/anchor-name-moved-across-tree-scopes.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ .anchor { position: absolute; top: 0; width: 10px; height: 10px; anchor-name: --moved; }
+ #follower { position: absolute; position-anchor: --moved; left: anchor(left, 7px); top: 50px; width: 5px; height: 5px; }
+</style>
+<div id="earlier" class="anchor" style="left: 100px"></div>
+<div id="container">
+ <div id="anchor" class="anchor" style="left: 200px"></div>
+ <div id="nested"></div>
+</div>
+<div id="host"></div>
+<div id="follower"></div>
+<script src="../../include.js"></script>
+<script>
+ test(() => {
+ const shadow = host.attachShadow({ mode: "open" });
+ shadow.innerHTML = `<style>.anchor { position: absolute; top: 0; width: 10px; height: 10px; anchor-name: --moved; }</style><div id="inner"></div>`;
+ const inner = shadow.getElementById("inner");
+ const left = () => follower.getBoundingClientRect().left;
+
+ // A shadow tree inside the moved subtree keeps its own tree scope, and its anchor names with it.
+ const nestedShadow = nested.attachShadow({ mode: "open" });
+ nestedShadow.innerHTML = `<style>
+ #nested-anchor { position: absolute; left: 300px; top: 0; width: 10px; height: 10px; anchor-name: --nested; }
+ #nested-follower { position: absolute; position-anchor: --nested; left: anchor(left, 7px); top: 60px; width: 5px; height: 5px; }
+ </style><div id="nested-anchor"></div><div id="nested-follower"></div>`;
+ const nestedLeft = () => nestedShadow.getElementById("nested-follower").getBoundingClientRect().left;
+
+ println(`follower before the move: ${left()}`);
+ println(`nested follower before the move: ${nestedLeft()}`);
+
+ // The moved anchor leaves the document's tree scope, so the follower in it falls back to the earlier anchor.
+ inner.moveBefore(anchor, null);
+ println(`follower after moving the anchor into the shadow tree: ${left()}`);
+
+ inner.moveBefore(nested, null);
+ println(`nested follower after moving its host into the shadow tree: ${nestedLeft()}`);
+
+ earlier.remove();
+ println(`follower after removing the earlier anchor: ${left()}`);
+
+ // Moving it back registers it in the document's tree scope again.
+ container.moveBefore(anchor, null);
+ println(`follower after moving the anchor back: ${left()}`);
+
+ anchor.remove();
+ println(`follower after removing the anchor: ${left()}`);
+ });
+</script>