diff --git a/dom/base/nsFocusManager.cpp b/dom/base/nsFocusManager.cpp index 5689ecaf019f..5e2f57f91f9a 100644 --- a/dom/base/nsFocusManager.cpp +++ b/dom/base/nsFocusManager.cpp @@ -462,17 +462,22 @@ nsFocusManager::GetFocusedElement(Element** aFocusedElement) { return NS_OK; } -uint32_t nsFocusManager::GetLastFocusMethod(nsPIDOMWindowOuter* aWindow) const { - nsPIDOMWindowOuter* window = aWindow ? aWindow : mFocusedWindow.get(); - uint32_t method = window ? window->GetFocusMethod() : 0; - NS_ASSERTION((method & METHOD_MASK) == method, "invalid focus method"); - return method; -} - NS_IMETHODIMP nsFocusManager::GetLastFocusMethod(mozIDOMWindowProxy* aWindow, uint32_t* aLastFocusMethod) { - *aLastFocusMethod = GetLastFocusMethod(nsPIDOMWindowOuter::From(aWindow)); + // the focus method is stored on the inner window + nsCOMPtr window; + if (aWindow) { + window = nsPIDOMWindowOuter::From(aWindow); + } + if (!window) { + window = mFocusedWindow; + } + + *aLastFocusMethod = window ? window->GetFocusMethod() : 0; + + NS_ASSERTION((*aLastFocusMethod & METHOD_MASK) == *aLastFocusMethod, + "invalid focus method"); return NS_OK; } diff --git a/dom/base/nsFocusManager.h b/dom/base/nsFocusManager.h index da8f7f5b1ffe..40e8d7031d9d 100644 --- a/dom/base/nsFocusManager.h +++ b/dom/base/nsFocusManager.h @@ -163,9 +163,6 @@ class nsFocusManager final : public nsIFocusManager, */ void UpdateCaretForCaretBrowsingMode(); - /** @see nsIFocusManager.getLastFocusMethod() */ - uint32_t GetLastFocusMethod(nsPIDOMWindowOuter*) const; - /** * Returns the content node that would be focused if aWindow was in an * active window. This will traverse down the frame hierarchy, starting at diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index e638c5c11764..5b6f967e4002 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -3755,18 +3755,11 @@ nsresult HTMLInputElement::PostHandleEvent(EventChainPostVisitor& aVisitor) { !aVisitor.mEvent->AsFocusEvent()->mFromRaise && SelectTextFieldOnFocus()) { if (Document* document = GetComposedDoc()) { - uint32_t lastFocusMethod = - fm->GetLastFocusMethod(document->GetWindow()); - const bool shouldSelectAllOnFocus = [&] { - if (lastFocusMethod & nsIFocusManager::FLAG_BYMOVEFOCUS) { - return true; - } - if (lastFocusMethod & nsIFocusManager::FLAG_BYJS) { - return false; - } - return bool(lastFocusMethod & nsIFocusManager::FLAG_BYKEY); - }(); - if (shouldSelectAllOnFocus) { + uint32_t lastFocusMethod; + fm->GetLastFocusMethod(document->GetWindow(), &lastFocusMethod); + if (lastFocusMethod & (nsIFocusManager::FLAG_BYKEY | + nsIFocusManager::FLAG_BYMOVEFOCUS) && + !(lastFocusMethod & nsIFocusManager::FLAG_BYJS)) { RefPtr presContext = GetPresContext(eForComposedDoc); DispatchSelectEvent(presContext); diff --git a/dom/xul/nsXULCommandDispatcher.cpp b/dom/xul/nsXULCommandDispatcher.cpp index 9f5e612f2c73..f83d9b549eaa 100644 --- a/dom/xul/nsXULCommandDispatcher.cpp +++ b/dom/xul/nsXULCommandDispatcher.cpp @@ -27,7 +27,6 @@ #include "mozilla/BasicEvents.h" #include "mozilla/EventDispatcher.h" #include "mozilla/dom/Element.h" -#include "mozilla/dom/ElementBinding.h" using namespace mozilla; using mozilla::dom::Document; @@ -193,30 +192,30 @@ nsXULCommandDispatcher::AdvanceFocus() { return AdvanceFocusIntoSubtree(nullptr); } -NS_IMETHODIMP -nsXULCommandDispatcher::AdvanceFocusIntoSubtree(Element* aElt) { - return MoveFocusIntoSubtree(aElt, /* aForward = */ true); -} - NS_IMETHODIMP nsXULCommandDispatcher::RewindFocus() { - return MoveFocusIntoSubtree(nullptr, /* aForward = */ false); -} - -nsresult nsXULCommandDispatcher::MoveFocusIntoSubtree(Element* aElt, - bool aForward) { nsCOMPtr win; GetRootFocusedContentAndWindow(getter_AddRefs(win)); RefPtr result; nsFocusManager* fm = nsFocusManager::GetFocusManager(); - if (!fm) { - return NS_OK; - } - auto flags = nsFocusManager::ProgrammaticFocusFlags(dom::FocusOptions()); - auto type = aForward ? nsIFocusManager::MOVEFOCUS_FORWARD - : nsIFocusManager::MOVEFOCUS_BACKWARD; - return fm->MoveFocus(win, aElt, type, flags, getter_AddRefs(result)); + if (fm) + return fm->MoveFocus(win, nullptr, nsIFocusManager::MOVEFOCUS_BACKWARD, 0, + getter_AddRefs(result)); + return NS_OK; +} + +NS_IMETHODIMP +nsXULCommandDispatcher::AdvanceFocusIntoSubtree(Element* aElt) { + nsCOMPtr win; + GetRootFocusedContentAndWindow(getter_AddRefs(win)); + + RefPtr result; + nsFocusManager* fm = nsFocusManager::GetFocusManager(); + if (fm) + return fm->MoveFocus(win, aElt, nsIFocusManager::MOVEFOCUS_FORWARD, 0, + getter_AddRefs(result)); + return NS_OK; } NS_IMETHODIMP diff --git a/dom/xul/nsXULCommandDispatcher.h b/dom/xul/nsXULCommandDispatcher.h index c324a7c10684..ff364067d985 100644 --- a/dom/xul/nsXULCommandDispatcher.h +++ b/dom/xul/nsXULCommandDispatcher.h @@ -32,11 +32,8 @@ class Element; class nsXULCommandDispatcher : public nsIDOMXULCommandDispatcher, public nsSupportsWeakReference { - using Document = mozilla::dom::Document; - using Element = mozilla::dom::Element; - public: - explicit nsXULCommandDispatcher(Document* aDocument); + explicit nsXULCommandDispatcher(mozilla::dom::Document* aDocument); // nsISupports NS_DECL_CYCLE_COLLECTING_ISUPPORTS @@ -53,21 +50,21 @@ class nsXULCommandDispatcher : public nsIDOMXULCommandDispatcher, already_AddRefed GetWindowRoot(); - Element* GetRootFocusedContentAndWindow(nsPIDOMWindowOuter** aWindow); - nsresult MoveFocusIntoSubtree(Element*, bool aForward); + mozilla::dom::Element* GetRootFocusedContentAndWindow( + nsPIDOMWindowOuter** aWindow); - RefPtr mDocument; + RefPtr mDocument; class Updater { public: - Updater(Element* aElement, const nsAString& aEvents, + Updater(mozilla::dom::Element* aElement, const nsAString& aEvents, const nsAString& aTargets) : mElement(aElement), mEvents(aEvents), mTargets(aTargets), mNext(nullptr) {} - RefPtr mElement; + RefPtr mElement; nsString mEvents; nsString mTargets; Updater* mNext;