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 JoinNodesTransaction_h
7 #define JoinNodesTransaction_h
9 #include "EditTransactionBase.h" // for EditTransactionBase, etc.
11 #include "EditorDOMPoint.h" // for EditorDOMPoint, etc.
12 #include "EditorForwards.h"
14 #include "nsCOMPtr.h" // for nsCOMPtr
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsID.h" // for REFNSIID
17 #include "nscore.h" // for NS_IMETHOD
25 * A transaction that joins two nodes E1 (left node) and E2 (right node) into a
26 * single node E. The children of E are the children of E1 followed by the
27 * children of E2. After DoTransaction() and RedoTransaction(), E1 is removed
28 * from the content tree and E2 remains.
30 class JoinNodesTransaction final
: public EditTransactionBase
{
32 JoinNodesTransaction(HTMLEditor
& aHTMLEditor
, nsIContent
& aLeftContent
,
33 nsIContent
& aRightContent
);
37 * Creates a join node transaction. This returns nullptr if cannot join the
40 * @param aHTMLEditor The provider of core editing operations.
41 * @param aLeftContent The first of two nodes to join.
42 * @param aRightContent The second of two nodes to join.
44 static already_AddRefed
<JoinNodesTransaction
> MaybeCreate(
45 HTMLEditor
& aHTMLEditor
, nsIContent
& aLeftContent
,
46 nsIContent
& aRightContent
);
49 * CanDoIt() returns true if there are enough members and can join or
50 * restore the nodes. Otherwise, false.
54 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(JoinNodesTransaction
,
56 NS_IMETHOD
QueryInterface(REFNSIID aIID
, void** aInstancePtr
) override
;
58 NS_DECL_EDITTRANSACTIONBASE
59 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(JoinNodesTransaction
)
61 MOZ_CAN_RUN_SCRIPT NS_IMETHOD
RedoTransaction() override
;
64 * GetExistingContent() and GetRemovedContent() never returns nullptr
65 * unless the cycle collector clears them out.
67 nsIContent
* GetExistingContent() const { return mKeepingContent
; }
68 nsIContent
* GetRemovedContent() const { return mRemovedContent
; }
69 nsINode
* GetParentNode() const { return mParentNode
; }
71 template <typename EditorDOMPointType
>
72 EditorDOMPointType
CreateJoinedPoint() const {
73 if (MOZ_UNLIKELY(!mKeepingContent
)) {
74 return EditorDOMPointType();
76 return EditorDOMPointType(
77 mKeepingContent
, std::min(mJoinedOffset
, mKeepingContent
->Length()));
80 friend std::ostream
& operator<<(std::ostream
& aStream
,
81 const JoinNodesTransaction
& aTransaction
);
84 virtual ~JoinNodesTransaction() = default;
86 enum class RedoingTransaction
{ No
, Yes
};
87 MOZ_CAN_RUN_SCRIPT nsresult
DoTransactionInternal(RedoingTransaction
);
89 RefPtr
<HTMLEditor
> mHTMLEditor
;
91 // The original parent of the left/right nodes.
92 nsCOMPtr
<nsINode
> mParentNode
;
94 // Removed content after joined.
95 nsCOMPtr
<nsIContent
> mRemovedContent
;
97 // The keeping content node which contains ex-children of mRemovedContent.
98 nsCOMPtr
<nsIContent
> mKeepingContent
;
100 // Offset where the original first content is in mKeepingContent after
102 uint32_t mJoinedOffset
= 0u;
105 } // namespace mozilla
107 #endif // #ifndef JoinNodesTransaction_h