1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_TextServicesDocument_h
#define mozilla_TextServicesDocument_h
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIEditActionListener.h"
#include "nsISupportsImpl.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nscore.h"
class nsIContent;
class nsIEditor;
class nsINode;
class nsISelectionController;
class nsRange;
namespace mozilla {
class EditorBase;
class FilteredContentIterator;
class OffsetEntry;
namespace dom {
class AbstractRange;
class Document;
class Element;
class StaticRange;
}; // namespace dom
/**
* The TextServicesDocument presents the document in as a bunch of flattened
* text blocks. Each text block can be retrieved as an nsString.
*/
class TextServicesDocument final : public nsIEditActionListener {
private:
enum class IteratorStatus : uint8_t {
// No iterator (I), or iterator doesn't point to anything valid.
eDone = 0,
// I points to first text node (TN) in current block (CB).
eValid,
// No TN in CB, I points to first TN in prev block.
ePrev,
// No TN in CB, I points to first TN in next block.
eNext,
};
class OffsetEntryArray final : public nsTArray<UniquePtr<OffsetEntry>> {
public:
/**
* Init() initializes this array with aFilteredIter.
*
* @param[in] aIterRange Can be nullptr.
* @param[out] aAllTextInBlock
* Returns all text in the block.
*/
Result<IteratorStatus, nsresult> Init(
FilteredContentIterator& aFilteredIter, IteratorStatus aIteratorStatus,
nsRange* aIterRange, nsAString* aAllTextInBlock = nullptr);
/**
* Returns index of first `OffsetEntry` which manages aTextNode.
*/
Maybe<size_t> FirstIndexOf(const dom::Text& aTextNode) const;
/**
* FindWordRange() returns a word range starting from aStartPointToScan
* in aAllTextInBlock.
*/
Result<EditorDOMRangeInTexts, nsresult> FindWordRange(
nsAString& aAllTextInBlock, const EditorRawDOMPoint& aStartPointToScan);
/**
* SplitElementAt() splits an `OffsetEntry` at aIndex if aOffsetInTextNode
* is middle of the range in the text node.
*
* @param aIndex Index of the entry which you want to split.
* @param aOffsetInTextNode
* Offset in the text node. I.e., the offset should be
* greater than 0 and less than `mLength`.
*/
nsresult SplitElementAt(size_t aIndex, uint32_t aOffsetInTextNode);
/**
* Remove all `OffsetEntry` elements whose `mIsValid` is set to false.
*/
void RemoveInvalidElements();
/**
* Called when non-collapsed selection will be deleted.
*/
nsresult WillDeleteSelection();
/**
* Called when non-collapsed selection is deleteded.
*/
OffsetEntry* DidDeleteSelection();
/**
* Called when aInsertedText is inserted.
*/
MOZ_CAN_RUN_SCRIPT nsresult DidInsertText(dom::Selection* aSelection,
const nsAString& aInsertedString);
/**
* Called when selection range will be applied to the DOM Selection.
*/
Result<EditorRawDOMRangeInTexts, nsresult> WillSetSelection(
uint32_t aOffsetInTextInBlock, uint32_t aLength);
class Selection final {
public:
size_t StartIndex() const {
MOZ_ASSERT(IsIndexesSet());
return *mStartIndex;
}
size_t EndIndex() const {
MOZ_ASSERT(IsIndexesSet());
return *mEndIndex;
}
uint32_t StartOffsetInTextInBlock() const {
MOZ_ASSERT(IsSet());
return *mStartOffsetInTextInBlock;
}
uint32_t EndOffsetInTextInBlock() const {
MOZ_ASSERT(IsSet());
return *mEndOffsetInTextInBlock;
}
uint32_t LengthInTextInBlock() const {
MOZ_ASSERT(IsSet());
return *mEndOffsetInTextInBlock - *mStartOffsetInTextInBlock;
}
bool IsCollapsed() {
return !IsSet() || (IsInSameElement() && StartOffsetInTextInBlock() ==
EndOffsetInTextInBlock());
}
bool IsIndexesSet() const {
return mStartIndex.isSome() && mEndIndex.isSome();
}
bool IsSet() const {
return IsIndexesSet() && mStartOffsetInTextInBlock.isSome() &&
mEndOffsetInTextInBlock.isSome();
}
bool IsInSameElement() const {
return IsIndexesSet() && StartIndex() == EndIndex();
}
void Reset() {
mStartIndex.reset();
mEndIndex.reset();
mStartOffsetInTextInBlock.reset();
mEndOffsetInTextInBlock.reset();
}
void SetIndex(size_t aIndex) { mEndIndex = mStartIndex = Some(aIndex); }
void Set(size_t aIndex, uint32_t aOffsetInTextInBlock) {
mEndIndex = mStartIndex = Some(aIndex);
mStartOffsetInTextInBlock = mEndOffsetInTextInBlock =
Some(aOffsetInTextInBlock);
}
void SetIndexes(size_t aStartIndex, size_t aEndIndex) {
MOZ_DIAGNOSTIC_ASSERT(aStartIndex <= aEndIndex);
mStartIndex = Some(aStartIndex);
mEndIndex = Some(aEndIndex);
}
void Set(size_t aStartIndex, size_t aEndIndex,
uint32_t aStartOffsetInTextInBlock,
uint32_t aEndOffsetInTextInBlock) {
MOZ_DIAGNOSTIC_ASSERT(aStartIndex <= aEndIndex);
mStartIndex = Some(aStartIndex);
mEndIndex = Some(aEndIndex);
mStartOffsetInTextInBlock = Some(aStartOffsetInTextInBlock);
mEndOffsetInTextInBlock = Some(aEndOffsetInTextInBlock);
}
void CollapseToStart() {
MOZ_ASSERT(mStartIndex.isSome());
MOZ_ASSERT(mStartOffsetInTextInBlock.isSome());
mEndIndex = mStartIndex;
mEndOffsetInTextInBlock = mStartOffsetInTextInBlock;
}
private:
Maybe<size_t> mStartIndex;
Maybe<size_t> mEndIndex;
// Selected start and end offset in all text in a block element.
Maybe<uint32_t> mStartOffsetInTextInBlock;
Maybe<uint32_t> mEndOffsetInTextInBlock;
};
Selection mSelection;
};
RefPtr<dom::Document> mDocument;
nsCOMPtr<nsISelectionController> mSelCon;
RefPtr<EditorBase> mEditorBase;
RefPtr<FilteredContentIterator> mFilteredIter;
nsCOMPtr<nsIContent> mPrevTextBlock;
nsCOMPtr<nsIContent> mNextTextBlock;
OffsetEntryArray mOffsetTable;
RefPtr<nsRange> mExtent;
uint32_t mTxtSvcFilterType;
IteratorStatus mIteratorStatus;
protected:
virtual ~TextServicesDocument() = default;
public:
TextServicesDocument();
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(TextServicesDocument)
/**
* Initializes the text services document to use a particular editor. The
* text services document will use the DOM document and presentation shell
* used by the editor.
*
* @param aEditor The editor to use.
*/
nsresult InitWithEditor(nsIEditor* aEditor);
/**
* Sets the range/extent over which the text services document will iterate.
* Note that InitWithEditor() should have been called prior to calling this
* method. If this method is never called, the text services defaults to
* iterating over the entire document.
*
* @param aAbstractRange The range to use. aAbstractRange must point to a
* valid range object.
*/
nsresult SetExtent(const dom::AbstractRange* aAbstractRange);
/**
* Expands the end points of the range so that it spans complete words. This
* call does not change any internal state of the text services document.
*
* @param aStaticRange [in/out] The range to be expanded/adjusted.
*/
nsresult ExpandRangeToWordBoundaries(dom::StaticRange* aStaticRange);
/**
* Sets the filter type to be used while iterating over content.
* This will clear the current filter type if it's not either
* FILTERTYPE_NORMAL or FILTERTYPE_MAIL.
*
* @param aFilterType The filter type to be used while iterating over
* content.
*/
nsresult SetFilterType(uint32_t aFilterType);
/**
* Returns the text in the current text block.
*
* @param aStr [OUT] This will contain the text.
*/
nsresult GetCurrentTextBlock(nsAString& aStr);
/**
* Tells the document to point to the first text block in the document. This
* method does not adjust the current cursor position or selection.
*/
nsresult FirstBlock();
enum class BlockSelectionStatus {
// There is no text block (TB) in or before the selection (S).
eBlockNotFound = 0,
// No TB in S, but found one before/after S.
eBlockOutside,
// S extends beyond the start and end of TB.
eBlockInside,
// TB contains entire S.
eBlockContains,
// S begins or ends in TB but extends outside of TB.
eBlockPartial,
};
/**
* Tells the document to point to the last text block that contains the
* current selection or caret.
*
* @param aSelectionStatus [OUT] This will contain the text block
* selection status.
* @param aSelectionOffset [OUT] This will contain the offset into the
* string returned by GetCurrentTextBlock() where
* the selection begins.
* @param aLength [OUT] This will contain the number of
* characters that are selected in the string.
*/
MOZ_CAN_RUN_SCRIPT
nsresult LastSelectedBlock(BlockSelectionStatus* aSelStatus,
uint32_t* aSelOffset, uint32_t* aSelLength);
/**
* Tells the document to point to the text block before the current one.
* This method will return NS_OK, even if there is no previous block.
* Callers should call IsDone() to check if we have gone beyond the first
* text block in the document.
*/
nsresult PrevBlock();
/**
* Tells the document to point to the text block after the current one.
* This method will return NS_OK, even if there is no next block. Callers
* should call IsDone() to check if we have gone beyond the last text block
* in the document.
*/
nsresult NextBlock();
/**
* IsDone() will always set aIsDone == false unless the document contains
* no text, PrevBlock() was called while the document was already pointing
* to the first text block in the document, or NextBlock() was called while
* the document was already pointing to the last text block in the document.
*
* @param aIsDone [OUT] This will contain the result.
*/
nsresult IsDone(bool* aIsDone);
/**
* SetSelection() allows the caller to set the selection based on an offset
* into the string returned by GetCurrentTextBlock(). A length of zero
* places the cursor at that offset. A positive non-zero length "n" selects
* n characters in the string.
*
* @param aOffset Offset into string returned by
* GetCurrentTextBlock().
* @param aLength Number of characters selected.
*/
MOZ_CAN_RUN_SCRIPT nsresult SetSelection(uint32_t aOffset, uint32_t aLength);
/**
* Scrolls the document so that the current selection is visible.
*/
nsresult ScrollSelectionIntoView();
/**
* Deletes the text selected by SetSelection(). Calling DeleteSelection()
* with nothing selected, or with a collapsed selection (cursor) does
* nothing and returns NS_OK.
*/
MOZ_CAN_RUN_SCRIPT
nsresult DeleteSelection();
/**
* Inserts the given text at the current cursor position. If there is a
* selection, it will be deleted before the text is inserted.
*/
MOZ_CAN_RUN_SCRIPT
nsresult InsertText(const nsAString& aText);
/**
* nsIEditActionListener method implementations.
*/
NS_DECL_NSIEDITACTIONLISTENER
/**
* Actual edit action listeners. When you add new method here for listening
* to new edit action, you need to make it called by EditorBase.
* Additionally, you need to call it from proper method of
* nsIEditActionListener too because if this is created not for inline
* spell checker of the editor, edit actions will be notified via
* nsIEditActionListener (slow path, though).
*/
void DidDeleteContent(const nsIContent& aChildContent);
void DidJoinContents(const EditorRawDOMPoint& aJoinedPoint,
const nsIContent& aRemovedContent);
private:
// TODO: We should get rid of this method since `aAbstractRange` has
// enough simple API to get them.
static nsresult GetRangeEndPoints(const dom::AbstractRange* aAbstractRange,
nsINode** aStartContainer,
uint32_t* aStartOffset,
nsINode** aEndContainer,
uint32_t* aEndOffset);
nsresult CreateFilteredContentIterator(
const dom::AbstractRange* aAbstractRange,
FilteredContentIterator** aFilteredIter);
dom::Element* GetDocumentContentRootNode() const;
already_AddRefed<nsRange> CreateDocumentContentRange();
already_AddRefed<nsRange> CreateDocumentContentRootToNodeOffsetRange(
nsINode* aParent, uint32_t aOffset, bool aToStart);
nsresult CreateDocumentContentIterator(
FilteredContentIterator** aFilteredIter);
nsresult AdjustContentIterator();
static nsresult FirstTextNode(FilteredContentIterator* aFilteredIter,
IteratorStatus* aIteratorStatus);
static nsresult LastTextNode(FilteredContentIterator* aFilteredIter,
IteratorStatus* aIteratorStatus);
static nsresult FirstTextNodeInCurrentBlock(
FilteredContentIterator* aFilteredIter);
static nsresult FirstTextNodeInPrevBlock(
FilteredContentIterator* aFilteredIter);
static nsresult FirstTextNodeInNextBlock(
FilteredContentIterator* aFilteredIter);
nsresult GetFirstTextNodeInPrevBlock(nsIContent** aContent);
nsresult GetFirstTextNodeInNextBlock(nsIContent** aContent);
static bool DidSkip(FilteredContentIterator* aFilteredIter);
static void ClearDidSkip(FilteredContentIterator* aFilteredIter);
static bool HasSameBlockNodeParent(dom::Text& aTextNode1,
dom::Text& aTextNode2);
MOZ_CAN_RUN_SCRIPT nsresult SetSelectionInternal(uint32_t aOffset,
uint32_t aLength,
bool aDoUpdate);
MOZ_CAN_RUN_SCRIPT nsresult GetSelection(BlockSelectionStatus* aSelStatus,
uint32_t* aSelOffset,
uint32_t* aSelLength);
MOZ_CAN_RUN_SCRIPT nsresult
GetCollapsedSelection(BlockSelectionStatus* aSelStatus, uint32_t* aSelOffset,
uint32_t* aSelLength);
nsresult GetUncollapsedSelection(BlockSelectionStatus* aSelStatus,
uint32_t* aSelOffset, uint32_t* aSelLength);
};
} // namespace mozilla
#endif // #ifndef mozilla_TextServicesDocument_h
|