Line data Source code
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 final : 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 : RefPtr<nsRange> mExtent;
87 :
88 : nsCOMPtr<nsITextServicesFilter> mTxtSvcFilter;
89 :
90 : protected:
91 : /** The default destructor.
92 : */
93 : virtual ~nsTextServicesDocument();
94 :
95 : public:
96 :
97 : /** The default constructor.
98 : */
99 : nsTextServicesDocument();
100 :
101 : /** To be called at module init
102 : */
103 : static void RegisterAtoms();
104 :
105 : /* Macro for AddRef(), Release(), and QueryInterface() */
106 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
107 0 : NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextServicesDocument, nsITextServicesDocument)
108 :
109 : /* nsITextServicesDocument method implementations. */
110 : NS_IMETHOD InitWithEditor(nsIEditor *aEditor) override;
111 : NS_IMETHOD GetDocument(nsIDOMDocument **aDoc) override;
112 : NS_IMETHOD SetExtent(nsIDOMRange* aDOMRange) override;
113 : NS_IMETHOD ExpandRangeToWordBoundaries(nsIDOMRange *aRange) override;
114 : NS_IMETHOD SetFilter(nsITextServicesFilter *aFilter) override;
115 : NS_IMETHOD GetCurrentTextBlock(nsString *aStr) override;
116 : NS_IMETHOD FirstBlock() override;
117 : NS_IMETHOD LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength) override;
118 : NS_IMETHOD PrevBlock() override;
119 : NS_IMETHOD NextBlock() override;
120 : NS_IMETHOD IsDone(bool *aIsDone) override;
121 : NS_IMETHOD SetSelection(int32_t aOffset, int32_t aLength) override;
122 : NS_IMETHOD ScrollSelectionIntoView() override;
123 : NS_IMETHOD DeleteSelection() override;
124 : NS_IMETHOD InsertText(const nsString *aText) override;
125 :
126 : /* nsIEditActionListener method implementations. */
127 : NS_IMETHOD WillInsertNode(nsIDOMNode *aNode,
128 : nsIDOMNode *aParent,
129 : int32_t aPosition) override;
130 : NS_IMETHOD DidInsertNode(nsIDOMNode *aNode,
131 : nsIDOMNode *aParent,
132 : int32_t aPosition,
133 : nsresult aResult) override;
134 :
135 : NS_IMETHOD WillDeleteNode(nsIDOMNode *aChild) override;
136 : NS_IMETHOD DidDeleteNode(nsIDOMNode *aChild, nsresult aResult) override;
137 :
138 : NS_IMETHOD WillSplitNode(nsIDOMNode * aExistingRightNode,
139 : int32_t aOffset) override;
140 : NS_IMETHOD DidSplitNode(nsIDOMNode *aExistingRightNode,
141 : int32_t aOffset,
142 : nsIDOMNode *aNewLeftNode,
143 : nsresult aResult) override;
144 :
145 : NS_IMETHOD WillJoinNodes(nsIDOMNode *aLeftNode,
146 : nsIDOMNode *aRightNode,
147 : nsIDOMNode *aParent) override;
148 : NS_IMETHOD DidJoinNodes(nsIDOMNode *aLeftNode,
149 : nsIDOMNode *aRightNode,
150 : nsIDOMNode *aParent,
151 : nsresult aResult) override;
152 : // these listen methods are unused:
153 : NS_IMETHOD WillCreateNode(const nsAString& aTag, nsIDOMNode *aParent, int32_t aPosition) override;
154 : NS_IMETHOD DidCreateNode(const nsAString& aTag, nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t aPosition, nsresult aResult) override;
155 : NS_IMETHOD WillInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString) override;
156 : NS_IMETHOD DidInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult) override;
157 : NS_IMETHOD WillDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength) override;
158 : NS_IMETHOD DidDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult) override;
159 : NS_IMETHOD WillDeleteSelection(nsISelection *aSelection) override;
160 : NS_IMETHOD DidDeleteSelection(nsISelection *aSelection) override;
161 :
162 : /* Helper functions */
163 : static nsresult GetRangeEndPoints(nsRange* aRange,
164 : nsIDOMNode** aStartContainer,
165 : int32_t* aStartOffset,
166 : nsIDOMNode** aEndContainer,
167 : int32_t* aEndOffset);
168 : static nsresult CreateRange(nsIDOMNode* aStartContainer, int32_t aStartOffset,
169 : nsIDOMNode* aEndContainer, int32_t aEndOffset,
170 : nsRange** aRange);
171 :
172 : private:
173 : /* nsTextServicesDocument private methods. */
174 :
175 : nsresult CreateContentIterator(nsRange* aRange,
176 : nsIContentIterator** aIterator);
177 :
178 : nsresult GetDocumentContentRootNode(nsIDOMNode **aNode);
179 : nsresult CreateDocumentContentRange(nsRange** aRange);
180 : nsresult CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode* aParent,
181 : int32_t aOffset,
182 : bool aToStart,
183 : nsRange** aRange);
184 : nsresult CreateDocumentContentIterator(nsIContentIterator **aIterator);
185 :
186 : nsresult AdjustContentIterator();
187 :
188 : static nsresult FirstTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
189 : static nsresult LastTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
190 :
191 : static nsresult FirstTextNodeInCurrentBlock(nsIContentIterator *aIterator);
192 : static nsresult FirstTextNodeInPrevBlock(nsIContentIterator *aIterator);
193 : static nsresult FirstTextNodeInNextBlock(nsIContentIterator *aIterator);
194 :
195 : nsresult GetFirstTextNodeInPrevBlock(nsIContent **aContent);
196 : nsresult GetFirstTextNodeInNextBlock(nsIContent **aContent);
197 :
198 : static bool IsBlockNode(nsIContent *aContent);
199 : static bool IsTextNode(nsIContent *aContent);
200 : static bool IsTextNode(nsIDOMNode *aNode);
201 :
202 : static bool DidSkip(nsIContentIterator* aFilteredIter);
203 : static void ClearDidSkip(nsIContentIterator* aFilteredIter);
204 :
205 : static bool HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2);
206 :
207 : nsresult SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate);
208 : nsresult GetSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
209 : nsresult GetCollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
210 : nsresult GetUncollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
211 :
212 : bool SelectionIsCollapsed();
213 : bool SelectionIsValid();
214 :
215 : static nsresult CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable,
216 : nsIContentIterator *aIterator,
217 : TSDIteratorStatus *aIteratorStatus,
218 : nsRange* aIterRange, nsString* aStr);
219 : static nsresult ClearOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable);
220 :
221 : static nsresult NodeHasOffsetEntry(nsTArray<OffsetEntry*> *aOffsetTable,
222 : nsIDOMNode *aNode,
223 : bool *aHasEntry,
224 : int32_t *aEntryIndex);
225 :
226 : nsresult RemoveInvalidOffsetEntries();
227 : nsresult SplitOffsetEntry(int32_t aTableIndex, int32_t aOffsetIntoEntry);
228 :
229 : static nsresult FindWordBounds(nsTArray<OffsetEntry*> *offsetTable,
230 : nsString *blockStr,
231 : nsIDOMNode *aNode, int32_t aNodeOffset,
232 : nsIDOMNode **aWordStartNode,
233 : int32_t *aWordStartOffset,
234 : nsIDOMNode **aWordEndNode,
235 : int32_t *aWordEndOffset);
236 : };
237 :
238 : #endif // nsTextServicesDocument_h__
|