JoinNodesTransaction.h (3622B)
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 JoinNodesTransaction_h 7 #define JoinNodesTransaction_h 8 9 #include "EditTransactionBase.h" // for EditTransactionBase, etc. 10 11 #include "EditorDOMPoint.h" // for EditorDOMPoint, etc. 12 #include "EditorForwards.h" 13 14 #include "nsCOMPtr.h" // for nsCOMPtr 15 #include "nsCycleCollectionParticipant.h" 16 #include "nsID.h" // for REFNSIID 17 #include "nscore.h" // for NS_IMETHOD 18 19 class nsIContent; 20 class nsINode; 21 22 namespace mozilla { 23 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. 29 */ 30 class JoinNodesTransaction final : public EditTransactionBase { 31 protected: 32 JoinNodesTransaction(HTMLEditor& aHTMLEditor, nsIContent& aLeftContent, 33 nsIContent& aRightContent); 34 35 public: 36 /** 37 * Creates a join node transaction. This returns nullptr if cannot join the 38 * nodes. 39 * 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. 43 */ 44 static already_AddRefed<JoinNodesTransaction> MaybeCreate( 45 HTMLEditor& aHTMLEditor, nsIContent& aLeftContent, 46 nsIContent& aRightContent); 47 48 /** 49 * CanDoIt() returns true if there are enough members and can join or 50 * restore the nodes. Otherwise, false. 51 */ 52 bool CanDoIt() const; 53 54 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(JoinNodesTransaction, 55 EditTransactionBase) 56 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; 57 58 NS_DECL_EDITTRANSACTIONBASE 59 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(JoinNodesTransaction) 60 61 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override; 62 63 /** 64 * GetExistingContent() and GetRemovedContent() never returns nullptr 65 * unless the cycle collector clears them out. 66 */ 67 nsIContent* GetExistingContent() const { return mKeepingContent; } 68 nsIContent* GetRemovedContent() const { return mRemovedContent; } 69 nsINode* GetParentNode() const { return mParentNode; } 70 71 template <typename EditorDOMPointType> 72 EditorDOMPointType CreateJoinedPoint() const { 73 if (MOZ_UNLIKELY(!mKeepingContent)) { 74 return EditorDOMPointType(); 75 } 76 return EditorDOMPointType( 77 mKeepingContent, std::min(mJoinedOffset, mKeepingContent->Length())); 78 } 79 80 friend std::ostream& operator<<(std::ostream& aStream, 81 const JoinNodesTransaction& aTransaction); 82 83 protected: 84 virtual ~JoinNodesTransaction() = default; 85 86 enum class RedoingTransaction { No, Yes }; 87 MOZ_CAN_RUN_SCRIPT nsresult DoTransactionInternal(RedoingTransaction); 88 89 RefPtr<HTMLEditor> mHTMLEditor; 90 91 // The original parent of the left/right nodes. 92 nsCOMPtr<nsINode> mParentNode; 93 94 // Removed content after joined. 95 nsCOMPtr<nsIContent> mRemovedContent; 96 97 // The keeping content node which contains ex-children of mRemovedContent. 98 nsCOMPtr<nsIContent> mKeepingContent; 99 100 // Offset where the original first content is in mKeepingContent after 101 // doing or redoing. 102 uint32_t mJoinedOffset = 0u; 103 }; 104 105 } // namespace mozilla 106 107 #endif // #ifndef JoinNodesTransaction_h