/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * 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 nsIDOMRange.h and nsIFind.h. #include "nsFind.h" #include "nsIComponentManager.h" #include "nsIScriptSecurityManager.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" #include "nsPIDOMWindow.h" #include "nsIURI.h" #include "nsIDocShell.h" #include "nsIPresShell.h" #include "nsPresContext.h" #include "nsIDocument.h" #include "nsIDOMDocument.h" #include "nsISelectionController.h" #include "nsISelection.h" #include "nsIFrame.h" #include "nsITextControlFrame.h" #include "nsReadableUtils.h" #include "nsIDOMHTMLElement.h" #include "nsIDOMHTMLDocument.h" #include "nsIContent.h" #include "nsContentCID.h" #include "nsIServiceManager.h" #include "nsIObserverService.h" #include "nsISupportsPrimitives.h" #include "nsFind.h" #include "nsError.h" #include "nsFocusManager.h" #include "mozilla/Services.h" #include "mozilla/dom/Element.h" #include "nsISimpleEnumerator.h" #include "nsContentUtils.h" #if DEBUG #include "nsIWebNavigation.h" #include "nsXPIDLString.h" #endif //***************************************************************************** // nsWebBrowserFind //***************************************************************************** nsWebBrowserFind::nsWebBrowserFind() : mFindBackwards(false), mWrapFind(false), mEntireWord(false), mMatchCase(false), mSearchSubFrames(true), mSearchParentFrames(true) { } nsWebBrowserFind::~nsWebBrowserFind() { } NS_IMPL_ISUPPORTS(nsWebBrowserFind, nsIWebBrowserFind, nsIWebBrowserFindInFrames) /* boolean findNext (); */ NS_IMETHODIMP nsWebBrowserFind::FindNext(bool *outDidFind) { NS_ENSURE_ARG_POINTER(outDidFind); *outDidFind = 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); NS_NAMED_LITERAL_STRING(dnStr, "down"); NS_NAMED_LITERAL_STRING(upStr, "up"); observerSvc->NotifyObservers(windowSupportsData, "nsWebBrowserFind_FindAgain", mFindBackwards? upStr.get(): dnStr.get()); windowSupportsData->GetData(getter_AddRefs(searchWindowSupports)); // findnext performed if search window data cleared out *outDidFind = searchWindowSupports == nullptr; if (*outDidFind) return NS_OK; } // next, look in the current frame. If found, return. // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, outDidFind); if (NS_FAILED(rv)) return rv; if (*outDidFind) return OnFind(searchFrame); // we are done // if we are not searching other frames, return if (!mSearchSubFrames && !mSearchParentFrames) return NS_OK; nsIDocShell *rootDocShell = GetDocShellFromWindow(rootFrame); if (!rootDocShell) return NS_ERROR_FAILURE; int32_t enumDirection; if (mFindBackwards) enumDirection = nsIDocShell::ENUMERATE_BACKWARDS; else enumDirection = nsIDocShell::ENUMERATE_FORWARDS; nsCOMPtr docShellEnumerator; rv = rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeAll, enumDirection, getter_AddRefs(docShellEnumerator)); if (NS_FAILED(rv)) return rv; // remember where we started nsCOMPtr startingItem = do_QueryInterface(GetDocShellFromWindow(searchFrame), &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr curItem; // XXX We should avoid searching in frameset documents here. // We also need to honour mSearchSubFrames and mSearchParentFrames. bool hasMore, doFind = false; while (NS_SUCCEEDED(docShellEnumerator->HasMoreElements(&hasMore)) && hasMore) { nsCOMPtr curSupports; rv = docShellEnumerator->GetNext(getter_AddRefs(curSupports)); if (NS_FAILED(rv)) break; curItem = do_QueryInterface(curSupports, &rv); if (NS_FAILED(rv)) break; if (doFind) { searchFrame = curItem->GetWindow(); if (!searchFrame) { break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, outDidFind); if (NS_FAILED(rv)) return rv; if (*outDidFind) 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 totally lame and isn't resettable, I // have to make a new one docShellEnumerator = nullptr; rv = rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeAll, enumDirection, getter_AddRefs(docShellEnumerator)); if (NS_FAILED(rv)) return rv; while (NS_SUCCEEDED(docShellEnumerator->HasMoreElements(&hasMore)) && hasMore) { nsCOMPtr curSupports; rv = docShellEnumerator->GetNext(getter_AddRefs(curSupports)); if (NS_FAILED(rv)) break; curItem = do_QueryInterface(curSupports, &rv); if (NS_FAILED(rv)) break; 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, outDidFind); if (NS_FAILED(rv)) return rv; if (*outDidFind) return OnFind(searchFrame); // we are done break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, outDidFind); if (NS_FAILED(rv)) return rv; if (*outDidFind) return OnFind(searchFrame); // we are done OnEndSearchFrame(searchFrame); } // remember where we left off SetCurrentSearchFrame(searchFrame); NS_ASSERTION(NS_SUCCEEDED(rv), "Something failed"); return rv; } /* attribute wstring searchString; */ NS_IMETHODIMP nsWebBrowserFind::GetSearchString(char16_t * *aSearchString) { NS_ENSURE_ARG_POINTER(aSearchString); *aSearchString = ToNewUnicode(mSearchString); return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetSearchString(const char16_t * aSearchString) { mSearchString.Assign(aSearchString); return NS_OK; } /* attribute boolean findBackwards; */ 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; } /* attribute boolean wrapFind; */ 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; } /* attribute boolean entireWord; */ 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; } /* attribute boolean matchCase; */ 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; } static bool IsInNativeAnonymousSubtree(nsIContent* aContent) { while (aContent) { nsIContent* bindingParent = aContent->GetBindingParent(); if (bindingParent == aContent) { return true; } aContent = bindingParent; } return false; } void nsWebBrowserFind::SetSelectionAndScroll(nsIDOMWindow* aWindow, nsIDOMRange* aRange) { nsCOMPtr domDoc; aWindow->GetDocument(getter_AddRefs(domDoc)); if (!domDoc) return; nsCOMPtr doc(do_QueryInterface(domDoc)); nsIPresShell* presShell = doc->GetShell(); if (!presShell) return; nsCOMPtr node; aRange->GetStartContainer(getter_AddRefs(node)); 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 //