|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsTextServicesDocument_h__ |
|
7 #define nsTextServicesDocument_h__ |
|
8 |
|
9 #include "nsCOMPtr.h" |
|
10 #include "nsCycleCollectionParticipant.h" |
|
11 #include "nsIEditActionListener.h" |
|
12 #include "nsISupportsImpl.h" |
|
13 #include "nsITextServicesDocument.h" |
|
14 #include "nsIWeakReferenceUtils.h" |
|
15 #include "nsTArray.h" |
|
16 #include "nscore.h" |
|
17 |
|
18 class OffsetEntry; |
|
19 class nsIAtom; |
|
20 class nsIContent; |
|
21 class nsIContentIterator; |
|
22 class nsIDOMCharacterData; |
|
23 class nsIDOMDocument; |
|
24 class nsIDOMNode; |
|
25 class nsIDOMRange; |
|
26 class nsIEditor; |
|
27 class nsISelection; |
|
28 class nsISelectionController; |
|
29 class nsITextServicesFilter; |
|
30 class nsString; |
|
31 |
|
32 /** implementation of a text services object. |
|
33 * |
|
34 */ |
|
35 class nsTextServicesDocument : public nsITextServicesDocument, |
|
36 public nsIEditActionListener |
|
37 { |
|
38 private: |
|
39 static nsIAtom *sAAtom; |
|
40 static nsIAtom *sAddressAtom; |
|
41 static nsIAtom *sBigAtom; |
|
42 static nsIAtom *sBAtom; |
|
43 static nsIAtom *sCiteAtom; |
|
44 static nsIAtom *sCodeAtom; |
|
45 static nsIAtom *sDfnAtom; |
|
46 static nsIAtom *sEmAtom; |
|
47 static nsIAtom *sFontAtom; |
|
48 static nsIAtom *sIAtom; |
|
49 static nsIAtom *sKbdAtom; |
|
50 static nsIAtom *sKeygenAtom; |
|
51 static nsIAtom *sNobrAtom; |
|
52 static nsIAtom *sSAtom; |
|
53 static nsIAtom *sSampAtom; |
|
54 static nsIAtom *sSmallAtom; |
|
55 static nsIAtom *sSpacerAtom; |
|
56 static nsIAtom *sSpanAtom; |
|
57 static nsIAtom *sStrikeAtom; |
|
58 static nsIAtom *sStrongAtom; |
|
59 static nsIAtom *sSubAtom; |
|
60 static nsIAtom *sSupAtom; |
|
61 static nsIAtom *sTtAtom; |
|
62 static nsIAtom *sUAtom; |
|
63 static nsIAtom *sVarAtom; |
|
64 static nsIAtom *sWbrAtom; |
|
65 |
|
66 typedef enum { eIsDone=0, // No iterator (I), or iterator doesn't point to anything valid. |
|
67 eValid, // I points to first text node (TN) in current block (CB). |
|
68 ePrev, // No TN in CB, I points to first TN in prev block. |
|
69 eNext // No TN in CB, I points to first TN in next block. |
|
70 } TSDIteratorStatus; |
|
71 |
|
72 nsCOMPtr<nsIDOMDocument> mDOMDocument; |
|
73 nsCOMPtr<nsISelectionController>mSelCon; |
|
74 nsWeakPtr mEditor; // avoid a cycle with the spell checker and editor |
|
75 nsCOMPtr<nsIContentIterator> mIterator; |
|
76 TSDIteratorStatus mIteratorStatus; |
|
77 nsCOMPtr<nsIContent> mPrevTextBlock; |
|
78 nsCOMPtr<nsIContent> mNextTextBlock; |
|
79 nsTArray<OffsetEntry*> mOffsetTable; |
|
80 |
|
81 int32_t mSelStartIndex; |
|
82 int32_t mSelStartOffset; |
|
83 int32_t mSelEndIndex; |
|
84 int32_t mSelEndOffset; |
|
85 |
|
86 nsCOMPtr<nsIDOMRange> mExtent; |
|
87 |
|
88 nsCOMPtr<nsITextServicesFilter> mTxtSvcFilter; |
|
89 |
|
90 public: |
|
91 |
|
92 /** The default constructor. |
|
93 */ |
|
94 nsTextServicesDocument(); |
|
95 |
|
96 /** The default destructor. |
|
97 */ |
|
98 virtual ~nsTextServicesDocument(); |
|
99 |
|
100 /** To be called at module init |
|
101 */ |
|
102 static void RegisterAtoms(); |
|
103 |
|
104 /* Macro for AddRef(), Release(), and QueryInterface() */ |
|
105 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
106 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextServicesDocument, nsITextServicesDocument) |
|
107 |
|
108 /* nsITextServicesDocument method implementations. */ |
|
109 NS_IMETHOD InitWithEditor(nsIEditor *aEditor); |
|
110 NS_IMETHOD GetDocument(nsIDOMDocument **aDoc); |
|
111 NS_IMETHOD SetExtent(nsIDOMRange* aDOMRange); |
|
112 NS_IMETHOD ExpandRangeToWordBoundaries(nsIDOMRange *aRange); |
|
113 NS_IMETHOD SetFilter(nsITextServicesFilter *aFilter); |
|
114 NS_IMETHOD GetCurrentTextBlock(nsString *aStr); |
|
115 NS_IMETHOD FirstBlock(); |
|
116 NS_IMETHOD LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); |
|
117 NS_IMETHOD PrevBlock(); |
|
118 NS_IMETHOD NextBlock(); |
|
119 NS_IMETHOD IsDone(bool *aIsDone); |
|
120 NS_IMETHOD SetSelection(int32_t aOffset, int32_t aLength); |
|
121 NS_IMETHOD ScrollSelectionIntoView(); |
|
122 NS_IMETHOD DeleteSelection(); |
|
123 NS_IMETHOD InsertText(const nsString *aText); |
|
124 |
|
125 /* nsIEditActionListener method implementations. */ |
|
126 NS_IMETHOD WillInsertNode(nsIDOMNode *aNode, |
|
127 nsIDOMNode *aParent, |
|
128 int32_t aPosition); |
|
129 NS_IMETHOD DidInsertNode(nsIDOMNode *aNode, |
|
130 nsIDOMNode *aParent, |
|
131 int32_t aPosition, |
|
132 nsresult aResult); |
|
133 |
|
134 NS_IMETHOD WillDeleteNode(nsIDOMNode *aChild); |
|
135 NS_IMETHOD DidDeleteNode(nsIDOMNode *aChild, nsresult aResult); |
|
136 |
|
137 NS_IMETHOD WillSplitNode(nsIDOMNode * aExistingRightNode, |
|
138 int32_t aOffset); |
|
139 NS_IMETHOD DidSplitNode(nsIDOMNode *aExistingRightNode, |
|
140 int32_t aOffset, |
|
141 nsIDOMNode *aNewLeftNode, |
|
142 nsresult aResult); |
|
143 |
|
144 NS_IMETHOD WillJoinNodes(nsIDOMNode *aLeftNode, |
|
145 nsIDOMNode *aRightNode, |
|
146 nsIDOMNode *aParent); |
|
147 NS_IMETHOD DidJoinNodes(nsIDOMNode *aLeftNode, |
|
148 nsIDOMNode *aRightNode, |
|
149 nsIDOMNode *aParent, |
|
150 nsresult aResult); |
|
151 // these listen methods are unused: |
|
152 NS_IMETHOD WillCreateNode(const nsAString& aTag, nsIDOMNode *aParent, int32_t aPosition); |
|
153 NS_IMETHOD DidCreateNode(const nsAString& aTag, nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t aPosition, nsresult aResult); |
|
154 NS_IMETHOD WillInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString); |
|
155 NS_IMETHOD DidInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult); |
|
156 NS_IMETHOD WillDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength); |
|
157 NS_IMETHOD DidDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult); |
|
158 NS_IMETHOD WillDeleteSelection(nsISelection *aSelection); |
|
159 NS_IMETHOD DidDeleteSelection(nsISelection *aSelection); |
|
160 |
|
161 /* Helper functions */ |
|
162 static nsresult GetRangeEndPoints(nsIDOMRange *aRange, nsIDOMNode **aParent1, int32_t *aOffset1, nsIDOMNode **aParent2, int32_t *aOffset2); |
|
163 static nsresult CreateRange(nsIDOMNode *aStartParent, int32_t aStartOffset, nsIDOMNode *aEndParent, int32_t aEndOffset, nsIDOMRange **aRange); |
|
164 |
|
165 private: |
|
166 /* nsTextServicesDocument private methods. */ |
|
167 |
|
168 nsresult CreateContentIterator(nsIDOMRange *aRange, nsIContentIterator **aIterator); |
|
169 |
|
170 nsresult GetDocumentContentRootNode(nsIDOMNode **aNode); |
|
171 nsresult CreateDocumentContentRange(nsIDOMRange **aRange); |
|
172 nsresult CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode *aParent, int32_t aOffset, bool aToStart, nsIDOMRange **aRange); |
|
173 nsresult CreateDocumentContentIterator(nsIContentIterator **aIterator); |
|
174 |
|
175 nsresult AdjustContentIterator(); |
|
176 |
|
177 static nsresult FirstTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus); |
|
178 static nsresult LastTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus); |
|
179 |
|
180 static nsresult FirstTextNodeInCurrentBlock(nsIContentIterator *aIterator); |
|
181 static nsresult FirstTextNodeInPrevBlock(nsIContentIterator *aIterator); |
|
182 static nsresult FirstTextNodeInNextBlock(nsIContentIterator *aIterator); |
|
183 |
|
184 nsresult GetFirstTextNodeInPrevBlock(nsIContent **aContent); |
|
185 nsresult GetFirstTextNodeInNextBlock(nsIContent **aContent); |
|
186 |
|
187 static bool IsBlockNode(nsIContent *aContent); |
|
188 static bool IsTextNode(nsIContent *aContent); |
|
189 static bool IsTextNode(nsIDOMNode *aNode); |
|
190 |
|
191 static bool DidSkip(nsIContentIterator* aFilteredIter); |
|
192 static void ClearDidSkip(nsIContentIterator* aFilteredIter); |
|
193 |
|
194 static bool HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2); |
|
195 |
|
196 nsresult SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate); |
|
197 nsresult GetSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); |
|
198 nsresult GetCollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); |
|
199 nsresult GetUncollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength); |
|
200 |
|
201 bool SelectionIsCollapsed(); |
|
202 bool SelectionIsValid(); |
|
203 |
|
204 static nsresult CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable, |
|
205 nsIContentIterator *aIterator, |
|
206 TSDIteratorStatus *aIteratorStatus, |
|
207 nsIDOMRange *aIterRange, |
|
208 nsString *aStr); |
|
209 static nsresult ClearOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable); |
|
210 |
|
211 static nsresult NodeHasOffsetEntry(nsTArray<OffsetEntry*> *aOffsetTable, |
|
212 nsIDOMNode *aNode, |
|
213 bool *aHasEntry, |
|
214 int32_t *aEntryIndex); |
|
215 |
|
216 nsresult RemoveInvalidOffsetEntries(); |
|
217 nsresult SplitOffsetEntry(int32_t aTableIndex, int32_t aOffsetIntoEntry); |
|
218 |
|
219 static nsresult FindWordBounds(nsTArray<OffsetEntry*> *offsetTable, |
|
220 nsString *blockStr, |
|
221 nsIDOMNode *aNode, int32_t aNodeOffset, |
|
222 nsIDOMNode **aWordStartNode, |
|
223 int32_t *aWordStartOffset, |
|
224 nsIDOMNode **aWordEndNode, |
|
225 int32_t *aWordEndOffset); |
|
226 |
|
227 #ifdef DEBUG_kin |
|
228 void PrintOffsetTable(); |
|
229 void PrintContentNode(nsIContent *aContent); |
|
230 #endif |
|
231 }; |
|
232 |
|
233 #endif // nsTextServicesDocument_h__ |