From a4864650c7d4e4539dcc0de8d683f74cf624114c Mon Sep 17 00:00:00 2001 From: Mirko Brodesser Date: Tue, 30 Mar 2021 08:45:03 +0000 Subject: [PATCH] Bug 1700051: part 20) Merge `mozInlineSpellStatus`'s constructor and its `Init` method. r=smaug Allows to `const`-qualify some members. Differential Revision: https://phabricator.services.mozilla.com/D110083 --- .../spellcheck/src/mozInlineSpellChecker.cpp | 16 ++++++----- .../spellcheck/src/mozInlineSpellWordUtil.cpp | 27 ++++++++++--------- .../spellcheck/src/mozInlineSpellWordUtil.h | 22 ++++++++------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/extensions/spellcheck/src/mozInlineSpellChecker.cpp b/extensions/spellcheck/src/mozInlineSpellChecker.cpp index 0ff263fe8982..9109bd141ac7 100644 --- a/extensions/spellcheck/src/mozInlineSpellChecker.cpp +++ b/extensions/spellcheck/src/mozInlineSpellChecker.cpp @@ -1555,9 +1555,11 @@ nsresult mozInlineSpellChecker::ResumeCheck( return NS_OK; } - mozInlineSpellWordUtil wordUtil; - nsresult rv = wordUtil.Init(*mTextEditor); - if (NS_FAILED(rv)) return NS_OK; // editor doesn't like us, don't assert + Maybe wordUtil{ + mozInlineSpellWordUtil::Create(*mTextEditor)}; + if (!wordUtil) { + return NS_OK; // editor doesn't like us, don't assert + } RefPtr spellCheckSelection = GetSpellCheckSelection(); if (NS_WARN_IF(!spellCheckSelection)) { @@ -1565,7 +1567,7 @@ nsresult mozInlineSpellChecker::ResumeCheck( } nsAutoCString currentDictionary; - rv = mSpellCheck->GetCurrentDictionary(currentDictionary); + nsresult rv = mSpellCheck->GetCurrentDictionary(currentDictionary); if (NS_FAILED(rv)) { MOZ_LOG(sInlineSpellCheckerLog, LogLevel::Debug, ("%s: no active dictionary.", __FUNCTION__)); @@ -1583,15 +1585,15 @@ nsresult mozInlineSpellChecker::ResumeCheck( CleanupRangesInSelection(spellCheckSelection); - rv = aStatus->FinishInitOnEvent(wordUtil); + rv = aStatus->FinishInitOnEvent(*wordUtil); NS_ENSURE_SUCCESS(rv, rv); if (!aStatus->mRange) return NS_OK; // empty range, nothing to do bool doneChecking = true; if (aStatus->GetOperation() == mozInlineSpellStatus::eOpSelection) - rv = DoSpellCheckSelection(wordUtil, spellCheckSelection); + rv = DoSpellCheckSelection(*wordUtil, spellCheckSelection); else - rv = DoSpellCheck(wordUtil, spellCheckSelection, aStatus, &doneChecking); + rv = DoSpellCheck(*wordUtil, spellCheckSelection, aStatus, &doneChecking); NS_ENSURE_SUCCESS(rv, rv); if (!doneChecking) rv = ScheduleSpellCheck(std::move(aStatus)); diff --git a/extensions/spellcheck/src/mozInlineSpellWordUtil.cpp b/extensions/spellcheck/src/mozInlineSpellWordUtil.cpp index cd65b690c108..683f0292530c 100644 --- a/extensions/spellcheck/src/mozInlineSpellWordUtil.cpp +++ b/extensions/spellcheck/src/mozInlineSpellWordUtil.cpp @@ -5,6 +5,9 @@ #include "mozInlineSpellWordUtil.h" +#include +#include + #include "mozilla/BinarySearch.h" #include "mozilla/HTMLEditor.h" #include "mozilla/Logging.h" @@ -21,7 +24,6 @@ #include "nsRange.h" #include "nsContentUtils.h" #include "nsIFrame.h" -#include using namespace mozilla; @@ -97,23 +99,24 @@ static bool IsDOMWordSeparator(char16_t ch) { return false; } -// mozInlineSpellWordUtil::Init - -nsresult mozInlineSpellWordUtil::Init(const TextEditor& aTextEditor) { - mDocument = aTextEditor.GetDocument(); - if (NS_WARN_IF(!mDocument)) { - return NS_ERROR_FAILURE; +// static +Maybe mozInlineSpellWordUtil::Create( + const TextEditor& aTextEditor) { + mozInlineSpellWordUtil util; + util.mDocument = aTextEditor.GetDocument(); + if (NS_WARN_IF(!util.mDocument)) { + return Nothing(); } - mIsContentEditableOrDesignMode = !!aTextEditor.AsHTMLEditor(); + util.mIsContentEditableOrDesignMode = !!aTextEditor.AsHTMLEditor(); // Find the root node for the editor. For contenteditable the mRootNode could // change to shadow root if the begin and end are inside the shadowDOM. - mRootNode = aTextEditor.GetRoot(); - if (NS_WARN_IF(!mRootNode)) { - return NS_ERROR_FAILURE; + util.mRootNode = aTextEditor.GetRoot(); + if (NS_WARN_IF(!util.mRootNode)) { + return Nothing(); } - return NS_OK; + return Some(std::move(util)); } static inline bool IsSpellCheckingTextNode(nsINode* aNode) { diff --git a/extensions/spellcheck/src/mozInlineSpellWordUtil.h b/extensions/spellcheck/src/mozInlineSpellWordUtil.h index e9a7f57341db..79bfd4e3a442 100644 --- a/extensions/spellcheck/src/mozInlineSpellWordUtil.h +++ b/extensions/spellcheck/src/mozInlineSpellWordUtil.h @@ -7,6 +7,7 @@ #define mozInlineSpellWordUtil_h #include "mozilla/Attributes.h" +#include "mozilla/Maybe.h" #include "mozilla/dom/Document.h" #include "nsCOMPtr.h" #include "nsString.h" @@ -78,15 +79,8 @@ class NodeOffsetRange { class MOZ_STACK_CLASS mozInlineSpellWordUtil { public: - mozInlineSpellWordUtil() - : mIsContentEditableOrDesignMode(false), - mRootNode(nullptr), - mSoftBegin(nullptr, 0), - mSoftEnd(nullptr, 0), - mNextWordIndex(-1), - mSoftTextValid(false) {} - - nsresult Init(const mozilla::TextEditor& aTextEditor); + static mozilla::Maybe Create( + const mozilla::TextEditor& aTextEditor); // sets the current position, this should be inside the range. If we are in // the middle of a word, we'll move to its start. @@ -125,7 +119,15 @@ class MOZ_STACK_CLASS mozInlineSpellWordUtil { const nsINode* GetRootNode() const { return mRootNode; } private: - // cached stuff for the editor, set by Init + mozInlineSpellWordUtil() + : mIsContentEditableOrDesignMode(false), + mRootNode(nullptr), + mSoftBegin(nullptr, 0), + mSoftEnd(nullptr, 0), + mNextWordIndex(-1), + mSoftTextValid(false) {} + + // cached stuff for the editor RefPtr mDocument; bool mIsContentEditableOrDesignMode;