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

Code before:
                )
            },
            Ok(ConversionResult::Failure(msg)) => Err(Error::Type(msg.to_string())),
            Err(_) => Err(Error::Type(
                "The provided value is not a sequence of 'CSSStylesheet'.".to_owned(),
            )),
        }
    }

    /// <https://fullscreen.spec.whatwg.org/#dom-document-fullscreenelement>
    pub(crate) fn get_fullscreen_element(
        node: &Node,
        fullscreen_element: Option<DomRoot<Element>>,
    ) -> Option<DomRoot<Element>> {
        // Step 1. If this is a shadow root and its host is not connected, then return null.
        if let Some(shadow_root) = node.downcast::<ShadowRoot>() {
            if !shadow_root.Host().is_connected() {
                return None;
            }
        }

        // Step 2. Let candidate be the result of retargeting fullscreen element against this.
        let retargeted = fullscreen_element?
            .upcast::<EventTarget>()
            .retarget(node.upcast());
        let candidate = retargeted.downcast::<Element>().unwrap();

        // Step 3. If candidate and this are in the same tree, then return candidate.
        if candidate
            .upcast::<Node>()
            .GetRootNode(&GetRootNodeOptions::empty()) ==
            DomRoot::from_ref(node)
        {
            return Some(DomRoot::from_ref(candidate));
        }

        // Step 4. Return null.
        None
    }
}


Review comment (suggestion):
This unwrap is scary. Please add a comment that it's okay because the [retarget](https://dom.spec.whatwg.org/#retarget) algorithm returns either `fullscreen_element` or a shadow host in its list of shadow-including ancestors, and all of those are elements.

Code after:
                )
            },
            Ok(ConversionResult::Failure(msg)) => Err(Error::Type(msg.to_string())),
            Err(_) => Err(Error::Type(
                "The provided value is not a sequence of 'CSSStylesheet'.".to_owned(),
            )),
        }
    }

    /// <https://fullscreen.spec.whatwg.org/#dom-document-fullscreenelement>
    pub(crate) fn get_fullscreen_element(
        node: &Node,
        fullscreen_element: Option<DomRoot<Element>>,
    ) -> Option<DomRoot<Element>> {
        // Step 1. If this is a shadow root and its host is not connected, then return null.
        if let Some(shadow_root) = node.downcast::<ShadowRoot>() {
            if !shadow_root.Host().is_connected() {
                return None;
            }
        }

        // Step 2. Let candidate be the result of retargeting fullscreen element against this.
        let retargeted = fullscreen_element?
            .upcast::<EventTarget>()
            .retarget(node.upcast());
        // It's safe to unwrap downcasting to `Element` because `retarget` either returns `fullscreen_element` or a host of `fullscreen_element` and hosts are always elements.
        let candidate = DomRoot::downcast::<Element>(retargeted).unwrap();

        // Step 3. If candidate and this are in the same tree, then return candidate.
        if *candidate
            .upcast::<Node>()
            .GetRootNode(&GetRootNodeOptions::empty()) ==
            *node
        {
            return Some(candidate);
        }

        // Step 4. Return null.
        None
    }
}