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 InsertNodeTransaction_h
7 #define InsertNodeTransaction_h
8 
9 #include "mozilla/EditTransactionBase.h"  // for EditTransactionBase, etc.
10 #include "mozilla/EditorDOMPoint.h"       // for EditorDOMPoint
11 #include "nsCOMPtr.h"                     // for nsCOMPtr
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsIContent.h"       // for nsIContent
14 #include "nsISupportsImpl.h"  // for NS_DECL_ISUPPORTS_INHERITED
15 
16 namespace mozilla {
17 
18 class EditorBase;
19 
20 /**
21  * A transaction that inserts a single element
22  */
23 class InsertNodeTransaction final : public EditTransactionBase {
24  protected:
25   template <typename PT, typename CT>
26   InsertNodeTransaction(EditorBase& aEditorBase, nsIContent& aContentToInsert,
27                         const EditorDOMPointBase<PT, CT>& aPointToInsert);
28 
29  public:
30   /**
31    * Create a transaction for inserting aContentToInsert before the child
32    * at aPointToInsert.
33    *
34    * @param aEditorBase         The editor which manages the transaction.
35    * @param aContentToInsert    The node to be inserted.
36    * @param aPointToInsert      The insertion point of aContentToInsert.
37    *                            If this refers end of the container, the
38    *                            transaction will append the node to the
39    *                            container.  Otherwise, will insert the node
40    *                            before child node referred by this.
41    * @return                    A InsertNodeTranaction which was initialized
42    *                            with the arguments.
43    */
44   template <typename PT, typename CT>
45   static already_AddRefed<InsertNodeTransaction> Create(
46       EditorBase& aEditorBase, nsIContent& aContentToInsert,
47       const EditorDOMPointBase<PT, CT>& aPointToInsert);
48 
49   NS_DECL_ISUPPORTS_INHERITED
50   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertNodeTransaction,
51                                            EditTransactionBase)
52 
53   NS_DECL_EDITTRANSACTIONBASE
54   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(InsertNodeTransaction)
55 
56   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
57 
58   friend std::ostream& operator<<(std::ostream& aStream,
59                                   const InsertNodeTransaction& aTransaction);
60 
61  protected:
62   virtual ~InsertNodeTransaction() = default;
63 
64   // The element to insert.
65   nsCOMPtr<nsIContent> mContentToInsert;
66 
67   // The DOM point we will insert mContentToInsert.
68   EditorDOMPoint mPointToInsert;
69 
70   // The editor for this transaction.
71   RefPtr<EditorBase> mEditorBase;
72 };
73 
74 }  // namespace mozilla
75 
76 #endif  // #ifndef InsertNodeTransaction_h
77