diff --git a/accessible/generic/DocAccessible.cpp b/accessible/generic/DocAccessible.cpp index 3a4a657fcab6..4367a1ec4d4f 100644 --- a/accessible/generic/DocAccessible.cpp +++ b/accessible/generic/DocAccessible.cpp @@ -1130,10 +1130,28 @@ Accessible* DocAccessible::GetAccessibleByUniqueIDInSubtree(void* aUniqueID) { Accessible* DocAccessible::GetAccessibleOrContainer(nsINode* aNode, int aARIAHiddenFlag) const { - if (!aNode || !aNode->GetComposedDoc()) return nullptr; + if (!aNode || !aNode->GetComposedDoc()) { + return nullptr; + } - for (nsINode* currNode = aNode; currNode; - currNode = currNode->GetFlattenedTreeParentNode()) { + nsINode* currNode = nullptr; + if (aNode->IsShadowRoot()) { + // This can happen, for example, when called within + // SelectionManager::ProcessSelectionChanged due to focusing a direct + // child of a shadow root. + // GetFlattenedTreeParent works on children of a shadow root, but not the + // shadow root itself. + const dom::ShadowRoot* shadowRoot = dom::ShadowRoot::FromNode(aNode); + currNode = shadowRoot->GetHost(); + if (!currNode) { + return nullptr; + } + } else { + currNode = aNode; + } + + MOZ_ASSERT(currNode); + for (; currNode; currNode = currNode->GetFlattenedTreeParentNode()) { // No container if is inside of aria-hidden subtree. if (aARIAHiddenFlag == eNoContainerIfARIAHidden && currNode->IsElement() && aria::HasDefinedARIAHidden(currNode->AsElement())) {