CompositionTransaction.h (4442B)
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 CompositionTransaction_h 7 #define CompositionTransaction_h 8 9 #include "EditTransactionBase.h" // base class 10 11 #include "EditorForwards.h" 12 13 #include "mozilla/WeakPtr.h" 14 #include "mozilla/dom/Text.h" 15 #include "nsCycleCollectionParticipant.h" // various macros 16 #include "nsString.h" // mStringToInsert 17 18 namespace mozilla { 19 class TextComposition; 20 class TextRangeArray; 21 22 /** 23 * CompositionTransaction stores all edit for a composition, i.e., 24 * from compositionstart event to compositionend event. E.g., inserting a 25 * composition string, modifying the composition string or its IME selection 26 * ranges and commit or cancel the composition. 27 */ 28 class CompositionTransaction : public EditTransactionBase, 29 public SupportsWeakPtr { 30 protected: 31 CompositionTransaction(EditorBase& aEditorBase, 32 const nsAString& aStringToInsert, 33 const EditorDOMPointInText& aPointToInsert); 34 35 public: 36 /** 37 * Creates a composition transaction. aEditorBase must not return from 38 * GetComposition() while calling this method. Note that this method will 39 * update text node information of aEditorBase.mComposition. 40 * 41 * @param aEditorBase The editor which has composition. 42 * @param aStringToInsert The new composition string to insert. This may 43 * be different from actual composition string. 44 * E.g., password editor can hide the character 45 * with a different character. 46 * @param aPointToInsert The insertion point. 47 */ 48 static already_AddRefed<CompositionTransaction> Create( 49 EditorBase& aEditorBase, const nsAString& aStringToInsert, 50 const EditorDOMPointInText& aPointToInsert); 51 52 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CompositionTransaction, 53 EditTransactionBase) 54 55 NS_DECL_ISUPPORTS_INHERITED 56 57 NS_DECL_EDITTRANSACTIONBASE 58 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(CompositionTransaction) 59 60 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override; 61 NS_IMETHOD Merge(nsITransaction* aOtherTransaction, bool* aDidMerge) override; 62 63 dom::Text* GetTextNode() const; 64 65 void MarkFixed(); 66 67 MOZ_CAN_RUN_SCRIPT static nsresult SetIMESelection( 68 EditorBase& aEditorBase, dom::Text* aTextNode, uint32_t aOffsetInNode, 69 uint32_t aLengthOfCompositionString, const TextRangeArray* aRanges); 70 71 friend std::ostream& operator<<(std::ostream& aStream, 72 const CompositionTransaction& aTransaction); 73 74 protected: 75 virtual ~CompositionTransaction() = default; 76 77 MOZ_CAN_RUN_SCRIPT nsresult SetSelectionForRanges(); 78 79 // The offsets in the text node where the insertion should be placed. 80 uint32_t mOffset; 81 uint32_t mReplaceLength; 82 83 // The range list. 84 RefPtr<TextRangeArray> mRanges; 85 86 // The text to insert into the text node at mOffset. 87 nsString mStringToInsert; 88 89 // The editor, which is used to get the selection controller. 90 RefPtr<EditorBase> mEditorBase; 91 92 bool mFixed; 93 }; 94 95 /** 96 * Private class for CompositionTransaction when it needs to handle a 97 * transaction of `HTMLEditor`. 98 */ 99 class CompositionInTextNodeTransaction final : CompositionTransaction { 100 public: 101 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CompositionInTextNodeTransaction, 102 CompositionTransaction) 103 NS_DECL_ISUPPORTS_INHERITED 104 105 friend std::ostream& operator<<( 106 std::ostream& aStream, 107 const CompositionInTextNodeTransaction& aTransaction); 108 109 private: 110 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE( 111 CompositionInTextNodeTransaction) 112 113 CompositionInTextNodeTransaction(EditorBase& aEditorBase, 114 const nsAString& aStringToInsert, 115 const EditorDOMPointInText& aPointToInsert); 116 virtual ~CompositionInTextNodeTransaction() = default; 117 118 // The text element to operate upon. 119 RefPtr<dom::Text> mTextNode; 120 121 friend class CompositionTransaction; 122 }; 123 124 } // namespace mozilla 125 126 #endif // #ifndef CompositionTransaction_h