diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index e1dfc24a863a..354df81c0f1c 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -11362,26 +11362,14 @@ NS_IMETHODIMP nsDocShell::EnsureFind() NS_ENSURE_TRUE(scriptGO, NS_ERROR_UNEXPECTED); // default to our window - nsCOMPtr windowToSearch(do_QueryInterface(mScriptGlobal)); - - nsCOMPtr root; - GetRootTreeItem(getter_AddRefs(root)); - - // if the active window is the same window that this docshell is in, - // use the currently focused frame - nsCOMPtr rootWindow = do_GetInterface(root); - nsCOMPtr fm = do_GetService(FOCUSMANAGER_CONTRACTID); - if (fm) { - nsCOMPtr activeWindow; - fm->GetActiveWindow(getter_AddRefs(activeWindow)); - if (activeWindow == rootWindow) - fm->GetFocusedWindow(getter_AddRefs(windowToSearch)); - } + nsCOMPtr ourWindow = do_QueryInterface(scriptGO); + nsCOMPtr windowToSearch; + nsFocusManager::GetFocusedDescendant(ourWindow, true, getter_AddRefs(windowToSearch)); nsCOMPtr findInFrames = do_QueryInterface(mFind); if (!findInFrames) return NS_ERROR_NO_INTERFACE; - rv = findInFrames->SetRootSearchFrame(rootWindow); + rv = findInFrames->SetRootSearchFrame(ourWindow); if (NS_FAILED(rv)) return rv; rv = findInFrames->SetCurrentSearchFrame(windowToSearch); if (NS_FAILED(rv)) return rv; diff --git a/embedding/components/find/src/nsWebBrowserFind.cpp b/embedding/components/find/src/nsWebBrowserFind.cpp index fd5d0819a7fa..ded7636c2112 100644 --- a/embedding/components/find/src/nsWebBrowserFind.cpp +++ b/embedding/components/find/src/nsWebBrowserFind.cpp @@ -206,6 +206,9 @@ NS_IMETHODIMP nsWebBrowserFind::FindNext(bool *outDidFind) curItem = do_QueryInterface(curSupports, &rv); if (NS_FAILED(rv)) break; + searchFrame = do_GetInterface(curItem, &rv); + if (NS_FAILED(rv)) break; + if (curItem.get() == startingItem.get()) { // Beware! This may flush notifications via synchronous @@ -217,9 +220,6 @@ NS_IMETHODIMP nsWebBrowserFind::FindNext(bool *outDidFind) break; } - searchFrame = do_GetInterface(curItem, &rv); - if (NS_FAILED(rv)) break; - OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous @@ -801,18 +801,13 @@ nsWebBrowserFind::GetFrameSelection(nsIDOMWindow* aWindow, // that we must use when they have focus. nsPresContext *presContext = presShell->GetPresContext(); - nsIFrame *frame = nullptr; - nsCOMPtr fm = do_GetService(FOCUSMANAGER_CONTRACTID); - if (fm) { - nsCOMPtr focusedElement; - fm->GetFocusedElement(getter_AddRefs(focusedElement)); - nsCOMPtr focusedContent(do_QueryInterface(focusedElement)); - if (focusedContent) { - frame = focusedContent->GetPrimaryFrame(); - if (frame && frame->PresContext() != presContext) - frame = nullptr; - } - } + nsCOMPtr window(do_QueryInterface(aWindow)); + + nsCOMPtr focusedWindow; + nsCOMPtr focusedContent = + nsFocusManager::GetFocusedDescendant(window, false, getter_AddRefs(focusedWindow)); + + nsIFrame *frame = focusedContent ? focusedContent->GetPrimaryFrame() : nullptr; nsCOMPtr selCon; if (frame) {