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 SplitNodeTransaction_h
7 #define SplitNodeTransaction_h
9 #include "EditorForwards.h"
10 #include "EditTransactionBase.h" // for EditorTransactionBase
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
21 * A transaction that splits a node into two identical nodes, with the children
22 * divided between the new nodes.
24 class SplitNodeTransaction final
: public EditTransactionBase
{
26 template <typename PT
, typename CT
>
27 SplitNodeTransaction(HTMLEditor
& aHTMLEditor
,
28 const EditorDOMPointBase
<PT
, CT
>& aStartOfRightContent
);
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.
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.
42 template <typename PT
, typename CT
>
43 static already_AddRefed
<SplitNodeTransaction
> Create(
44 HTMLEditor
& aHTMLEditor
,
45 const EditorDOMPointBase
<PT
, CT
>& aStartOfRightContent
);
47 NS_DECL_ISUPPORTS_INHERITED
48 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SplitNodeTransaction
,
51 NS_DECL_EDITTRANSACTIONBASE
52 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(SplitNodeTransaction
)
54 MOZ_CAN_RUN_SCRIPT NS_IMETHOD
RedoTransaction() override
;
56 nsIContent
* GetSplitContent() const { return mSplitContent
; }
57 nsIContent
* GetNewContent() const { return mNewContent
; }
58 nsINode
* GetParentNode() const { return mParentNode
; }
60 // The split offset. At undoing, this is recomputed with tracking the
61 // first child of mSplitContent.
62 uint32_t SplitOffset() const { return mSplitOffset
; }
64 friend std::ostream
& operator<<(std::ostream
& aStream
,
65 const SplitNodeTransaction
& aTransaction
);
68 virtual ~SplitNodeTransaction() = default;
70 MOZ_CAN_RUN_SCRIPT Result
<SplitNodeResult
, nsresult
> DoTransactionInternal(
71 HTMLEditor
& aHTMLEditor
, nsIContent
& aSplittingContent
,
72 nsIContent
& aNewContent
, uint32_t aSplitOffset
);
74 RefPtr
<HTMLEditor
> mHTMLEditor
;
76 // The node which should be parent of both mNewContent and mSplitContent.
77 nsCOMPtr
<nsINode
> mParentNode
;
79 // The node we create when splitting mSplitContent.
80 nsCOMPtr
<nsIContent
> mNewContent
;
82 // The content node which we split.
83 nsCOMPtr
<nsIContent
> mSplitContent
;
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
;
90 } // namespace mozilla
92 #endif // #ifndef SplitNodeTransaction_h