Repository: servo/servo
File: components/script/dom/documentorshadowroot.rs (Rust)

Code before:
        let elements_from_point_query = self
            .window
            .elements_from_point_query(LayoutPoint::new(x, y), ElementsFromPointFlags::empty());
        let Some(result) = elements_from_point_query.first() else {
            if shadow_root.is_some() {
                return None;
            } else {
                return self.window.Document().GetDocumentElement();
            }
        };

        // SAFETY: This is safe because `Self::query_elements_from_point` has ensured that
        // layout has run and any OpaqueNodes that no longer refer to real nodes are gone.
        let address = UntrustedNodeAddress(result.node.0 as *const c_void);
        let mut node = unsafe { node::from_untrusted_node_address(address) };
        while let Some(node_shadow_root) = node.containing_shadow_root() {
            // See https://github.com/w3c/csswg-drafts/issues/556.
            //
            // The behavior when the hit node is within a shadow root and when this is run via
            // ShadowRoot.elementFromPoint is under-specified. What we are trying to do here is to
            // return the first node in the light tree, unless this was called on the ShadowRoot
            // itself. In that case we return the parent element of the ShadowRoot.
            if Some(&*node_shadow_root) == shadow_root {
                break;
            }
            node = DomRoot::from_ref(node_shadow_root.Host().upcast());
        }
        if let Some(element) = node.downcast::<Element>() {
            return Some(DomRoot::from_ref(element));
        }
        node.GetParentElement()
    }

    #[expect(unsafe_code)]
    // https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint
    pub(crate) fn elements_from_point(
        &self,
        x: Finite<f64>,
        y: Finite<f64>,
        document_element: Option<DomRoot<Element>>,
        has_browsing_context: bool,
    ) -> Vec<DomRoot<Element>> {
        let x = *x as f32;
        let y = *y as f32;
        let viewport = self.window.viewport_details().size;

        if !has_browsing_context {
            return vec![];
        }

        // Step 2

Review comment (suggestion):
nit: You can use `DomRoot::upcast` to root and unroot less here.

Code after:
            .elements_from_point_query(LayoutPoint::new(x, y), ElementsFromPointFlags::empty());
        let Some(result) = results.first() else {
            return document_element;
        };

        // SAFETY: This is safe because `Self::query_elements_from_point` has ensured that
        // layout has run and any OpaqueNodes that no longer refer to real nodes are gone.
        let address = UntrustedNodeAddress(result.node.0 as *const c_void);
        let node = unsafe { node::from_untrusted_node_address(address) };
        DomRoot::downcast::<Element>(node.clone()).or_else(|| {
            let parent_node = node.GetParentNode()?;
            if let Some(shadow_root) = parent_node.downcast::<ShadowRoot>() {
                Some(shadow_root.Host())
            } else {
                node.GetParentElement()
            }
        })
    }

    #[expect(unsafe_code)]
    // https://drafts.csswg.org/cssom-view/#dom-document-elementsfrompoint
    pub(crate) fn elements_from_point(
        &self,
        x: Finite<f64>,
        y: Finite<f64>,
        document_element: Option<DomRoot<Element>>,
        has_browsing_context: bool,
    ) -> Vec<DomRoot<Element>> {
        let x = *x as f32;
        let y = *y as f32;
        let viewport = self.window.viewport_details().size;

        if !has_browsing_context {
            return vec![];
        }

        // Step 2
        if x < 0.0 || y < 0.0 || x > viewport.width || y > viewport.height {
            return vec![];
        }

        // Step 1 and Step 3
        let nodes = self
            .window
            .elements_from_point_query(LayoutPoint::new(x, y), ElementsFromPointFlags::FindAll);
        let mut elements: Vec<DomRoot<Element>> = nodes
            .iter()
            .flat_map(|result| {
                // SAFETY: This is safe because `Self::query_elements_from_point` has ensured that
                // layout has run and any OpaqueNodes that no longer refer to real nodes are gone.
                let address = UntrustedNodeAddress(result.node.0 as *const c_void);