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 InsertTextTransaction_h
7 #define InsertTextTransaction_h
8 
9 #include "mozilla/EditTransactionBase.h"  // base class
10 
11 #include "mozilla/EditorDOMPoint.h"        // EditorDOMPointInText
12 #include "nsCycleCollectionParticipant.h"  // various macros
13 #include "nsID.h"                          // NS_DECLARE_STATIC_IID_ACCESSOR
14 #include "nsISupportsImpl.h"               // NS_DECL_ISUPPORTS_INHERITED
15 #include "nsString.h"                      // nsString members
16 #include "nscore.h"                        // NS_IMETHOD, nsAString
17 
18 namespace mozilla {
19 
20 class EditorBase;
21 
22 namespace dom {
23 class Text;
24 }  // namespace dom
25 
26 /**
27  * A transaction that inserts text into a content node.
28  */
29 class InsertTextTransaction final : public EditTransactionBase {
30  protected:
31   InsertTextTransaction(EditorBase& aEditorBase,
32                         const nsAString& aStringToInsert,
33                         const EditorDOMPointInText& aPointToInsert);
34 
35  public:
36   /**
37    * Creates new InsertTextTransaction instance.  This never returns nullptr.
38    *
39    * @param aEditorBase     The editor which manages the transaction.
40    * @param aPointToInsert  The insertion point.
41    * @param aStringToInsert The new string to insert.
42    */
43   static already_AddRefed<InsertTextTransaction> Create(
44       EditorBase& aEditorBase, const nsAString& aStringToInsert,
45       const EditorDOMPointInText& aPointToInsert);
46 
47   NS_DECL_ISUPPORTS_INHERITED
48   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertTextTransaction,
49                                            EditTransactionBase)
50 
51   NS_DECL_EDITTRANSACTIONBASE
52   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(InsertTextTransaction)
53 
54   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
55   NS_IMETHOD Merge(nsITransaction* aOtherTransaction, bool* aDidMerge) override;
56 
57   /**
58    * Return the string data associated with this transaction.
59    */
60   void GetData(nsString& aResult);
61 
62   friend std::ostream& operator<<(std::ostream& aStream,
63                                   const InsertTextTransaction& aTransaction);
64 
65  private:
66   virtual ~InsertTextTransaction() = default;
67 
68   // Return true if aOtherTransaction immediately follows this transaction.
69   bool IsSequentialInsert(InsertTextTransaction& aOtherTrasaction);
70 
71   // The Text node to operate upon.
72   RefPtr<dom::Text> mTextNode;
73 
74   // The offset into mTextNode where the insertion is to take place.
75   uint32_t mOffset;
76 
77   // The text to insert into mTextNode at mOffset.
78   nsString mStringToInsert;
79 
80   // The editor, which we'll need to get the selection.
81   RefPtr<EditorBase> mEditorBase;
82 };
83 
84 }  // namespace mozilla
85 
86 #endif  // #ifndef InsertTextTransaction_h
87