Bumping manifests a=b2g-bump
[gecko.git] / editor / txtsvc / nsTextServicesDocument.h
blob383e2b5ca30e9668e4e456845b34a83af3c03d47
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/. */
6 #ifndef nsTextServicesDocument_h__
7 #define nsTextServicesDocument_h__
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"
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;
32 /** implementation of a text services object.
35 class nsTextServicesDocument : public nsITextServicesDocument,
36 public nsIEditActionListener
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;
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;
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;
81 int32_t mSelStartIndex;
82 int32_t mSelStartOffset;
83 int32_t mSelEndIndex;
84 int32_t mSelEndOffset;
86 nsCOMPtr<nsIDOMRange> mExtent;
88 nsCOMPtr<nsITextServicesFilter> mTxtSvcFilter;
90 protected:
91 /** The default destructor.
93 virtual ~nsTextServicesDocument();
95 public:
97 /** The default constructor.
99 nsTextServicesDocument();
101 /** To be called at module init
103 static void RegisterAtoms();
105 /* Macro for AddRef(), Release(), and QueryInterface() */
106 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
107 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextServicesDocument, nsITextServicesDocument)
109 /* nsITextServicesDocument method implementations. */
110 NS_IMETHOD InitWithEditor(nsIEditor *aEditor);
111 NS_IMETHOD GetDocument(nsIDOMDocument **aDoc);
112 NS_IMETHOD SetExtent(nsIDOMRange* aDOMRange);
113 NS_IMETHOD ExpandRangeToWordBoundaries(nsIDOMRange *aRange);
114 NS_IMETHOD SetFilter(nsITextServicesFilter *aFilter);
115 NS_IMETHOD GetCurrentTextBlock(nsString *aStr);
116 NS_IMETHOD FirstBlock();
117 NS_IMETHOD LastSelectedBlock(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
118 NS_IMETHOD PrevBlock();
119 NS_IMETHOD NextBlock();
120 NS_IMETHOD IsDone(bool *aIsDone);
121 NS_IMETHOD SetSelection(int32_t aOffset, int32_t aLength);
122 NS_IMETHOD ScrollSelectionIntoView();
123 NS_IMETHOD DeleteSelection();
124 NS_IMETHOD InsertText(const nsString *aText);
126 /* nsIEditActionListener method implementations. */
127 NS_IMETHOD WillInsertNode(nsIDOMNode *aNode,
128 nsIDOMNode *aParent,
129 int32_t aPosition);
130 NS_IMETHOD DidInsertNode(nsIDOMNode *aNode,
131 nsIDOMNode *aParent,
132 int32_t aPosition,
133 nsresult aResult);
135 NS_IMETHOD WillDeleteNode(nsIDOMNode *aChild);
136 NS_IMETHOD DidDeleteNode(nsIDOMNode *aChild, nsresult aResult);
138 NS_IMETHOD WillSplitNode(nsIDOMNode * aExistingRightNode,
139 int32_t aOffset);
140 NS_IMETHOD DidSplitNode(nsIDOMNode *aExistingRightNode,
141 int32_t aOffset,
142 nsIDOMNode *aNewLeftNode,
143 nsresult aResult);
145 NS_IMETHOD WillJoinNodes(nsIDOMNode *aLeftNode,
146 nsIDOMNode *aRightNode,
147 nsIDOMNode *aParent);
148 NS_IMETHOD DidJoinNodes(nsIDOMNode *aLeftNode,
149 nsIDOMNode *aRightNode,
150 nsIDOMNode *aParent,
151 nsresult aResult);
152 // these listen methods are unused:
153 NS_IMETHOD WillCreateNode(const nsAString& aTag, nsIDOMNode *aParent, int32_t aPosition);
154 NS_IMETHOD DidCreateNode(const nsAString& aTag, nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t aPosition, nsresult aResult);
155 NS_IMETHOD WillInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString);
156 NS_IMETHOD DidInsertText(nsIDOMCharacterData *aTextNode, int32_t aOffset, const nsAString &aString, nsresult aResult);
157 NS_IMETHOD WillDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength);
158 NS_IMETHOD DidDeleteText(nsIDOMCharacterData *aTextNode, int32_t aOffset, int32_t aLength, nsresult aResult);
159 NS_IMETHOD WillDeleteSelection(nsISelection *aSelection);
160 NS_IMETHOD DidDeleteSelection(nsISelection *aSelection);
162 /* Helper functions */
163 static nsresult GetRangeEndPoints(nsIDOMRange *aRange, nsIDOMNode **aParent1, int32_t *aOffset1, nsIDOMNode **aParent2, int32_t *aOffset2);
164 static nsresult CreateRange(nsIDOMNode *aStartParent, int32_t aStartOffset, nsIDOMNode *aEndParent, int32_t aEndOffset, nsIDOMRange **aRange);
166 private:
167 /* nsTextServicesDocument private methods. */
169 nsresult CreateContentIterator(nsIDOMRange *aRange, nsIContentIterator **aIterator);
171 nsresult GetDocumentContentRootNode(nsIDOMNode **aNode);
172 nsresult CreateDocumentContentRange(nsIDOMRange **aRange);
173 nsresult CreateDocumentContentRootToNodeOffsetRange(nsIDOMNode *aParent, int32_t aOffset, bool aToStart, nsIDOMRange **aRange);
174 nsresult CreateDocumentContentIterator(nsIContentIterator **aIterator);
176 nsresult AdjustContentIterator();
178 static nsresult FirstTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
179 static nsresult LastTextNode(nsIContentIterator *aIterator, TSDIteratorStatus *IteratorStatus);
181 static nsresult FirstTextNodeInCurrentBlock(nsIContentIterator *aIterator);
182 static nsresult FirstTextNodeInPrevBlock(nsIContentIterator *aIterator);
183 static nsresult FirstTextNodeInNextBlock(nsIContentIterator *aIterator);
185 nsresult GetFirstTextNodeInPrevBlock(nsIContent **aContent);
186 nsresult GetFirstTextNodeInNextBlock(nsIContent **aContent);
188 static bool IsBlockNode(nsIContent *aContent);
189 static bool IsTextNode(nsIContent *aContent);
190 static bool IsTextNode(nsIDOMNode *aNode);
192 static bool DidSkip(nsIContentIterator* aFilteredIter);
193 static void ClearDidSkip(nsIContentIterator* aFilteredIter);
195 static bool HasSameBlockNodeParent(nsIContent *aContent1, nsIContent *aContent2);
197 nsresult SetSelectionInternal(int32_t aOffset, int32_t aLength, bool aDoUpdate);
198 nsresult GetSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
199 nsresult GetCollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
200 nsresult GetUncollapsedSelection(TSDBlockSelectionStatus *aSelStatus, int32_t *aSelOffset, int32_t *aSelLength);
202 bool SelectionIsCollapsed();
203 bool SelectionIsValid();
205 static nsresult CreateOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable,
206 nsIContentIterator *aIterator,
207 TSDIteratorStatus *aIteratorStatus,
208 nsIDOMRange *aIterRange,
209 nsString *aStr);
210 static nsresult ClearOffsetTable(nsTArray<OffsetEntry*> *aOffsetTable);
212 static nsresult NodeHasOffsetEntry(nsTArray<OffsetEntry*> *aOffsetTable,
213 nsIDOMNode *aNode,
214 bool *aHasEntry,
215 int32_t *aEntryIndex);
217 nsresult RemoveInvalidOffsetEntries();
218 nsresult SplitOffsetEntry(int32_t aTableIndex, int32_t aOffsetIntoEntry);
220 static nsresult FindWordBounds(nsTArray<OffsetEntry*> *offsetTable,
221 nsString *blockStr,
222 nsIDOMNode *aNode, int32_t aNodeOffset,
223 nsIDOMNode **aWordStartNode,
224 int32_t *aWordStartOffset,
225 nsIDOMNode **aWordEndNode,
226 int32_t *aWordEndOffset);
229 #endif // nsTextServicesDocument_h__