/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsWebBrowserFind.h" // Only need this for NS_FIND_CONTRACTID, // else we could use nsRange.h and nsIFind.h. #include "nsFind.h" #include "mozilla/dom/ScriptSettings.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" #include "nsPIDOMWindow.h" #include "nsIDocShell.h" #include "nsPresContext.h" #include "mozilla/dom/Document.h" #include "nsISelectionController.h" #include "nsIFrame.h" #include "nsITextControlFrame.h" #include "nsReadableUtils.h" #include "nsIContent.h" #include "nsContentCID.h" #include "nsIObserverService.h" #include "nsISupportsPrimitives.h" #include "nsFind.h" #include "nsError.h" #include "nsFocusManager.h" #include "nsRange.h" #include "mozilla/PresShell.h" #include "mozilla/Services.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/Selection.h" #include "nsISimpleEnumerator.h" #include "nsComponentManagerUtils.h" #include "nsContentUtils.h" #include "nsGenericHTMLElement.h" #if DEBUG # include "nsIWebNavigation.h" # include "nsString.h" #endif using namespace mozilla; using mozilla::dom::Document; using mozilla::dom::Element; using mozilla::dom::Selection; nsWebBrowserFind::nsWebBrowserFind() : mFindBackwards(false), mWrapFind(false), mEntireWord(false), mMatchCase(false), mMatchDiacritics(false), mSearchSubFrames(true), mSearchParentFrames(true) {} nsWebBrowserFind::~nsWebBrowserFind() = default; NS_IMPL_ISUPPORTS(nsWebBrowserFind, nsIWebBrowserFind, nsIWebBrowserFindInFrames) NS_IMETHODIMP nsWebBrowserFind::FindNext(bool* aResult) { NS_ENSURE_ARG_POINTER(aResult); *aResult = false; NS_ENSURE_TRUE(CanFindNext(), NS_ERROR_NOT_INITIALIZED); nsresult rv = NS_OK; nsCOMPtr searchFrame = do_QueryReferent(mCurrentSearchFrame); NS_ENSURE_TRUE(searchFrame, NS_ERROR_NOT_INITIALIZED); nsCOMPtr rootFrame = do_QueryReferent(mRootSearchFrame); NS_ENSURE_TRUE(rootFrame, NS_ERROR_NOT_INITIALIZED); // first, if there's a "cmd_findagain" observer around, check to see if it // wants to perform the find again command . If it performs the find again // it will return true, in which case we exit ::FindNext() early. // Otherwise, nsWebBrowserFind needs to perform the find again command itself // this is used by nsTypeAheadFind, which controls find again when it was // the last executed find in the current window. nsCOMPtr observerSvc = mozilla::services::GetObserverService(); if (observerSvc) { nsCOMPtr windowSupportsData = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr searchWindowSupports = do_QueryInterface(rootFrame); windowSupportsData->SetData(searchWindowSupports); observerSvc->NotifyObservers(windowSupportsData, "nsWebBrowserFind_FindAgain", mFindBackwards ? u"up" : u"down"); windowSupportsData->GetData(getter_AddRefs(searchWindowSupports)); // findnext performed if search window data cleared out *aResult = searchWindowSupports == nullptr; if (*aResult) { return NS_OK; } } // next, look in the current frame. If found, return. // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } // if we are not searching other frames, return if (!mSearchSubFrames && !mSearchParentFrames) { return NS_OK; } nsIDocShell* rootDocShell = rootFrame->GetDocShell(); if (!rootDocShell) { return NS_ERROR_FAILURE; } auto enumDirection = mFindBackwards ? nsIDocShell::ENUMERATE_BACKWARDS : nsIDocShell::ENUMERATE_FORWARDS; nsTArray> docShells; rv = rootDocShell->GetAllDocShellsInSubtree(nsIDocShellTreeItem::typeAll, enumDirection, docShells); if (NS_FAILED(rv)) { return rv; } // remember where we started nsCOMPtr startingItem = searchFrame->GetDocShell(); // XXX We should avoid searching in frameset documents here. // We also need to honour mSearchSubFrames and mSearchParentFrames. bool doFind = false; for (const auto& curItem : docShells) { if (doFind) { searchFrame = curItem->GetWindow(); if (!searchFrame) { break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } OnEndSearchFrame(searchFrame); } if (curItem.get() == startingItem.get()) { doFind = true; // start looking in frames after this one } } if (!mWrapFind) { // remember where we left off SetCurrentSearchFrame(searchFrame); return NS_OK; } // From here on, we're wrapping, first through the other frames, then finally // from the beginning of the starting frame back to the starting point. // because nsISimpleEnumerator is bad and isn't resettable, I have to // make a new one rv = rootDocShell->GetAllDocShellsInSubtree(nsIDocShellTreeItem::typeAll, enumDirection, docShells); if (NS_FAILED(rv)) { return rv; } for (const auto& curItem : docShells) { searchFrame = curItem->GetWindow(); if (!searchFrame) { rv = NS_ERROR_FAILURE; break; } if (curItem.get() == startingItem.get()) { // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, true, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } OnEndSearchFrame(searchFrame); } // remember where we left off SetCurrentSearchFrame(searchFrame); NS_ASSERTION(NS_SUCCEEDED(rv), "Something failed"); return rv; } NS_IMETHODIMP nsWebBrowserFind::GetSearchString(nsAString& aSearchString) { aSearchString = mSearchString; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetSearchString(const nsAString& aSearchString) { mSearchString = aSearchString; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetFindBackwards(bool* aFindBackwards) { NS_ENSURE_ARG_POINTER(aFindBackwards); *aFindBackwards = mFindBackwards; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetFindBackwards(bool aFindBackwards) { mFindBackwards = aFindBackwards; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetWrapFind(bool* aWrapFind) { NS_ENSURE_ARG_POINTER(aWrapFind); *aWrapFind = mWrapFind; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetWrapFind(bool aWrapFind) { mWrapFind = aWrapFind; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetEntireWord(bool* aEntireWord) { NS_ENSURE_ARG_POINTER(aEntireWord); *aEntireWord = mEntireWord; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetEntireWord(bool aEntireWord) { mEntireWord = aEntireWord; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetMatchCase(bool* aMatchCase) { NS_ENSURE_ARG_POINTER(aMatchCase); *aMatchCase = mMatchCase; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetMatchCase(bool aMatchCase) { mMatchCase = aMatchCase; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetMatchDiacritics(bool* aMatchDiacritics) { NS_ENSURE_ARG_POINTER(aMatchDiacritics); *aMatchDiacritics = mMatchDiacritics; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetMatchDiacritics(bool aMatchDiacritics) { mMatchDiacritics = aMatchDiacritics; return NS_OK; } void nsWebBrowserFind::SetSelectionAndScroll(nsPIDOMWindowOuter* aWindow, nsRange* aRange) { RefPtr doc = aWindow->GetDoc(); if (!doc) { return; } PresShell* presShell = doc->GetPresShell(); if (!presShell) { return; } nsCOMPtr node = aRange->GetStartContainer(); nsCOMPtr content(do_QueryInterface(node)); nsIFrame* frame = content->GetPrimaryFrame(); if (!frame) { return; } nsCOMPtr selCon; frame->GetSelectionController(presShell->GetPresContext(), getter_AddRefs(selCon)); // since the match could be an anonymous textnode inside a //