forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.h
407 lines (343 loc) · 15 KB
/
TextEditor.h
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
/* -*- 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_TextEditor_h
#define mozilla_TextEditor_h
#include "mozilla/EditorBase.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsIEditor.h"
#include "nsIEditorMailSupport.h"
#include "nsIPlaintextEditor.h"
#include "nsISupportsImpl.h"
#include "nscore.h"
class nsIContent;
class nsIDocumentEncoder;
class nsIOutputStream;
class nsISelectionController;
class nsITransferable;
namespace mozilla {
class AutoEditInitRulesTrigger;
enum class EditSubAction : int32_t;
namespace dom {
class DragEvent;
class Selection;
} // namespace dom
/**
* The text editor implementation.
* Use to edit text document represented as a DOM tree.
*/
class TextEditor : public EditorBase
, public nsIPlaintextEditor
, public nsIEditorMailSupport
{
public:
/****************************************************************************
* NOTE: DO NOT MAKE YOUR NEW METHODS PUBLIC IF they are called by other
* classes under libeditor except EditorEventListener and
* HTMLEditorEventListener because each public method which may fire
* eEditorInput event will need to instantiate new stack class for
* managing input type value of eEditorInput and cache some objects
* for smarter handling. In other words, when you add new root
* method to edit the DOM tree, you can make your new method public.
****************************************************************************/
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextEditor, EditorBase)
TextEditor();
// nsIPlaintextEditor methods
NS_DECL_NSIPLAINTEXTEDITOR
// nsIEditorMailSupport overrides
NS_DECL_NSIEDITORMAILSUPPORT
// Overrides of nsIEditor
NS_IMETHOD GetDocumentIsEmpty(bool* aDocumentIsEmpty) override;
NS_IMETHOD DeleteSelection(EDirection aAction,
EStripWrappers aStripWrappers) override;
NS_IMETHOD SetDocumentCharacterSet(const nsACString& characterSet) override;
// If there are some good name to create non-virtual Undo()/Redo() methods,
// we should create them and those methods should just run them.
NS_IMETHOD Undo(uint32_t aCount) final;
NS_IMETHOD Redo(uint32_t aCount) final;
NS_IMETHOD Cut() override;
NS_IMETHOD CanCut(bool* aCanCut) override;
NS_IMETHOD Copy() override;
NS_IMETHOD CanCopy(bool* aCanCopy) override;
NS_IMETHOD CanDelete(bool* aCanDelete) override;
NS_IMETHOD Paste(int32_t aSelectionType) override;
NS_IMETHOD CanPaste(int32_t aSelectionType, bool* aCanPaste) override;
NS_IMETHOD PasteTransferable(nsITransferable* aTransferable) override;
NS_IMETHOD OutputToString(const nsAString& aFormatType,
uint32_t aFlags,
nsAString& aOutputString) override;
/** Can we paste |aTransferable| or, if |aTransferable| is null, will a call
* to pasteTransferable later possibly succeed if given an instance of
* nsITransferable then? True if the doc is modifiable, and, if
* |aTransfeable| is non-null, we have pasteable data in |aTransfeable|.
*/
virtual bool CanPasteTransferable(nsITransferable* aTransferable);
// Overrides of EditorBase
virtual nsresult Init(nsIDocument& aDoc, Element* aRoot,
nsISelectionController* aSelCon, uint32_t aFlags,
const nsAString& aValue) override;
nsresult DocumentIsEmpty(bool* aIsEmpty);
virtual nsresult HandleKeyPressEvent(
WidgetKeyboardEvent* aKeyboardEvent) override;
virtual dom::EventTarget* GetDOMEventTarget() override;
/**
* InsertTextAsAction() inserts aStringToInsert at selection.
* Although this method is implementation of nsIPlaintextEditor.insertText(),
* this treats the input is an edit action.
*
* @param aStringToInsert The string to insert.
*/
nsresult InsertTextAsAction(const nsAString& aStringToInsert);
/**
* DeleteSelectionAsAction() removes selection content or content around
* caret with transactions. This should be used for handling it as an
* edit action. If you'd like to remove selection for preparing to insert
* something, you probably should use DeleteSelectionAsSubAction().
*
* @param aDirection How much range should be removed.
* @param aStripWrappers Whether the parent blocks should be removed
* when they become empty.
*/
nsresult DeleteSelectionAsAction(EDirection aDirection,
EStripWrappers aStripWrappers);
/**
* The maximum number of characters allowed.
* default: -1 (unlimited).
*/
int32_t MaxTextLength() const { return mMaxTextLength; }
void SetMaxTextLength(int32_t aLength) { mMaxTextLength = aLength; }
/**
* Replace existed string with a string.
* This is fast path to replace all string when using single line control.
*
* @ param aString the string to be set
*/
nsresult SetText(const nsAString& aString);
/**
* OnInputParagraphSeparator() is called when user tries to separate current
* paragraph with Enter key press or something.
*/
nsresult OnInputParagraphSeparator();
/**
* OnCompositionStart() is called when editor receives eCompositionStart
* event which should be handled in this editor.
*/
nsresult OnCompositionStart(WidgetCompositionEvent& aCompositionStartEvent);
/**
* OnCompositionChange() is called when editor receives an eCompositioChange
* event which should be handled in this editor.
*
* @param aCompositionChangeEvent eCompositionChange event which should
* be handled in this editor.
*/
nsresult
OnCompositionChange(WidgetCompositionEvent& aCompositionChangeEvent);
/**
* OnCompositionEnd() is called when editor receives an eCompositionChange
* event and it's followed by eCompositionEnd event and after
* OnCompositionChange() is called.
*/
void OnCompositionEnd(WidgetCompositionEvent& aCompositionEndEvent);
/**
* OnDrop() is called from EditorEventListener::Drop that is handler of drop
* event.
*/
nsresult OnDrop(dom::DragEvent* aDropEvent);
protected: // May be called by friends.
/****************************************************************************
* Some classes like TextEditRules, HTMLEditRules, WSRunObject which are
* part of handling edit actions are allowed to call the following protected
* methods. However, those methods won't prepare caches of some objects
* which are necessary for them. So, if you want some following methods
* to do that for you, you need to create a wrapper method in public scope
* and call it.
****************************************************************************/
// Overrides of EditorBase
virtual nsresult RemoveAttributeOrEquivalent(
Element* aElement,
nsAtom* aAttribute,
bool aSuppressTransaction) override;
virtual nsresult SetAttributeOrEquivalent(Element* aElement,
nsAtom* aAttribute,
const nsAString& aValue,
bool aSuppressTransaction) override;
using EditorBase::RemoveAttributeOrEquivalent;
using EditorBase::SetAttributeOrEquivalent;
/**
* DeleteSelectionAsSubAction() removes selection content or content around
* caret with transactions. This should be used for handling it as an
* edit sub-action.
*
* @param aDirection How much range should be removed.
* @param aStripWrappers Whether the parent blocks should be removed
* when they become empty.
*/
nsresult DeleteSelectionAsSubAction(EDirection aDirection,
EStripWrappers aStripWrappers);
/**
* DeleteSelectionWithTransaction() removes selected content or content
* around caret with transactions.
*
* @param aDirection How much range should be removed.
* @param aStripWrappers Whether the parent blocks should be removed
* when they become empty.
*/
virtual nsresult
DeleteSelectionWithTransaction(EDirection aAction,
EStripWrappers aStripWrappers);
/**
* InsertBrElementWithTransaction() creates a <br> element and inserts it
* before aPointToInsert. Then, tries to collapse selection at or after the
* new <br> node if aSelect is not eNone.
*
* @param aSelection The selection of this editor.
* @param aPointToInsert The DOM point where should be <br> node inserted
* before.
* @param aSelect If eNone, this won't change selection.
* If eNext, selection will be collapsed after
* the <br> element.
* If ePrevious, selection will be collapsed at
* the <br> element.
* @return The new <br> node. If failed to create new
* <br> node, returns nullptr.
*/
template<typename PT, typename CT>
already_AddRefed<Element>
InsertBrElementWithTransaction(
Selection& aSelection,
const EditorDOMPointBase<PT, CT>& aPointToInsert,
EDirection aSelect = eNone);
/**
* Extends the selection for given deletion operation
* If done, also update aAction to what's actually left to do after the
* extension.
*/
nsresult ExtendSelectionForDelete(Selection* aSelection,
nsIEditor::EDirection* aAction);
static void GetDefaultEditorPrefs(int32_t& aNewLineHandling,
int32_t& aCaretStyle);
protected: // Called by helper classes.
virtual void
OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;
virtual void OnEndHandlingTopLevelEditSubAction() override;
void BeginEditorInit();
nsresult EndEditorInit();
protected: // Shouldn't be used by friend classes
virtual ~TextEditor();
/**
* Make the given selection span the entire document.
*/
virtual nsresult SelectEntireDocument(Selection* aSelection) override;
/**
* OnInputText() is called when user inputs text with keyboard or something.
*
* @param aStringToInsert The string to insert.
*/
nsresult OnInputText(const nsAString& aStringToInsert);
/**
* InsertParagraphSeparatorAsAction() inserts a line break if it's TextEditor
* or inserts new paragraph if it's HTMLEditor and it's possible.
* Although, this method is implementation of
* nsIPlaintextEditor.insertLineBreak(), this treats the input is an edit
* action.
*/
nsresult InsertParagraphSeparatorAsAction();
nsresult InsertTextAt(const nsAString& aStringToInsert,
nsINode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection);
virtual nsresult InsertFromDataTransfer(dom::DataTransfer* aDataTransfer,
int32_t aIndex,
nsIDocument* aSourceDoc,
nsINode* aDestinationNode,
int32_t aDestOffset,
bool aDoDeleteSelection) override;
/**
* Return true if the data is safe to insert as the source and destination
* principals match, or we are in a editor context where this doesn't matter.
* Otherwise, the data must be sanitized first.
*/
bool IsSafeToInsertData(nsIDocument* aSourceDoc);
virtual nsresult InitRules();
already_AddRefed<nsIDocumentEncoder> GetAndInitDocEncoder(
const nsAString& aFormatType,
uint32_t aFlags,
const nsACString& aCharset);
/**
* Factored methods for handling insertion of data from transferables
* (drag&drop or clipboard).
*/
virtual nsresult PrepareTransferable(nsITransferable** transferable);
nsresult InsertTextFromTransferable(nsITransferable* transferable);
/**
* DeleteSelectionAndCreateElement() creates a element whose name is aTag.
* And insert it into the DOM tree after removing the selected content.
*
* @param aTag The element name to be created.
* @return Created new element.
*/
already_AddRefed<Element> DeleteSelectionAndCreateElement(nsAtom& aTag);
/**
* This method first deletes the selection, if it's not collapsed. Then if
* the selection lies in a CharacterData node, it splits it. If the
* selection is at this point collapsed in a CharacterData node, it's
* adjusted to be collapsed right before or after the node instead (which is
* always possible, since the node was split).
*/
nsresult DeleteSelectionAndPrepareToCreateNode();
/**
* Shared outputstring; returns whether selection is collapsed and resulting
* string.
*/
nsresult SharedOutputString(uint32_t aFlags, bool* aIsCollapsed,
nsAString& aResult);
enum PasswordFieldAllowed
{
ePasswordFieldAllowed,
ePasswordFieldNotAllowed
};
bool CanCutOrCopy(PasswordFieldAllowed aPasswordFieldAllowed);
bool FireClipboardEvent(EventMessage aEventMessage,
int32_t aSelectionType,
bool* aActionTaken = nullptr);
bool UpdateMetaCharset(nsIDocument& aDocument,
const nsACString& aCharacterSet);
/**
* EnsureComposition() should be called by composition event handlers. This
* tries to get the composition for the event and set it to mComposition.
* However, this may fail because the composition may be committed before
* the event comes to the editor.
*
* @return true if there is a composition. Otherwise, for example,
* a composition event handler in web contents moved focus
* for committing the composition, returns false.
*/
bool EnsureComposition(WidgetCompositionEvent& aCompositionEvent);
virtual already_AddRefed<nsIContent> GetInputEventTargetContent() override;
protected:
nsCOMPtr<nsIDocumentEncoder> mCachedDocumentEncoder;
nsString mCachedDocumentEncoderType;
int32_t mWrapColumn;
int32_t mMaxTextLength;
int32_t mInitTriggerCounter;
int32_t mNewlineHandling;
int32_t mCaretStyle;
friend class AutoEditInitRulesTrigger;
friend class TextEditRules;
};
} // namespace mozilla
mozilla::TextEditor*
nsIEditor::AsTextEditor()
{
return static_cast<mozilla::TextEditor*>(this);
}
const mozilla::TextEditor*
nsIEditor::AsTextEditor() const
{
return static_cast<const mozilla::TextEditor*>(this);
}
#endif // #ifndef mozilla_TextEditor_h