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

Code before:
                .owner_doc()
                .GetDocumentElement()
                .unwrap()
        } else {
            self.upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::No)
                .filter_map(DomRoot::downcast)
                .last()
                .expect("We know inclusive_ancestors will return `self` which is an element")
        }
    }

    pub(crate) fn is_css_root_element(&self) -> bool {
        // check if we're top of the tree that we're in
        if *self.root_element() != *self {
            return false;
        }

        // we need to be in a document tree but not in a shadow tree
        if self.upcast::<Node>().is_in_a_shadow_tree() {
            return false;
        }

        self.node.is_in_a_document_tree()
    }

    // https://dom.spec.whatwg.org/#locate-a-namespace-prefix
    pub(crate) fn lookup_prefix(&self, namespace: Namespace) -> Option<DOMString> {
        for node in self
            .upcast::<Node>()
            .inclusive_ancestors(ShadowIncluding::No)
        {
            let element = node.downcast::<Element>()?;
            // Step 1.
            if *element.namespace() == namespace {
                if let Some(prefix) = element.GetPrefix() {
                    return Some(prefix);
                }
            }

            // Step 2.
            for attr in element.attrs.borrow().iter() {
                if attr.prefix() == Some(&namespace_prefix!("xmlns")) &&
                    **attr.value() == *namespace
                {
                    return Some(attr.LocalName());
                }
            }
        }
        None
    }

Review comment (bug):
Maybe it would make more sense to have a  `shadow_including_root_element` and `shadow_excluding_root_element`. "CSS root element" is not a specification concept AFAIK (correct me if I'm wrong), so we should avoid adding it.

Code after:
                .owner_doc()
                .GetDocumentElement()
                .unwrap()
        } else {
            self.upcast::<Node>()
                .inclusive_ancestors(ShadowIncluding::No)
                .filter_map(DomRoot::downcast)
                .last()
                .expect("We know inclusive_ancestors will return `self` which is an element")
        }
    }

    // https://dom.spec.whatwg.org/#locate-a-namespace-prefix
    pub(crate) fn lookup_prefix(&self, namespace: Namespace) -> Option<DOMString> {
        for node in self
            .upcast::<Node>()
            .inclusive_ancestors(ShadowIncluding::No)
        {
            let element = node.downcast::<Element>()?;
            // Step 1.
            if *element.namespace() == namespace {
                if let Some(prefix) = element.GetPrefix() {
                    return Some(prefix);
                }
            }

            // Step 2.
            for attr in element.attrs.borrow().iter() {
                if attr.prefix() == Some(&namespace_prefix!("xmlns")) &&
                    **attr.value() == *namespace
                {
                    return Some(attr.LocalName());
                }
            }
        }
        None
    }

    /// <https://dom.spec.whatwg.org/#document-element>
    pub(crate) fn is_document_element(&self) -> bool {
        if let Some(document_element) = self.owner_document().GetDocumentElement() {
            *document_element == *self
        } else {
            false
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#dom-document-activeelement>
    pub(crate) fn is_active_element(&self) -> bool {
        if let Some(active_element) = self.owner_document().GetActiveElement() {
            *active_element == *self