From 06fab178bf53ccd1f0f571906615a69ead21a274 Mon Sep 17 00:00:00 2001 From: Marco Castelluccio Date: Sun, 16 Jul 2023 01:36:31 +0000 Subject: [PATCH] Bug 1841505: postpone setting the popover invoker in the "show popover" algo. r=emilio See . Differential Revision: https://phabricator.services.mozilla.com/D182709 UltraBlame original commit: 0b5333b2ef93c5e4153ac198b577c6f54c5ad538 --- dom/base/Element.cpp | 7 ++----- dom/base/Element.h | 2 +- dom/html/nsGenericHTMLElement.cpp | 19 +++++++++++-------- dom/html/nsGenericHTMLElement.h | 4 ++-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/dom/base/Element.cpp b/dom/base/Element.cpp index 07b2cd1fb743..23bbb050318f 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()); - - - RefPtr invoker = newPopover->GetPopoverData()->GetInvoker(); - checkAncestor(invoker); + checkAncestor(aInvoker); return topmostPopoverAncestor; } diff --git a/dom/base/Element.h b/dom/base/Element.h index 656059fb3e62..9a2f592a1440 100644 --- a/dom/base/Element.h +++ b/dom/base/Element.h @@ -597,7 +597,7 @@ class Element : public FragmentOrElement { - 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 1ef0145684ac..8bc63e0b79c3 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); + } FocusPopover(); diff --git a/dom/html/nsGenericHTMLElement.h b/dom/html/nsGenericHTMLElement.h index 02ccfce11dde..f39067d836e0 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,