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 JoinNodeTransaction_h
7 #define JoinNodeTransaction_h
8 
9 #include "mozilla/EditTransactionBase.h"  // for EditTransactionBase, etc.
10 #include "nsCOMPtr.h"                     // for nsCOMPtr
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsID.h"    // for REFNSIID
13 #include "nscore.h"  // for NS_IMETHOD
14 
15 class nsIContent;
16 class nsINode;
17 
18 namespace mozilla {
19 
20 class HTMLEditor;
21 
22 /**
23  * A transaction that joins two nodes E1 (left node) and E2 (right node) into a
24  * single node E.  The children of E are the children of E1 followed by the
25  * children of E2.  After DoTransaction() and RedoTransaction(), E1 is removed
26  * from the content tree and E2 remains.
27  */
28 class JoinNodeTransaction final : public EditTransactionBase {
29  protected:
30   JoinNodeTransaction(HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
31                       nsIContent& aRightContent);
32 
33  public:
34   /**
35    * Creates a join node transaction.  This returns nullptr if cannot join the
36    * nodes.
37    *
38    * @param aHTMLEditor     The provider of core editing operations.
39    * @param aLeftContent    The first of two nodes to join.
40    * @param aRightContent   The second of two nodes to join.
41    */
42   static already_AddRefed<JoinNodeTransaction> MaybeCreate(
43       HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
44       nsIContent& aRightContent);
45 
46   /**
47    * CanDoIt() returns true if there are enough members and can join or
48    * restore the nodes.  Otherwise, false.
49    */
50   bool CanDoIt() const;
51 
52   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(JoinNodeTransaction,
53                                            EditTransactionBase)
54   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
55 
56   NS_DECL_EDITTRANSACTIONBASE
57   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(JoinNodeTransaction)
58 
59   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
60 
61   friend std::ostream& operator<<(std::ostream& aStream,
62                                   const JoinNodeTransaction& aTransaction);
63 
64  protected:
65   RefPtr<HTMLEditor> mHTMLEditor;
66 
67   // The nodes to operate upon.  After the merge, mRightContent remains and
68   // mLeftContent is removed from the content tree.
69   nsCOMPtr<nsIContent> mLeftContent;
70   nsCOMPtr<nsIContent> mRightContent;
71 
72   // The offset into mNode where the children of mElement are split (for
73   // undo). mOffset is the index of the first child in the right node.  -1
74   // means the left node had no children.
75   uint32_t mOffset;
76 
77   // The parent node containing mLeftContent and mRightContent.
78   nsCOMPtr<nsINode> mParentNode;
79 };
80 
81 }  // namespace mozilla
82 
83 #endif  // #ifndef JoinNodeTransaction_h
84