SplitNodeTransaction.h (3461B)
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 SplitNodeTransaction_h 7 #define SplitNodeTransaction_h 8 9 #include "EditorForwards.h" 10 #include "EditTransactionBase.h" // for EditorTransactionBase 11 12 #include "nsCOMPtr.h" // for nsCOMPtr 13 #include "nsCycleCollectionParticipant.h" 14 #include "nsIContent.h" 15 #include "nsISupportsImpl.h" // for NS_DECL_ISUPPORTS_INHERITED 16 #include "nscore.h" // for NS_IMETHOD 17 18 namespace mozilla { 19 20 /** 21 * A transaction that splits a node into two identical nodes, with the children 22 * divided between the new nodes. 23 */ 24 class SplitNodeTransaction final : public EditTransactionBase { 25 private: 26 template <typename PT, typename CT> 27 SplitNodeTransaction(HTMLEditor& aHTMLEditor, 28 const EditorDOMPointBase<PT, CT>& aStartOfRightContent); 29 30 public: 31 /** 32 * Creates a transaction to create a new node identical to an existing node, 33 * and split the contents between the same point in both nodes. 34 * 35 * @param aHTMLEditor The provider of core editing operations. 36 * @param aStartOfRightContent The point to split. Its container will be 37 * split, and its preceding or following 38 * content will be moved to the new node. And 39 * the point will be start of the right node or 40 * end of the left node. 41 */ 42 template <typename PT, typename CT> 43 static already_AddRefed<SplitNodeTransaction> Create( 44 HTMLEditor& aHTMLEditor, 45 const EditorDOMPointBase<PT, CT>& aStartOfRightContent); 46 47 NS_DECL_ISUPPORTS_INHERITED 48 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SplitNodeTransaction, 49 EditTransactionBase) 50 51 NS_DECL_EDITTRANSACTIONBASE 52 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(SplitNodeTransaction) 53 54 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override; 55 56 nsIContent* GetSplitContent() const { return mSplitContent; } 57 nsIContent* GetNewContent() const { return mNewContent; } 58 nsINode* GetParentNode() const { return mParentNode; } 59 60 // The split offset. At undoing, this is recomputed with tracking the 61 // first child of mSplitContent. 62 uint32_t SplitOffset() const { return mSplitOffset; } 63 64 friend std::ostream& operator<<(std::ostream& aStream, 65 const SplitNodeTransaction& aTransaction); 66 67 protected: 68 virtual ~SplitNodeTransaction() = default; 69 70 MOZ_CAN_RUN_SCRIPT Result<SplitNodeResult, nsresult> DoTransactionInternal( 71 HTMLEditor& aHTMLEditor, nsIContent& aSplittingContent, 72 nsIContent& aNewContent, uint32_t aSplitOffset); 73 74 RefPtr<HTMLEditor> mHTMLEditor; 75 76 // The node which should be parent of both mNewContent and mSplitContent. 77 nsCOMPtr<nsINode> mParentNode; 78 79 // The node we create when splitting mSplitContent. 80 nsCOMPtr<nsIContent> mNewContent; 81 82 // The content node which we split. 83 nsCOMPtr<nsIContent> mSplitContent; 84 85 // The offset where we split in mSplitContent. This is required for doing and 86 // redoing. Therefore, this is updated when undoing. 87 uint32_t mSplitOffset; 88 }; 89 90 } // namespace mozilla 91 92 #endif // #ifndef SplitNodeTransaction_h