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/. */
6 #ifndef InsertTextTransaction_h
7 #define InsertTextTransaction_h
9 #include "EditTransactionBase.h" // base class
11 #include "EditorDOMPoint.h"
12 #include "EditorForwards.h"
14 #include "nsCycleCollectionParticipant.h" // various macros
15 #include "nsID.h" // NS_DECLARE_STATIC_IID_ACCESSOR
16 #include "nsISupportsImpl.h" // NS_DECL_ISUPPORTS_INHERITED
17 #include "nsString.h" // nsString members
18 #include "nscore.h" // NS_IMETHOD, nsAString
26 * A transaction that inserts text into a content node.
28 class InsertTextTransaction final
: public EditTransactionBase
{
30 InsertTextTransaction(EditorBase
& aEditorBase
,
31 const nsAString
& aStringToInsert
,
32 const EditorDOMPointInText
& aPointToInsert
);
36 * Creates new InsertTextTransaction instance. This never returns nullptr.
38 * @param aEditorBase The editor which manages the transaction.
39 * @param aPointToInsert The insertion point.
40 * @param aStringToInsert The new string to insert.
42 static already_AddRefed
<InsertTextTransaction
> Create(
43 EditorBase
& aEditorBase
, const nsAString
& aStringToInsert
,
44 const EditorDOMPointInText
& aPointToInsert
);
46 NS_DECL_ISUPPORTS_INHERITED
47 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertTextTransaction
,
50 NS_DECL_EDITTRANSACTIONBASE
51 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(InsertTextTransaction
)
53 MOZ_CAN_RUN_SCRIPT NS_IMETHOD
RedoTransaction() override
;
54 NS_IMETHOD
Merge(nsITransaction
* aOtherTransaction
, bool* aDidMerge
) override
;
57 * Return the string data associated with this transaction.
59 const nsString
& GetData() const { return mStringToInsert
; }
61 template <typename EditorDOMPointType
>
62 EditorDOMPointType
SuggestPointToPutCaret() const {
63 if (NS_WARN_IF(!mTextNode
)) {
64 return EditorDOMPointType();
66 return EditorDOMPointType(mTextNode
, mOffset
+ mStringToInsert
.Length());
69 friend std::ostream
& operator<<(std::ostream
& aStream
,
70 const InsertTextTransaction
& aTransaction
);
73 virtual ~InsertTextTransaction() = default;
75 // Return true if aOtherTransaction immediately follows this transaction.
76 bool IsSequentialInsert(InsertTextTransaction
& aOtherTransaction
) const;
78 // The Text node to operate upon.
79 RefPtr
<dom::Text
> mTextNode
;
81 // The offset into mTextNode where the insertion is to take place.
84 // The text to insert into mTextNode at mOffset.
85 nsString mStringToInsert
;
87 // The editor, which we'll need to get the selection.
88 RefPtr
<EditorBase
> mEditorBase
;
91 } // namespace mozilla
93 #endif // #ifndef InsertTextTransaction_h