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