From d7354191d2b53d818b991ffa91c6790457cba8a3 Mon Sep 17 00:00:00 2001 From: webbeef Date: Thu, 27 Nov 2025 23:40:49 -0800 Subject: [PATCH] dom: avoid short lived rooting in Node::is_ancestor_of (#40776) When using the iterator nodes are rooted but they are dropped immediately. Testing: Green wpt run: https://github.com/webbeef/servo/actions/runs/19560383039/job/56012660243 Signed-off-by: webbeef --- components/script/dom/node.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 0ac803eaef1..a2ef31db9cf 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -915,7 +915,19 @@ impl Node { } pub(crate) fn is_ancestor_of(&self, child: &Node) -> bool { - child.ancestors().any(|ancestor| &*ancestor == self) + let mut current = &MutNullableDom::new(Some(child)); + let mut done = false; + + while let Some(node) = current.if_is_some(|node| { + done = node == self; + &node.parent_node + }) { + if done { + break; + } + current = node + } + done } pub(crate) fn is_shadow_including_inclusive_ancestor_of(&self, node: &Node) -> bool {