Bug 1883706: part 3) Implement `createHTML`, `createScript` and `createScriptURL...
[gecko.git] / editor / libeditor / InsertTextTransaction.h
blob8411d87abca8a3840bc6ec417bc4cb0b186c28fb
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
20 namespace mozilla {
21 namespace dom {
22 class Text;
23 } // namespace dom
25 /**
26 * A transaction that inserts text into a content node.
28 class InsertTextTransaction final : public EditTransactionBase {
29 protected:
30 InsertTextTransaction(EditorBase& aEditorBase,
31 const nsAString& aStringToInsert,
32 const EditorDOMPointInText& aPointToInsert);
34 public:
35 /**
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,
48 EditTransactionBase)
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;
56 /**
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);
72 private:
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.
82 uint32_t mOffset;
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