Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / editor / libeditor / JoinNodesTransaction.h
blobe762cec4733860517a09d0b9b23f6665f263f1cc
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
19 class nsIContent;
20 class nsINode;
22 namespace mozilla {
24 /**
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 {
31 protected:
32 JoinNodesTransaction(HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
33 nsIContent& aRightContent);
35 public:
36 /**
37 * Creates a join node transaction. This returns nullptr if cannot join the
38 * nodes.
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);
48 /**
49 * CanDoIt() returns true if there are enough members and can join or
50 * restore the nodes. Otherwise, false.
52 bool CanDoIt() const;
54 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(JoinNodesTransaction,
55 EditTransactionBase)
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;
63 /**
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);
83 protected:
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
101 // doing or redoing.
102 uint32_t mJoinedOffset = 0u;
105 } // namespace mozilla
107 #endif // #ifndef JoinNodesTransaction_h