diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index f510460adb36a..0423810c432d5 100644 --- a/dom/base/Element.cpp +++ b/dom/base/Element.cpp @@ -4276,7 +4276,7 @@ bool Element::IsPopoverOpen() const { return htmlElement && htmlElement->PopoverOpen(); } -Element* Element::GetTopmostPopoverAncestor() const { +Element* Element::GetTopmostPopoverAncestor(const Element* aInvoker) const { const Element* newPopover = this; nsTHashMap, size_t> popoverPositions; @@ -4308,10 +4308,7 @@ Element* Element::GetTopmostPopoverAncestor() const { }; checkAncestor(newPopover->GetFlattenedTreeParentElement()); - - // https://github.com/whatwg/html/issues/9160 - RefPtr invoker = newPopover->GetPopoverData()->GetInvoker(); - checkAncestor(invoker); + checkAncestor(aInvoker); return topmostPopoverAncestor; } diff --git a/dom/base/Element.h b/dom/base/Element.h index e6bc3145d5ffa..fe532bbb77802 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -597,7 +597,7 @@ class Element : public FragmentOrElement { /** * https://html.spec.whatwg.org/multipage/popover.html#topmost-popover-ancestor */ - mozilla::dom::Element* GetTopmostPopoverAncestor() const; + Element* GetTopmostPopoverAncestor(const Element* aInvoker) const; ElementAnimationData* GetAnimationData() const { if (!MayHaveAnimations()) { diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp index 4d616ba6bf79e..464929ec6ee66 100644 --- a/dom/html/nsGenericHTMLElement.cpp +++ b/dom/html/nsGenericHTMLElement.cpp @@ -3330,16 +3330,14 @@ void nsGenericHTMLElement::RunPopoverToggleEventTask( void nsGenericHTMLElement::ShowPopover(ErrorResult& aRv) { return ShowPopoverInternal(nullptr, aRv); } -void nsGenericHTMLElement::ShowPopoverInternal( - nsGenericHTMLFormControlElementWithState* aInvoker, ErrorResult& aRv) { - if (PopoverData* data = GetPopoverData()) { - data->SetInvoker(aInvoker); - } - +void nsGenericHTMLElement::ShowPopoverInternal(Element* aInvoker, + ErrorResult& aRv) { if (!CheckPopoverValidity(PopoverVisibilityState::Hidden, nullptr, aRv)) { return; } RefPtr document = OwnerDoc(); + + MOZ_ASSERT(!GetPopoverData() || !GetPopoverData()->GetInvoker()); MOZ_ASSERT(!OwnerDoc()->TopLayerContains(*this)); bool wasShowingOrHiding = GetPopoverData()->IsShowingOrHiding(); @@ -3363,7 +3361,7 @@ void nsGenericHTMLElement::ShowPopoverInternal( nsWeakPtr originallyFocusedElement; if (IsAutoPopover()) { auto originalState = GetPopoverAttributeState(); - RefPtr ancestor = GetTopmostPopoverAncestor(); + RefPtr ancestor = GetTopmostPopoverAncestor(aInvoker); if (!ancestor) { ancestor = document; } @@ -3396,7 +3394,12 @@ void nsGenericHTMLElement::ShowPopoverInternal( document->AddPopoverToTopLayer(*this); PopoverPseudoStateUpdate(true, true); - GetPopoverData()->SetPopoverVisibilityState(PopoverVisibilityState::Showing); + + { + auto* popoverData = GetPopoverData(); + popoverData->SetPopoverVisibilityState(PopoverVisibilityState::Showing); + popoverData->SetInvoker(aInvoker); + } // Run the popover focusing steps given element. FocusPopover(); diff --git a/dom/html/nsGenericHTMLElement.h b/dom/html/nsGenericHTMLElement.h index 1da582691a432..f6523c813608a 100644 --- a/dom/html/nsGenericHTMLElement.h +++ b/dom/html/nsGenericHTMLElement.h @@ -166,8 +166,8 @@ class nsGenericHTMLElement : public nsGenericHTMLElementBase { mozilla::dom::PopoverToggleEventTask* aTask, mozilla::dom::PopoverVisibilityState aOldState); MOZ_CAN_RUN_SCRIPT void ShowPopover(ErrorResult& aRv); - MOZ_CAN_RUN_SCRIPT void ShowPopoverInternal( - nsGenericHTMLFormControlElementWithState* aInvoker, ErrorResult& aRv); + MOZ_CAN_RUN_SCRIPT void ShowPopoverInternal(Element* aInvoker, + ErrorResult& aRv); MOZ_CAN_RUN_SCRIPT_BOUNDARY void HidePopoverWithoutRunningScript(); MOZ_CAN_RUN_SCRIPT void HidePopoverInternal(bool aFocusPreviousElement, bool aFireEvents,