1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/txtsvc/src/nsTextServicesDocument.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,233 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsTextServicesDocument_h__ 1.10 +#define nsTextServicesDocument_h__ 1.11 + 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsCycleCollectionParticipant.h" 1.14 +#include "nsIEditActionListener.h" 1.15 +#include "nsISupportsImpl.h" 1.16 +#include "nsITextServicesDocument.h" 1.17 +#include "nsIWeakReferenceUtils.h" 1.18 +#include "nsTArray.h" 1.19 +#include "nscore.h" 1.20 + 1.21 +class OffsetEntry; 1.22 +class nsIAtom; 1.23 +class nsIContent; 1.24 +class nsIContentIterator; 1.25 +class nsIDOMCharacterData; 1.26 +class nsIDOMDocument; 1.27 +class nsIDOMNode; 1.28 +class nsIDOMRange; 1.29 +class nsIEditor; 1.30 +class nsISelection; 1.31 +class nsISelectionController; 1.32 +class nsITextServicesFilter; 1.33 +class nsString; 1.34 + 1.35 +/** implementation of a text services object. 1.36 + * 1.37 + */ 1.38 +class nsTextServicesDocument : public nsITextServicesDocument, 1.39 + public nsIEditActionListener 1.40 +{ 1.41 +private: 1.42 + static nsIAtom *sAAtom; 1.43 + static nsIAtom *sAddressAtom; 1.44 + static nsIAtom *sBigAtom; 1.45 + static nsIAtom *sBAtom; 1.46 + static nsIAtom *sCiteAtom; 1.47 + static nsIAtom *sCodeAtom; 1.48 + static nsIAtom *sDfnAtom; 1.49 + static nsIAtom *sEmAtom; 1.50 + static nsIAtom *sFontAtom; 1.51 + static nsIAtom *sIAtom; 1.52 + static nsIAtom *sKbdAtom; 1.53 + static nsIAtom *sKeygenAtom; 1.54 + static nsIAtom *sNobrAtom; 1.55 + static nsIAtom *sSAtom; 1.56 + static nsIAtom *sSampAtom; 1.57 + static nsIAtom *sSmallAtom; 1.58 + static nsIAtom *sSpacerAtom; 1.59 + static nsIAtom *sSpanAtom; 1.60 + static nsIAtom *sStrikeAtom; 1.61 + static nsIAtom *sStrongAtom; 1.62 + static nsIAtom *sSubAtom; 1.63 + static nsIAtom *sSupAtom; 1.64 + static nsIAtom *sTtAtom; 1.65 + static nsIAtom *sUAtom; 1.66 + static nsIAtom *sVarAtom; 1.67 + static nsIAtom *sWbrAtom; 1.68 + 1.69 + typedef enum { eIsDone=0, // No iterator (I), or iterator doesn't point to anything valid. 1.70 + eValid, // I points to first text node (TN) in current block (CB). 1.71 + ePrev, // No TN in CB, I points to first TN in prev block. 1.72 + eNext // No TN in CB, I points to first TN in next block. 1.73 + } TSDIteratorStatus; 1.74 + 1.75 + nsCOMPtr<nsIDOMDocument> mDOMDocument; 1.76 + nsCOMPtr<nsISelectionController>mSelCon; 1.77 + nsWeakPtr mEditor; // avoid a cycle with the spell checker and editor 1.78 + nsCOMPtr<nsIContentIterator> mIterator; 1.79 + TSDIteratorStatus mIteratorStatus; 1.80 + nsCOMPtr<nsIContent> mPrevTextBlock; 1.81 + nsCOMPtr<nsIContent> mNextTextBlock; 1.82 + nsTArray<OffsetEntry*> mOffsetTable; 1.83 + 1.84 + int32_t mSelStartIndex; 1.85 + int32_t mSelStartOffset; 1.86 + int32_t mSelEndIndex; 1.87 + int32_t mSelEndOffset; 1.88 + 1.89 + nsCOMPtr<nsIDOMRange> mExtent; 1.90 + 1.91 + nsCOMPtr<nsITextServicesFilter> mTxtSvcFilter; 1.92 + 1.93 +public: 1.94 + 1.95 + /** The default constructor. 1.96 + */ 1.97 + nsTextServicesDocument(); 1.98 + 1.99 + /** The default destructor. 1.100 + */ 1.101 + virtual ~nsTextServicesDocument(); 1.102 + 1.103 + /** To be called at module init 1.104 + */ 1.105 + static void RegisterAtoms(); 1.106 + 1.107 + /* Macro for AddRef(), Release(), and QueryInterface() */ 1.108 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.109 + NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextServicesDocument, nsITextServicesDocument) 1.110 + 1.111 + /* nsITextServicesDocument method implementations. */ 1.112 + NS_IMETHOD InitWithEditor(nsIEditor *aEditor); 1.113 + NS_IMETHOD GetDocument(nsIDOMDocument **aDoc); 1.114 + NS_IMETHOD SetExtent(nsIDOMRange* aDOMRange); 1.115 + NS_IMETHOD ExpandRangeToWordBoundaries(nsIDOMRange *aRange); 1.116 + NS_IMETHOD SetFilter(nsITextServicesFilter *aFilter); 1.117 + NS_IMETHOD GetCurrentTextBlock(nsString *aStr); 1.118 + NS_IMETHOD FirstBlock(); 1.119 + NS_IMETHOD LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); 1.120 + NS_IMETHOD PrevBlock(); 1.121 + NS_IMETHOD NextBlock(); 1.122 + NS_IMETHOD IsDone(bool *aIsDone); 1.123 + NS_IMETHOD SetSelection(int32_t aOffset, int32_t aLength); 1.124 + NS_IMETHOD ScrollSelectionIntoView(); 1.125 + NS_IMETHOD DeleteSelection(); 1.126 + NS_IMETHOD InsertText(const nsString *aText); 1.127 + 1.128 + /* nsIEditActionListener method implementations. */ 1.129 + NS_IMETHOD WillInsertNode(nsIDOMNode *aNode, 1.130 + nsIDOMNode *aParent, 1.131 + int32_t aPosition); 1.132 + NS_IMETHOD DidInsertNode(nsIDOMNode *aNode, 1.133 + nsIDOMNode *aParent, 1.134 + int32_t aPosition, 1.135 + nsresult aResult); 1.136 + 1.137 + NS_IMETHOD WillDeleteNode(nsIDOMNode *aChild); 1.138 + NS_IMETHOD DidDeleteNode(nsIDOMNode *aChild, nsresult aResult); 1.139 + 1.140 + NS_IMETHOD WillSplitNode(nsIDOMNode * aExistingRightNode, 1.141 + int32_t aOffset); 1.142 + NS_IMETHOD DidSplitNode(nsIDOMNode *aExistingRightNode, 1.143 + int32_t aOffset, 1.144 + nsIDOMNode *aNewLeftNode, 1.145 + nsresult aResult); 1.146 + 1.147 + NS_IMETHOD WillJoinNodes(nsIDOMNode *aLeftNode, 1.148 + nsIDOMNode *aRightNode, 1.149 + nsIDOMNode *aParent); 1.150 + NS_IMETHOD DidJoinNodes(nsIDOMNode *aLeftNode, 1.151 + nsIDOMNode *aRightNode, 1.152 + nsIDOMNode *aParent, 1.153 + nsresult aResult); 1.154 + // these listen methods are unused: 1.155 + NS_IMETHOD WillCreateNode(const nsAString& aTag, nsIDOMNode *aParent, int32_t aPosition); 1.156 + NS_IMETHOD DidCreateNode(const nsAString& aTag, nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t aPosition, nsresult aResult); 1.157 + NS_IMETHOD WillInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString); 1.158 + NS_IMETHOD DidInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult); 1.159 + NS_IMETHOD WillDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength); 1.160 + NS_IMETHOD DidDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult); 1.161 + NS_IMETHOD WillDeleteSelection(nsISelection *aSelection); 1.162 + NS_IMETHOD DidDeleteSelection(nsISelection *aSelection); 1.163 + 1.164 + /* Helper functions */ 1.165 + static nsresult GetRangeEndPoints(nsIDOMRange *aRange, nsIDOMNode **aParent1, int32_t *aOffset1, nsIDOMNode **aParent2, int32_t *aOffset2); 1.166 + static nsresult CreateRange(nsIDOMNode *aStartParent, int32_t aStartOffset, nsIDOMNode *aEndParent, int32_t aEndOffset, nsIDOMRange **aRange); 1.167 + 1.168 +private: 1.169 + /* nsTextServicesDocument private methods. */ 1.170 + 1.171 + nsresult CreateContentIterator(nsIDOMRange *aRange, nsIContentIterator **aIterator); 1.172 + 1.173 + nsresult GetDocumentContentRootNode(nsIDOMNode **aNode); 1.174 + nsresult CreateDocumentContentRange(nsIDOMRange **aRange); 1.175 + nsresult CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode *aParent, int32_t aOffset, bool aToStart, nsIDOMRange **aRange); 1.176 + nsresult CreateDocumentContentIterator(nsIContentIterator **aIterator); 1.177 + 1.178 + nsresult AdjustContentIterator(); 1.179 + 1.180 + static nsresult FirstTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus); 1.181 + static nsresult LastTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus); 1.182 + 1.183 + static nsresult FirstTextNodeInCurrentBlock(nsIContentIterator *aIterator); 1.184 + static nsresult FirstTextNodeInPrevBlock(nsIContentIterator *aIterator); 1.185 + static nsresult FirstTextNodeInNextBlock(nsIContentIterator *aIterator); 1.186 + 1.187 + nsresult GetFirstTextNodeInPrevBlock(nsIContent **aContent); 1.188 + nsresult GetFirstTextNodeInNextBlock(nsIContent **aContent); 1.189 + 1.190 + static bool IsBlockNode(nsIContent *aContent); 1.191 + static bool IsTextNode(nsIContent *aContent); 1.192 + static bool IsTextNode(nsIDOMNode *aNode); 1.193 + 1.194 + static bool DidSkip(nsIContentIterator* aFilteredIter); 1.195 + static void ClearDidSkip(nsIContentIterator* aFilteredIter); 1.196 + 1.197 + static bool HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2); 1.198 + 1.199 + nsresult SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate); 1.200 + nsresult GetSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); 1.201 + nsresult GetCollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); 1.202 + nsresult GetUncollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); 1.203 + 1.204 + bool SelectionIsCollapsed(); 1.205 + bool SelectionIsValid(); 1.206 + 1.207 + static nsresult CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable, 1.208 + nsIContentIterator *aIterator, 1.209 + TSDIteratorStatus *aIteratorStatus, 1.210 + nsIDOMRange *aIterRange, 1.211 + nsString *aStr); 1.212 + static nsresult ClearOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable); 1.213 + 1.214 + static nsresult NodeHasOffsetEntry(nsTArray<OffsetEntry*> *aOffsetTable, 1.215 + nsIDOMNode *aNode, 1.216 + bool *aHasEntry, 1.217 + int32_t *aEntryIndex); 1.218 + 1.219 + nsresult RemoveInvalidOffsetEntries(); 1.220 + nsresult SplitOffsetEntry(int32_t aTableIndex, int32_t aOffsetIntoEntry); 1.221 + 1.222 + static nsresult FindWordBounds(nsTArray<OffsetEntry*> *offsetTable, 1.223 + nsString *blockStr, 1.224 + nsIDOMNode *aNode, int32_t aNodeOffset, 1.225 + nsIDOMNode **aWordStartNode, 1.226 + int32_t *aWordStartOffset, 1.227 + nsIDOMNode **aWordEndNode, 1.228 + int32_t *aWordEndOffset); 1.229 + 1.230 +#ifdef DEBUG_kin 1.231 + void PrintOffsetTable(); 1.232 + void PrintContentNode(nsIContent *aContent); 1.233 +#endif 1.234 +}; 1.235 + 1.236 +#endif // nsTextServicesDocument_h__