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 mozilla_TextEditor_h
7 : #define mozilla_TextEditor_h
8 :
9 : #include "mozilla/EditorBase.h"
10 : #include "nsCOMPtr.h"
11 : #include "nsCycleCollectionParticipant.h"
12 : #include "nsIEditor.h"
13 : #include "nsIEditorMailSupport.h"
14 : #include "nsIPlaintextEditor.h"
15 : #include "nsISupportsImpl.h"
16 : #include "nscore.h"
17 :
18 : class nsIContent;
19 : class nsIDOMDocument;
20 : class nsIDOMElement;
21 : class nsIDOMEvent;
22 : class nsIDOMNode;
23 : class nsIDocumentEncoder;
24 : class nsIEditRules;
25 : class nsIOutputStream;
26 : class nsISelectionController;
27 : class nsITransferable;
28 :
29 : namespace mozilla {
30 :
31 : class AutoEditInitRulesTrigger;
32 : class HTMLEditRules;
33 : class TextEditRules;
34 : namespace dom {
35 : class Selection;
36 : } // namespace dom
37 :
38 : /**
39 : * The text editor implementation.
40 : * Use to edit text document represented as a DOM tree.
41 : */
42 : class TextEditor : public EditorBase
43 : , public nsIPlaintextEditor
44 : , public nsIEditorMailSupport
45 : {
46 : public:
47 : NS_DECL_ISUPPORTS_INHERITED
48 1 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextEditor, EditorBase)
49 :
50 : enum ETypingAction
51 : {
52 : eTypedText, /* user typed text */
53 : eTypedBR, /* user typed shift-enter to get a br */
54 : eTypedBreak /* user typed enter */
55 : };
56 :
57 : TextEditor();
58 :
59 0 : virtual TextEditor* AsTextEditor() override { return this; }
60 0 : virtual const TextEditor* AsTextEditor() const override { return this; }
61 : virtual HTMLEditor* AsHTMLEditor() override;
62 : virtual const HTMLEditor* AsHTMLEditor() const override;
63 :
64 : // nsIPlaintextEditor methods
65 : NS_DECL_NSIPLAINTEXTEDITOR
66 :
67 : // nsIEditorMailSupport overrides
68 : NS_DECL_NSIEDITORMAILSUPPORT
69 :
70 : // Overrides of EditorBase
71 : virtual nsresult RemoveAttributeOrEquivalent(
72 : Element* aElement,
73 : nsIAtom* aAttribute,
74 : bool aSuppressTransaction) override;
75 : virtual nsresult SetAttributeOrEquivalent(Element* aElement,
76 : nsIAtom* aAttribute,
77 : const nsAString& aValue,
78 : bool aSuppressTransaction) override;
79 : using EditorBase::RemoveAttributeOrEquivalent;
80 : using EditorBase::SetAttributeOrEquivalent;
81 :
82 : NS_IMETHOD Init(nsIDOMDocument* aDoc, nsIContent* aRoot,
83 : nsISelectionController* aSelCon, uint32_t aFlags,
84 : const nsAString& aValue) override;
85 :
86 : NS_IMETHOD GetDocumentIsEmpty(bool* aDocumentIsEmpty) override;
87 :
88 : NS_IMETHOD DeleteSelection(EDirection aAction,
89 : EStripWrappers aStripWrappers) override;
90 :
91 : NS_IMETHOD SetDocumentCharacterSet(const nsACString& characterSet) override;
92 :
93 : NS_IMETHOD Undo(uint32_t aCount) override;
94 : NS_IMETHOD Redo(uint32_t aCount) override;
95 :
96 : NS_IMETHOD Cut() override;
97 : NS_IMETHOD CanCut(bool* aCanCut) override;
98 : NS_IMETHOD Copy() override;
99 : NS_IMETHOD CanCopy(bool* aCanCopy) override;
100 : NS_IMETHOD CanDelete(bool* aCanDelete) override;
101 : NS_IMETHOD Paste(int32_t aSelectionType) override;
102 : NS_IMETHOD CanPaste(int32_t aSelectionType, bool* aCanPaste) override;
103 : NS_IMETHOD PasteTransferable(nsITransferable* aTransferable) override;
104 : NS_IMETHOD CanPasteTransferable(nsITransferable* aTransferable,
105 : bool* aCanPaste) override;
106 :
107 : NS_IMETHOD OutputToString(const nsAString& aFormatType,
108 : uint32_t aFlags,
109 : nsAString& aOutputString) override;
110 :
111 : NS_IMETHOD OutputToStream(nsIOutputStream* aOutputStream,
112 : const nsAString& aFormatType,
113 : const nsACString& aCharsetOverride,
114 : uint32_t aFlags) override;
115 :
116 : /**
117 : * All editor operations which alter the doc should be prefaced
118 : * with a call to StartOperation, naming the action and direction.
119 : */
120 : NS_IMETHOD StartOperation(EditAction opID,
121 : nsIEditor::EDirection aDirection) override;
122 :
123 : /**
124 : * All editor operations which alter the doc should be followed
125 : * with a call to EndOperation.
126 : */
127 : NS_IMETHOD EndOperation() override;
128 :
129 : /**
130 : * Make the given selection span the entire document.
131 : */
132 : virtual nsresult SelectEntireDocument(Selection* aSelection) override;
133 :
134 : virtual nsresult HandleKeyPressEvent(
135 : WidgetKeyboardEvent* aKeyboardEvent) override;
136 :
137 : virtual already_AddRefed<dom::EventTarget> GetDOMEventTarget() override;
138 :
139 : virtual nsresult BeginIMEComposition(WidgetCompositionEvent* aEvent) override;
140 : virtual nsresult UpdateIMEComposition(
141 : WidgetCompositionEvent* aCompositionChangeEvet) override;
142 :
143 : virtual already_AddRefed<nsIContent> GetInputEventTargetContent() override;
144 :
145 : // Utility Routines, not part of public API
146 : NS_IMETHOD TypedText(const nsAString& aString, ETypingAction aAction);
147 :
148 : nsresult InsertTextAt(const nsAString& aStringToInsert,
149 : nsIDOMNode* aDestinationNode,
150 : int32_t aDestOffset,
151 : bool aDoDeleteSelection);
152 :
153 : virtual nsresult InsertFromDataTransfer(dom::DataTransfer* aDataTransfer,
154 : int32_t aIndex,
155 : nsIDOMDocument* aSourceDoc,
156 : nsIDOMNode* aDestinationNode,
157 : int32_t aDestOffset,
158 : bool aDoDeleteSelection) override;
159 :
160 : virtual nsresult InsertFromDrop(nsIDOMEvent* aDropEvent) override;
161 :
162 : /**
163 : * Extends the selection for given deletion operation
164 : * If done, also update aAction to what's actually left to do after the
165 : * extension.
166 : */
167 : nsresult ExtendSelectionForDelete(Selection* aSelection,
168 : nsIEditor::EDirection* aAction);
169 :
170 : /**
171 : * Return true if the data is safe to insert as the source and destination
172 : * principals match, or we are in a editor context where this doesn't matter.
173 : * Otherwise, the data must be sanitized first.
174 : */
175 : bool IsSafeToInsertData(nsIDOMDocument* aSourceDoc);
176 :
177 : static void GetDefaultEditorPrefs(int32_t& aNewLineHandling,
178 : int32_t& aCaretStyle);
179 :
180 1 : int32_t MaxTextLength() const { return mMaxTextLength; }
181 :
182 : protected:
183 : virtual ~TextEditor();
184 :
185 : NS_IMETHOD InitRules();
186 : void BeginEditorInit();
187 : nsresult EndEditorInit();
188 :
189 : already_AddRefed<nsIDocumentEncoder> GetAndInitDocEncoder(
190 : const nsAString& aFormatType,
191 : uint32_t aFlags,
192 : const nsACString& aCharset);
193 :
194 : NS_IMETHOD CreateBR(nsIDOMNode* aNode, int32_t aOffset,
195 : nsCOMPtr<nsIDOMNode>* outBRNode,
196 : EDirection aSelect = eNone);
197 : already_AddRefed<Element> CreateBRImpl(nsCOMPtr<nsINode>* aInOutParent,
198 : int32_t* aInOutOffset,
199 : EDirection aSelect);
200 : nsresult CreateBRImpl(nsCOMPtr<nsIDOMNode>* aInOutParent,
201 : int32_t* aInOutOffset,
202 : nsCOMPtr<nsIDOMNode>* outBRNode,
203 : EDirection aSelect);
204 :
205 : /**
206 : * Factored methods for handling insertion of data from transferables
207 : * (drag&drop or clipboard).
208 : */
209 : NS_IMETHOD PrepareTransferable(nsITransferable** transferable);
210 : nsresult InsertTextFromTransferable(nsITransferable* transferable,
211 : nsIDOMNode* aDestinationNode,
212 : int32_t aDestOffset,
213 : bool aDoDeleteSelection);
214 :
215 : /**
216 : * Shared outputstring; returns whether selection is collapsed and resulting
217 : * string.
218 : */
219 : nsresult SharedOutputString(uint32_t aFlags, bool* aIsCollapsed,
220 : nsAString& aResult);
221 :
222 : enum PasswordFieldAllowed
223 : {
224 : ePasswordFieldAllowed,
225 : ePasswordFieldNotAllowed
226 : };
227 : bool CanCutOrCopy(PasswordFieldAllowed aPasswordFieldAllowed);
228 : bool FireClipboardEvent(EventMessage aEventMessage,
229 : int32_t aSelectionType,
230 : bool* aActionTaken = nullptr);
231 :
232 : bool UpdateMetaCharset(nsIDocument& aDocument,
233 : const nsACString& aCharacterSet);
234 :
235 : protected:
236 : nsCOMPtr<nsIEditRules> mRules;
237 : nsCOMPtr<nsIDocumentEncoder> mCachedDocumentEncoder;
238 : nsString mCachedDocumentEncoderType;
239 : int32_t mWrapColumn;
240 : int32_t mMaxTextLength;
241 : int32_t mInitTriggerCounter;
242 : int32_t mNewlineHandling;
243 : int32_t mCaretStyle;
244 :
245 : friend class AutoEditInitRulesTrigger;
246 : friend class HTMLEditRules;
247 : friend class TextEditRules;
248 : };
249 :
250 : } // namespace mozilla
251 :
252 : #endif // #ifndef mozilla_TextEditor_h
|