Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef __mozinlinespellchecker_h__ |
michael@0 | 7 | #define __mozinlinespellchecker_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsAutoPtr.h" |
michael@0 | 10 | #include "nsRange.h" |
michael@0 | 11 | #include "nsIEditorSpellCheck.h" |
michael@0 | 12 | #include "nsIEditActionListener.h" |
michael@0 | 13 | #include "nsIInlineSpellChecker.h" |
michael@0 | 14 | #include "nsIDOMTreeWalker.h" |
michael@0 | 15 | #include "nsWeakReference.h" |
michael@0 | 16 | #include "nsEditor.h" |
michael@0 | 17 | #include "nsIDOMEventListener.h" |
michael@0 | 18 | #include "nsWeakReference.h" |
michael@0 | 19 | #include "mozISpellI18NUtil.h" |
michael@0 | 20 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 21 | |
michael@0 | 22 | // X.h defines KeyPress |
michael@0 | 23 | #ifdef KeyPress |
michael@0 | 24 | #undef KeyPress |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | class nsIDOMMouseEventListener; |
michael@0 | 28 | class mozInlineSpellWordUtil; |
michael@0 | 29 | class mozInlineSpellChecker; |
michael@0 | 30 | class mozInlineSpellResume; |
michael@0 | 31 | class InitEditorSpellCheckCallback; |
michael@0 | 32 | class UpdateCurrentDictionaryCallback; |
michael@0 | 33 | class mozInlineSpellResume; |
michael@0 | 34 | |
michael@0 | 35 | class mozInlineSpellStatus |
michael@0 | 36 | { |
michael@0 | 37 | public: |
michael@0 | 38 | mozInlineSpellStatus(mozInlineSpellChecker* aSpellChecker); |
michael@0 | 39 | |
michael@0 | 40 | nsresult InitForEditorChange(EditAction aAction, |
michael@0 | 41 | nsIDOMNode* aAnchorNode, int32_t aAnchorOffset, |
michael@0 | 42 | nsIDOMNode* aPreviousNode, int32_t aPreviousOffset, |
michael@0 | 43 | nsIDOMNode* aStartNode, int32_t aStartOffset, |
michael@0 | 44 | nsIDOMNode* aEndNode, int32_t aEndOffset); |
michael@0 | 45 | nsresult InitForNavigation(bool aForceCheck, int32_t aNewPositionOffset, |
michael@0 | 46 | nsIDOMNode* aOldAnchorNode, int32_t aOldAnchorOffset, |
michael@0 | 47 | nsIDOMNode* aNewAnchorNode, int32_t aNewAnchorOffset, |
michael@0 | 48 | bool* aContinue); |
michael@0 | 49 | nsresult InitForSelection(); |
michael@0 | 50 | nsresult InitForRange(nsRange* aRange); |
michael@0 | 51 | |
michael@0 | 52 | nsresult FinishInitOnEvent(mozInlineSpellWordUtil& aWordUtil); |
michael@0 | 53 | |
michael@0 | 54 | // Return true if we plan to spell-check everything |
michael@0 | 55 | bool IsFullSpellCheck() const { |
michael@0 | 56 | return mOp == eOpChange && !mRange; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | nsRefPtr<mozInlineSpellChecker> mSpellChecker; |
michael@0 | 60 | |
michael@0 | 61 | // The total number of words checked in this sequence, using this tally tells |
michael@0 | 62 | // us when to stop. This count is preserved as we continue checking in new |
michael@0 | 63 | // messages. |
michael@0 | 64 | int32_t mWordCount; |
michael@0 | 65 | |
michael@0 | 66 | // what happened? |
michael@0 | 67 | enum Operation { eOpChange, // for SpellCheckAfterChange except deleteSelection |
michael@0 | 68 | eOpChangeDelete, // for SpellCheckAfterChange deleteSelection |
michael@0 | 69 | eOpNavigation, // for HandleNavigationEvent |
michael@0 | 70 | eOpSelection, // re-check all misspelled words |
michael@0 | 71 | eOpResume }; // for resuming a previously started check |
michael@0 | 72 | Operation mOp; |
michael@0 | 73 | |
michael@0 | 74 | // Used for events where we have already computed the range to use. It can |
michael@0 | 75 | // also be nullptr in these cases where we need to check the entire range. |
michael@0 | 76 | nsRefPtr<nsRange> mRange; |
michael@0 | 77 | |
michael@0 | 78 | // If we happen to know something was inserted, this is that range. |
michael@0 | 79 | // Can be nullptr (this only allows an optimization, so not setting doesn't hurt) |
michael@0 | 80 | nsCOMPtr<nsIDOMRange> mCreatedRange; |
michael@0 | 81 | |
michael@0 | 82 | // Contains the range computed for the current word. Can be nullptr. |
michael@0 | 83 | nsRefPtr<nsRange> mNoCheckRange; |
michael@0 | 84 | |
michael@0 | 85 | // Indicates the position of the cursor for the event (so we can compute |
michael@0 | 86 | // mNoCheckRange). It can be nullptr if we don't care about the cursor position |
michael@0 | 87 | // (such as for the intial check of everything). |
michael@0 | 88 | // |
michael@0 | 89 | // For mOp == eOpNavigation, this is the NEW position of the cursor |
michael@0 | 90 | nsCOMPtr<nsIDOMRange> mAnchorRange; |
michael@0 | 91 | |
michael@0 | 92 | // ----- |
michael@0 | 93 | // The following members are only for navigation events and are only |
michael@0 | 94 | // stored for FinishNavigationEvent to initialize the other members. |
michael@0 | 95 | // ----- |
michael@0 | 96 | |
michael@0 | 97 | // this is the OLD position of the cursor |
michael@0 | 98 | nsCOMPtr<nsIDOMRange> mOldNavigationAnchorRange; |
michael@0 | 99 | |
michael@0 | 100 | // Set when we should force checking the current word. See |
michael@0 | 101 | // mozInlineSpellChecker::HandleNavigationEvent for a description of why we |
michael@0 | 102 | // have this. |
michael@0 | 103 | bool mForceNavigationWordCheck; |
michael@0 | 104 | |
michael@0 | 105 | // Contains the offset passed in to HandleNavigationEvent |
michael@0 | 106 | int32_t mNewNavigationPositionOffset; |
michael@0 | 107 | |
michael@0 | 108 | protected: |
michael@0 | 109 | nsresult FinishNavigationEvent(mozInlineSpellWordUtil& aWordUtil); |
michael@0 | 110 | |
michael@0 | 111 | nsresult FillNoCheckRangeFromAnchor(mozInlineSpellWordUtil& aWordUtil); |
michael@0 | 112 | |
michael@0 | 113 | nsresult GetDocument(nsIDOMDocument** aDocument); |
michael@0 | 114 | nsresult PositionToCollapsedRange(nsIDOMDocument* aDocument, |
michael@0 | 115 | nsIDOMNode* aNode, int32_t aOffset, |
michael@0 | 116 | nsIDOMRange** aRange); |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | class mozInlineSpellChecker : public nsIInlineSpellChecker, |
michael@0 | 120 | public nsIEditActionListener, |
michael@0 | 121 | public nsIDOMEventListener, |
michael@0 | 122 | public nsSupportsWeakReference |
michael@0 | 123 | { |
michael@0 | 124 | private: |
michael@0 | 125 | friend class mozInlineSpellStatus; |
michael@0 | 126 | friend class InitEditorSpellCheckCallback; |
michael@0 | 127 | friend class UpdateCurrentDictionaryCallback; |
michael@0 | 128 | friend class AutoChangeNumPendingSpellChecks; |
michael@0 | 129 | friend class mozInlineSpellResume; |
michael@0 | 130 | |
michael@0 | 131 | // Access with CanEnableInlineSpellChecking |
michael@0 | 132 | enum SpellCheckingState { SpellCheck_Uninitialized = -1, |
michael@0 | 133 | SpellCheck_NotAvailable = 0, |
michael@0 | 134 | SpellCheck_Available = 1}; |
michael@0 | 135 | static SpellCheckingState gCanEnableSpellChecking; |
michael@0 | 136 | |
michael@0 | 137 | nsWeakPtr mEditor; |
michael@0 | 138 | nsCOMPtr<nsIEditorSpellCheck> mSpellCheck; |
michael@0 | 139 | nsCOMPtr<nsIEditorSpellCheck> mPendingSpellCheck; |
michael@0 | 140 | nsCOMPtr<nsIDOMTreeWalker> mTreeWalker; |
michael@0 | 141 | nsCOMPtr<mozISpellI18NUtil> mConverter; |
michael@0 | 142 | |
michael@0 | 143 | int32_t mNumWordsInSpellSelection; |
michael@0 | 144 | int32_t mMaxNumWordsInSpellSelection; |
michael@0 | 145 | |
michael@0 | 146 | // How many misspellings we can add at once. This is often less than the max |
michael@0 | 147 | // total number of misspellings. When you have a large textarea prepopulated |
michael@0 | 148 | // with text with many misspellings, we can hit this limit. By making it |
michael@0 | 149 | // lower than the total number of misspelled words, new text typed by the |
michael@0 | 150 | // user can also have spellchecking in it. |
michael@0 | 151 | int32_t mMaxMisspellingsPerCheck; |
michael@0 | 152 | |
michael@0 | 153 | // we need to keep track of the current text position in the document |
michael@0 | 154 | // so we can spell check the old word when the user clicks around the document. |
michael@0 | 155 | nsCOMPtr<nsIDOMNode> mCurrentSelectionAnchorNode; |
michael@0 | 156 | int32_t mCurrentSelectionOffset; |
michael@0 | 157 | |
michael@0 | 158 | // Tracks the number of pending spell checks *and* async operations that may |
michael@0 | 159 | // lead to spell checks, like updating the current dictionary. This is |
michael@0 | 160 | // necessary so that observers can know when to wait for spell check to |
michael@0 | 161 | // complete. |
michael@0 | 162 | int32_t mNumPendingSpellChecks; |
michael@0 | 163 | |
michael@0 | 164 | // The number of calls to UpdateCurrentDictionary that haven't finished yet. |
michael@0 | 165 | int32_t mNumPendingUpdateCurrentDictionary; |
michael@0 | 166 | |
michael@0 | 167 | // This number is incremented each time the spell checker is disabled so that |
michael@0 | 168 | // pending scheduled spell checks and UpdateCurrentDictionary calls can be |
michael@0 | 169 | // ignored when they finish. |
michael@0 | 170 | uint32_t mDisabledAsyncToken; |
michael@0 | 171 | |
michael@0 | 172 | // When mPendingSpellCheck is non-null, this is the callback passed when |
michael@0 | 173 | // it was initialized. |
michael@0 | 174 | nsRefPtr<InitEditorSpellCheckCallback> mPendingInitEditorSpellCheckCallback; |
michael@0 | 175 | |
michael@0 | 176 | // Set when we have spellchecked after the last edit operation. See the |
michael@0 | 177 | // commment at the top of the .cpp file for more info. |
michael@0 | 178 | bool mNeedsCheckAfterNavigation; |
michael@0 | 179 | |
michael@0 | 180 | // Set when we have a pending mozInlineSpellResume which will check |
michael@0 | 181 | // the whole document. |
michael@0 | 182 | bool mFullSpellCheckScheduled; |
michael@0 | 183 | |
michael@0 | 184 | // Maintains state during the asynchronous UpdateCurrentDictionary call. |
michael@0 | 185 | nsString mPreviousDictionary; |
michael@0 | 186 | |
michael@0 | 187 | public: |
michael@0 | 188 | |
michael@0 | 189 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 190 | NS_DECL_NSIEDITACTIONLISTENER |
michael@0 | 191 | NS_DECL_NSIINLINESPELLCHECKER |
michael@0 | 192 | NS_DECL_NSIDOMEVENTLISTENER |
michael@0 | 193 | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(mozInlineSpellChecker, nsIDOMEventListener) |
michael@0 | 194 | |
michael@0 | 195 | // returns true if there are any spell checking dictionaries available |
michael@0 | 196 | static bool CanEnableInlineSpellChecking(); |
michael@0 | 197 | // update the cached value whenever the list of available dictionaries changes |
michael@0 | 198 | static void UpdateCanEnableInlineSpellChecking(); |
michael@0 | 199 | |
michael@0 | 200 | nsresult Blur(nsIDOMEvent* aEvent); |
michael@0 | 201 | nsresult MouseClick(nsIDOMEvent* aMouseEvent); |
michael@0 | 202 | nsresult KeyPress(nsIDOMEvent* aKeyEvent); |
michael@0 | 203 | |
michael@0 | 204 | mozInlineSpellChecker(); |
michael@0 | 205 | virtual ~mozInlineSpellChecker(); |
michael@0 | 206 | |
michael@0 | 207 | // spell checks all of the words between two nodes |
michael@0 | 208 | nsresult SpellCheckBetweenNodes(nsIDOMNode *aStartNode, |
michael@0 | 209 | int32_t aStartOffset, |
michael@0 | 210 | nsIDOMNode *aEndNode, |
michael@0 | 211 | int32_t aEndOffset); |
michael@0 | 212 | |
michael@0 | 213 | // examines the dom node in question and returns true if the inline spell |
michael@0 | 214 | // checker should skip the node (i.e. the text is inside of a block quote |
michael@0 | 215 | // or an e-mail signature...) |
michael@0 | 216 | nsresult SkipSpellCheckForNode(nsIEditor* aEditor, |
michael@0 | 217 | nsIDOMNode *aNode, bool * aCheckSpelling); |
michael@0 | 218 | |
michael@0 | 219 | nsresult SpellCheckAfterChange(nsIDOMNode* aCursorNode, int32_t aCursorOffset, |
michael@0 | 220 | nsIDOMNode* aPreviousNode, int32_t aPreviousOffset, |
michael@0 | 221 | nsISelection* aSpellCheckSelection); |
michael@0 | 222 | |
michael@0 | 223 | // spell check the text contained within aRange, potentially scheduling |
michael@0 | 224 | // another check in the future if the time threshold is reached |
michael@0 | 225 | nsresult ScheduleSpellCheck(const mozInlineSpellStatus& aStatus); |
michael@0 | 226 | |
michael@0 | 227 | nsresult DoSpellCheckSelection(mozInlineSpellWordUtil& aWordUtil, |
michael@0 | 228 | nsISelection* aSpellCheckSelection, |
michael@0 | 229 | mozInlineSpellStatus* aStatus); |
michael@0 | 230 | nsresult DoSpellCheck(mozInlineSpellWordUtil& aWordUtil, |
michael@0 | 231 | nsISelection *aSpellCheckSelection, |
michael@0 | 232 | mozInlineSpellStatus* aStatus, |
michael@0 | 233 | bool* aDoneChecking); |
michael@0 | 234 | |
michael@0 | 235 | // helper routine to determine if a point is inside of the passed in selection. |
michael@0 | 236 | nsresult IsPointInSelection(nsISelection *aSelection, |
michael@0 | 237 | nsIDOMNode *aNode, |
michael@0 | 238 | int32_t aOffset, |
michael@0 | 239 | nsIDOMRange **aRange); |
michael@0 | 240 | |
michael@0 | 241 | nsresult CleanupRangesInSelection(nsISelection *aSelection); |
michael@0 | 242 | |
michael@0 | 243 | nsresult RemoveRange(nsISelection *aSpellCheckSelection, nsIDOMRange * aRange); |
michael@0 | 244 | nsresult AddRange(nsISelection *aSpellCheckSelection, nsIDOMRange * aRange); |
michael@0 | 245 | bool SpellCheckSelectionIsFull() { return mNumWordsInSpellSelection >= mMaxNumWordsInSpellSelection; } |
michael@0 | 246 | |
michael@0 | 247 | nsresult MakeSpellCheckRange(nsIDOMNode* aStartNode, int32_t aStartOffset, |
michael@0 | 248 | nsIDOMNode* aEndNode, int32_t aEndOffset, |
michael@0 | 249 | nsRange** aRange); |
michael@0 | 250 | |
michael@0 | 251 | // DOM and editor event registration helper routines |
michael@0 | 252 | nsresult RegisterEventListeners(); |
michael@0 | 253 | nsresult UnregisterEventListeners(); |
michael@0 | 254 | nsresult HandleNavigationEvent(bool aForceWordSpellCheck, int32_t aNewPositionOffset = 0); |
michael@0 | 255 | |
michael@0 | 256 | nsresult GetSpellCheckSelection(nsISelection ** aSpellCheckSelection); |
michael@0 | 257 | nsresult SaveCurrentSelectionPosition(); |
michael@0 | 258 | |
michael@0 | 259 | nsresult ResumeCheck(mozInlineSpellStatus* aStatus); |
michael@0 | 260 | |
michael@0 | 261 | protected: |
michael@0 | 262 | |
michael@0 | 263 | // called when async nsIEditorSpellCheck methods complete |
michael@0 | 264 | nsresult EditorSpellCheckInited(); |
michael@0 | 265 | nsresult CurrentDictionaryUpdated(); |
michael@0 | 266 | |
michael@0 | 267 | // track the number of pending spell checks and async operations that may lead |
michael@0 | 268 | // to spell checks, notifying observers accordingly |
michael@0 | 269 | void ChangeNumPendingSpellChecks(int32_t aDelta, |
michael@0 | 270 | nsIEditor* aEditor = nullptr); |
michael@0 | 271 | void NotifyObservers(const char* aTopic, nsIEditor* aEditor); |
michael@0 | 272 | }; |
michael@0 | 273 | |
michael@0 | 274 | #endif /* __mozinlinespellchecker_h__ */ |