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 CreateElementTransaction_h
7 #define CreateElementTransaction_h
8 
9 #include "mozilla/EditorDOMPoint.h"
10 #include "mozilla/EditTransactionBase.h"
11 #include "mozilla/RefPtr.h"
12 #include "nsCycleCollectionParticipant.h"
13 #include "nsISupportsImpl.h"
14 
15 class nsAtom;
16 
17 /**
18  * A transaction that creates a new node in the content tree.
19  */
20 namespace mozilla {
21 namespace dom {
22 class Element;
23 }
24 
25 class EditorBase;
26 
27 class CreateElementTransaction final : public EditTransactionBase {
28  protected:
29   template <typename PT, typename CT>
30   CreateElementTransaction(EditorBase& aEditorBase, nsAtom& aTag,
31                            const EditorDOMPointBase<PT, CT>& aPointToInsert);
32 
33  public:
34   /**
35    * Create a transaction for creating a new child node of the container of
36    * aPointToInsert of type aTag.
37    *
38    * @param aEditorBase     The editor which manages the transaction.
39    * @param aTag            The tag (P, HR, TABLE, etc.) for the new element.
40    * @param aPointToInsert  The new node will be inserted before the child at
41    *                        aPointToInsert.  If this refers end of the container
42    *                        or after, the new node will be appended to the
43    *                        container.
44    */
45   template <typename PT, typename CT>
46   static already_AddRefed<CreateElementTransaction> Create(
47       EditorBase& aEditorBase, nsAtom& aTag,
48       const EditorDOMPointBase<PT, CT>& aPointToInsert);
49 
50   NS_DECL_ISUPPORTS_INHERITED
51   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CreateElementTransaction,
52                                            EditTransactionBase)
53 
54   NS_DECL_EDITTRANSACTIONBASE
55   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(CreateElementTransaction)
56 
57   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
58 
GetNewElement()59   dom::Element* GetNewElement() const { return mNewElement; }
60 
61   friend std::ostream& operator<<(std::ostream& aStream,
62                                   const CreateElementTransaction& aTransaction);
63 
64  protected:
65   virtual ~CreateElementTransaction() = default;
66 
67   /**
68    * InsertNewNode() inserts mNewNode before the child node at mPointToInsert.
69    */
70   MOZ_CAN_RUN_SCRIPT void InsertNewNode(ErrorResult& aError);
71 
72   // The document into which the new node will be inserted.
73   RefPtr<EditorBase> mEditorBase;
74 
75   // The tag (mapping to object type) for the new element.
76   RefPtr<nsAtom> mTag;
77 
78   // The DOM point we will insert mNewNode.
79   EditorDOMPoint mPointToInsert;
80 
81   // The new node to insert.
82   RefPtr<dom::Element> mNewElement;
83 };
84 
85 }  // namespace mozilla
86 
87 #endif  // #ifndef CreateElementTransaction_h
88