Commit 8ea3ac77 authored by Cathie Chen's avatar Cathie Chen
Browse files

Bug 1827244 - Popover: implement topmost popover ancestor, r=emilio

parent dc01df27
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -4286,6 +4286,45 @@ bool Element::IsPopoverOpen() const {
  return htmlElement && htmlElement->PopoverOpen();
}

Element* Element::GetTopmostPopoverAncestor() const {
  const Element* newPopover = this;

  nsTHashMap<nsPtrHashKey<const Element>, size_t> popoverPositions;
  size_t index = 0;
  for (Element* popover : OwnerDoc()->AutoPopoverList()) {
    popoverPositions.LookupOrInsert(popover, index++);
  }
  popoverPositions.LookupOrInsert(newPopover, index);

  Element* topmostPopoverAncestor = nullptr;

  auto checkAncestor = [&](const Element* candidate) {
    if (!candidate) {
      return;
    }
    Element* candidateAncestor = candidate->GetNearestInclusiveOpenPopover();
    if (!candidateAncestor) {
      return;
    }
    size_t candidatePosition;
    if (popoverPositions.Get(candidateAncestor, &candidatePosition)) {
      size_t topmostPosition;
      if (!topmostPopoverAncestor ||
          (popoverPositions.Get(topmostPopoverAncestor, &topmostPosition) &&
           topmostPosition < candidatePosition)) {
        topmostPopoverAncestor = candidateAncestor;
      }
    }
  };

  checkAncestor(newPopover->GetFlattenedTreeParentElement());

  // TODO: To handle the button invokers
  // https://github.com/whatwg/html/issues/9160

  return topmostPopoverAncestor;
}

ElementAnimationData& Element::CreateAnimationData() {
  MOZ_ASSERT(!GetAnimationData());
  SetMayHaveAnimations();
+5 −0
Original line number Diff line number Diff line
@@ -588,6 +588,11 @@ class Element : public FragmentOrElement {
  bool IsAutoPopover() const;
  bool IsPopoverOpen() const;

  /**
   * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor
   */
  mozilla::dom::Element* GetTopmostPopoverAncestor() const;

  ElementAnimationData* GetAnimationData() const {
    if (!MayHaveAnimations()) {
      return nullptr;